]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiFormSpecMenu.cpp
02b114f43ff4ddc3b74351921dd61314022a202d
[dragonfireclient.git] / src / guiFormSpecMenu.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20
21 #include <cstdlib>
22 #include <algorithm>
23 #include <iterator>
24 #include <sstream>
25 #include <limits>
26 #include "guiFormSpecMenu.h"
27 #include "constants.h"
28 #include "gamedef.h"
29 #include "keycode.h"
30 #include "strfnd.h"
31 #include <IGUICheckBox.h>
32 #include <IGUIEditBox.h>
33 #include <IGUIButton.h>
34 #include <IGUIStaticText.h>
35 #include <IGUIFont.h>
36 #include <IGUIListBox.h>
37 #include <IGUITabControl.h>
38 #include <IGUIScrollBar.h>
39 #include <IGUIComboBox.h>
40 #include "log.h"
41 #include "tile.h" // ITextureSource
42 #include "hud.h" // drawItemStack
43 #include "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                         std::wstring wlabel = narrow_to_wide(buttons[i]); //Needed for displaying text on windows
1257                         wbutton = (wchar_t*) wlabel.c_str();
1258
1259                         e->addTab(wbutton,-1);
1260                 }
1261
1262                 if ((tab_index >= 0) &&
1263                                 (buttons.size() < INT_MAX) &&
1264                                 (tab_index < (int) buttons.size()))
1265                         e->setActiveTab(tab_index);
1266
1267                 m_fields.push_back(spec);
1268                 return;
1269         }
1270         errorstream<< "Invalid TabHeader element(" << parts.size() << "): '" << element << "'"  << std::endl;
1271 }
1272
1273 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element) {
1274
1275         if (m_gamedef == 0) {
1276                 errorstream<<"WARNING: invalid use of item_image_button with m_gamedef==0"<<std::endl;
1277                 return;
1278         }
1279
1280         std::vector<std::string> parts = split(element,';');
1281
1282         if (parts.size() == 5) {
1283                 std::vector<std::string> v_pos = split(parts[0],',');
1284                 std::vector<std::string> v_geom = split(parts[1],',');
1285                 std::string item_name = parts[2];
1286                 std::string name = parts[3];
1287                 std::string label = parts[4];
1288
1289                 MY_CHECKPOS("itemimagebutton",0);
1290                 MY_CHECKGEOM("itemimagebutton",1);
1291
1292                 v2s32 pos = padding;
1293                 pos.X += stof(v_pos[0]) * (float)spacing.X;
1294                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1295                 v2s32 geom;
1296                 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1297                 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1298
1299                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1300
1301                 if(data->bp_set != 2)
1302                         errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1303
1304                 IItemDefManager *idef = m_gamedef->idef();
1305                 ItemStack item;
1306                 item.deSerialize(item_name, idef);
1307                 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1308                 std::string tooltip = item.getDefinition(idef).description;
1309
1310                 label = unescape_string(label);
1311                 FieldSpec spec = FieldSpec(
1312                         narrow_to_wide(name.c_str()),
1313                         narrow_to_wide(label.c_str()),
1314                         narrow_to_wide(item_name.c_str()),
1315                         258+m_fields.size()
1316                 );
1317
1318                 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1319
1320                 if (spec.fname == data->focused_fieldname) {
1321                         Environment->setFocus(e);
1322                 }
1323
1324                 e->setUseAlphaChannel(true);
1325                 e->setImage(texture);
1326                 e->setPressedImage(texture);
1327                 e->setScaleImage(true);
1328                 spec.ftype = f_Button;
1329                 rect+=data->basepos-padding;
1330                 spec.rect=rect;
1331                 if (tooltip!="")
1332                         spec.tooltip=tooltip;
1333                 m_fields.push_back(spec);
1334                 return;
1335         }
1336         errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'"  << std::endl;
1337 }
1338
1339 void GUIFormSpecMenu::parseBox(parserData* data,std::string element) {
1340         std::vector<std::string> parts = split(element,';');
1341
1342         if (parts.size() == 3) {
1343                 std::vector<std::string> v_pos = split(parts[0],',');
1344                 std::vector<std::string> v_geom = split(parts[1],',');
1345                 std::string color_str = parts[2];
1346
1347                 MY_CHECKPOS("box",0);
1348                 MY_CHECKGEOM("box",1);
1349
1350                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1351                 pos.X += stof(v_pos[0]) * (float) spacing.X;
1352                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1353
1354                 v2s32 geom;
1355                 geom.X = stof(v_geom[0]) * (float)spacing.X;
1356                 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1357
1358                 irr::video::SColor color;
1359
1360                 if (parseColor(color_str, color)) {
1361                         BoxDrawSpec spec(pos,geom,color);
1362
1363                         m_boxes.push_back(spec);
1364                 }
1365                 else {
1366                         errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'  INVALID COLOR"  << std::endl;
1367                 }
1368                 return;
1369         }
1370         errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'"  << std::endl;
1371 }
1372
1373 void GUIFormSpecMenu::parseElement(parserData* data,std::string element) {
1374
1375         //some prechecks
1376         if (element == "")
1377                 return;
1378
1379         std::vector<std::string> parts = split(element,'[');
1380
1381         // ugly workaround to keep compatibility
1382         if (parts.size() > 2) {
1383                 if (trim(parts[0]) == "image") {
1384                         for (unsigned int i=2;i< parts.size(); i++) {
1385                                 parts[1] += "[" + parts[i];
1386                         }
1387                 }
1388                 else { return; }
1389         }
1390
1391         if (parts.size() < 2) {
1392                 return;
1393         }
1394
1395         std::string type = trim(parts[0]);
1396         std::string description = trim(parts[1]);
1397
1398         if ((type == "size") || (type == "invsize")){
1399                 parseSize(data,description);
1400                 return;
1401         }
1402
1403         if (type == "list") {
1404                 parseList(data,description);
1405                 return;
1406         }
1407
1408         if (type == "checkbox") {
1409                 parseCheckbox(data,description);
1410                 return;
1411         }
1412
1413         if (type == "image") {
1414                 parseImage(data,description);
1415                 return;
1416         }
1417
1418         if (type == "item_image") {
1419                 parseItemImage(data,description);
1420                 return;
1421         }
1422
1423         if ((type == "button") || (type == "button_exit")) {
1424                 parseButton(data,description,type);
1425                 return;
1426         }
1427
1428         if (type == "background") {
1429                 parseBackground(data,description);
1430                 return;
1431         }
1432
1433         if (type == "textlist"){
1434                 parseTextList(data,description);
1435                 return;
1436         }
1437
1438         if (type == "dropdown"){
1439                 parseDropDown(data,description);
1440                 return;
1441         }
1442
1443         if (type == "pwdfield") {
1444                 parsePwdField(data,description);
1445                 return;
1446         }
1447
1448         if ((type == "field") || (type == "textarea")){
1449                 parseField(data,description,type);
1450                 return;
1451         }
1452
1453         if (type == "label") {
1454                 parseLabel(data,description);
1455                 return;
1456         }
1457
1458         if (type == "vertlabel") {
1459                 parseVertLabel(data,description);
1460                 return;
1461         }
1462
1463         if (type == "item_image_button") {
1464                 parseItemImageButton(data,description);
1465                 return;
1466         }
1467
1468         if ((type == "image_button") || (type == "image_button_exit")) {
1469                 parseImageButton(data,description,type);
1470                 return;
1471         }
1472
1473         if (type == "tabheader") {
1474                 parseTabHeader(data,description);
1475                 return;
1476         }
1477
1478         if (type == "box") {
1479                 parseBox(data,description);
1480                 return;
1481         }
1482
1483         // Ignore others
1484         infostream
1485                 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1486                 <<std::endl;
1487 }
1488
1489
1490
1491 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1492 {
1493         parserData mydata;
1494
1495         //preserve listboxes
1496         for (unsigned int i = 0; i < m_listboxes.size(); i++) {
1497                 std::wstring listboxname = m_listboxes[i].first.fname;
1498                 gui::IGUIListBox *listbox = m_listboxes[i].second;
1499
1500                 int selection = listbox->getSelected();
1501                 if (selection != -1) {
1502                         mydata.listbox_selections[listboxname] = selection;
1503                 }
1504
1505                 gui::IGUIScrollBar *scrollbar = getListboxScrollbar(listbox);
1506                 if (scrollbar) {
1507                         mydata.listbox_scroll[listboxname] = scrollbar->getPos();
1508                 }
1509         }
1510
1511         //preserve focus
1512         gui::IGUIElement *focused_element = Environment->getFocus();
1513         if (focused_element && focused_element->getParent() == this) {
1514                 s32 focused_id = focused_element->getID();
1515                 if (focused_id > 257) {
1516                         for (u32 i=0; i<m_fields.size(); i++) {
1517                                 if (m_fields[i].fid == focused_id) {
1518                                         mydata.focused_fieldname =
1519                                                 m_fields[i].fname;
1520                                         break;
1521                                 }
1522                         }
1523                 }
1524         }
1525
1526         // Remove children
1527         removeChildren();
1528
1529         mydata.size= v2s32(100,100);
1530         mydata.helptext_h = 15;
1531         mydata.screensize = screensize;
1532
1533         // Base position of contents of form
1534         mydata.basepos = getBasePos();
1535
1536         // State of basepos, 0 = not set, 1= set by formspec, 2 = set by size[] element
1537         // Used to adjust form size automatically if needed
1538         // A proceed button is added if there is no size[] element
1539         mydata.bp_set = 0;
1540
1541         
1542         /* Convert m_init_draw_spec to m_inventorylists */
1543         
1544         m_inventorylists.clear();
1545         m_images.clear();
1546         m_backgrounds.clear();
1547         m_itemimages.clear();
1548         m_listboxes.clear();
1549         m_checkboxes.clear();
1550         m_fields.clear();
1551         m_boxes.clear();
1552
1553
1554         std::vector<std::string> elements = split(m_formspec_string,']');
1555
1556         for (unsigned int i=0;i< elements.size();i++) {
1557                 parseElement(&mydata,elements[i]);
1558         }
1559
1560         // If there's inventory, put the usage string at the bottom
1561         if (m_inventorylists.size())
1562         {
1563                 changeCtype("");
1564                 core::rect<s32> rect(0, 0, mydata.size.X-padding.X*2, mydata.helptext_h);
1565                 rect = rect + v2s32((mydata.size.X/2 - mydata.rect.getWidth()/2) +5,
1566                                 mydata.size.Y-5-mydata.helptext_h);
1567                 const wchar_t *text = wgettext("Left click: Move all items, Right click: Move single item");
1568                 Environment->addStaticText(text, rect, false, true, this, 256);
1569                 delete[] text;
1570                 changeCtype("C");
1571         }
1572         // If there's fields, add a Proceed button
1573         if (m_fields.size() && mydata.bp_set != 2)
1574         {
1575                 // if the size wasn't set by an invsize[] or size[] adjust it now to fit all the fields
1576                 mydata.rect = core::rect<s32>(
1577                                 mydata.screensize.X/2 - 580/2,
1578                                 mydata.screensize.Y/2 - 300/2,
1579                                 mydata.screensize.X/2 + 580/2,
1580                                 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
1581                 );
1582                 DesiredRect = mydata.rect;
1583                 recalculateAbsolutePosition(false);
1584                 mydata.basepos = getBasePos();
1585
1586                 changeCtype("");
1587                 {
1588                         v2s32 pos = mydata.basepos;
1589                         pos.Y = ((m_fields.size()+2)*60);
1590
1591                         v2s32 size = DesiredRect.getSize();
1592                         mydata.rect = core::rect<s32>(size.X/2-70, pos.Y, (size.X/2-70)+140, pos.Y+30);
1593                         wchar_t* text = wgettext("Proceed");
1594                         Environment->addButton(mydata.rect, this, 257, text);
1595                         delete[] text;
1596                 }
1597                 changeCtype("C");
1598         }
1599         // Add tooltip
1600         {
1601                 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1602                 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1603                 m_tooltip_element->enableOverrideColor(true);
1604                 m_tooltip_element->setBackgroundColor(video::SColor(255,110,130,60));
1605                 m_tooltip_element->setDrawBackground(true);
1606                 m_tooltip_element->setDrawBorder(true);
1607                 m_tooltip_element->setOverrideColor(video::SColor(255,255,255,255));
1608                 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1609                 m_tooltip_element->setWordWrap(false);
1610         }
1611
1612         //set initial focus if parser didn't set it
1613         focused_element = Environment->getFocus();
1614         if (!focused_element
1615                         || !isMyChild(focused_element)
1616                         || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
1617                 setInitialFocus();
1618 }
1619
1620 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
1621 {
1622         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1623         
1624         for(u32 i=0; i<m_inventorylists.size(); i++)
1625         {
1626                 const ListDrawSpec &s = m_inventorylists[i];
1627
1628                 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1629                 {
1630                         s32 item_i = i + s.start_item_i;
1631                         s32 x = (i%s.geom.X) * spacing.X;
1632                         s32 y = (i/s.geom.X) * spacing.Y;
1633                         v2s32 p0(x,y);
1634                         core::rect<s32> rect = imgrect + s.pos + p0;
1635                         if(rect.isPointInside(p))
1636                         {
1637                                 return ItemSpec(s.inventoryloc, s.listname, item_i);
1638                         }
1639                 }
1640         }
1641
1642         return ItemSpec(InventoryLocation(), "", -1);
1643 }
1644
1645 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
1646 {
1647         video::IVideoDriver* driver = Environment->getVideoDriver();
1648
1649         // Get font
1650         gui::IGUIFont *font = NULL;
1651         gui::IGUISkin* skin = Environment->getSkin();
1652         if (skin)
1653                 font = skin->getFont();
1654         
1655         Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1656         if(!inv){
1657                 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1658                                 <<"The inventory location "
1659                                 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
1660                                 <<std::endl;
1661                 return;
1662         }
1663         InventoryList *ilist = inv->getList(s.listname);
1664         if(!ilist){
1665                 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1666                                 <<"The inventory list \""<<s.listname<<"\" @ \""
1667                                 <<s.inventoryloc.dump()<<"\" doesn't exist"
1668                                 <<std::endl;
1669                 return;
1670         }
1671         
1672         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1673         
1674         for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1675         {
1676                 s32 item_i = i + s.start_item_i;
1677                 if(item_i >= (s32) ilist->getSize())
1678                         break;
1679                 s32 x = (i%s.geom.X) * spacing.X;
1680                 s32 y = (i/s.geom.X) * spacing.Y;
1681                 v2s32 p(x,y);
1682                 core::rect<s32> rect = imgrect + s.pos + p;
1683                 ItemStack item;
1684                 if(ilist)
1685                         item = ilist->getItem(item_i);
1686
1687                 bool selected = m_selected_item
1688                         && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
1689                         && m_selected_item->listname == s.listname
1690                         && m_selected_item->i == item_i;
1691                 bool hovering = rect.isPointInside(m_pointer);
1692
1693                 if(phase == 0)
1694                 {
1695                         if(hovering && m_selected_item)
1696                         {
1697                                 video::SColor bgcolor(255,192,192,192);
1698                                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
1699                         }
1700                         else
1701                         {
1702                                 video::SColor bgcolor(255,128,128,128);
1703                                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
1704                         }
1705                 }
1706
1707                 if(phase == 1)
1708                 {
1709                         // Draw item stack
1710                         if(selected)
1711                         {
1712                                 item.takeItem(m_selected_amount);
1713                         }
1714                         if(!item.empty())
1715                         {
1716                                 drawItemStack(driver, font, item,
1717                                                 rect, &AbsoluteClippingRect, m_gamedef);
1718                         }
1719
1720                         // Draw tooltip
1721                         std::string tooltip_text = "";
1722                         if(hovering && !m_selected_item)
1723                                 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
1724                         if(tooltip_text != "")
1725                         {
1726                                 m_tooltip_element->setVisible(true);
1727                                 this->bringToFront(m_tooltip_element);
1728                                 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
1729                                 s32 tooltip_x = m_pointer.X + 15;
1730                                 s32 tooltip_y = m_pointer.Y + 15;
1731                                 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1732                                 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1733                                 m_tooltip_element->setRelativePosition(core::rect<s32>(
1734                                                 core::position2d<s32>(tooltip_x, tooltip_y),
1735                                                 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1736                         }
1737                 }
1738         }
1739 }
1740
1741 void GUIFormSpecMenu::drawSelectedItem()
1742 {
1743         if(!m_selected_item)
1744                 return;
1745
1746         video::IVideoDriver* driver = Environment->getVideoDriver();
1747
1748         // Get font
1749         gui::IGUIFont *font = NULL;
1750         gui::IGUISkin* skin = Environment->getSkin();
1751         if (skin)
1752                 font = skin->getFont();
1753         
1754         Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
1755         assert(inv);
1756         InventoryList *list = inv->getList(m_selected_item->listname);
1757         assert(list);
1758         ItemStack stack = list->getItem(m_selected_item->i);
1759         stack.count = m_selected_amount;
1760
1761         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1762         core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
1763         drawItemStack(driver, font, stack, rect, NULL, m_gamedef);
1764 }
1765
1766 void GUIFormSpecMenu::drawMenu()
1767 {
1768         if(m_form_src){
1769                 std::string newform = m_form_src->getForm();
1770                 if(newform != m_formspec_string){
1771                         m_formspec_string = newform;
1772                         regenerateGui(m_screensize_old);
1773                 }
1774         }
1775
1776         m_pointer = m_device->getCursorControl()->getPosition();
1777
1778         updateSelectedItem();
1779
1780         gui::IGUISkin* skin = Environment->getSkin();
1781         if (!skin)
1782                 return;
1783         video::IVideoDriver* driver = Environment->getVideoDriver();
1784         
1785         video::SColor bgcolor(140,0,0,0);
1786         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
1787
1788         m_tooltip_element->setVisible(false);
1789
1790         /*
1791                 Draw backgrounds
1792         */
1793         for(u32 i=0; i<m_backgrounds.size(); i++)
1794         {
1795                 const ImageDrawSpec &spec = m_backgrounds[i];
1796                 video::ITexture *texture = 0;
1797
1798                 if (m_gamedef != 0)
1799                         texture = m_gamedef->tsrc()->getTexture(spec.name);
1800                 else
1801                 {
1802                         texture = driver->getTexture(spec.name.c_str());
1803                         m_Textures.push_back(texture);
1804                 }
1805
1806                 if (texture != 0) {
1807                         // Image size on screen
1808                         core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1809                         // Image rectangle on screen
1810                         core::rect<s32> rect = imgrect + spec.pos;
1811                         const video::SColor color(255,255,255,255);
1812                         const video::SColor colors[] = {color,color,color,color};
1813                         driver->draw2DImage(texture, rect,
1814                                 core::rect<s32>(core::position2d<s32>(0,0),
1815                                                 core::dimension2di(texture->getOriginalSize())),
1816                                 NULL/*&AbsoluteClippingRect*/, colors, true);
1817                 }
1818                 else {
1819                         errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
1820                         errorstream << "\t" << spec.name << std::endl;
1821                 }
1822         }
1823         
1824         /*
1825                 Draw Boxes
1826         */
1827         for(u32 i=0; i<m_boxes.size(); i++)
1828         {
1829                 const BoxDrawSpec &spec = m_boxes[i];
1830
1831                 irr::video::SColor todraw = spec.color;
1832
1833                 todraw.setAlpha(140);
1834
1835                 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
1836                                                         spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
1837
1838                 driver->draw2DRectangle(todraw, rect, 0);
1839         }
1840         /*
1841                 Draw images
1842         */
1843         for(u32 i=0; i<m_images.size(); i++)
1844         {
1845                 const ImageDrawSpec &spec = m_images[i];
1846                 video::ITexture *texture = 0;
1847
1848                 if (m_gamedef != 0)
1849                         texture = m_gamedef->tsrc()->getTexture(spec.name);
1850                 else
1851                 {
1852                         texture = driver->getTexture(spec.name.c_str());
1853                         m_Textures.push_back(texture);
1854                 }
1855                 if (texture != 0) {
1856                         const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
1857                         // Image size on screen
1858                         core::rect<s32> imgrect;
1859
1860                         if (spec.scale)
1861                                 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
1862                         else {
1863
1864                                 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
1865                         }
1866                         // Image rectangle on screen
1867                         core::rect<s32> rect = imgrect + spec.pos;
1868                         const video::SColor color(255,255,255,255);
1869                         const video::SColor colors[] = {color,color,color,color};
1870                         driver->draw2DImage(texture, rect,
1871                                 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
1872                                 NULL/*&AbsoluteClippingRect*/, colors, true);
1873                 }
1874                 else {
1875                         errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
1876                         errorstream << "\t" << spec.name << std::endl;
1877                 }
1878         }
1879         
1880         /*
1881                 Draw item images
1882         */
1883         for(u32 i=0; i<m_itemimages.size(); i++)
1884         {
1885                 if (m_gamedef == 0)
1886                         break;
1887
1888                 const ImageDrawSpec &spec = m_itemimages[i];
1889                 IItemDefManager *idef = m_gamedef->idef();
1890                 ItemStack item;
1891                 item.deSerialize(spec.name, idef);
1892                 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);         
1893                 // Image size on screen
1894                 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1895                 // Image rectangle on screen
1896                 core::rect<s32> rect = imgrect + spec.pos;
1897                 const video::SColor color(255,255,255,255);
1898                 const video::SColor colors[] = {color,color,color,color};
1899                 driver->draw2DImage(texture, rect,
1900                         core::rect<s32>(core::position2d<s32>(0,0),
1901                                         core::dimension2di(texture->getOriginalSize())),
1902                         NULL/*&AbsoluteClippingRect*/, colors, true);
1903         }
1904         
1905         /*
1906                 Draw items
1907                 Phase 0: Item slot rectangles
1908                 Phase 1: Item images; prepare tooltip
1909                 If backgrounds used, do not draw Item slot rectangles
1910         */
1911         int start_phase=0;
1912         if (m_backgrounds.size() > 0) start_phase=1;
1913         for(int phase=start_phase; phase<=1; phase++)
1914         for(u32 i=0; i<m_inventorylists.size(); i++)
1915         {
1916                 drawList(m_inventorylists[i], phase);
1917         }
1918
1919         /*
1920                 Call base class
1921         */
1922         gui::IGUIElement::draw();
1923         
1924         /*
1925                 Draw fields/buttons tooltips
1926         */
1927         for(u32 i=0; i<m_fields.size(); i++)
1928         {
1929                 const FieldSpec &spec = m_fields[i];
1930                 if (spec.tooltip != "")
1931                 {
1932                         core::rect<s32> rect = spec.rect;
1933                         if (rect.isPointInside(m_pointer)) 
1934                         {
1935                                 m_tooltip_element->setVisible(true);
1936                                 this->bringToFront(m_tooltip_element);
1937                                 m_tooltip_element->setText(narrow_to_wide(spec.tooltip).c_str());
1938                                 s32 tooltip_x = m_pointer.X + 15;
1939                                 s32 tooltip_y = m_pointer.Y + 15;
1940                                 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1941                                 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1942                                 m_tooltip_element->setRelativePosition(core::rect<s32>(
1943                                 core::position2d<s32>(tooltip_x, tooltip_y),
1944                                 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1945                         }
1946                 }
1947         }
1948         
1949         /*
1950                 Draw dragged item stack
1951         */
1952         drawSelectedItem();
1953 }
1954
1955 void GUIFormSpecMenu::updateSelectedItem()
1956 {
1957         // If the selected stack has become empty for some reason, deselect it.
1958         // If the selected stack has become inaccessible, deselect it.
1959         // If the selected stack has become smaller, adjust m_selected_amount.
1960         ItemStack selected = verifySelectedItem();
1961
1962         // WARNING: BLACK MAGIC
1963         // See if there is a stack suited for our current guess.
1964         // If such stack does not exist, clear the guess.
1965         if(m_selected_content_guess.name != "" &&
1966                         selected.name == m_selected_content_guess.name &&
1967                         selected.count == m_selected_content_guess.count){
1968                 // Selected item fits the guess. Skip the black magic.
1969         }
1970         else if(m_selected_content_guess.name != ""){
1971                 bool found = false;
1972                 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
1973                         const ListDrawSpec &s = m_inventorylists[i];
1974                         Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1975                         if(!inv)
1976                                 continue;
1977                         InventoryList *list = inv->getList(s.listname);
1978                         if(!list)
1979                                 continue;
1980                         for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
1981                                 u32 item_i = i + s.start_item_i;
1982                                 if(item_i >= list->getSize())
1983                                         continue;
1984                                 ItemStack stack = list->getItem(item_i);
1985                                 if(stack.name == m_selected_content_guess.name &&
1986                                                 stack.count == m_selected_content_guess.count){
1987                                         found = true;
1988                                         infostream<<"Client: Changing selected content guess to "
1989                                                         <<s.inventoryloc.dump()<<" "<<s.listname
1990                                                         <<" "<<item_i<<std::endl;
1991                                         delete m_selected_item;
1992                                         m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
1993                                         m_selected_amount = stack.count;
1994                                 }
1995                         }
1996                 }
1997                 if(!found){
1998                         infostream<<"Client: Discarding selected content guess: "
1999                                         <<m_selected_content_guess.getItemString()<<std::endl;
2000                         m_selected_content_guess.name = "";
2001                 }
2002         }
2003
2004         // If craftresult is nonempty and nothing else is selected, select it now.
2005         if(!m_selected_item)
2006         {
2007                 for(u32 i=0; i<m_inventorylists.size(); i++)
2008                 {
2009                         const ListDrawSpec &s = m_inventorylists[i];
2010                         if(s.listname == "craftpreview")
2011                         {
2012                                 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2013                                 InventoryList *list = inv->getList("craftresult");
2014                                 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2015                                 {
2016                                         m_selected_item = new ItemSpec;
2017                                         m_selected_item->inventoryloc = s.inventoryloc;
2018                                         m_selected_item->listname = "craftresult";
2019                                         m_selected_item->i = 0;
2020                                         m_selected_amount = 0;
2021                                         m_selected_dragging = false;
2022                                         break;
2023                                 }
2024                         }
2025                 }
2026         }
2027
2028         // If craftresult is selected, keep the whole stack selected
2029         if(m_selected_item && m_selected_item->listname == "craftresult")
2030         {
2031                 m_selected_amount = verifySelectedItem().count;
2032         }
2033 }
2034
2035 ItemStack GUIFormSpecMenu::verifySelectedItem()
2036 {
2037         // If the selected stack has become empty for some reason, deselect it.
2038         // If the selected stack has become inaccessible, deselect it.
2039         // If the selected stack has become smaller, adjust m_selected_amount.
2040         // Return the selected stack.
2041
2042         if(m_selected_item)
2043         {
2044                 if(m_selected_item->isValid())
2045                 {
2046                         Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2047                         if(inv)
2048                         {
2049                                 InventoryList *list = inv->getList(m_selected_item->listname);
2050                                 if(list && (u32) m_selected_item->i < list->getSize())
2051                                 {
2052                                         ItemStack stack = list->getItem(m_selected_item->i);
2053                                         if(m_selected_amount > stack.count)
2054                                                 m_selected_amount = stack.count;
2055                                         if(!stack.empty())
2056                                                 return stack;
2057                                 }
2058                         }
2059                 }
2060
2061                 // selection was not valid
2062                 delete m_selected_item;
2063                 m_selected_item = NULL;
2064                 m_selected_amount = 0;
2065                 m_selected_dragging = false;
2066         }
2067         return ItemStack();
2068 }
2069
2070 void GUIFormSpecMenu::acceptInput()
2071 {
2072         if(m_text_dst)
2073         {
2074                 std::map<std::string, std::string> fields;
2075
2076                 if (current_keys_pending.key_down) {
2077                         fields["key_down"] = "true";
2078                         current_keys_pending.key_down = false;
2079                 }
2080
2081                 if (current_keys_pending.key_up) {
2082                         fields["key_up"] = "true";
2083                         current_keys_pending.key_up = false;
2084                 }
2085
2086                 if (current_keys_pending.key_enter) {
2087                         fields["key_enter"] = "true";
2088                         current_keys_pending.key_enter = false;
2089                 }
2090
2091                 if (current_keys_pending.key_escape) {
2092                         fields["key_escape"] = "true";
2093                         current_keys_pending.key_escape = false;
2094                 }
2095
2096                 for(u32 i=0; i<m_fields.size(); i++)
2097                 {
2098                         const FieldSpec &s = m_fields[i];
2099                         if(s.send) 
2100                         {
2101                                 if(s.ftype == f_Button)
2102                                 {
2103                                         fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(s.flabel.c_str());
2104                                 }
2105                                 else if(s.ftype == f_ListBox) {
2106                                         std::stringstream ss;
2107
2108                                         if (m_listbox_doubleclick) {
2109                                                 ss << "DCL:";
2110                                         }
2111                                         else {
2112                                                 ss << "CHG:";
2113                                         }
2114                                         ss << (getListboxIndex(wide_to_narrow(s.fname.c_str()))+1);
2115                                         fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2116                                 }
2117                                 else if(s.ftype == f_DropDown) {
2118                                         // no dynamic cast possible due to some distributions shipped
2119                                         // without rtti support in irrlicht
2120                                         IGUIElement * element = getElementFromId(s.fid);
2121                                         gui::IGUIComboBox *e = NULL;
2122                                         if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2123                                                 e = static_cast<gui::IGUIComboBox*>(element);
2124                                         }
2125                                         fields[wide_to_narrow(s.fname.c_str())] =
2126                                                         wide_to_narrow(e->getItem(e->getSelected()));
2127                                 }
2128                                 else if (s.ftype == f_TabHeader) {
2129                                         // no dynamic cast possible due to some distributions shipped
2130                                         // without rtti support in irrlicht
2131                                         IGUIElement * element = getElementFromId(s.fid);
2132                                         gui::IGUITabControl *e = NULL;
2133                                         if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2134                                                 e = static_cast<gui::IGUITabControl*>(element);
2135                                         }
2136
2137                                         if (e != 0) {
2138                                                 std::stringstream ss;
2139                                                 ss << (e->getActiveTab() +1);
2140                                                 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2141                                         }
2142                                 }
2143                                 else if (s.ftype == f_CheckBox) {
2144                                         // no dynamic cast possible due to some distributions shipped
2145                                         // without rtti support in irrlicht
2146                                         IGUIElement * element = getElementFromId(s.fid);
2147                                         gui::IGUICheckBox *e = NULL;
2148                                         if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2149                                                 e = static_cast<gui::IGUICheckBox*>(element);
2150                                         }
2151
2152                                         if (e != 0) {
2153                                                 if (e->isChecked())
2154                                                         fields[wide_to_narrow(s.fname.c_str())] = "true";
2155                                                 else
2156                                                         fields[wide_to_narrow(s.fname.c_str())] = "false";
2157                                         }
2158                                 }
2159                                 else
2160                                 {
2161                                         IGUIElement* e = getElementFromId(s.fid);
2162                                         if(e != NULL)
2163                                         {
2164                                                 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(e->getText());
2165                                         }
2166                                 }
2167                         }
2168                 }
2169
2170                 m_text_dst->gotText(fields);
2171         }
2172 }
2173
2174 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2175 {
2176         // Fix Esc/Return key being eaten by checkboxen and listboxen
2177         if(event.EventType==EET_KEY_INPUT_EVENT)
2178         {
2179                 KeyPress kp(event.KeyInput);
2180                 if (kp == EscapeKey || kp == getKeySetting("keymap_inventory")
2181                                 || event.KeyInput.Key==KEY_RETURN)
2182                 {
2183                         gui::IGUIElement *focused = Environment->getFocus();
2184                         if (focused && isMyChild(focused) &&
2185                                         (focused->getType() == gui::EGUIET_LIST_BOX ||
2186                                          focused->getType() == gui::EGUIET_CHECK_BOX)) {
2187                                 OnEvent(event);
2188                                 return true;
2189                         }
2190                 }
2191         }
2192         // Mouse wheel events: send to hovered element instead of focused
2193         if(event.EventType==EET_MOUSE_INPUT_EVENT
2194                         && event.MouseInput.Event == EMIE_MOUSE_WHEEL)
2195         {
2196                 s32 x = event.MouseInput.X;
2197                 s32 y = event.MouseInput.Y;
2198                 gui::IGUIElement *hovered =
2199                         Environment->getRootGUIElement()->getElementFromPoint(
2200                                 core::position2d<s32>(x, y));
2201                 if (hovered && isMyChild(hovered)) {
2202                         hovered->OnEvent(event);
2203                         return true;
2204                 }
2205         }
2206         return false;
2207 }
2208
2209 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
2210 {
2211         if(event.EventType==EET_KEY_INPUT_EVENT)
2212         {
2213                 KeyPress kp(event.KeyInput);
2214                 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
2215                         kp == getKeySetting("keymap_inventory")))
2216                 {
2217                         if (m_allowclose)
2218                                 quitMenu();
2219                         else
2220                                 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
2221                         return true;
2222                 }
2223                 if (event.KeyInput.PressedDown &&
2224                         (event.KeyInput.Key==KEY_RETURN ||
2225                          event.KeyInput.Key==KEY_UP ||
2226                          event.KeyInput.Key==KEY_DOWN)
2227                         ) {
2228
2229
2230                         switch (event.KeyInput.Key) {
2231                                 case KEY_RETURN:
2232                                         if (m_allowclose) {
2233                                                 acceptInput();
2234                                                 quitMenu();
2235                                         }
2236                                         else
2237                                                 current_keys_pending.key_enter = true;
2238                                         break;
2239                                 case KEY_UP:
2240                                         current_keys_pending.key_up = true;
2241                                         break;
2242                                 case KEY_DOWN:
2243                                         current_keys_pending.key_down = true;
2244                                         break;
2245                                 break;
2246                                 default:
2247                                         //can't happen at all!
2248                                         assert("reached a source line that can't ever been reached" == 0);
2249                                         break;
2250                         }
2251                         acceptInput();
2252                         return true;
2253                 }
2254
2255         }
2256         if(event.EventType==EET_MOUSE_INPUT_EVENT
2257                         && event.MouseInput.Event != EMIE_MOUSE_MOVED)
2258         {
2259                 // Mouse event other than movement
2260
2261                 // Get selected item and hovered/clicked item (s)
2262
2263                 updateSelectedItem();
2264                 ItemSpec s = getItemAtPos(m_pointer);
2265
2266                 Inventory *inv_selected = NULL;
2267                 Inventory *inv_s = NULL;
2268
2269                 if(m_selected_item)
2270                 {
2271                         inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
2272                         assert(inv_selected);
2273                         assert(inv_selected->getList(m_selected_item->listname) != NULL);
2274                 }
2275
2276                 u32 s_count = 0;
2277
2278                 if(s.isValid())
2279                 do{ // breakable
2280                         inv_s = m_invmgr->getInventory(s.inventoryloc);
2281
2282                         if(!inv_s){
2283                                 errorstream<<"InventoryMenu: The selected inventory location "
2284                                                 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2285                                                 <<std::endl;
2286                                 s.i = -1;  // make it invalid again
2287                                 break;
2288                         }
2289
2290                         InventoryList *list = inv_s->getList(s.listname);
2291                         if(list == NULL){
2292                                 verbosestream<<"InventoryMenu: The selected inventory list \""
2293                                                 <<s.listname<<"\" does not exist"<<std::endl;
2294                                 s.i = -1;  // make it invalid again
2295                                 break;
2296                         }
2297
2298                         if((u32)s.i >= list->getSize()){
2299                                 infostream<<"InventoryMenu: The selected inventory list \""
2300                                                 <<s.listname<<"\" is too small (i="<<s.i<<", size="
2301                                                 <<list->getSize()<<")"<<std::endl;
2302                                 s.i = -1;  // make it invalid again
2303                                 break;
2304                         }
2305
2306                         s_count = list->getItem(s.i).count;
2307                 }while(0);
2308
2309                 bool identical = (m_selected_item != NULL) && s.isValid() &&
2310                         (inv_selected == inv_s) &&
2311                         (m_selected_item->listname == s.listname) &&
2312                         (m_selected_item->i == s.i);
2313
2314                 // buttons: 0 = left, 1 = right, 2 = middle
2315                 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event
2316                 int button = 0;
2317                 int updown = 2;
2318                 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
2319                         { button = 0; updown = 0; }
2320                 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
2321                         { button = 1; updown = 0; }
2322                 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
2323                         { button = 2; updown = 0; }
2324                 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
2325                         { button = 0; updown = 1; }
2326                 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
2327                         { button = 1; updown = 1; }
2328                 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
2329                         { button = 2; updown = 1; }
2330
2331                 // Set this number to a positive value to generate a move action
2332                 // from m_selected_item to s.
2333                 u32 move_amount = 0;
2334
2335                 // Set this number to a positive value to generate a drop action
2336                 // from m_selected_item.
2337                 u32 drop_amount = 0;
2338
2339                 // Set this number to a positive value to generate a craft action at s.
2340                 u32 craft_amount = 0;
2341
2342                 if(updown == 0)
2343                 {
2344                         // Some mouse button has been pressed
2345
2346                         //infostream<<"Mouse button "<<button<<" pressed at p=("
2347                         //      <<p.X<<","<<p.Y<<")"<<std::endl;
2348
2349                         m_selected_dragging = false;
2350
2351                         if(s.isValid() && s.listname == "craftpreview")
2352                         {
2353                                 // Craft preview has been clicked: craft
2354                                 craft_amount = (button == 2 ? 10 : 1);
2355                         }
2356                         else if(m_selected_item == NULL)
2357                         {
2358                                 if(s_count != 0)
2359                                 {
2360                                         // Non-empty stack has been clicked: select it
2361                                         m_selected_item = new ItemSpec(s);
2362
2363                                         if(button == 1)  // right
2364                                                 m_selected_amount = (s_count + 1) / 2;
2365                                         else if(button == 2)  // middle
2366                                                 m_selected_amount = MYMIN(s_count, 10);
2367                                         else  // left
2368                                                 m_selected_amount = s_count;
2369
2370                                         m_selected_dragging = true;
2371                                 }
2372                         }
2373                         else  // m_selected_item != NULL
2374                         {
2375                                 assert(m_selected_amount >= 1);
2376
2377                                 if(s.isValid())
2378                                 {
2379                                         // Clicked a slot: move
2380                                         if(button == 1)  // right
2381                                                 move_amount = 1;
2382                                         else if(button == 2)  // middle
2383                                                 move_amount = MYMIN(m_selected_amount, 10);
2384                                         else  // left
2385                                                 move_amount = m_selected_amount;
2386
2387                                         if(identical)
2388                                         {
2389                                                 if(move_amount >= m_selected_amount)
2390                                                         m_selected_amount = 0;
2391                                                 else
2392                                                         m_selected_amount -= move_amount;
2393                                                 move_amount = 0;
2394                                         }
2395                                 }
2396                                 else if(getAbsoluteClippingRect().isPointInside(m_pointer))
2397                                 {
2398                                         // Clicked somewhere else: deselect
2399                                         m_selected_amount = 0;
2400                                 }
2401                                 else
2402                                 {
2403                                         // Clicked outside of the window: drop
2404                                         if(button == 1)  // right
2405                                                 drop_amount = 1;
2406                                         else if(button == 2)  // middle
2407                                                 drop_amount = MYMIN(m_selected_amount, 10);
2408                                         else  // left
2409                                                 drop_amount = m_selected_amount;
2410                                 }
2411                         }
2412                 }
2413                 else if(updown == 1)
2414                 {
2415                         // Some mouse button has been released
2416
2417                         //infostream<<"Mouse button "<<button<<" released at p=("
2418                         //      <<p.X<<","<<p.Y<<")"<<std::endl;
2419
2420                         if(m_selected_item != NULL && m_selected_dragging && s.isValid())
2421                         {
2422                                 if(!identical)
2423                                 {
2424                                         // Dragged to different slot: move all selected
2425                                         move_amount = m_selected_amount;
2426                                 }
2427                         }
2428                         else if(m_selected_item != NULL && m_selected_dragging &&
2429                                 !(getAbsoluteClippingRect().isPointInside(m_pointer)))
2430                         {
2431                                 // Dragged outside of window: drop all selected
2432                                 drop_amount = m_selected_amount;
2433                         }
2434
2435                         m_selected_dragging = false;
2436                 }
2437
2438                 // Possibly send inventory action to server
2439                 if(move_amount > 0)
2440                 {
2441                         // Send IACTION_MOVE
2442
2443                         assert(m_selected_item && m_selected_item->isValid());
2444                         assert(s.isValid());
2445
2446                         assert(inv_selected && inv_s);
2447                         InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2448                         InventoryList *list_to = inv_s->getList(s.listname);
2449                         assert(list_from && list_to);
2450                         ItemStack stack_from = list_from->getItem(m_selected_item->i);
2451                         ItemStack stack_to = list_to->getItem(s.i);
2452
2453                         // Check how many items can be moved
2454                         move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
2455                         ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
2456                         // If source stack cannot be added to destination stack at all,
2457                         // they are swapped
2458                         if(leftover.count == stack_from.count && leftover.name == stack_from.name)
2459                         {
2460                                 m_selected_amount = stack_to.count;
2461                                 // In case the server doesn't directly swap them but instead
2462                                 // moves stack_to somewhere else, set this
2463                                 m_selected_content_guess = stack_to;
2464                                 m_selected_content_guess_inventory = s.inventoryloc;
2465                         }
2466                         // Source stack goes fully into destination stack
2467                         else if(leftover.empty())
2468                         {
2469                                 m_selected_amount -= move_amount;
2470                                 m_selected_content_guess = ItemStack(); // Clear
2471                         }
2472                         // Source stack goes partly into destination stack
2473                         else
2474                         {
2475                                 move_amount -= leftover.count;
2476                                 m_selected_amount -= move_amount;
2477                                 m_selected_content_guess = ItemStack(); // Clear
2478                         }
2479
2480                         infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
2481                         IMoveAction *a = new IMoveAction();
2482                         a->count = move_amount;
2483                         a->from_inv = m_selected_item->inventoryloc;
2484                         a->from_list = m_selected_item->listname;
2485                         a->from_i = m_selected_item->i;
2486                         a->to_inv = s.inventoryloc;
2487                         a->to_list = s.listname;
2488                         a->to_i = s.i;
2489                         m_invmgr->inventoryAction(a);
2490                 }
2491                 else if(drop_amount > 0)
2492                 {
2493                         m_selected_content_guess = ItemStack(); // Clear
2494
2495                         // Send IACTION_DROP
2496
2497                         assert(m_selected_item && m_selected_item->isValid());
2498                         assert(inv_selected);
2499                         InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2500                         assert(list_from);
2501                         ItemStack stack_from = list_from->getItem(m_selected_item->i);
2502
2503                         // Check how many items can be dropped
2504                         drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
2505                         assert(drop_amount > 0 && drop_amount <= m_selected_amount);
2506                         m_selected_amount -= drop_amount;
2507
2508                         infostream<<"Handing IACTION_DROP to manager"<<std::endl;
2509                         IDropAction *a = new IDropAction();
2510                         a->count = drop_amount;
2511                         a->from_inv = m_selected_item->inventoryloc;
2512                         a->from_list = m_selected_item->listname;
2513                         a->from_i = m_selected_item->i;
2514                         m_invmgr->inventoryAction(a);
2515                 }
2516                 else if(craft_amount > 0)
2517                 {
2518                         m_selected_content_guess = ItemStack(); // Clear
2519
2520                         // Send IACTION_CRAFT
2521
2522                         assert(s.isValid());
2523                         assert(inv_s);
2524
2525                         infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
2526                         ICraftAction *a = new ICraftAction();
2527                         a->count = craft_amount;
2528                         a->craft_inv = s.inventoryloc;
2529                         m_invmgr->inventoryAction(a);
2530                 }
2531
2532                 // If m_selected_amount has been decreased to zero, deselect
2533                 if(m_selected_amount == 0)
2534                 {
2535                         delete m_selected_item;
2536                         m_selected_item = NULL;
2537                         m_selected_amount = 0;
2538                         m_selected_dragging = false;
2539                         m_selected_content_guess = ItemStack();
2540                 }
2541         }
2542         if(event.EventType==EET_GUI_EVENT)
2543         {
2544
2545                 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
2546                                                 && isVisible())
2547                 {
2548                         // find the element that was clicked
2549                         for(u32 i=0; i<m_fields.size(); i++)
2550                         {
2551                                 FieldSpec &s = m_fields[i];
2552                                 // if its a button, set the send field so
2553                                 // lua knows which button was pressed
2554                                 if ((s.ftype == f_TabHeader) && (s.fid == event.GUIEvent.Caller->getID()))
2555                                 {
2556                                         s.send = true;
2557                                         acceptInput();
2558                                         s.send = false;
2559                                         return true;
2560                                 }
2561                         }
2562                 }
2563                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
2564                                 && isVisible())
2565                 {
2566                         if(!canTakeFocus(event.GUIEvent.Element))
2567                         {
2568                                 infostream<<"GUIFormSpecMenu: Not allowing focus change."
2569                                                 <<std::endl;
2570                                 // Returning true disables focus change
2571                                 return true;
2572                         }
2573                 }
2574                 if((event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED) ||
2575                                 (event.GUIEvent.EventType==gui::EGET_CHECKBOX_CHANGED))
2576                 {
2577                         unsigned int btn_id = event.GUIEvent.Caller->getID();
2578
2579                         if (btn_id == 257) {
2580                                 acceptInput();
2581                                 if (m_allowclose)
2582                                         quitMenu();
2583                                 else
2584                                         m_text_dst->gotText(narrow_to_wide("ExitButton"));
2585                                 // quitMenu deallocates menu
2586                                 return true;
2587                         }
2588
2589                         // find the element that was clicked
2590                         for(u32 i=0; i<m_fields.size(); i++)
2591                         {
2592                                 FieldSpec &s = m_fields[i];
2593                                 // if its a button, set the send field so 
2594                                 // lua knows which button was pressed
2595                                 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
2596                                                 (s.fid == event.GUIEvent.Caller->getID()))
2597                                 {
2598                                         s.send = true;
2599                                         acceptInput();
2600                                         if(s.is_exit){
2601                                                 if (m_allowclose)
2602                                                         quitMenu();
2603                                                 else
2604                                                         m_text_dst->gotText(narrow_to_wide("ExitButton"));
2605                                                 return true;
2606                                         }else{
2607                                                 s.send = false;
2608                                                 return true;
2609                                         }
2610                                 }
2611                         }
2612                 }
2613                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
2614                 {
2615                         if(event.GUIEvent.Caller->getID() > 257)
2616                         {
2617
2618                                 if (m_allowclose) {
2619                                         acceptInput();
2620                                         quitMenu();
2621                                 }
2622                                 else {
2623                                         current_keys_pending.key_enter = true;
2624                                         acceptInput();
2625                                 }
2626                                 // quitMenu deallocates menu
2627                                 return true;
2628                         }
2629                 }
2630
2631                 if((event.GUIEvent.EventType==gui::EGET_LISTBOX_SELECTED_AGAIN) ||
2632                         (event.GUIEvent.EventType==gui::EGET_LISTBOX_CHANGED))
2633                 {
2634                         int current_id = event.GUIEvent.Caller->getID();
2635                         if(current_id > 257)
2636                         {
2637                                 // find the element that was clicked
2638                                 for(u32 i=0; i<m_fields.size(); i++)
2639                                 {
2640                                         FieldSpec &s = m_fields[i];
2641                                         // if its a listbox, set the send field so
2642                                         // lua knows which listbox was changed
2643                                         // checkListboxClick() is black magic
2644                                         // for properly handling double clicks
2645                                         if ((s.ftype == f_ListBox) && (s.fid == current_id)
2646                                                         && checkListboxClick(s.fname,
2647                                                                 event.GUIEvent.EventType))
2648                                         {
2649                                                 s.send = true;
2650                                                 acceptInput();
2651                                                 s.send=false;
2652                                         }
2653                                 }
2654                                 return true;
2655                         }
2656                 }
2657         }
2658
2659         return Parent ? Parent->OnEvent(event) : false;
2660 }
2661
2662 bool GUIFormSpecMenu::parseColor(std::string color, irr::video::SColor& outcolor) {
2663         outcolor = irr::video::SColor(0,0,0,0);
2664
2665         if (!string_allowed(color, "0123456789abcdefABCDEF"))
2666                 return false;
2667
2668         u32 color_value;
2669         std::istringstream iss(color);
2670         iss >> std::hex >> color_value;
2671         
2672         outcolor = irr::video::SColor(color_value);
2673
2674         outcolor.setAlpha(255);
2675         return true;
2676 }