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