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