]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiFormSpecMenu.cpp
Fix formspec field labels
[dragonfireclient.git] / src / guiFormSpecMenu.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20
21 #include <cstdlib>
22 #include <algorithm>
23 #include <iterator>
24 #include <sstream>
25 #include <limits>
26 #include "guiFormSpecMenu.h"
27 #include "constants.h"
28 #include "gamedef.h"
29 #include "keycode.h"
30 #include "strfnd.h"
31 #include <IGUICheckBox.h>
32 #include <IGUIEditBox.h>
33 #include <IGUIButton.h>
34 #include <IGUIStaticText.h>
35 #include <IGUIFont.h>
36 #include <IGUIListBox.h>
37 #include <IGUITabControl.h>
38 #include <IGUIScrollBar.h>
39 #include <IGUIComboBox.h>
40 #include "log.h"
41 #include "tile.h" // ITextureSource
42 #include "hud.h" // drawItemStack
43 #include "hex.h"
44 #include "util/string.h"
45 #include "util/numeric.h"
46 #include "filesys.h"
47 #include "gettime.h"
48 #include "gettext.h"
49
50 #define MY_CHECKPOS(a,b)                                                                                                        \
51         if (v_pos.size() != 2) {                                                                                                \
52                 errorstream<< "Invalid pos for element " << a << "specified: \""        \
53                         << parts[b] << "\"" << std::endl;                                                               \
54                         return;                                                                                                                 \
55         }
56
57 #define MY_CHECKGEOM(a,b)                                                                                                       \
58         if (v_geom.size() != 2) {                                                                                               \
59                 errorstream<< "Invalid pos for element " << a << "specified: \""        \
60                         << parts[b] << "\"" << std::endl;                                                               \
61                         return;                                                                                                                 \
62         }
63
64
65 /*
66         GUIFormSpecMenu
67 */
68
69 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
70                 gui::IGUIElement* parent, s32 id,
71                 IMenuManager *menumgr,
72                 InventoryManager *invmgr,
73                 IGameDef *gamedef,
74                 ISimpleTextureSource *tsrc
75 ):
76         GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
77         m_device(dev),
78         m_invmgr(invmgr),
79         m_gamedef(gamedef),
80         m_tsrc(tsrc),
81         m_form_src(NULL),
82         m_text_dst(NULL),
83         m_selected_item(NULL),
84         m_selected_amount(0),
85         m_selected_dragging(false),
86         m_listbox_click_fname(),
87         m_listbox_click_index(-1),
88         m_listbox_click_time(0),
89         m_listbox_doubleclick(false),
90         m_tooltip_element(NULL),
91         m_allowclose(true),
92         m_lock(false)
93 {
94         current_keys_pending.key_down = false;
95         current_keys_pending.key_up = false;
96         current_keys_pending.key_enter = false;
97         current_keys_pending.key_escape = false;
98
99 }
100
101 GUIFormSpecMenu::~GUIFormSpecMenu()
102 {
103         removeChildren();
104
105         delete m_selected_item;
106         delete m_form_src;
107         delete m_text_dst;
108 }
109
110 void GUIFormSpecMenu::removeChildren()
111 {
112         const core::list<gui::IGUIElement*> &children = getChildren();
113         core::list<gui::IGUIElement*> children_copy;
114         for(core::list<gui::IGUIElement*>::ConstIterator
115                         i = children.begin(); i != children.end(); i++)
116         {
117                 children_copy.push_back(*i);
118         }
119         for(core::list<gui::IGUIElement*>::Iterator
120                         i = children_copy.begin();
121                         i != children_copy.end(); i++)
122         {
123                 (*i)->remove();
124         }
125         /*{
126                 gui::IGUIElement *e = getElementFromId(256);
127                 if(e != NULL)
128                         e->remove();
129         }*/
130
131         if(m_tooltip_element)
132         {
133                 m_tooltip_element->remove();
134                 m_tooltip_element->drop();
135                 m_tooltip_element = NULL;
136         }
137
138 }
139
140 void GUIFormSpecMenu::setInitialFocus()
141 {
142         // Set initial focus according to following order of precedence:
143         // 1. first empty editbox
144         // 2. first editbox
145         // 3. first listbox
146         // 4. last button
147         // 5. first focusable (not statictext, not tabheader)
148         // 6. first child element
149
150         core::list<gui::IGUIElement*> children = getChildren();
151
152         // in case "children" contains any NULL elements, remove them
153         for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
154                         it != children.end();) {
155                 if (*it)
156                         ++it;
157                 else
158                         it = children.erase(it);
159         }
160
161         // 1. first empty editbox
162         for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
163                         it != children.end(); ++it) {
164                 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
165                                 && (*it)->getText()[0] == 0) {
166                         Environment->setFocus(*it);
167                         return;
168                 }
169         }
170
171         // 2. first editbox
172         for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
173                         it != children.end(); ++it) {
174                 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
175                         Environment->setFocus(*it);
176                         return;
177                 }
178         }
179
180         // 3. first listbox
181         for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
182                         it != children.end(); ++it) {
183                 if ((*it)->getType() == gui::EGUIET_LIST_BOX) {
184                         Environment->setFocus(*it);
185                         return;
186                 }
187         }
188
189         // 4. last button
190         for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
191                         it != children.end(); --it) {
192                 if ((*it)->getType() == gui::EGUIET_BUTTON) {
193                         Environment->setFocus(*it);
194                         return;
195                 }
196         }
197
198         // 5. first focusable (not statictext, not tabheader)
199         for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
200                         it != children.end(); ++it) {
201                 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
202                                 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
203                         Environment->setFocus(*it);
204                         return;
205                 }
206         }
207
208         // 6. first child element
209         if (children.empty())
210                 Environment->setFocus(this);
211         else
212                 Environment->setFocus(*(children.begin()));
213 }
214
215 int GUIFormSpecMenu::getListboxIndex(std::string listboxname) {
216
217         std::wstring wlistboxname = narrow_to_wide(listboxname.c_str());
218
219         for(unsigned int i=0; i < m_listboxes.size(); i++) {
220
221                 std::wstring name(m_listboxes[i].first.fname.c_str());
222                 if ( name == wlistboxname) {
223                         return m_listboxes[i].second->getSelected();
224                 }
225         }
226         return -1;
227 }
228
229 bool GUIFormSpecMenu::checkListboxClick(std::wstring wlistboxname,
230                 int eventtype)
231 {
232         // WARNING: BLACK IRRLICHT MAGIC
233         // Used to fix Irrlicht's subpar reporting of single clicks and double
234         // clicks in listboxes (gui::EGET_LISTBOX_CHANGED,
235         // gui::EGET_LISTBOX_SELECTED_AGAIN):
236         // 1. IGUIListBox::setSelected() is counted as a click.
237         //    Including the initial setSelected() done by parseTextList().
238         // 2. Clicking on a the selected item and then dragging for less
239         //    than 500ms is counted as a doubleclick, no matter when the
240         //    item was previously selected (e.g. more than 500ms ago)
241
242         // So when Irrlicht reports a doubleclick, we need to check
243         // for ourselves if really was a doubleclick. Or just a fake.
244
245         for(unsigned int i=0; i < m_listboxes.size(); i++) {
246                 std::wstring name(m_listboxes[i].first.fname.c_str());
247                 int selected = m_listboxes[i].second->getSelected();
248                 if (name == wlistboxname && selected >= 0) {
249                         u32 now = getTimeMs();
250                         bool doubleclick =
251                                 (eventtype == gui::EGET_LISTBOX_SELECTED_AGAIN)
252                                 && (name == m_listbox_click_fname)
253                                 && (selected == m_listbox_click_index)
254                                 && (m_listbox_click_time >= now - 500);
255                         m_listbox_click_fname = name;
256                         m_listbox_click_index = selected;
257                         m_listbox_click_time = now;
258                         m_listbox_doubleclick = doubleclick;
259                         return true;
260                 }
261         }
262         return false;
263 }
264
265 gui::IGUIScrollBar* GUIFormSpecMenu::getListboxScrollbar(
266                 gui::IGUIListBox *listbox)
267 {
268         // WARNING: BLACK IRRLICHT MAGIC
269         // Ordinarily, due to how formspecs work (recreating the entire GUI
270         // when something changes), when you select an item in a textlist
271         // with more items than fit in the visible area, the newly selected
272         // item is scrolled to the bottom of the visible area. This is
273         // annoying and breaks GUI designs that use double clicks.
274
275         // This function helps fixing this problem by giving direct access
276         // to a listbox's scrollbar. This works because CGUIListBox doesn't
277         // cache the scrollbar position anywhere.
278
279         // If this stops working in a future irrlicht version, consider
280         // maintaining a local copy of irr::gui::CGUIListBox, possibly also
281         // fixing the other reasons why black irrlicht magic is needed.
282
283         core::list<gui::IGUIElement*> children = listbox->getChildren();
284         for(core::list<gui::IGUIElement*>::Iterator it = children.begin();
285                         it != children.end(); ++it) {
286                 gui::IGUIElement* child = *it;
287                 if (child && child->getType() == gui::EGUIET_SCROLL_BAR) {
288                         return static_cast<gui::IGUIScrollBar*>(child);
289                 }
290         }
291
292         verbosestream<<"getListboxScrollbar: WARNING: "
293                         <<"listbox has no scrollbar"<<std::endl;
294         return NULL;
295 }
296
297 std::vector<std::string> split(const std::string &s, char delim) {
298         std::vector<std::string> tokens;
299
300         std::string current = "";
301         bool last_was_escape = false;
302         for(unsigned int i=0; i < s.size(); i++) {
303                 if (last_was_escape) {
304                         current += '\\';
305                         current += s.c_str()[i];
306                         last_was_escape = false;
307                 }
308                 else {
309                         if (s.c_str()[i] == delim) {
310                                 tokens.push_back(current);
311                                 current = "";
312                                 last_was_escape = false;
313                         }
314                         else if (s.c_str()[i] == '\\'){
315                                 last_was_escape = true;
316                         }
317                         else {
318                                 current += s.c_str()[i];
319                                 last_was_escape = false;
320                         }
321                 }
322         }
323         //push last element
324         tokens.push_back(current);
325
326         return tokens;
327 }
328
329 void GUIFormSpecMenu::parseSize(parserData* data,std::string element) {
330         std::vector<std::string> parts = split(element,',');
331
332         if (parts.size() == 2) {
333                 v2f invsize;
334
335                 if (parts[1].find(';') != std::string::npos)
336                         parts[1] = parts[1].substr(0,parts[1].find(';'));
337
338                 invsize.X = stof(parts[0]);
339                 invsize.Y = stof(parts[1]);
340
341                 if (m_lock) {
342                         v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
343                         v2u32 delta = current_screensize - m_lockscreensize;
344
345                         if (current_screensize.Y > m_lockscreensize.Y)
346                                 delta.Y /= 2;
347                         else
348                                 delta.Y = 0;
349
350                         if (current_screensize.X > m_lockscreensize.X)
351                                 delta.X /= 2;
352                         else
353                                 delta.X = 0;
354
355                         offset = v2s32(delta.X,delta.Y);
356
357                         data->screensize = m_lockscreensize;
358                 }
359                 else {
360                         offset = v2s32(0,0);
361                 }
362
363                 padding = v2s32(data->screensize.Y/40, data->screensize.Y/40);
364                 spacing = v2s32(data->screensize.Y/12, data->screensize.Y/13);
365                 imgsize = v2s32(data->screensize.Y/15, data->screensize.Y/15);
366                 data->size = v2s32(
367                         padding.X*2+spacing.X*(invsize.X-1.0)+imgsize.X,
368                         padding.Y*2+spacing.Y*(invsize.Y-1.0)+imgsize.Y + (data->helptext_h-5)
369                 );
370                 data->rect = core::rect<s32>(
371                                 data->screensize.X/2 - data->size.X/2 + offset.X,
372                                 data->screensize.Y/2 - data->size.Y/2 + offset.Y,
373                                 data->screensize.X/2 + data->size.X/2 + offset.X,
374                                 data->screensize.Y/2 + data->size.Y/2 + offset.Y
375                 );
376
377                 DesiredRect = data->rect;
378                 recalculateAbsolutePosition(false);
379                 data->basepos = getBasePos();
380                 data->bp_set = 2;
381                 return;
382         }
383         errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'"  << std::endl;
384 }
385
386 void GUIFormSpecMenu::parseList(parserData* data,std::string element) {
387
388         if (m_gamedef == 0) {
389                 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
390                 return;
391         }
392
393         std::vector<std::string> parts = split(element,';');
394
395         if ((parts.size() == 4) || (parts.size() == 5)) {
396                 std::string location = parts[0];
397                 std::string listname = parts[1];
398                 std::vector<std::string> v_pos  = split(parts[2],',');
399                 std::vector<std::string> v_geom = split(parts[3],',');
400                 std::string startindex = "";
401                 if (parts.size() == 5)
402                         startindex = parts[4];
403
404                 MY_CHECKPOS("list",2);
405                 MY_CHECKGEOM("list",3);
406
407                 InventoryLocation loc;
408
409                 if(location == "context" || location == "current_name")
410                         loc = m_current_inventory_location;
411                 else
412                         loc.deSerialize(location);
413
414                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
415                 pos.X += stof(v_pos[0]) * (float)spacing.X;
416                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
417
418                 v2s32 geom;
419                 geom.X = stoi(v_geom[0]);
420                 geom.Y = stoi(v_geom[1]);
421
422                 s32 start_i = 0;
423                 if(startindex != "")
424                         start_i = stoi(startindex);
425
426                 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
427                         errorstream<< "Invalid list element: '" << element << "'"  << std::endl;
428                         return;
429                 }
430
431                 if(data->bp_set != 2)
432                         errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
433                 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
434                 return;
435         }
436         errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'"  << std::endl;
437 }
438
439 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element) {
440         std::vector<std::string> parts = split(element,';');
441
442         if ((parts.size() == 3) || (parts.size() == 4)) {
443                 std::vector<std::string> v_pos = split(parts[0],',');
444                 std::string name = parts[1];
445                 std::string label = parts[2];
446                 std::string selected = "";
447
448                 if (parts.size() == 4)
449                         selected = parts[3];
450
451                 MY_CHECKPOS("checkbox",0);
452
453                 v2s32 pos = padding;
454                 pos.X += stof(v_pos[0]) * (float) spacing.X;
455                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
456
457                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
458
459                 bool fselected = false;
460
461                 if (selected == "true")
462                         fselected = true;
463
464                 std::wstring wlabel = narrow_to_wide(label.c_str());
465
466                 FieldSpec spec = FieldSpec(
467                                 narrow_to_wide(name.c_str()),
468                                 L"",
469                                 wlabel,
470                                 258+m_fields.size()
471                         );
472
473                 spec.ftype = f_CheckBox;
474                 spec.flabel = wlabel; //Needed for displaying text on MSVC
475                 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
476                                         spec.fid, spec.flabel.c_str());
477
478                 if (spec.fname == data->focused_fieldname) {
479                         Environment->setFocus(e);
480                 }
481
482                 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
483                 m_fields.push_back(spec);
484                 return;
485         }
486         errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'"  << std::endl;
487 }
488
489 void GUIFormSpecMenu::parseImage(parserData* data,std::string element) {
490         std::vector<std::string> parts = split(element,';');
491
492         if (parts.size() == 3) {
493                 std::vector<std::string> v_pos = split(parts[0],',');
494                 std::vector<std::string> v_geom = split(parts[1],',');
495                 std::string name = unescape_string(parts[2]);
496
497                 MY_CHECKPOS("image",0);
498                 MY_CHECKGEOM("image",1);
499
500                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
501                 pos.X += stof(v_pos[0]) * (float) spacing.X;
502                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
503
504                 v2s32 geom;
505                 geom.X = stof(v_geom[0]) * (float)imgsize.X;
506                 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
507
508                 if(data->bp_set != 2)
509                         errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
510                 m_images.push_back(ImageDrawSpec(name, pos, geom));
511                 return;
512         }
513
514         if (parts.size() == 2) {
515                 std::vector<std::string> v_pos = split(parts[0],',');
516                 std::string name = unescape_string(parts[1]);
517
518                 MY_CHECKPOS("image",0);
519
520                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
521                 pos.X += stof(v_pos[0]) * (float) spacing.X;
522                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
523
524                 if(data->bp_set != 2)
525                         errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
526                 m_images.push_back(ImageDrawSpec(name, pos));
527                 return;
528         }
529         errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'"  << std::endl;
530 }
531
532 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element) {
533         std::vector<std::string> parts = split(element,';');
534
535         if (parts.size() == 3) {
536                 std::vector<std::string> v_pos = split(parts[0],',');
537                 std::vector<std::string> v_geom = split(parts[1],',');
538                 std::string name = parts[2];
539
540                 MY_CHECKPOS("itemimage",0);
541                 MY_CHECKGEOM("itemimage",1);
542
543                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
544                 pos.X += stof(v_pos[0]) * (float) spacing.X;
545                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
546
547                 v2s32 geom;
548                 geom.X = stoi(v_geom[0]) * (float)imgsize.X;
549                 geom.Y = stoi(v_geom[1]) * (float)imgsize.Y;
550
551                 if(data->bp_set != 2)
552                         errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
553                 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
554                 return;
555         }
556         errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'"  << std::endl;
557 }
558
559 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,std::string type) {
560         std::vector<std::string> parts = split(element,';');
561
562         if (parts.size() == 4) {
563                 std::vector<std::string> v_pos = split(parts[0],',');
564                 std::vector<std::string> v_geom = split(parts[1],',');
565                 std::string name = parts[2];
566                 std::string label = parts[3];
567
568                 MY_CHECKPOS("button",0);
569                 MY_CHECKGEOM("button",1);
570
571                 v2s32 pos = padding;
572                 pos.X += stof(v_pos[0]) * (float)spacing.X;
573                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
574
575                 v2s32 geom;
576                 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
577                 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
578
579                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y-15, pos.X+geom.X, pos.Y+15);
580
581                 if(data->bp_set != 2)
582                         errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
583
584                 label = unescape_string(label);
585
586                 std::wstring wlabel = narrow_to_wide(label.c_str());
587
588                 FieldSpec spec = FieldSpec(
589                         narrow_to_wide(name.c_str()),
590                         wlabel,
591                         L"",
592                         258+m_fields.size()
593                 );
594                 spec.ftype = f_Button;
595                 if(type == "button_exit")
596                         spec.is_exit = true;
597
598                 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
599                                 spec.flabel.c_str());
600
601                 if (spec.fname == data->focused_fieldname) {
602                         Environment->setFocus(e);
603                 }
604
605                 m_fields.push_back(spec);
606                 return;
607         }
608         errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'"  << std::endl;
609 }
610
611 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element) {
612         std::vector<std::string> parts = split(element,';');
613
614         if ((parts.size() == 3) || (parts.size() == 4)) {
615                 std::vector<std::string> v_pos = split(parts[0],',');
616                 std::vector<std::string> v_geom = split(parts[1],',');
617                 std::string name = unescape_string(parts[2]);
618
619                 MY_CHECKPOS("background",0);
620                 MY_CHECKGEOM("background",1);
621
622                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
623                 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
624                 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
625
626                 v2s32 geom;
627                 geom.X = stof(v_geom[0]) * (float)spacing.X;
628                 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
629
630                 if (parts.size() == 4) {
631                         m_clipbackground = is_yes(parts[3]);
632                         if (m_clipbackground) {
633                                 pos.X = stoi(v_pos[0]); //acts as offset
634                                 pos.Y = stoi(v_pos[1]); //acts as offset
635                         }
636                 }
637
638                 if(data->bp_set != 2)
639                         errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
640                 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
641                 return;
642         }
643         errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'"  << std::endl;
644 }
645
646 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element) {
647         std::vector<std::string> parts = split(element,';');
648
649         if ((parts.size() == 5) || (parts.size() == 6)) {
650                 std::vector<std::string> v_pos = split(parts[0],',');
651                 std::vector<std::string> v_geom = split(parts[1],',');
652                 std::string name = parts[2];
653                 std::vector<std::string> items = split(parts[3],',');
654                 std::string str_initial_selection = "";
655                 std::string str_transparent = "false";
656
657                 if (parts.size() >= 5)
658                         str_initial_selection = parts[4];
659
660                 if (parts.size() >= 6)
661                         str_transparent = parts[5];
662
663                 MY_CHECKPOS("textlist",0);
664                 MY_CHECKGEOM("textlist",1);
665
666                 v2s32 pos = padding;
667                 pos.X += stof(v_pos[0]) * (float)spacing.X;
668                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
669
670                 v2s32 geom;
671                 geom.X = stof(v_geom[0]) * (float)spacing.X;
672                 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
673
674
675                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
676
677                 std::wstring fname_w = narrow_to_wide(name.c_str());
678
679                 FieldSpec spec = FieldSpec(
680                         fname_w,
681                         L"",
682                         L"",
683                         258+m_fields.size()
684                 );
685
686                 spec.ftype = f_ListBox;
687
688                 //now really show list
689                 gui::IGUIListBox *e = Environment->addListBox(rect, this,spec.fid);
690
691                 if (spec.fname == data->focused_fieldname) {
692                         Environment->setFocus(e);
693                 }
694
695                 if (str_transparent == "false")
696                         e->setDrawBackground(true);
697
698                 for (unsigned int i=0; i < items.size(); i++) {
699                         if (items[i].c_str()[0] == '#') {
700                                 if (items[i].c_str()[1] == '#') {
701                                         e->addItem(narrow_to_wide(unescape_string(items[i])).c_str() +1);
702                                 }
703                                 else {
704                                         std::string color = items[i].substr(0,7);
705                                         std::wstring toadd =
706                                                 narrow_to_wide(unescape_string(items[i]).c_str() + 7);
707
708                                         e->addItem(toadd.c_str());
709
710                                         video::SColor tmp_color;
711
712                                         if (parseColor(color, tmp_color, false))
713                                         e->setItemOverrideColor(i,tmp_color);
714                                 }
715                         }
716                         else {
717                                 e->addItem(narrow_to_wide(unescape_string(items[i])).c_str());
718                         }
719                 }
720
721                 if (data->listbox_selections.find(fname_w) != data->listbox_selections.end()) {
722                         e->setSelected(data->listbox_selections[fname_w]);
723                 }
724
725                 if (data->listbox_scroll.find(fname_w) != data->listbox_scroll.end()) {
726                         gui::IGUIScrollBar *scrollbar = getListboxScrollbar(e);
727                         if (scrollbar) {
728                                 scrollbar->setPos(data->listbox_scroll[fname_w]);
729                         }
730                 }
731                 else {
732                         gui::IGUIScrollBar *scrollbar = getListboxScrollbar(e);
733                         if (scrollbar) {
734                                 scrollbar->setPos(0);
735                         }
736                 }
737
738                 if ((str_initial_selection != "") &&
739                                 (str_initial_selection != "0"))
740                         e->setSelected(stoi(str_initial_selection.c_str())-1);
741
742                 m_listboxes.push_back(std::pair<FieldSpec,gui::IGUIListBox*>(spec,e));
743                 m_fields.push_back(spec);
744                 return;
745         }
746         errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'"  << std::endl;
747 }
748
749
750 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element) {
751         std::vector<std::string> parts = split(element,';');
752
753         if (parts.size() == 5) {
754                 std::vector<std::string> v_pos = split(parts[0],',');
755                 std::string name = parts[2];
756                 std::vector<std::string> items = split(parts[3],',');
757                 std::string str_initial_selection = "";
758                 str_initial_selection = parts[4];
759
760                 MY_CHECKPOS("dropdown",0);
761
762                 v2s32 pos = padding;
763                 pos.X += stof(v_pos[0]) * (float)spacing.X;
764                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
765
766                 s32 width = stof(parts[1]) * (float)spacing.Y;
767
768                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+width, pos.Y+30);
769
770                 std::wstring fname_w = narrow_to_wide(name.c_str());
771
772                 FieldSpec spec = FieldSpec(
773                         fname_w,
774                         L"",
775                         L"",
776                         258+m_fields.size()
777                 );
778
779                 spec.ftype = f_DropDown;
780                 spec.send = true;
781
782                 //now really show list
783                 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
784
785                 if (spec.fname == data->focused_fieldname) {
786                         Environment->setFocus(e);
787                 }
788
789                 for (unsigned int i=0; i < items.size(); i++) {
790                         e->addItem(narrow_to_wide(items[i]).c_str());
791                 }
792
793                 if (str_initial_selection != "")
794                         e->setSelected(stoi(str_initial_selection.c_str())-1);
795
796                 m_fields.push_back(spec);
797                 return;
798         }
799         errorstream << "Invalid dropdown element(" << parts.size() << "): '"
800                                 << element << "'"  << std::endl;
801 }
802
803 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element) {
804         std::vector<std::string> parts = split(element,';');
805
806         if (parts.size() == 4) {
807                 std::vector<std::string> v_pos = split(parts[0],',');
808                 std::vector<std::string> v_geom = split(parts[1],',');
809                 std::string name = parts[2];
810                 std::string label = parts[3];
811
812                 MY_CHECKPOS("pwdfield",0);
813                 MY_CHECKGEOM("pwdfield",1);
814
815                 v2s32 pos;
816                 pos.X += stof(v_pos[0]) * (float)spacing.X;
817                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
818
819                 v2s32 geom;
820                 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
821
822                 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
823                 pos.Y -= 15;
824                 geom.Y = 30;
825
826                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
827
828                 label = unescape_string(label);
829
830                 std::wstring wlabel = narrow_to_wide(label.c_str());
831
832                 FieldSpec spec = FieldSpec(
833                         narrow_to_wide(name.c_str()),
834                         wlabel,
835                         L"",
836                         258+m_fields.size()
837                         );
838
839                 spec.send = true;
840                 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
841
842                 if (spec.fname == data->focused_fieldname) {
843                         Environment->setFocus(e);
844                 }
845
846                 if (label.length() >= 1)
847                 {
848                         rect.UpperLeftCorner.Y -= 15;
849                         rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
850                         Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
851                 }
852
853                 e->setPasswordBox(true,L'*');
854
855                 irr::SEvent evt;
856                 evt.EventType            = EET_KEY_INPUT_EVENT;
857                 evt.KeyInput.Key         = KEY_END;
858                 evt.KeyInput.Char        = 0;
859                 evt.KeyInput.Control     = 0;
860                 evt.KeyInput.Shift       = 0;
861                 evt.KeyInput.PressedDown = true;
862                 e->OnEvent(evt);
863                 m_fields.push_back(spec);
864                 return;
865         }
866         errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'"  << std::endl;
867 }
868
869 void GUIFormSpecMenu::parseSimpleField(parserData* data,std::vector<std::string> &parts) {
870         std::string name = parts[0];
871         std::string label = parts[1];
872         std::string default_val = parts[2];
873
874         core::rect<s32> rect;
875
876         if(!data->bp_set)
877         {
878                 rect = core::rect<s32>(
879                         data->screensize.X/2 - 580/2,
880                         data->screensize.Y/2 - 300/2,
881                         data->screensize.X/2 + 580/2,
882                         data->screensize.Y/2 + 300/2
883                 );
884                 DesiredRect = rect;
885                 recalculateAbsolutePosition(false);
886                 data->basepos = getBasePos();
887                 data->bp_set = 1;
888         }
889         else if(data->bp_set == 2)
890                 errorstream<<"WARNING: invalid use of unpositioned \"field\" in inventory"<<std::endl;
891
892         v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
893         pos.Y = ((m_fields.size()+2)*60);
894         v2s32 size = DesiredRect.getSize();
895
896         rect = core::rect<s32>(size.X/2-150, pos.Y, (size.X/2-150)+300, pos.Y+30);
897
898
899         if(m_form_src)
900                 default_val = m_form_src->resolveText(default_val);
901
902         default_val = unescape_string(default_val);
903         label = unescape_string(label);
904
905         std::wstring wlabel = narrow_to_wide(label.c_str());
906
907         FieldSpec spec = FieldSpec(
908                 narrow_to_wide(name.c_str()),
909                 wlabel,
910                 narrow_to_wide(default_val.c_str()),
911                 258+m_fields.size()
912         );
913
914         if (name == "")
915         {
916                 // spec field id to 0, this stops submit searching for a value that isn't there
917                 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
918         }
919         else
920         {
921                 spec.send = true;
922                 gui::IGUIEditBox *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
923
924                 if (spec.fname == data->focused_fieldname) {
925                         Environment->setFocus(e);
926                 }
927
928                 irr::SEvent evt;
929                 evt.EventType            = EET_KEY_INPUT_EVENT;
930                 evt.KeyInput.Key         = KEY_END;
931                 evt.KeyInput.Char        = 0;
932                 evt.KeyInput.Control     = 0;
933                 evt.KeyInput.Shift       = 0;
934                 evt.KeyInput.PressedDown = true;
935                 e->OnEvent(evt);
936
937                 if (label.length() >= 1)
938                 {
939                         rect.UpperLeftCorner.Y -= 15;
940                         rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
941                         Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
942                 }
943         }
944
945         m_fields.push_back(spec);
946 }
947
948 void GUIFormSpecMenu::parseTextArea(parserData* data,std::vector<std::string>& parts,std::string type) {
949
950         std::vector<std::string> v_pos = split(parts[0],',');
951         std::vector<std::string> v_geom = split(parts[1],',');
952         std::string name = parts[2];
953         std::string label = parts[3];
954         std::string default_val = parts[4];
955
956         MY_CHECKPOS(type,0);
957         MY_CHECKGEOM(type,1);
958
959         v2s32 pos;
960         pos.X = stof(v_pos[0]) * (float) spacing.X;
961         pos.Y = stof(v_pos[1]) * (float) spacing.Y;
962
963         v2s32 geom;
964
965         geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
966
967         if (type == "textarea")
968         {
969                 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
970                 pos.Y += 15;
971         }
972         else
973         {
974                 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
975                 pos.Y -= 15;
976                 geom.Y = 30;
977         }
978
979         core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
980
981         if(data->bp_set != 2)
982                 errorstream<<"WARNING: invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
983
984         if(m_form_src)
985                 default_val = m_form_src->resolveText(default_val);
986
987
988         default_val = unescape_string(default_val);
989         label = unescape_string(label);
990
991         std::wstring wlabel = narrow_to_wide(label.c_str());
992
993         FieldSpec spec = FieldSpec(
994                 narrow_to_wide(name.c_str()),
995                 wlabel,
996                 narrow_to_wide(default_val.c_str()),
997                 258+m_fields.size()
998         );
999
1000         if (name == "")
1001         {
1002                 // spec field id to 0, this stops submit searching for a value that isn't there
1003                 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1004         }
1005         else
1006         {
1007                 spec.send = true;
1008                 gui::IGUIEditBox *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1009
1010                 if (spec.fname == data->focused_fieldname) {
1011                         Environment->setFocus(e);
1012                 }
1013
1014                 if (type == "textarea")
1015                 {
1016                         e->setMultiLine(true);
1017                         e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1018                 } else {
1019                         irr::SEvent evt;
1020                         evt.EventType            = EET_KEY_INPUT_EVENT;
1021                         evt.KeyInput.Key         = KEY_END;
1022                         evt.KeyInput.Char        = 0;
1023                         evt.KeyInput.Control     = 0;
1024                         evt.KeyInput.Shift       = 0;
1025                         evt.KeyInput.PressedDown = true;
1026                         e->OnEvent(evt);
1027                 }
1028
1029                 if (label.length() >= 1)
1030                 {
1031                         rect.UpperLeftCorner.Y -= 15;
1032                         rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
1033                         Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1034                 }
1035         }
1036         m_fields.push_back(spec);
1037 }
1038
1039 void GUIFormSpecMenu::parseField(parserData* data,std::string element,std::string type) {
1040         std::vector<std::string> parts = split(element,';');
1041
1042         if (parts.size() == 3) {
1043                 parseSimpleField(data,parts);
1044                 return;
1045         }
1046
1047         if (parts.size() == 5) {
1048                 parseTextArea(data,parts,type);
1049                 return;
1050         }
1051         errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'"  << std::endl;
1052 }
1053
1054 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element) {
1055         std::vector<std::string> parts = split(element,';');
1056
1057         if (parts.size() == 2) {
1058                 std::vector<std::string> v_pos = split(parts[0],',');
1059                 std::string text = parts[1];
1060
1061                 MY_CHECKPOS("label",0);
1062
1063                 v2s32 pos = padding;
1064                 pos.X += stof(v_pos[0]) * (float)spacing.X;
1065                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1066
1067                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
1068
1069                 if(data->bp_set != 2)
1070                         errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1071
1072                 text = unescape_string(text);
1073
1074                 std::wstring wlabel = narrow_to_wide(text.c_str());
1075
1076                 FieldSpec spec = FieldSpec(
1077                         L"",
1078                         wlabel,
1079                         L"",
1080                         258+m_fields.size()
1081                 );
1082                 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1083                 m_fields.push_back(spec);
1084                 return;
1085         }
1086         errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'"  << std::endl;
1087 }
1088
1089 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element) {
1090         std::vector<std::string> parts = split(element,';');
1091
1092         if (parts.size() == 2) {
1093                 std::vector<std::string> v_pos = split(parts[0],',');
1094                 std::wstring text = narrow_to_wide(unescape_string(parts[1]));
1095
1096                 MY_CHECKPOS("vertlabel",1);
1097
1098                 v2s32 pos = padding;
1099                 pos.X += stof(v_pos[0]) * (float)spacing.X;
1100                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1101
1102                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+15, pos.Y+300);
1103
1104                 if(data->bp_set != 2)
1105                         errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1106
1107                 std::wstring label = L"";
1108
1109                 for (unsigned int i=0; i < text.length(); i++) {
1110                         label += text[i];
1111                         label += L"\n";
1112                 }
1113
1114                 FieldSpec spec = FieldSpec(
1115                         L"",
1116                         label,
1117                         L"",
1118                         258+m_fields.size()
1119                 );
1120                 gui::IGUIStaticText *t =
1121                                 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1122                 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1123                 m_fields.push_back(spec);
1124                 return;
1125         }
1126         errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'"  << std::endl;
1127 }
1128
1129 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,std::string type) {
1130         std::vector<std::string> parts = split(element,';');
1131
1132         if ((parts.size() == 5) || (parts.size() == 7) || (parts.size() == 8)) {
1133                 std::vector<std::string> v_pos = split(parts[0],',');
1134                 std::vector<std::string> v_geom = split(parts[1],',');
1135                 std::string image_name = parts[2];
1136                 std::string name = parts[3];
1137                 std::string label = parts[4];
1138
1139                 MY_CHECKPOS("imagebutton",0);
1140                 MY_CHECKGEOM("imagebutton",1);
1141
1142                 v2s32 pos = padding;
1143                 pos.X += stof(v_pos[0]) * (float)spacing.X;
1144                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1145                 v2s32 geom;
1146                 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1147                 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1148
1149                 bool noclip = false;
1150                 bool drawborder = true;
1151
1152                 if ((parts.size() >= 7)) {
1153                         if (parts[5] == "true")
1154                                 noclip = true;
1155
1156                         if (parts[6] == "false")
1157                                 drawborder = false;
1158                 }
1159                 
1160                 std::string pressed_image_name = "";
1161                 
1162                 if ((parts.size() == 8)) {
1163                         pressed_image_name = parts[7];
1164                 }
1165
1166                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1167
1168                 if(data->bp_set != 2)
1169                         errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
1170
1171                 image_name = unescape_string(image_name);
1172                 pressed_image_name = unescape_string(pressed_image_name);
1173                 label = unescape_string(label);
1174
1175                 std::wstring wlabel = narrow_to_wide(label.c_str());
1176
1177                 FieldSpec spec = FieldSpec(
1178                         narrow_to_wide(name.c_str()),
1179                         wlabel,
1180                         narrow_to_wide(image_name.c_str()),
1181                         258+m_fields.size()
1182                 );
1183                 spec.ftype = f_Button;
1184                 if(type == "image_button_exit")
1185                         spec.is_exit = true;
1186
1187                 video::ITexture *texture = 0;
1188                 video::ITexture *pressed_texture = 0;
1189                 texture = m_tsrc->getTexture(image_name);
1190                 if (parts.size() == 8)
1191                         pressed_texture = m_tsrc->getTexture(pressed_image_name);
1192                 else
1193                         pressed_texture = texture;
1194
1195                 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1196
1197                 if (spec.fname == data->focused_fieldname) {
1198                         Environment->setFocus(e);
1199                 }
1200
1201                 e->setUseAlphaChannel(true);
1202                 e->setImage(texture);
1203                 e->setPressedImage(pressed_texture);
1204                 e->setScaleImage(true);
1205                 e->setNotClipped(noclip);
1206                 e->setDrawBorder(drawborder);
1207
1208                 m_fields.push_back(spec);
1209                 return;
1210         }
1211
1212         errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'"  << std::endl;
1213 }
1214
1215 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element) {
1216         std::vector<std::string> parts = split(element,';');
1217
1218         if ((parts.size() == 4) || (parts.size() == 6)) {
1219                 std::vector<std::string> v_pos = split(parts[0],',');
1220                 std::string name = parts[1];
1221                 std::vector<std::string> buttons = split(parts[2],',');
1222                 std::string str_index = parts[3];
1223                 bool show_background = true;
1224                 bool show_border = true;
1225                 int tab_index = stoi(str_index) -1;
1226
1227                 MY_CHECKPOS("tabheader",0);
1228
1229                 if (parts.size() == 6) {
1230                         if (parts[4] == "true")
1231                                 show_background = false;
1232                         if (parts[5] == "false")
1233                                 show_border = false;
1234                 }
1235
1236                 FieldSpec spec = FieldSpec(
1237                         narrow_to_wide(name.c_str()),
1238                         L"",
1239                         L"",
1240                         258+m_fields.size()
1241                 );
1242
1243                 spec.ftype = f_TabHeader;
1244
1245                 v2s32 pos = padding;
1246                 pos.X += stof(v_pos[0]) * (float)spacing.X;
1247                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1248                 v2s32 geom;
1249                 geom.X = data->screensize.Y;
1250                 geom.Y = 30;
1251
1252                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1253
1254                 gui::IGUITabControl *e = Environment->addTabControl(rect,this,show_background,show_border,spec.fid);
1255
1256                 if (spec.fname == data->focused_fieldname) {
1257                         Environment->setFocus(e);
1258                 }
1259
1260                 e->setNotClipped(true);
1261
1262                 for (unsigned int i=0; i< buttons.size(); i++) {
1263                         wchar_t* wbutton = 0;
1264
1265                         std::wstring wlabel = narrow_to_wide(buttons[i]); //Needed for displaying text on windows
1266                         wbutton = (wchar_t*) wlabel.c_str();
1267
1268                         e->addTab(wbutton,-1);
1269                 }
1270
1271                 if ((tab_index >= 0) &&
1272                                 (buttons.size() < INT_MAX) &&
1273                                 (tab_index < (int) buttons.size()))
1274                         e->setActiveTab(tab_index);
1275
1276                 m_fields.push_back(spec);
1277                 return;
1278         }
1279         errorstream<< "Invalid TabHeader element(" << parts.size() << "): '" << element << "'"  << std::endl;
1280 }
1281
1282 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element) {
1283
1284         if (m_gamedef == 0) {
1285                 errorstream<<"WARNING: invalid use of item_image_button with m_gamedef==0"<<std::endl;
1286                 return;
1287         }
1288
1289         std::vector<std::string> parts = split(element,';');
1290
1291         if (parts.size() == 5) {
1292                 std::vector<std::string> v_pos = split(parts[0],',');
1293                 std::vector<std::string> v_geom = split(parts[1],',');
1294                 std::string item_name = parts[2];
1295                 std::string name = parts[3];
1296                 std::string label = parts[4];
1297
1298                 MY_CHECKPOS("itemimagebutton",0);
1299                 MY_CHECKGEOM("itemimagebutton",1);
1300
1301                 v2s32 pos = padding;
1302                 pos.X += stof(v_pos[0]) * (float)spacing.X;
1303                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1304                 v2s32 geom;
1305                 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1306                 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1307
1308                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1309
1310                 if(data->bp_set != 2)
1311                         errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1312
1313                 IItemDefManager *idef = m_gamedef->idef();
1314                 ItemStack item;
1315                 item.deSerialize(item_name, idef);
1316                 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1317                 std::string tooltip = item.getDefinition(idef).description;
1318
1319                 label = unescape_string(label);
1320                 FieldSpec spec = FieldSpec(
1321                         narrow_to_wide(name.c_str()),
1322                         narrow_to_wide(label.c_str()),
1323                         narrow_to_wide(item_name.c_str()),
1324                         258+m_fields.size()
1325                 );
1326
1327                 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1328
1329                 if (spec.fname == data->focused_fieldname) {
1330                         Environment->setFocus(e);
1331                 }
1332
1333                 e->setUseAlphaChannel(true);
1334                 e->setImage(texture);
1335                 e->setPressedImage(texture);
1336                 e->setScaleImage(true);
1337                 spec.ftype = f_Button;
1338                 rect+=data->basepos-padding;
1339                 spec.rect=rect;
1340                 if (tooltip!="")
1341                         spec.tooltip=tooltip;
1342                 m_fields.push_back(spec);
1343                 return;
1344         }
1345         errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'"  << std::endl;
1346 }
1347
1348 void GUIFormSpecMenu::parseBox(parserData* data,std::string element) {
1349         std::vector<std::string> parts = split(element,';');
1350
1351         if (parts.size() == 3) {
1352                 std::vector<std::string> v_pos = split(parts[0],',');
1353                 std::vector<std::string> v_geom = split(parts[1],',');
1354
1355                 MY_CHECKPOS("box",0);
1356                 MY_CHECKGEOM("box",1);
1357
1358                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1359                 pos.X += stof(v_pos[0]) * (float) spacing.X;
1360                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1361
1362                 v2s32 geom;
1363                 geom.X = stof(v_geom[0]) * (float)spacing.X;
1364                 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1365
1366                 video::SColor tmp_color;
1367
1368                 if (parseColor(parts[2], tmp_color, false)) {
1369                         BoxDrawSpec spec(pos, geom, tmp_color);
1370
1371                         m_boxes.push_back(spec);
1372                 }
1373                 else {
1374                         errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'  INVALID COLOR"  << std::endl;
1375                 }
1376                 return;
1377         }
1378         errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'"  << std::endl;
1379 }
1380
1381 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element) {
1382         std::vector<std::string> parts = split(element,';');
1383
1384         if ((parts.size() == 1) || (parts.size() == 2)) {
1385                 parseColor(parts[0],m_bgcolor,false);
1386
1387                 if (parts.size() == 2) {
1388                         std::string fullscreen = parts[1];
1389                         m_bgfullscreen = is_yes(fullscreen);
1390                 }
1391                 return;
1392         }
1393         errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'"  << std::endl;
1394 }
1395
1396 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element) {
1397         std::vector<std::string> parts = split(element,';');
1398
1399         if ((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) {
1400                 parseColor(parts[0], m_slotbg_n, false);
1401                 parseColor(parts[1], m_slotbg_h, false);
1402                 
1403                 if (parts.size() >= 3) {
1404                         if (parseColor(parts[2], m_slotbordercolor, false)) {
1405                                 m_slotborder = true;
1406                         }
1407                 }
1408                 if (parts.size() == 5) {
1409                         video::SColor tmp_color;
1410
1411                         if (parseColor(parts[3], tmp_color, false))
1412                                 m_tooltip_element->setBackgroundColor(tmp_color);
1413                         if (parseColor(parts[4], tmp_color, false))
1414                                 m_tooltip_element->setOverrideColor(tmp_color);
1415                 }
1416                 return;
1417         }
1418         errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'"  << std::endl;
1419 }
1420
1421 void GUIFormSpecMenu::parseElement(parserData* data,std::string element) {
1422
1423         //some prechecks
1424         if (element == "")
1425                 return;
1426
1427         std::vector<std::string> parts = split(element,'[');
1428
1429         // ugly workaround to keep compatibility
1430         if (parts.size() > 2) {
1431                 if (trim(parts[0]) == "image") {
1432                         for (unsigned int i=2;i< parts.size(); i++) {
1433                                 parts[1] += "[" + parts[i];
1434                         }
1435                 }
1436                 else { return; }
1437         }
1438
1439         if (parts.size() < 2) {
1440                 return;
1441         }
1442
1443         std::string type = trim(parts[0]);
1444         std::string description = trim(parts[1]);
1445
1446         if ((type == "size") || (type == "invsize")){
1447                 parseSize(data,description);
1448                 return;
1449         }
1450
1451         if (type == "list") {
1452                 parseList(data,description);
1453                 return;
1454         }
1455
1456         if (type == "checkbox") {
1457                 parseCheckbox(data,description);
1458                 return;
1459         }
1460
1461         if (type == "image") {
1462                 parseImage(data,description);
1463                 return;
1464         }
1465
1466         if (type == "item_image") {
1467                 parseItemImage(data,description);
1468                 return;
1469         }
1470
1471         if ((type == "button") || (type == "button_exit")) {
1472                 parseButton(data,description,type);
1473                 return;
1474         }
1475
1476         if (type == "background") {
1477                 parseBackground(data,description);
1478                 return;
1479         }
1480
1481         if (type == "textlist"){
1482                 parseTextList(data,description);
1483                 return;
1484         }
1485
1486         if (type == "dropdown"){
1487                 parseDropDown(data,description);
1488                 return;
1489         }
1490
1491         if (type == "pwdfield") {
1492                 parsePwdField(data,description);
1493                 return;
1494         }
1495
1496         if ((type == "field") || (type == "textarea")){
1497                 parseField(data,description,type);
1498                 return;
1499         }
1500
1501         if (type == "label") {
1502                 parseLabel(data,description);
1503                 return;
1504         }
1505
1506         if (type == "vertlabel") {
1507                 parseVertLabel(data,description);
1508                 return;
1509         }
1510
1511         if (type == "item_image_button") {
1512                 parseItemImageButton(data,description);
1513                 return;
1514         }
1515
1516         if ((type == "image_button") || (type == "image_button_exit")) {
1517                 parseImageButton(data,description,type);
1518                 return;
1519         }
1520
1521         if (type == "tabheader") {
1522                 parseTabHeader(data,description);
1523                 return;
1524         }
1525
1526         if (type == "box") {
1527                 parseBox(data,description);
1528                 return;
1529         }
1530
1531         if (type == "bgcolor") {
1532                 parseBackgroundColor(data,description);
1533                 return;
1534         }
1535
1536         if (type == "listcolors") {
1537                 parseListColors(data,description);
1538                 return;
1539         }
1540
1541         // Ignore others
1542         infostream
1543                 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1544                 <<std::endl;
1545 }
1546
1547
1548
1549 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1550 {
1551         parserData mydata;
1552
1553         //preserve listboxes
1554         for (unsigned int i = 0; i < m_listboxes.size(); i++) {
1555                 std::wstring listboxname = m_listboxes[i].first.fname;
1556                 gui::IGUIListBox *listbox = m_listboxes[i].second;
1557
1558                 int selection = listbox->getSelected();
1559                 if (selection != -1) {
1560                         mydata.listbox_selections[listboxname] = selection;
1561                 }
1562
1563                 gui::IGUIScrollBar *scrollbar = getListboxScrollbar(listbox);
1564                 if (scrollbar) {
1565                         mydata.listbox_scroll[listboxname] = scrollbar->getPos();
1566                 }
1567         }
1568
1569         //preserve focus
1570         gui::IGUIElement *focused_element = Environment->getFocus();
1571         if (focused_element && focused_element->getParent() == this) {
1572                 s32 focused_id = focused_element->getID();
1573                 if (focused_id > 257) {
1574                         for (u32 i=0; i<m_fields.size(); i++) {
1575                                 if (m_fields[i].fid == focused_id) {
1576                                         mydata.focused_fieldname =
1577                                                 m_fields[i].fname;
1578                                         break;
1579                                 }
1580                         }
1581                 }
1582         }
1583
1584         // Remove children
1585         removeChildren();
1586
1587         mydata.size= v2s32(100,100);
1588         mydata.helptext_h = 15;
1589         mydata.screensize = screensize;
1590
1591         // Base position of contents of form
1592         mydata.basepos = getBasePos();
1593
1594         // State of basepos, 0 = not set, 1= set by formspec, 2 = set by size[] element
1595         // Used to adjust form size automatically if needed
1596         // A proceed button is added if there is no size[] element
1597         mydata.bp_set = 0;
1598
1599         
1600         /* Convert m_init_draw_spec to m_inventorylists */
1601         
1602         m_inventorylists.clear();
1603         m_images.clear();
1604         m_backgrounds.clear();
1605         m_itemimages.clear();
1606         m_listboxes.clear();
1607         m_checkboxes.clear();
1608         m_fields.clear();
1609         m_boxes.clear();
1610
1611         // Set default values (fits old formspec values)
1612         m_bgcolor = video::SColor(140,0,0,0);
1613         m_bgfullscreen = false;
1614
1615         m_slotbg_n = video::SColor(255,128,128,128);
1616         m_slotbg_h = video::SColor(255,192,192,192);
1617
1618         m_slotbordercolor = video::SColor(200,0,0,0);
1619         m_slotborder = false;
1620
1621         m_clipbackground = false;
1622         // Add tooltip
1623         {
1624                 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1625                 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1626                 m_tooltip_element->enableOverrideColor(true);
1627                 m_tooltip_element->setBackgroundColor(video::SColor(255,110,130,60));
1628                 m_tooltip_element->setDrawBackground(true);
1629                 m_tooltip_element->setDrawBorder(true);
1630                 m_tooltip_element->setOverrideColor(video::SColor(255,255,255,255));
1631                 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1632                 m_tooltip_element->setWordWrap(false);
1633                 //we're not parent so no autograb for this one!
1634                 m_tooltip_element->grab();
1635         }
1636
1637
1638         std::vector<std::string> elements = split(m_formspec_string,']');
1639         for (unsigned int i=0;i< elements.size();i++) {
1640                 parseElement(&mydata,elements[i]);
1641         }
1642
1643         // If there's fields, add a Proceed button
1644         if (m_fields.size() && mydata.bp_set != 2)
1645         {
1646                 // if the size wasn't set by an invsize[] or size[] adjust it now to fit all the fields
1647                 mydata.rect = core::rect<s32>(
1648                                 mydata.screensize.X/2 - 580/2,
1649                                 mydata.screensize.Y/2 - 300/2,
1650                                 mydata.screensize.X/2 + 580/2,
1651                                 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
1652                 );
1653                 DesiredRect = mydata.rect;
1654                 recalculateAbsolutePosition(false);
1655                 mydata.basepos = getBasePos();
1656
1657                 {
1658                         v2s32 pos = mydata.basepos;
1659                         pos.Y = ((m_fields.size()+2)*60);
1660
1661                         v2s32 size = DesiredRect.getSize();
1662                         mydata.rect = core::rect<s32>(size.X/2-70, pos.Y, (size.X/2-70)+140, pos.Y+30);
1663                         wchar_t* text = wgettext("Proceed");
1664                         Environment->addButton(mydata.rect, this, 257, text);
1665                         delete[] text;
1666                 }
1667
1668         }
1669
1670         //set initial focus if parser didn't set it
1671         focused_element = Environment->getFocus();
1672         if (!focused_element
1673                         || !isMyChild(focused_element)
1674                         || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
1675                 setInitialFocus();
1676 }
1677
1678 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
1679 {
1680         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1681         
1682         for(u32 i=0; i<m_inventorylists.size(); i++)
1683         {
1684                 const ListDrawSpec &s = m_inventorylists[i];
1685
1686                 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1687                 {
1688                         s32 item_i = i + s.start_item_i;
1689                         s32 x = (i%s.geom.X) * spacing.X;
1690                         s32 y = (i/s.geom.X) * spacing.Y;
1691                         v2s32 p0(x,y);
1692                         core::rect<s32> rect = imgrect + s.pos + p0;
1693                         if(rect.isPointInside(p))
1694                         {
1695                                 return ItemSpec(s.inventoryloc, s.listname, item_i);
1696                         }
1697                 }
1698         }
1699
1700         return ItemSpec(InventoryLocation(), "", -1);
1701 }
1702
1703 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
1704 {
1705         video::IVideoDriver* driver = Environment->getVideoDriver();
1706
1707         // Get font
1708         gui::IGUIFont *font = NULL;
1709         gui::IGUISkin* skin = Environment->getSkin();
1710         if (skin)
1711                 font = skin->getFont();
1712         
1713         Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1714         if(!inv){
1715                 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1716                                 <<"The inventory location "
1717                                 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
1718                                 <<std::endl;
1719                 return;
1720         }
1721         InventoryList *ilist = inv->getList(s.listname);
1722         if(!ilist){
1723                 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1724                                 <<"The inventory list \""<<s.listname<<"\" @ \""
1725                                 <<s.inventoryloc.dump()<<"\" doesn't exist"
1726                                 <<std::endl;
1727                 return;
1728         }
1729         
1730         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1731         
1732         for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1733         {
1734                 s32 item_i = i + s.start_item_i;
1735                 if(item_i >= (s32) ilist->getSize())
1736                         break;
1737                 s32 x = (i%s.geom.X) * spacing.X;
1738                 s32 y = (i/s.geom.X) * spacing.Y;
1739                 v2s32 p(x,y);
1740                 core::rect<s32> rect = imgrect + s.pos + p;
1741                 ItemStack item;
1742                 if(ilist)
1743                         item = ilist->getItem(item_i);
1744
1745                 bool selected = m_selected_item
1746                         && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
1747                         && m_selected_item->listname == s.listname
1748                         && m_selected_item->i == item_i;
1749                 bool hovering = rect.isPointInside(m_pointer);
1750
1751                 if(phase == 0)
1752                 {
1753                         if(hovering)
1754                                 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
1755                         else
1756                                 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
1757                 }
1758
1759                 //Draw inv slot borders
1760                 if (m_slotborder) {
1761                         s32 x1 = rect.UpperLeftCorner.X;
1762                         s32 y1 = rect.UpperLeftCorner.Y;
1763                         s32 x2 = rect.LowerRightCorner.X;
1764                         s32 y2 = rect.LowerRightCorner.Y;
1765                         s32 border = 1;
1766                         driver->draw2DRectangle(m_slotbordercolor,
1767                                 core::rect<s32>(v2s32(x1 - border, y1 - border),
1768                                                                 v2s32(x2 + border, y1)), NULL);
1769                         driver->draw2DRectangle(m_slotbordercolor,
1770                                 core::rect<s32>(v2s32(x1 - border, y2),
1771                                                                 v2s32(x2 + border, y2 + border)), NULL);
1772                         driver->draw2DRectangle(m_slotbordercolor,
1773                                 core::rect<s32>(v2s32(x1 - border, y1),
1774                                                                 v2s32(x1, y2)), NULL);
1775                         driver->draw2DRectangle(m_slotbordercolor,
1776                                 core::rect<s32>(v2s32(x2, y1),
1777                                                                 v2s32(x2 + border, y2)), NULL);
1778                 }
1779
1780                 if(phase == 1)
1781                 {
1782                         // Draw item stack
1783                         if(selected)
1784                         {
1785                                 item.takeItem(m_selected_amount);
1786                         }
1787                         if(!item.empty())
1788                         {
1789                                 drawItemStack(driver, font, item,
1790                                                 rect, &AbsoluteClippingRect, m_gamedef);
1791                         }
1792
1793                         // Draw tooltip
1794                         std::string tooltip_text = "";
1795                         if(hovering && !m_selected_item)
1796                                 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
1797                         if(tooltip_text != "")
1798                         {
1799                                 m_tooltip_element->setVisible(true);
1800                                 this->bringToFront(m_tooltip_element);
1801                                 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
1802                                 s32 tooltip_x = m_pointer.X + 15;
1803                                 s32 tooltip_y = m_pointer.Y + 15;
1804                                 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1805                                 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1806                                 m_tooltip_element->setRelativePosition(core::rect<s32>(
1807                                                 core::position2d<s32>(tooltip_x, tooltip_y),
1808                                                 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1809                         }
1810                 }
1811         }
1812 }
1813
1814 void GUIFormSpecMenu::drawSelectedItem()
1815 {
1816         if(!m_selected_item)
1817                 return;
1818
1819         video::IVideoDriver* driver = Environment->getVideoDriver();
1820
1821         // Get font
1822         gui::IGUIFont *font = NULL;
1823         gui::IGUISkin* skin = Environment->getSkin();
1824         if (skin)
1825                 font = skin->getFont();
1826         
1827         Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
1828         assert(inv);
1829         InventoryList *list = inv->getList(m_selected_item->listname);
1830         assert(list);
1831         ItemStack stack = list->getItem(m_selected_item->i);
1832         stack.count = m_selected_amount;
1833
1834         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1835         core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
1836         drawItemStack(driver, font, stack, rect, NULL, m_gamedef);
1837 }
1838
1839 void GUIFormSpecMenu::drawMenu()
1840 {
1841         if(m_form_src){
1842                 std::string newform = m_form_src->getForm();
1843                 if(newform != m_formspec_string){
1844                         m_formspec_string = newform;
1845                         regenerateGui(m_screensize_old);
1846                 }
1847         }
1848
1849         m_pointer = m_device->getCursorControl()->getPosition();
1850
1851         updateSelectedItem();
1852
1853         gui::IGUISkin* skin = Environment->getSkin();
1854         if (!skin)
1855                 return;
1856         video::IVideoDriver* driver = Environment->getVideoDriver();
1857         
1858         v2u32 screenSize = driver->getScreenSize();
1859         core::rect<s32> allbg(0, 0, screenSize.X ,      screenSize.Y);
1860         if (m_bgfullscreen)
1861                 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
1862         else
1863                 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
1864
1865         m_tooltip_element->setVisible(false);
1866
1867         /*
1868                 Draw backgrounds
1869         */
1870         for(u32 i=0; i<m_backgrounds.size(); i++)
1871         {
1872                 const ImageDrawSpec &spec = m_backgrounds[i];
1873                 video::ITexture *texture = m_tsrc->getTexture(spec.name);
1874
1875                 if (texture != 0) {
1876                         // Image size on screen
1877                         core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1878                         // Image rectangle on screen
1879                         core::rect<s32> rect = imgrect + spec.pos;
1880
1881                         if (m_clipbackground) {
1882                                 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
1883                                 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
1884                                                                         AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
1885                                                                         AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
1886                                                                         AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
1887                         }
1888
1889                         const video::SColor color(255,255,255,255);
1890                         const video::SColor colors[] = {color,color,color,color};
1891                         driver->draw2DImage(texture, rect,
1892                                 core::rect<s32>(core::position2d<s32>(0,0),
1893                                                 core::dimension2di(texture->getOriginalSize())),
1894                                 NULL/*&AbsoluteClippingRect*/, colors, true);
1895                 }
1896                 else {
1897                         errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
1898                         errorstream << "\t" << spec.name << std::endl;
1899                 }
1900         }
1901         
1902         /*
1903                 Draw Boxes
1904         */
1905         for(u32 i=0; i<m_boxes.size(); i++)
1906         {
1907                 const BoxDrawSpec &spec = m_boxes[i];
1908
1909                 irr::video::SColor todraw = spec.color;
1910
1911                 todraw.setAlpha(140);
1912
1913                 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
1914                                                         spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
1915
1916                 driver->draw2DRectangle(todraw, rect, 0);
1917         }
1918         /*
1919                 Draw images
1920         */
1921         for(u32 i=0; i<m_images.size(); i++)
1922         {
1923                 const ImageDrawSpec &spec = m_images[i];
1924                 video::ITexture *texture = m_tsrc->getTexture(spec.name);
1925
1926                 if (texture != 0) {
1927                         const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
1928                         // Image size on screen
1929                         core::rect<s32> imgrect;
1930
1931                         if (spec.scale)
1932                                 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
1933                         else {
1934
1935                                 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
1936                         }
1937                         // Image rectangle on screen
1938                         core::rect<s32> rect = imgrect + spec.pos;
1939                         const video::SColor color(255,255,255,255);
1940                         const video::SColor colors[] = {color,color,color,color};
1941                         driver->draw2DImage(texture, rect,
1942                                 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
1943                                 NULL/*&AbsoluteClippingRect*/, colors, true);
1944                 }
1945                 else {
1946                         errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
1947                         errorstream << "\t" << spec.name << std::endl;
1948                 }
1949         }
1950         
1951         /*
1952                 Draw item images
1953         */
1954         for(u32 i=0; i<m_itemimages.size(); i++)
1955         {
1956                 if (m_gamedef == 0)
1957                         break;
1958
1959                 const ImageDrawSpec &spec = m_itemimages[i];
1960                 IItemDefManager *idef = m_gamedef->idef();
1961                 ItemStack item;
1962                 item.deSerialize(spec.name, idef);
1963                 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1964                 // Image size on screen
1965                 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1966                 // Image rectangle on screen
1967                 core::rect<s32> rect = imgrect + spec.pos;
1968                 const video::SColor color(255,255,255,255);
1969                 const video::SColor colors[] = {color,color,color,color};
1970                 driver->draw2DImage(texture, rect,
1971                         core::rect<s32>(core::position2d<s32>(0,0),
1972                                         core::dimension2di(texture->getOriginalSize())),
1973                         NULL/*&AbsoluteClippingRect*/, colors, true);
1974         }
1975         
1976         /*
1977                 Draw items
1978                 Phase 0: Item slot rectangles
1979                 Phase 1: Item images; prepare tooltip
1980         */
1981         int start_phase=0;
1982         for(int phase=start_phase; phase<=1; phase++)
1983         for(u32 i=0; i<m_inventorylists.size(); i++)
1984         {
1985                 drawList(m_inventorylists[i], phase);
1986         }
1987
1988         /*
1989                 Call base class
1990         */
1991         gui::IGUIElement::draw();
1992         
1993         /*
1994                 Draw fields/buttons tooltips
1995         */
1996         for(u32 i=0; i<m_fields.size(); i++)
1997         {
1998                 const FieldSpec &spec = m_fields[i];
1999                 if (spec.tooltip != "")
2000                 {
2001                         core::rect<s32> rect = spec.rect;
2002                         if (rect.isPointInside(m_pointer))
2003                         {
2004                                 m_tooltip_element->setVisible(true);
2005                                 this->bringToFront(m_tooltip_element);
2006                                 m_tooltip_element->setText(narrow_to_wide(spec.tooltip).c_str());
2007                                 s32 tooltip_x = m_pointer.X + 15;
2008                                 s32 tooltip_y = m_pointer.Y + 15;
2009                                 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
2010                                 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2011                                 m_tooltip_element->setRelativePosition(core::rect<s32>(
2012                                 core::position2d<s32>(tooltip_x, tooltip_y),
2013                                 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2014                         }
2015                 }
2016         }
2017         
2018         /*
2019                 Draw dragged item stack
2020         */
2021         drawSelectedItem();
2022 }
2023
2024 void GUIFormSpecMenu::updateSelectedItem()
2025 {
2026         // If the selected stack has become empty for some reason, deselect it.
2027         // If the selected stack has become inaccessible, deselect it.
2028         // If the selected stack has become smaller, adjust m_selected_amount.
2029         ItemStack selected = verifySelectedItem();
2030
2031         // WARNING: BLACK MAGIC
2032         // See if there is a stack suited for our current guess.
2033         // If such stack does not exist, clear the guess.
2034         if(m_selected_content_guess.name != "" &&
2035                         selected.name == m_selected_content_guess.name &&
2036                         selected.count == m_selected_content_guess.count){
2037                 // Selected item fits the guess. Skip the black magic.
2038         }
2039         else if(m_selected_content_guess.name != ""){
2040                 bool found = false;
2041                 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2042                         const ListDrawSpec &s = m_inventorylists[i];
2043                         Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2044                         if(!inv)
2045                                 continue;
2046                         InventoryList *list = inv->getList(s.listname);
2047                         if(!list)
2048                                 continue;
2049                         for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2050                                 u32 item_i = i + s.start_item_i;
2051                                 if(item_i >= list->getSize())
2052                                         continue;
2053                                 ItemStack stack = list->getItem(item_i);
2054                                 if(stack.name == m_selected_content_guess.name &&
2055                                                 stack.count == m_selected_content_guess.count){
2056                                         found = true;
2057                                         infostream<<"Client: Changing selected content guess to "
2058                                                         <<s.inventoryloc.dump()<<" "<<s.listname
2059                                                         <<" "<<item_i<<std::endl;
2060                                         delete m_selected_item;
2061                                         m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2062                                         m_selected_amount = stack.count;
2063                                 }
2064                         }
2065                 }
2066                 if(!found){
2067                         infostream<<"Client: Discarding selected content guess: "
2068                                         <<m_selected_content_guess.getItemString()<<std::endl;
2069                         m_selected_content_guess.name = "";
2070                 }
2071         }
2072
2073         // If craftresult is nonempty and nothing else is selected, select it now.
2074         if(!m_selected_item)
2075         {
2076                 for(u32 i=0; i<m_inventorylists.size(); i++)
2077                 {
2078                         const ListDrawSpec &s = m_inventorylists[i];
2079                         if(s.listname == "craftpreview")
2080                         {
2081                                 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2082                                 InventoryList *list = inv->getList("craftresult");
2083                                 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2084                                 {
2085                                         m_selected_item = new ItemSpec;
2086                                         m_selected_item->inventoryloc = s.inventoryloc;
2087                                         m_selected_item->listname = "craftresult";
2088                                         m_selected_item->i = 0;
2089                                         m_selected_amount = 0;
2090                                         m_selected_dragging = false;
2091                                         break;
2092                                 }
2093                         }
2094                 }
2095         }
2096
2097         // If craftresult is selected, keep the whole stack selected
2098         if(m_selected_item && m_selected_item->listname == "craftresult")
2099         {
2100                 m_selected_amount = verifySelectedItem().count;
2101         }
2102 }
2103
2104 ItemStack GUIFormSpecMenu::verifySelectedItem()
2105 {
2106         // If the selected stack has become empty for some reason, deselect it.
2107         // If the selected stack has become inaccessible, deselect it.
2108         // If the selected stack has become smaller, adjust m_selected_amount.
2109         // Return the selected stack.
2110
2111         if(m_selected_item)
2112         {
2113                 if(m_selected_item->isValid())
2114                 {
2115                         Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2116                         if(inv)
2117                         {
2118                                 InventoryList *list = inv->getList(m_selected_item->listname);
2119                                 if(list && (u32) m_selected_item->i < list->getSize())
2120                                 {
2121                                         ItemStack stack = list->getItem(m_selected_item->i);
2122                                         if(m_selected_amount > stack.count)
2123                                                 m_selected_amount = stack.count;
2124                                         if(!stack.empty())
2125                                                 return stack;
2126                                 }
2127                         }
2128                 }
2129
2130                 // selection was not valid
2131                 delete m_selected_item;
2132                 m_selected_item = NULL;
2133                 m_selected_amount = 0;
2134                 m_selected_dragging = false;
2135         }
2136         return ItemStack();
2137 }
2138
2139 void GUIFormSpecMenu::acceptInput(bool quit=false)
2140 {
2141         if(m_text_dst)
2142         {
2143                 std::map<std::string, std::string> fields;
2144
2145                 if (quit) {
2146                         fields["quit"] = "true";
2147                 }
2148
2149                 if (current_keys_pending.key_down) {
2150                         fields["key_down"] = "true";
2151                         current_keys_pending.key_down = false;
2152                 }
2153
2154                 if (current_keys_pending.key_up) {
2155                         fields["key_up"] = "true";
2156                         current_keys_pending.key_up = false;
2157                 }
2158
2159                 if (current_keys_pending.key_enter) {
2160                         fields["key_enter"] = "true";
2161                         current_keys_pending.key_enter = false;
2162                 }
2163
2164                 if (current_keys_pending.key_escape) {
2165                         fields["key_escape"] = "true";
2166                         current_keys_pending.key_escape = false;
2167                 }
2168
2169                 for(u32 i=0; i<m_fields.size(); i++)
2170                 {
2171                         const FieldSpec &s = m_fields[i];
2172                         if(s.send)
2173                         {
2174                                 if(s.ftype == f_Button)
2175                                 {
2176                                         fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(s.flabel.c_str());
2177                                 }
2178                                 else if(s.ftype == f_ListBox) {
2179                                         std::stringstream ss;
2180
2181                                         if (m_listbox_doubleclick) {
2182                                                 ss << "DCL:";
2183                                         }
2184                                         else {
2185                                                 ss << "CHG:";
2186                                         }
2187                                         ss << (getListboxIndex(wide_to_narrow(s.fname.c_str()))+1);
2188                                         fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2189                                 }
2190                                 else if(s.ftype == f_DropDown) {
2191                                         // no dynamic cast possible due to some distributions shipped
2192                                         // without rtti support in irrlicht
2193                                         IGUIElement * element = getElementFromId(s.fid);
2194                                         gui::IGUIComboBox *e = NULL;
2195                                         if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2196                                                 e = static_cast<gui::IGUIComboBox*>(element);
2197                                         }
2198                                         s32 selected = e->getSelected();
2199                                         if (selected >= 0) {
2200                                                 fields[wide_to_narrow(s.fname.c_str())] =
2201                                                         wide_to_narrow(e->getItem(selected));
2202                                         }
2203                                 }
2204                                 else if (s.ftype == f_TabHeader) {
2205                                         // no dynamic cast possible due to some distributions shipped
2206                                         // without rtti support in irrlicht
2207                                         IGUIElement * element = getElementFromId(s.fid);
2208                                         gui::IGUITabControl *e = NULL;
2209                                         if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2210                                                 e = static_cast<gui::IGUITabControl*>(element);
2211                                         }
2212
2213                                         if (e != 0) {
2214                                                 std::stringstream ss;
2215                                                 ss << (e->getActiveTab() +1);
2216                                                 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2217                                         }
2218                                 }
2219                                 else if (s.ftype == f_CheckBox) {
2220                                         // no dynamic cast possible due to some distributions shipped
2221                                         // without rtti support in irrlicht
2222                                         IGUIElement * element = getElementFromId(s.fid);
2223                                         gui::IGUICheckBox *e = NULL;
2224                                         if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2225                                                 e = static_cast<gui::IGUICheckBox*>(element);
2226                                         }
2227
2228                                         if (e != 0) {
2229                                                 if (e->isChecked())
2230                                                         fields[wide_to_narrow(s.fname.c_str())] = "true";
2231                                                 else
2232                                                         fields[wide_to_narrow(s.fname.c_str())] = "false";
2233                                         }
2234                                 }
2235                                 else
2236                                 {
2237                                         IGUIElement* e = getElementFromId(s.fid);
2238                                         if(e != NULL)
2239                                         {
2240                                                 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(e->getText());
2241                                         }
2242                                 }
2243                         }
2244                 }
2245
2246                 m_text_dst->gotText(fields);
2247         }
2248 }
2249
2250 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2251 {
2252         // Fix Esc/Return key being eaten by checkboxen and listboxen
2253         if(event.EventType==EET_KEY_INPUT_EVENT)
2254         {
2255                 KeyPress kp(event.KeyInput);
2256                 if (kp == EscapeKey || kp == getKeySetting("keymap_inventory")
2257                                 || event.KeyInput.Key==KEY_RETURN)
2258                 {
2259                         gui::IGUIElement *focused = Environment->getFocus();
2260                         if (focused && isMyChild(focused) &&
2261                                         (focused->getType() == gui::EGUIET_LIST_BOX ||
2262                                          focused->getType() == gui::EGUIET_CHECK_BOX)) {
2263                                 OnEvent(event);
2264                                 return true;
2265                         }
2266                 }
2267         }
2268         // Mouse wheel events: send to hovered element instead of focused
2269         if(event.EventType==EET_MOUSE_INPUT_EVENT
2270                         && event.MouseInput.Event == EMIE_MOUSE_WHEEL)
2271         {
2272                 s32 x = event.MouseInput.X;
2273                 s32 y = event.MouseInput.Y;
2274                 gui::IGUIElement *hovered =
2275                         Environment->getRootGUIElement()->getElementFromPoint(
2276                                 core::position2d<s32>(x, y));
2277                 if (hovered && isMyChild(hovered)) {
2278                         hovered->OnEvent(event);
2279                         return true;
2280                 }
2281         }
2282         return false;
2283 }
2284
2285 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
2286 {
2287         if(event.EventType==EET_KEY_INPUT_EVENT)
2288         {
2289                 KeyPress kp(event.KeyInput);
2290                 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
2291                         kp == getKeySetting("keymap_inventory")))
2292                 {
2293                         if (m_allowclose) {
2294                                 acceptInput(true);
2295                                 quitMenu();
2296                          } else {
2297                                 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
2298                         }
2299                         return true;
2300                 }
2301                 if (event.KeyInput.PressedDown &&
2302                         (event.KeyInput.Key==KEY_RETURN ||
2303                          event.KeyInput.Key==KEY_UP ||
2304                          event.KeyInput.Key==KEY_DOWN)
2305                         ) {
2306
2307
2308                         switch (event.KeyInput.Key) {
2309                                 case KEY_RETURN:
2310                                         current_keys_pending.key_enter = true;
2311                                         break;
2312                                 case KEY_UP:
2313                                         current_keys_pending.key_up = true;
2314                                         break;
2315                                 case KEY_DOWN:
2316                                         current_keys_pending.key_down = true;
2317                                         break;
2318                                 break;
2319                                 default:
2320                                         //can't happen at all!
2321                                         assert("reached a source line that can't ever been reached" == 0);
2322                                         break;
2323                         }
2324                         if (current_keys_pending.key_enter && m_allowclose) {
2325                                 acceptInput(true);
2326                                 quitMenu();
2327                         }
2328                         else {
2329                                 acceptInput();
2330                         }
2331                         return true;
2332                 }
2333
2334         }
2335         if(event.EventType==EET_MOUSE_INPUT_EVENT
2336                         && event.MouseInput.Event != EMIE_MOUSE_MOVED)
2337         {
2338                 // Mouse event other than movement
2339
2340                 // Get selected item and hovered/clicked item (s)
2341
2342                 updateSelectedItem();
2343                 ItemSpec s = getItemAtPos(m_pointer);
2344
2345                 Inventory *inv_selected = NULL;
2346                 Inventory *inv_s = NULL;
2347
2348                 if(m_selected_item)
2349                 {
2350                         inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
2351                         assert(inv_selected);
2352                         assert(inv_selected->getList(m_selected_item->listname) != NULL);
2353                 }
2354
2355                 u32 s_count = 0;
2356
2357                 if(s.isValid())
2358                 do{ // breakable
2359                         inv_s = m_invmgr->getInventory(s.inventoryloc);
2360
2361                         if(!inv_s){
2362                                 errorstream<<"InventoryMenu: The selected inventory location "
2363                                                 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2364                                                 <<std::endl;
2365                                 s.i = -1;  // make it invalid again
2366                                 break;
2367                         }
2368
2369                         InventoryList *list = inv_s->getList(s.listname);
2370                         if(list == NULL){
2371                                 verbosestream<<"InventoryMenu: The selected inventory list \""
2372                                                 <<s.listname<<"\" does not exist"<<std::endl;
2373                                 s.i = -1;  // make it invalid again
2374                                 break;
2375                         }
2376
2377                         if((u32)s.i >= list->getSize()){
2378                                 infostream<<"InventoryMenu: The selected inventory list \""
2379                                                 <<s.listname<<"\" is too small (i="<<s.i<<", size="
2380                                                 <<list->getSize()<<")"<<std::endl;
2381                                 s.i = -1;  // make it invalid again
2382                                 break;
2383                         }
2384
2385                         s_count = list->getItem(s.i).count;
2386                 }while(0);
2387
2388                 bool identical = (m_selected_item != NULL) && s.isValid() &&
2389                         (inv_selected == inv_s) &&
2390                         (m_selected_item->listname == s.listname) &&
2391                         (m_selected_item->i == s.i);
2392
2393                 // buttons: 0 = left, 1 = right, 2 = middle
2394                 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event
2395                 int button = 0;
2396                 int updown = 2;
2397                 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
2398                         { button = 0; updown = 0; }
2399                 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
2400                         { button = 1; updown = 0; }
2401                 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
2402                         { button = 2; updown = 0; }
2403                 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
2404                         { button = 0; updown = 1; }
2405                 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
2406                         { button = 1; updown = 1; }
2407                 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
2408                         { button = 2; updown = 1; }
2409
2410                 // Set this number to a positive value to generate a move action
2411                 // from m_selected_item to s.
2412                 u32 move_amount = 0;
2413
2414                 // Set this number to a positive value to generate a drop action
2415                 // from m_selected_item.
2416                 u32 drop_amount = 0;
2417
2418                 // Set this number to a positive value to generate a craft action at s.
2419                 u32 craft_amount = 0;
2420
2421                 if(updown == 0)
2422                 {
2423                         // Some mouse button has been pressed
2424
2425                         //infostream<<"Mouse button "<<button<<" pressed at p=("
2426                         //      <<p.X<<","<<p.Y<<")"<<std::endl;
2427
2428                         m_selected_dragging = false;
2429
2430                         if(s.isValid() && s.listname == "craftpreview")
2431                         {
2432                                 // Craft preview has been clicked: craft
2433                                 craft_amount = (button == 2 ? 10 : 1);
2434                         }
2435                         else if(m_selected_item == NULL)
2436                         {
2437                                 if(s_count != 0)
2438                                 {
2439                                         // Non-empty stack has been clicked: select it
2440                                         m_selected_item = new ItemSpec(s);
2441
2442                                         if(button == 1)  // right
2443                                                 m_selected_amount = (s_count + 1) / 2;
2444                                         else if(button == 2)  // middle
2445                                                 m_selected_amount = MYMIN(s_count, 10);
2446                                         else  // left
2447                                                 m_selected_amount = s_count;
2448
2449                                         m_selected_dragging = true;
2450                                 }
2451                         }
2452                         else  // m_selected_item != NULL
2453                         {
2454                                 assert(m_selected_amount >= 1);
2455
2456                                 if(s.isValid())
2457                                 {
2458                                         // Clicked a slot: move
2459                                         if(button == 1)  // right
2460                                                 move_amount = 1;
2461                                         else if(button == 2)  // middle
2462                                                 move_amount = MYMIN(m_selected_amount, 10);
2463                                         else  // left
2464                                                 move_amount = m_selected_amount;
2465
2466                                         if(identical)
2467                                         {
2468                                                 if(move_amount >= m_selected_amount)
2469                                                         m_selected_amount = 0;
2470                                                 else
2471                                                         m_selected_amount -= move_amount;
2472                                                 move_amount = 0;
2473                                         }
2474                                 }
2475                                 else if (!getAbsoluteClippingRect().isPointInside(m_pointer))
2476                                 {
2477                                         // Clicked outside of the window: drop
2478                                         if(button == 1)  // right
2479                                                 drop_amount = 1;
2480                                         else if(button == 2)  // middle
2481                                                 drop_amount = MYMIN(m_selected_amount, 10);
2482                                         else  // left
2483                                                 drop_amount = m_selected_amount;
2484                                 }
2485                         }
2486                 }
2487                 else if(updown == 1)
2488                 {
2489                         // Some mouse button has been released
2490
2491                         //infostream<<"Mouse button "<<button<<" released at p=("
2492                         //      <<p.X<<","<<p.Y<<")"<<std::endl;
2493
2494                         if(m_selected_item != NULL && m_selected_dragging && s.isValid())
2495                         {
2496                                 if(!identical)
2497                                 {
2498                                         // Dragged to different slot: move all selected
2499                                         move_amount = m_selected_amount;
2500                                 }
2501                         }
2502                         else if(m_selected_item != NULL && m_selected_dragging &&
2503                                 !(getAbsoluteClippingRect().isPointInside(m_pointer)))
2504                         {
2505                                 // Dragged outside of window: drop all selected
2506                                 drop_amount = m_selected_amount;
2507                         }
2508
2509                         m_selected_dragging = false;
2510                 }
2511
2512                 // Possibly send inventory action to server
2513                 if(move_amount > 0)
2514                 {
2515                         // Send IACTION_MOVE
2516
2517                         assert(m_selected_item && m_selected_item->isValid());
2518                         assert(s.isValid());
2519
2520                         assert(inv_selected && inv_s);
2521                         InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2522                         InventoryList *list_to = inv_s->getList(s.listname);
2523                         assert(list_from && list_to);
2524                         ItemStack stack_from = list_from->getItem(m_selected_item->i);
2525                         ItemStack stack_to = list_to->getItem(s.i);
2526
2527                         // Check how many items can be moved
2528                         move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
2529                         ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
2530                         // If source stack cannot be added to destination stack at all,
2531                         // they are swapped
2532                         if(leftover.count == stack_from.count && leftover.name == stack_from.name)
2533                         {
2534                                 m_selected_amount = stack_to.count;
2535                                 // In case the server doesn't directly swap them but instead
2536                                 // moves stack_to somewhere else, set this
2537                                 m_selected_content_guess = stack_to;
2538                                 m_selected_content_guess_inventory = s.inventoryloc;
2539                         }
2540                         // Source stack goes fully into destination stack
2541                         else if(leftover.empty())
2542                         {
2543                                 m_selected_amount -= move_amount;
2544                                 m_selected_content_guess = ItemStack(); // Clear
2545                         }
2546                         // Source stack goes partly into destination stack
2547                         else
2548                         {
2549                                 move_amount -= leftover.count;
2550                                 m_selected_amount -= move_amount;
2551                                 m_selected_content_guess = ItemStack(); // Clear
2552                         }
2553
2554                         infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
2555                         IMoveAction *a = new IMoveAction();
2556                         a->count = move_amount;
2557                         a->from_inv = m_selected_item->inventoryloc;
2558                         a->from_list = m_selected_item->listname;
2559                         a->from_i = m_selected_item->i;
2560                         a->to_inv = s.inventoryloc;
2561                         a->to_list = s.listname;
2562                         a->to_i = s.i;
2563                         m_invmgr->inventoryAction(a);
2564                 }
2565                 else if(drop_amount > 0)
2566                 {
2567                         m_selected_content_guess = ItemStack(); // Clear
2568
2569                         // Send IACTION_DROP
2570
2571                         assert(m_selected_item && m_selected_item->isValid());
2572                         assert(inv_selected);
2573                         InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2574                         assert(list_from);
2575                         ItemStack stack_from = list_from->getItem(m_selected_item->i);
2576
2577                         // Check how many items can be dropped
2578                         drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
2579                         assert(drop_amount > 0 && drop_amount <= m_selected_amount);
2580                         m_selected_amount -= drop_amount;
2581
2582                         infostream<<"Handing IACTION_DROP to manager"<<std::endl;
2583                         IDropAction *a = new IDropAction();
2584                         a->count = drop_amount;
2585                         a->from_inv = m_selected_item->inventoryloc;
2586                         a->from_list = m_selected_item->listname;
2587                         a->from_i = m_selected_item->i;
2588                         m_invmgr->inventoryAction(a);
2589                 }
2590                 else if(craft_amount > 0)
2591                 {
2592                         m_selected_content_guess = ItemStack(); // Clear
2593
2594                         // Send IACTION_CRAFT
2595
2596                         assert(s.isValid());
2597                         assert(inv_s);
2598
2599                         infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
2600                         ICraftAction *a = new ICraftAction();
2601                         a->count = craft_amount;
2602                         a->craft_inv = s.inventoryloc;
2603                         m_invmgr->inventoryAction(a);
2604                 }
2605
2606                 // If m_selected_amount has been decreased to zero, deselect
2607                 if(m_selected_amount == 0)
2608                 {
2609                         delete m_selected_item;
2610                         m_selected_item = NULL;
2611                         m_selected_amount = 0;
2612                         m_selected_dragging = false;
2613                         m_selected_content_guess = ItemStack();
2614                 }
2615         }
2616         if(event.EventType==EET_GUI_EVENT)
2617         {
2618
2619                 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
2620                                                 && isVisible())
2621                 {
2622                         // find the element that was clicked
2623                         for(u32 i=0; i<m_fields.size(); i++)
2624                         {
2625                                 FieldSpec &s = m_fields[i];
2626                                 // if its a button, set the send field so
2627                                 // lua knows which button was pressed
2628                                 if ((s.ftype == f_TabHeader) && (s.fid == event.GUIEvent.Caller->getID()))
2629                                 {
2630                                         s.send = true;
2631                                         acceptInput();
2632                                         s.send = false;
2633                                         return true;
2634                                 }
2635                         }
2636                 }
2637                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
2638                                 && isVisible())
2639                 {
2640                         if(!canTakeFocus(event.GUIEvent.Element))
2641                         {
2642                                 infostream<<"GUIFormSpecMenu: Not allowing focus change."
2643                                                 <<std::endl;
2644                                 // Returning true disables focus change
2645                                 return true;
2646                         }
2647                 }
2648                 if((event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED) ||
2649                                 (event.GUIEvent.EventType==gui::EGET_CHECKBOX_CHANGED))
2650                 {
2651                         unsigned int btn_id = event.GUIEvent.Caller->getID();
2652
2653                         if (btn_id == 257) {
2654                                 if (m_allowclose) {
2655                                         acceptInput(true);
2656                                         quitMenu();
2657                                 } else {
2658                                         acceptInput();
2659                                         m_text_dst->gotText(narrow_to_wide("ExitButton"));
2660                                 }
2661                                 // quitMenu deallocates menu
2662                                 return true;
2663                         }
2664
2665                         // find the element that was clicked
2666                         for(u32 i=0; i<m_fields.size(); i++)
2667                         {
2668                                 FieldSpec &s = m_fields[i];
2669                                 // if its a button, set the send field so
2670                                 // lua knows which button was pressed
2671                                 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
2672                                                 (s.fid == event.GUIEvent.Caller->getID()))
2673                                 {
2674                                         s.send = true;
2675                                         acceptInput();
2676                                         if(s.is_exit){
2677                                                 if (m_allowclose) {
2678                                                         acceptInput(true);
2679                                                         quitMenu();
2680                                                 } else {
2681                                                         m_text_dst->gotText(narrow_to_wide("ExitButton"));
2682                                                 }
2683                                                 return true;
2684                                         }else{
2685                                                 s.send = false;
2686                                                 return true;
2687                                         }
2688                                 }
2689                         }
2690                 }
2691                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
2692                 {
2693                         if(event.GUIEvent.Caller->getID() > 257)
2694                         {
2695
2696                                 if (m_allowclose) {
2697                                         acceptInput(true);
2698                                         quitMenu();
2699                                 }
2700                                 else {
2701                                         current_keys_pending.key_enter = true;
2702                                         acceptInput();
2703                                 }
2704                                 // quitMenu deallocates menu
2705                                 return true;
2706                         }
2707                 }
2708
2709                 if((event.GUIEvent.EventType==gui::EGET_LISTBOX_SELECTED_AGAIN) ||
2710                         (event.GUIEvent.EventType==gui::EGET_LISTBOX_CHANGED))
2711                 {
2712                         int current_id = event.GUIEvent.Caller->getID();
2713                         if(current_id > 257)
2714                         {
2715                                 // find the element that was clicked
2716                                 for(u32 i=0; i<m_fields.size(); i++)
2717                                 {
2718                                         FieldSpec &s = m_fields[i];
2719                                         // if its a listbox, set the send field so
2720                                         // lua knows which listbox was changed
2721                                         // checkListboxClick() is black magic
2722                                         // for properly handling double clicks
2723                                         if ((s.ftype == f_ListBox) && (s.fid == current_id)
2724                                                         && checkListboxClick(s.fname,
2725                                                                 event.GUIEvent.EventType))
2726                                         {
2727                                                 s.send = true;
2728                                                 acceptInput();
2729                                                 s.send=false;
2730                                         }
2731                                 }
2732                                 return true;
2733                         }
2734                 }
2735         }
2736
2737         return Parent ? Parent->OnEvent(event) : false;
2738 }
2739
2740 bool GUIFormSpecMenu::parseColor(std::string &value, video::SColor &color, bool quiet)
2741 {
2742         const char *hexpattern = NULL;
2743         if (value[0] == '#') {
2744                 if (value.size() == 9)
2745                         hexpattern = "#RRGGBBAA";
2746                 else if (value.size() == 7)
2747                         hexpattern = "#RRGGBB";
2748                 else if (value.size() == 5)
2749                         hexpattern = "#RGBA";
2750                 else if (value.size() == 4)
2751                         hexpattern = "#RGB";
2752         }
2753
2754         if (hexpattern) {
2755                 assert(strlen(hexpattern) == value.size());
2756                 video::SColor outcolor(255, 255, 255, 255);
2757                 for (size_t pos = 0; pos < value.size(); ++pos) {
2758                         // '#' in the pattern means skip that character
2759                         if (hexpattern[pos] == '#')
2760                                 continue;
2761
2762                         // Else assume hexpattern[pos] is one of 'R' 'G' 'B' 'A'
2763                         // Read one or two digits, depending on hexpattern
2764                         unsigned char c1, c2;
2765                         if (hexpattern[pos+1] == hexpattern[pos]) {
2766                                 // Two digits, e.g. hexpattern == "#RRGGBB"
2767                                 if (!hex_digit_decode(value[pos], c1) ||
2768                                     !hex_digit_decode(value[pos+1], c2))
2769                                         goto fail;
2770                                 ++pos;
2771                         }
2772                         else {
2773                                 // One digit, e.g. hexpattern == "#RGB"
2774                                 if (!hex_digit_decode(value[pos], c1))
2775                                         goto fail;
2776                                 c2 = c1;
2777                         }
2778                         u32 colorpart = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
2779
2780                         // Update outcolor with newly read color part
2781                         if (hexpattern[pos] == 'R')
2782                                 outcolor.setRed(colorpart);
2783                         else if (hexpattern[pos] == 'G')
2784                                 outcolor.setGreen(colorpart);
2785                         else if (hexpattern[pos] == 'B')
2786                                 outcolor.setBlue(colorpart);
2787                         else if (hexpattern[pos] == 'A')
2788                                 outcolor.setAlpha(colorpart);
2789                 }
2790
2791                 color = outcolor;
2792                 return true;
2793         }
2794
2795         // Optionally, named colors could be implemented here
2796
2797 fail:
2798         if (!quiet)
2799                 errorstream<<"Invalid color: \""<<value<<"\""<<std::endl;
2800         return false;
2801 }