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