]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiFormSpecMenu.cpp
Fix background formspec elements from interfering with each other
[dragonfireclient.git] / src / guiFormSpecMenu.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20
21 #include <cstdlib>
22 #include <algorithm>
23 #include <iterator>
24 #include <sstream>
25 #include <limits>
26 #include "guiFormSpecMenu.h"
27 #include "guiTable.h"
28 #include "constants.h"
29 #include "gamedef.h"
30 #include "keycode.h"
31 #include "util/strfnd.h"
32 #include <IGUICheckBox.h>
33 #include <IGUIEditBox.h>
34 #include <IGUIButton.h>
35 #include <IGUIStaticText.h>
36 #include <IGUIFont.h>
37 #include <IGUITabControl.h>
38 #include <IGUIComboBox.h>
39 #include "log.h"
40 #include "client/tile.h" // ITextureSource
41 #include "hud.h" // drawItemStack
42 #include "filesys.h"
43 #include "gettime.h"
44 #include "gettext.h"
45 #include "scripting_game.h"
46 #include "porting.h"
47 #include "settings.h"
48 #include "client.h"
49 #include "fontengine.h"
50 #include "util/hex.h"
51 #include "util/numeric.h"
52 #include "util/string.h" // for parseColorString()
53 #include "irrlicht_changes/static_text.h"
54 #include "guiscalingfilter.h"
55
56 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
57 #include "intlGUIEditBox.h"
58 #endif
59
60 #define MY_CHECKPOS(a,b)                                                                                                        \
61         if (v_pos.size() != 2) {                                                                                                \
62                 errorstream<< "Invalid pos for element " << a << "specified: \""        \
63                         << parts[b] << "\"" << std::endl;                                                               \
64                         return;                                                                                                                 \
65         }
66
67 #define MY_CHECKGEOM(a,b)                                                                                                       \
68         if (v_geom.size() != 2) {                                                                                               \
69                 errorstream<< "Invalid pos for element " << a << "specified: \""        \
70                         << parts[b] << "\"" << std::endl;                                                               \
71                         return;                                                                                                                 \
72         }
73 /*
74         GUIFormSpecMenu
75 */
76 static unsigned int font_line_height(gui::IGUIFont *font)
77 {
78         return font->getDimension(L"Ay").Height + font->getKerningHeight();
79 }
80
81 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
82                 JoystickController *joystick,
83                 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
84                 InventoryManager *invmgr, IGameDef *gamedef,
85                 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
86                 Client* client, bool remap_dbl_click) :
87         GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
88         m_device(dev),
89         m_invmgr(invmgr),
90         m_gamedef(gamedef),
91         m_tsrc(tsrc),
92         m_client(client),
93         m_selected_item(NULL),
94         m_selected_amount(0),
95         m_selected_dragging(false),
96         m_tooltip_element(NULL),
97         m_hovered_time(0),
98         m_old_tooltip_id(-1),
99         m_rmouse_auto_place(false),
100         m_allowclose(true),
101         m_lock(false),
102         m_form_src(fsrc),
103         m_text_dst(tdst),
104         m_formspec_version(0),
105         m_focused_element(""),
106         m_joystick(joystick),
107         current_field_enter_pending(""),
108         m_font(NULL),
109         m_remap_dbl_click(remap_dbl_click)
110 #ifdef __ANDROID__
111         , m_JavaDialogFieldName("")
112 #endif
113 {
114         current_keys_pending.key_down = false;
115         current_keys_pending.key_up = false;
116         current_keys_pending.key_enter = false;
117         current_keys_pending.key_escape = false;
118
119         m_doubleclickdetect[0].time = 0;
120         m_doubleclickdetect[1].time = 0;
121
122         m_doubleclickdetect[0].pos = v2s32(0, 0);
123         m_doubleclickdetect[1].pos = v2s32(0, 0);
124
125         m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
126 }
127
128 GUIFormSpecMenu::~GUIFormSpecMenu()
129 {
130         removeChildren();
131
132         for (u32 i = 0; i < m_tables.size(); ++i) {
133                 GUITable *table = m_tables[i].second;
134                 table->drop();
135         }
136
137         delete m_selected_item;
138
139         if (m_form_src != NULL) {
140                 delete m_form_src;
141         }
142         if (m_text_dst != NULL) {
143                 delete m_text_dst;
144         }
145 }
146
147 void GUIFormSpecMenu::removeChildren()
148 {
149         const core::list<gui::IGUIElement*> &children = getChildren();
150
151         while(!children.empty()) {
152                 (*children.getLast())->remove();
153         }
154
155         if(m_tooltip_element) {
156                 m_tooltip_element->remove();
157                 m_tooltip_element->drop();
158                 m_tooltip_element = NULL;
159         }
160
161 }
162
163 void GUIFormSpecMenu::setInitialFocus()
164 {
165         // Set initial focus according to following order of precedence:
166         // 1. first empty editbox
167         // 2. first editbox
168         // 3. first table
169         // 4. last button
170         // 5. first focusable (not statictext, not tabheader)
171         // 6. first child element
172
173         core::list<gui::IGUIElement*> children = getChildren();
174
175         // in case "children" contains any NULL elements, remove them
176         for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
177                         it != children.end();) {
178                 if (*it)
179                         ++it;
180                 else
181                         it = children.erase(it);
182         }
183
184         // 1. first empty editbox
185         for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
186                         it != children.end(); ++it) {
187                 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
188                                 && (*it)->getText()[0] == 0) {
189                         Environment->setFocus(*it);
190                         return;
191                 }
192         }
193
194         // 2. first editbox
195         for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
196                         it != children.end(); ++it) {
197                 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
198                         Environment->setFocus(*it);
199                         return;
200                 }
201         }
202
203         // 3. first table
204         for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
205                         it != children.end(); ++it) {
206                 if ((*it)->getTypeName() == std::string("GUITable")) {
207                         Environment->setFocus(*it);
208                         return;
209                 }
210         }
211
212         // 4. last button
213         for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
214                         it != children.end(); --it) {
215                 if ((*it)->getType() == gui::EGUIET_BUTTON) {
216                         Environment->setFocus(*it);
217                         return;
218                 }
219         }
220
221         // 5. first focusable (not statictext, not tabheader)
222         for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
223                         it != children.end(); ++it) {
224                 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
225                                 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
226                         Environment->setFocus(*it);
227                         return;
228                 }
229         }
230
231         // 6. first child element
232         if (children.empty())
233                 Environment->setFocus(this);
234         else
235                 Environment->setFocus(*(children.begin()));
236 }
237
238 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
239 {
240         for (u32 i = 0; i < m_tables.size(); ++i) {
241                 if (tablename == m_tables[i].first.fname)
242                         return m_tables[i].second;
243         }
244         return 0;
245 }
246
247 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
248 {
249         for (u32 i = 0; i < m_dropdowns.size(); ++i) {
250                 if (name == m_dropdowns[i].first.fname)
251                         return &m_dropdowns[i].second;
252         }
253         return NULL;
254 }
255
256 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
257 {
258         std::vector<std::string> parts = split(element,',');
259
260         if (((parts.size() == 2) || parts.size() == 3) ||
261                 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
262         {
263                 if (parts[1].find(';') != std::string::npos)
264                         parts[1] = parts[1].substr(0,parts[1].find(';'));
265
266                 data->invsize.X = MYMAX(0, stof(parts[0]));
267                 data->invsize.Y = MYMAX(0, stof(parts[1]));
268
269                 lockSize(false);
270                 if (parts.size() == 3) {
271                         if (parts[2] == "true") {
272                                 lockSize(true,v2u32(800,600));
273                         }
274                 }
275
276                 data->explicit_size = true;
277                 return;
278         }
279         errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'"  << std::endl;
280 }
281
282 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
283 {
284         if (m_gamedef == 0) {
285                 warningstream<<"invalid use of 'list' with m_gamedef==0"<<std::endl;
286                 return;
287         }
288
289         std::vector<std::string> parts = split(element,';');
290
291         if (((parts.size() == 4) || (parts.size() == 5)) ||
292                 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
293         {
294                 std::string location = parts[0];
295                 std::string listname = parts[1];
296                 std::vector<std::string> v_pos  = split(parts[2],',');
297                 std::vector<std::string> v_geom = split(parts[3],',');
298                 std::string startindex = "";
299                 if (parts.size() == 5)
300                         startindex = parts[4];
301
302                 MY_CHECKPOS("list",2);
303                 MY_CHECKGEOM("list",3);
304
305                 InventoryLocation loc;
306
307                 if(location == "context" || location == "current_name")
308                         loc = m_current_inventory_location;
309                 else
310                         loc.deSerialize(location);
311
312                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
313                 pos.X += stof(v_pos[0]) * (float)spacing.X;
314                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
315
316                 v2s32 geom;
317                 geom.X = stoi(v_geom[0]);
318                 geom.Y = stoi(v_geom[1]);
319
320                 s32 start_i = 0;
321                 if(startindex != "")
322                         start_i = stoi(startindex);
323
324                 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
325                         errorstream<< "Invalid list element: '" << element << "'"  << std::endl;
326                         return;
327                 }
328
329                 if(!data->explicit_size)
330                         warningstream<<"invalid use of list without a size[] element"<<std::endl;
331                 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
332                 return;
333         }
334         errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'"  << std::endl;
335 }
336
337 void GUIFormSpecMenu::parseListRing(parserData* data, std::string element)
338 {
339         if (m_gamedef == 0) {
340                 errorstream << "WARNING: invalid use of 'listring' with m_gamedef==0" << std::endl;
341                 return;
342         }
343
344         std::vector<std::string> parts = split(element, ';');
345
346         if (parts.size() == 2) {
347                 std::string location = parts[0];
348                 std::string listname = parts[1];
349
350                 InventoryLocation loc;
351
352                 if (location == "context" || location == "current_name")
353                         loc = m_current_inventory_location;
354                 else
355                         loc.deSerialize(location);
356
357                 m_inventory_rings.push_back(ListRingSpec(loc, listname));
358                 return;
359         } else if ((element == "") && (m_inventorylists.size() > 1)) {
360                 size_t siz = m_inventorylists.size();
361                 // insert the last two inv list elements into the list ring
362                 const ListDrawSpec &spa = m_inventorylists[siz - 2];
363                 const ListDrawSpec &spb = m_inventorylists[siz - 1];
364                 m_inventory_rings.push_back(ListRingSpec(spa.inventoryloc, spa.listname));
365                 m_inventory_rings.push_back(ListRingSpec(spb.inventoryloc, spb.listname));
366                 return;
367         }
368         errorstream<< "Invalid list ring element(" << parts.size() << ", "
369                 << m_inventorylists.size() << "): '" << element << "'"  << std::endl;
370 }
371
372 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
373 {
374         std::vector<std::string> parts = split(element,';');
375
376         if (((parts.size() >= 3) && (parts.size() <= 4)) ||
377                 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
378         {
379                 std::vector<std::string> v_pos = split(parts[0],',');
380                 std::string name = parts[1];
381                 std::string label = parts[2];
382                 std::string selected = "";
383
384                 if (parts.size() >= 4)
385                         selected = parts[3];
386
387                 MY_CHECKPOS("checkbox",0);
388
389                 v2s32 pos = padding;
390                 pos.X += stof(v_pos[0]) * (float) spacing.X;
391                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
392
393                 bool fselected = false;
394
395                 if (selected == "true")
396                         fselected = true;
397
398                 std::wstring wlabel = utf8_to_wide(unescape_string(label));
399
400                 core::rect<s32> rect = core::rect<s32>(
401                                 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
402                                 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
403                                 pos.Y + ((imgsize.Y/2) + m_btn_height));
404
405                 FieldSpec spec(
406                                 name,
407                                 wlabel, //Needed for displaying text on MSVC
408                                 wlabel,
409                                 258+m_fields.size()
410                         );
411
412                 spec.ftype = f_CheckBox;
413
414                 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
415                                         spec.fid, spec.flabel.c_str());
416
417                 if (spec.fname == data->focused_fieldname) {
418                         Environment->setFocus(e);
419                 }
420
421                 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
422                 m_fields.push_back(spec);
423                 return;
424         }
425         errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'"  << std::endl;
426 }
427
428 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
429 {
430         std::vector<std::string> parts = split(element,';');
431
432         if (parts.size() >= 5) {
433                 std::vector<std::string> v_pos = split(parts[0],',');
434                 std::vector<std::string> v_dim = split(parts[1],',');
435                 std::string name = parts[3];
436                 std::string value = parts[4];
437
438                 MY_CHECKPOS("scrollbar",0);
439
440                 v2s32 pos = padding;
441                 pos.X += stof(v_pos[0]) * (float) spacing.X;
442                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
443
444                 if (v_dim.size() != 2) {
445                         errorstream<< "Invalid size for element " << "scrollbar"
446                                 << "specified: \"" << parts[1] << "\"" << std::endl;
447                         return;
448                 }
449
450                 v2s32 dim;
451                 dim.X = stof(v_dim[0]) * (float) spacing.X;
452                 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
453
454                 core::rect<s32> rect =
455                                 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
456
457                 FieldSpec spec(
458                                 name,
459                                 L"",
460                                 L"",
461                                 258+m_fields.size()
462                         );
463
464                 bool is_horizontal = true;
465
466                 if (parts[2] == "vertical")
467                         is_horizontal = false;
468
469                 spec.ftype = f_ScrollBar;
470                 spec.send  = true;
471                 gui::IGUIScrollBar* e =
472                                 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
473
474                 e->setMax(1000);
475                 e->setMin(0);
476                 e->setPos(stoi(parts[4]));
477                 e->setSmallStep(10);
478                 e->setLargeStep(100);
479
480                 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
481                 m_fields.push_back(spec);
482                 return;
483         }
484         errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'"  << std::endl;
485 }
486
487 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
488 {
489         std::vector<std::string> parts = split(element,';');
490
491         if ((parts.size() == 3) ||
492                 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
493         {
494                 std::vector<std::string> v_pos = split(parts[0],',');
495                 std::vector<std::string> v_geom = split(parts[1],',');
496                 std::string name = unescape_string(parts[2]);
497
498                 MY_CHECKPOS("image",0);
499                 MY_CHECKGEOM("image",1);
500
501                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
502                 pos.X += stof(v_pos[0]) * (float) spacing.X;
503                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
504
505                 v2s32 geom;
506                 geom.X = stof(v_geom[0]) * (float)imgsize.X;
507                 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
508
509                 if(!data->explicit_size)
510                         warningstream<<"invalid use of image without a size[] element"<<std::endl;
511                 m_images.push_back(ImageDrawSpec(name, pos, geom));
512                 return;
513         }
514
515         if (parts.size() == 2) {
516                 std::vector<std::string> v_pos = split(parts[0],',');
517                 std::string name = unescape_string(parts[1]);
518
519                 MY_CHECKPOS("image",0);
520
521                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
522                 pos.X += stof(v_pos[0]) * (float) spacing.X;
523                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
524
525                 if(!data->explicit_size)
526                         warningstream<<"invalid use of image without a size[] element"<<std::endl;
527                 m_images.push_back(ImageDrawSpec(name, pos));
528                 return;
529         }
530         errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'"  << std::endl;
531 }
532
533 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
534 {
535         std::vector<std::string> parts = split(element,';');
536
537         if ((parts.size() == 3) ||
538                 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
539         {
540                 std::vector<std::string> v_pos = split(parts[0],',');
541                 std::vector<std::string> v_geom = split(parts[1],',');
542                 std::string name = parts[2];
543
544                 MY_CHECKPOS("itemimage",0);
545                 MY_CHECKGEOM("itemimage",1);
546
547                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
548                 pos.X += stof(v_pos[0]) * (float) spacing.X;
549                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
550
551                 v2s32 geom;
552                 geom.X = stof(v_geom[0]) * (float)imgsize.X;
553                 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
554
555                 if(!data->explicit_size)
556                         warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
557                 m_itemimages.push_back(ImageDrawSpec("", name, pos, geom));
558                 return;
559         }
560         errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'"  << std::endl;
561 }
562
563 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
564                 std::string type)
565 {
566         std::vector<std::string> parts = split(element,';');
567
568         if ((parts.size() == 4) ||
569                 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
570         {
571                 std::vector<std::string> v_pos = split(parts[0],',');
572                 std::vector<std::string> v_geom = split(parts[1],',');
573                 std::string name = parts[2];
574                 std::string label = parts[3];
575
576                 MY_CHECKPOS("button",0);
577                 MY_CHECKGEOM("button",1);
578
579                 v2s32 pos = padding;
580                 pos.X += stof(v_pos[0]) * (float)spacing.X;
581                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
582
583                 v2s32 geom;
584                 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
585                 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
586
587                 core::rect<s32> rect =
588                                 core::rect<s32>(pos.X, pos.Y - m_btn_height,
589                                                 pos.X + geom.X, pos.Y + m_btn_height);
590
591                 if(!data->explicit_size)
592                         warningstream<<"invalid use of button without a size[] element"<<std::endl;
593
594                 std::wstring wlabel = utf8_to_wide(unescape_string(label));
595
596                 FieldSpec spec(
597                         name,
598                         wlabel,
599                         L"",
600                         258+m_fields.size()
601                 );
602                 spec.ftype = f_Button;
603                 if(type == "button_exit")
604                         spec.is_exit = true;
605                 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
606                                 spec.flabel.c_str());
607
608                 if (spec.fname == data->focused_fieldname) {
609                         Environment->setFocus(e);
610                 }
611
612                 m_fields.push_back(spec);
613                 return;
614         }
615         errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'"  << std::endl;
616 }
617
618 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
619 {
620         std::vector<std::string> parts = split(element,';');
621
622         if (((parts.size() == 3) || (parts.size() == 4)) ||
623                 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
624         {
625                 std::vector<std::string> v_pos = split(parts[0],',');
626                 std::vector<std::string> v_geom = split(parts[1],',');
627                 std::string name = unescape_string(parts[2]);
628
629                 MY_CHECKPOS("background",0);
630                 MY_CHECKGEOM("background",1);
631
632                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
633                 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
634                 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
635
636                 v2s32 geom;
637                 geom.X = stof(v_geom[0]) * (float)spacing.X;
638                 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
639
640                 if (!data->explicit_size)
641                         warningstream<<"invalid use of background without a size[] element"<<std::endl;
642
643                 bool clip = false;
644                 if (parts.size() == 4 && is_yes(parts[3])) {
645                         pos.X = stoi(v_pos[0]); //acts as offset
646                         pos.Y = stoi(v_pos[1]); //acts as offset
647                         clip = true;
648                 }
649                 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom, clip));
650
651                 return;
652         }
653         errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'"  << std::endl;
654 }
655
656 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
657 {
658         std::vector<std::string> parts = split(element,';');
659
660         data->table_options.clear();
661         for (size_t i = 0; i < parts.size(); ++i) {
662                 // Parse table option
663                 std::string opt = unescape_string(parts[i]);
664                 data->table_options.push_back(GUITable::splitOption(opt));
665         }
666 }
667
668 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
669 {
670         std::vector<std::string> parts = split(element,';');
671
672         data->table_columns.clear();
673         for (size_t i = 0; i < parts.size(); ++i) {
674                 std::vector<std::string> col_parts = split(parts[i],',');
675                 GUITable::TableColumn column;
676                 // Parse column type
677                 if (!col_parts.empty())
678                         column.type = col_parts[0];
679                 // Parse column options
680                 for (size_t j = 1; j < col_parts.size(); ++j) {
681                         std::string opt = unescape_string(col_parts[j]);
682                         column.options.push_back(GUITable::splitOption(opt));
683                 }
684                 data->table_columns.push_back(column);
685         }
686 }
687
688 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
689 {
690         std::vector<std::string> parts = split(element,';');
691
692         if (((parts.size() == 4) || (parts.size() == 5)) ||
693                 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
694         {
695                 std::vector<std::string> v_pos = split(parts[0],',');
696                 std::vector<std::string> v_geom = split(parts[1],',');
697                 std::string name = parts[2];
698                 std::vector<std::string> items = split(parts[3],',');
699                 std::string str_initial_selection = "";
700                 std::string str_transparent = "false";
701
702                 if (parts.size() >= 5)
703                         str_initial_selection = parts[4];
704
705                 MY_CHECKPOS("table",0);
706                 MY_CHECKGEOM("table",1);
707
708                 v2s32 pos = padding;
709                 pos.X += stof(v_pos[0]) * (float)spacing.X;
710                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
711
712                 v2s32 geom;
713                 geom.X = stof(v_geom[0]) * (float)spacing.X;
714                 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
715
716                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
717
718                 FieldSpec spec(
719                         name,
720                         L"",
721                         L"",
722                         258+m_fields.size()
723                 );
724
725                 spec.ftype = f_Table;
726
727                 for (unsigned int i = 0; i < items.size(); ++i) {
728                         items[i] = unescape_enriched(unescape_string(items[i]));
729                 }
730
731                 //now really show table
732                 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
733                                 m_tsrc);
734
735                 if (spec.fname == data->focused_fieldname) {
736                         Environment->setFocus(e);
737                 }
738
739                 e->setTable(data->table_options, data->table_columns, items);
740
741                 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
742                         e->setDynamicData(data->table_dyndata[name]);
743                 }
744
745                 if ((str_initial_selection != "") &&
746                                 (str_initial_selection != "0"))
747                         e->setSelected(stoi(str_initial_selection.c_str()));
748
749                 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
750                 m_fields.push_back(spec);
751                 return;
752         }
753         errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'"  << std::endl;
754 }
755
756 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
757 {
758         std::vector<std::string> parts = split(element,';');
759
760         if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
761                 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
762         {
763                 std::vector<std::string> v_pos = split(parts[0],',');
764                 std::vector<std::string> v_geom = split(parts[1],',');
765                 std::string name = parts[2];
766                 std::vector<std::string> items = split(parts[3],',');
767                 std::string str_initial_selection = "";
768                 std::string str_transparent = "false";
769
770                 if (parts.size() >= 5)
771                         str_initial_selection = parts[4];
772
773                 if (parts.size() >= 6)
774                         str_transparent = parts[5];
775
776                 MY_CHECKPOS("textlist",0);
777                 MY_CHECKGEOM("textlist",1);
778
779                 v2s32 pos = padding;
780                 pos.X += stof(v_pos[0]) * (float)spacing.X;
781                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
782
783                 v2s32 geom;
784                 geom.X = stof(v_geom[0]) * (float)spacing.X;
785                 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
786
787
788                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
789
790                 FieldSpec spec(
791                         name,
792                         L"",
793                         L"",
794                         258+m_fields.size()
795                 );
796
797                 spec.ftype = f_Table;
798
799                 for (unsigned int i = 0; i < items.size(); ++i) {
800                         items[i] = unescape_enriched(unescape_string(items[i]));
801                 }
802
803                 //now really show list
804                 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
805                                 m_tsrc);
806
807                 if (spec.fname == data->focused_fieldname) {
808                         Environment->setFocus(e);
809                 }
810
811                 e->setTextList(items, is_yes(str_transparent));
812
813                 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
814                         e->setDynamicData(data->table_dyndata[name]);
815                 }
816
817                 if ((str_initial_selection != "") &&
818                                 (str_initial_selection != "0"))
819                         e->setSelected(stoi(str_initial_selection.c_str()));
820
821                 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
822                 m_fields.push_back(spec);
823                 return;
824         }
825         errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'"  << std::endl;
826 }
827
828
829 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
830 {
831         std::vector<std::string> parts = split(element,';');
832
833         if ((parts.size() == 5) ||
834                 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
835         {
836                 std::vector<std::string> v_pos = split(parts[0],',');
837                 std::string name = parts[2];
838                 std::vector<std::string> items = split(parts[3],',');
839                 std::string str_initial_selection = "";
840                 str_initial_selection = parts[4];
841
842                 MY_CHECKPOS("dropdown",0);
843
844                 v2s32 pos = padding;
845                 pos.X += stof(v_pos[0]) * (float)spacing.X;
846                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
847
848                 s32 width = stof(parts[1]) * (float)spacing.Y;
849
850                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
851                                 pos.X + width, pos.Y + (m_btn_height * 2));
852
853                 FieldSpec spec(
854                         name,
855                         L"",
856                         L"",
857                         258+m_fields.size()
858                 );
859
860                 spec.ftype = f_DropDown;
861                 spec.send = true;
862
863                 //now really show list
864                 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
865
866                 if (spec.fname == data->focused_fieldname) {
867                         Environment->setFocus(e);
868                 }
869
870                 for (unsigned int i=0; i < items.size(); i++) {
871                         e->addItem(unescape_enriched(unescape_string(
872                                 utf8_to_wide(items[i]))).c_str());
873                 }
874
875                 if (str_initial_selection != "")
876                         e->setSelected(stoi(str_initial_selection.c_str())-1);
877
878                 m_fields.push_back(spec);
879
880                 m_dropdowns.push_back(std::pair<FieldSpec,
881                         std::vector<std::string> >(spec, std::vector<std::string>()));
882                 std::vector<std::string> &values = m_dropdowns.back().second;
883                 for (unsigned int i = 0; i < items.size(); i++) {
884                         values.push_back(unescape_string(items[i]));
885                 }
886
887                 return;
888         }
889         errorstream << "Invalid dropdown element(" << parts.size() << "): '"
890                                 << element << "'"  << std::endl;
891 }
892
893 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
894 {
895         std::vector<std::string> parts = split(element,';');
896
897         if ((parts.size() == 4) || (parts.size() == 5) ||
898                 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
899         {
900                 std::vector<std::string> v_pos = split(parts[0],',');
901                 std::vector<std::string> v_geom = split(parts[1],',');
902                 std::string name = parts[2];
903                 std::string label = parts[3];
904
905                 MY_CHECKPOS("pwdfield",0);
906                 MY_CHECKGEOM("pwdfield",1);
907
908                 v2s32 pos;
909                 pos.X += stof(v_pos[0]) * (float)spacing.X;
910                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
911
912                 v2s32 geom;
913                 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
914
915                 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
916                 pos.Y -= m_btn_height;
917                 geom.Y = m_btn_height*2;
918
919                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
920
921                 std::wstring wlabel = utf8_to_wide(unescape_string(label));
922
923                 FieldSpec spec(
924                         name,
925                         wlabel,
926                         L"",
927                         258+m_fields.size()
928                         );
929
930                 spec.send = true;
931                 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
932
933                 if (spec.fname == data->focused_fieldname) {
934                         Environment->setFocus(e);
935                 }
936
937                 if (label.length() >= 1)
938                 {
939                         int font_height = g_fontengine->getTextHeight();
940                         rect.UpperLeftCorner.Y -= font_height;
941                         rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
942                         addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
943                 }
944
945                 e->setPasswordBox(true,L'*');
946
947                 irr::SEvent evt;
948                 evt.EventType            = EET_KEY_INPUT_EVENT;
949                 evt.KeyInput.Key         = KEY_END;
950                 evt.KeyInput.Char        = 0;
951                 evt.KeyInput.Control     = 0;
952                 evt.KeyInput.Shift       = 0;
953                 evt.KeyInput.PressedDown = true;
954                 e->OnEvent(evt);
955
956                 if (parts.size() >= 5 && !is_yes(parts[4])) {
957                         spec.close_on_enter = false;
958                 }
959
960                 m_fields.push_back(spec);
961                 return;
962         }
963         errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'"  << std::endl;
964 }
965
966 void GUIFormSpecMenu::parseSimpleField(parserData* data,
967                 std::vector<std::string> &parts)
968 {
969         std::string name = parts[0];
970         std::string label = parts[1];
971         std::string default_val = parts[2];
972
973         core::rect<s32> rect;
974
975         if(data->explicit_size)
976                 warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
977
978         v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
979         pos.Y = ((m_fields.size()+2)*60);
980         v2s32 size = DesiredRect.getSize();
981
982         rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
983                         (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
984
985
986         if(m_form_src)
987                 default_val = m_form_src->resolveText(default_val);
988
989
990         std::wstring wlabel = utf8_to_wide(unescape_string(label));
991
992         FieldSpec spec(
993                 name,
994                 wlabel,
995                 utf8_to_wide(unescape_string(default_val)),
996                 258+m_fields.size()
997         );
998
999         if (name == "")
1000         {
1001                 // spec field id to 0, this stops submit searching for a value that isn't there
1002                 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1003         }
1004         else
1005         {
1006                 spec.send = true;
1007                 gui::IGUIElement *e;
1008 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1009                 if (g_settings->getBool("freetype")) {
1010                         e = (gui::IGUIElement *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1011                                 true, Environment, this, spec.fid, rect);
1012                         e->drop();
1013                 } else {
1014 #else
1015                 {
1016 #endif
1017                         e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1018                 }
1019                 if (spec.fname == data->focused_fieldname) {
1020                         Environment->setFocus(e);
1021                 }
1022
1023                 irr::SEvent evt;
1024                 evt.EventType            = EET_KEY_INPUT_EVENT;
1025                 evt.KeyInput.Key         = KEY_END;
1026                 evt.KeyInput.Char        = 0;
1027                 evt.KeyInput.Control     = 0;
1028                 evt.KeyInput.Shift       = 0;
1029                 evt.KeyInput.PressedDown = true;
1030                 e->OnEvent(evt);
1031
1032                 if (label.length() >= 1)
1033                 {
1034                         int font_height = g_fontengine->getTextHeight();
1035                         rect.UpperLeftCorner.Y -= font_height;
1036                         rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1037                         addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1038                 }
1039         }
1040
1041         if (parts.size() >= 4 && !is_yes(parts[3])) {
1042                 spec.close_on_enter = false;
1043         }
1044
1045         m_fields.push_back(spec);
1046 }
1047
1048 void GUIFormSpecMenu::parseTextArea(parserData* data,
1049                 std::vector<std::string>& parts,std::string type)
1050 {
1051
1052         std::vector<std::string> v_pos = split(parts[0],',');
1053         std::vector<std::string> v_geom = split(parts[1],',');
1054         std::string name = parts[2];
1055         std::string label = parts[3];
1056         std::string default_val = parts[4];
1057
1058         MY_CHECKPOS(type,0);
1059         MY_CHECKGEOM(type,1);
1060
1061         v2s32 pos;
1062         pos.X = stof(v_pos[0]) * (float) spacing.X;
1063         pos.Y = stof(v_pos[1]) * (float) spacing.Y;
1064
1065         v2s32 geom;
1066
1067         geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1068
1069         if (type == "textarea")
1070         {
1071                 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1072                 pos.Y += m_btn_height;
1073         }
1074         else
1075         {
1076                 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1077                 pos.Y -= m_btn_height;
1078                 geom.Y = m_btn_height*2;
1079         }
1080
1081         core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1082
1083         if(!data->explicit_size)
1084                 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1085
1086         if(m_form_src)
1087                 default_val = m_form_src->resolveText(default_val);
1088
1089
1090         std::wstring wlabel = utf8_to_wide(unescape_string(label));
1091
1092         FieldSpec spec(
1093                 name,
1094                 wlabel,
1095                 utf8_to_wide(unescape_string(default_val)),
1096                 258+m_fields.size()
1097         );
1098
1099         if (name == "")
1100         {
1101                 // spec field id to 0, this stops submit searching for a value that isn't there
1102                 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1103         }
1104         else
1105         {
1106                 spec.send = true;
1107
1108                 gui::IGUIEditBox *e;
1109 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1110                 if (g_settings->getBool("freetype")) {
1111                         e = (gui::IGUIEditBox *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1112                                 true, Environment, this, spec.fid, rect);
1113                         e->drop();
1114                 } else {
1115 #else
1116                 {
1117 #endif
1118                         e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1119                 }
1120
1121                 if (spec.fname == data->focused_fieldname) {
1122                         Environment->setFocus(e);
1123                 }
1124
1125                 if (type == "textarea")
1126                 {
1127                         e->setMultiLine(true);
1128                         e->setWordWrap(true);
1129                         e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1130                 } else {
1131                         irr::SEvent evt;
1132                         evt.EventType            = EET_KEY_INPUT_EVENT;
1133                         evt.KeyInput.Key         = KEY_END;
1134                         evt.KeyInput.Char        = 0;
1135                         evt.KeyInput.Control     = 0;
1136                         evt.KeyInput.Shift       = 0;
1137                         evt.KeyInput.PressedDown = true;
1138                         e->OnEvent(evt);
1139                 }
1140
1141                 if (label.length() >= 1)
1142                 {
1143                         int font_height = g_fontengine->getTextHeight();
1144                         rect.UpperLeftCorner.Y -= font_height;
1145                         rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1146                         addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1147                 }
1148         }
1149
1150         if (parts.size() >= 6 && !is_yes(parts[5])) {
1151                 spec.close_on_enter = false;
1152         }
1153
1154         m_fields.push_back(spec);
1155 }
1156
1157 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1158                 std::string type)
1159 {
1160         std::vector<std::string> parts = split(element,';');
1161
1162         if (parts.size() == 3 || parts.size() == 4) {
1163                 parseSimpleField(data,parts);
1164                 return;
1165         }
1166
1167         if ((parts.size() == 5) || (parts.size() == 6) ||
1168                 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1169         {
1170                 parseTextArea(data,parts,type);
1171                 return;
1172         }
1173         errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'"  << std::endl;
1174 }
1175
1176 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1177 {
1178         std::vector<std::string> parts = split(element,';');
1179
1180         if ((parts.size() == 2) ||
1181                 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1182         {
1183                 std::vector<std::string> v_pos = split(parts[0],',');
1184                 std::string text = parts[1];
1185
1186                 MY_CHECKPOS("label",0);
1187
1188                 v2s32 pos = padding;
1189                 pos.X += stof(v_pos[0]) * (float)spacing.X;
1190                 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1191
1192                 if(!data->explicit_size)
1193                         warningstream<<"invalid use of label without a size[] element"<<std::endl;
1194
1195                 std::vector<std::string> lines = split(text, '\n');
1196
1197                 for (unsigned int i = 0; i != lines.size(); i++) {
1198                         // Lines are spaced at the nominal distance of
1199                         // 2/5 inventory slot, even if the font doesn't
1200                         // quite match that.  This provides consistent
1201                         // form layout, at the expense of sometimes
1202                         // having sub-optimal spacing for the font.
1203                         // We multiply by 2 and then divide by 5, rather
1204                         // than multiply by 0.4, to get exact results
1205                         // in the integer cases: 0.4 is not exactly
1206                         // representable in binary floating point.
1207                         s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1208                         std::wstring wlabel = utf8_to_wide(unescape_string(lines[i]));
1209                         core::rect<s32> rect = core::rect<s32>(
1210                                 pos.X, posy - m_btn_height,
1211                                 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1212                                 posy + m_btn_height);
1213                         FieldSpec spec(
1214                                 "",
1215                                 wlabel,
1216                                 L"",
1217                                 258+m_fields.size()
1218                         );
1219                         gui::IGUIStaticText *e =
1220                                 addStaticText(Environment, spec.flabel.c_str(),
1221                                         rect, false, false, this, spec.fid);
1222                         e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1223                                                 gui::EGUIA_CENTER);
1224                         m_fields.push_back(spec);
1225                 }
1226
1227                 return;
1228         }
1229         errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'"  << std::endl;
1230 }
1231
1232 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1233 {
1234         std::vector<std::string> parts = split(element,';');
1235
1236         if ((parts.size() == 2) ||
1237                 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1238         {
1239                 std::vector<std::string> v_pos = split(parts[0],',');
1240                 std::wstring text = unescape_enriched(
1241                         unescape_string(utf8_to_wide(parts[1])));
1242
1243                 MY_CHECKPOS("vertlabel",1);
1244
1245                 v2s32 pos = padding;
1246                 pos.X += stof(v_pos[0]) * (float)spacing.X;
1247                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1248
1249                 core::rect<s32> rect = core::rect<s32>(
1250                                 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1251                                 pos.X+15, pos.Y +
1252                                         font_line_height(m_font)
1253                                         * (text.length()+1)
1254                                         +((imgsize.Y/2)- m_btn_height));
1255                 //actually text.length() would be correct but adding +1 avoids to break all mods
1256
1257                 if(!data->explicit_size)
1258                         warningstream<<"invalid use of label without a size[] element"<<std::endl;
1259
1260                 std::wstring label = L"";
1261
1262                 for (unsigned int i=0; i < text.length(); i++) {
1263                         label += text[i];
1264                         label += L"\n";
1265                 }
1266
1267                 FieldSpec spec(
1268                         "",
1269                         label,
1270                         L"",
1271                         258+m_fields.size()
1272                 );
1273                 gui::IGUIStaticText *t =
1274                                 addStaticText(Environment, spec.flabel.c_str(), rect, false, false, this, spec.fid);
1275                 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1276                 m_fields.push_back(spec);
1277                 return;
1278         }
1279         errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'"  << std::endl;
1280 }
1281
1282 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1283                 std::string type)
1284 {
1285         std::vector<std::string> parts = split(element,';');
1286
1287         if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1288                 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1289         {
1290                 std::vector<std::string> v_pos = split(parts[0],',');
1291                 std::vector<std::string> v_geom = split(parts[1],',');
1292                 std::string image_name = parts[2];
1293                 std::string name = parts[3];
1294                 std::string label = parts[4];
1295
1296                 MY_CHECKPOS("imagebutton",0);
1297                 MY_CHECKGEOM("imagebutton",1);
1298
1299                 v2s32 pos = padding;
1300                 pos.X += stof(v_pos[0]) * (float)spacing.X;
1301                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1302                 v2s32 geom;
1303                 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1304                 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1305
1306                 bool noclip     = false;
1307                 bool drawborder = true;
1308                 std::string pressed_image_name = "";
1309
1310                 if (parts.size() >= 7) {
1311                         if (parts[5] == "true")
1312                                 noclip = true;
1313                         if (parts[6] == "false")
1314                                 drawborder = false;
1315                 }
1316
1317                 if (parts.size() >= 8) {
1318                         pressed_image_name = parts[7];
1319                 }
1320
1321                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1322
1323                 if(!data->explicit_size)
1324                         warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1325
1326                 image_name = unescape_string(image_name);
1327                 pressed_image_name = unescape_string(pressed_image_name);
1328
1329                 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1330
1331                 FieldSpec spec(
1332                         name,
1333                         wlabel,
1334                         utf8_to_wide(image_name),
1335                         258+m_fields.size()
1336                 );
1337                 spec.ftype = f_Button;
1338                 if(type == "image_button_exit")
1339                         spec.is_exit = true;
1340
1341                 video::ITexture *texture = 0;
1342                 video::ITexture *pressed_texture = 0;
1343                 texture = m_tsrc->getTexture(image_name);
1344                 if (pressed_image_name != "")
1345                         pressed_texture = m_tsrc->getTexture(pressed_image_name);
1346                 else
1347                         pressed_texture = texture;
1348
1349                 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1350
1351                 if (spec.fname == data->focused_fieldname) {
1352                         Environment->setFocus(e);
1353                 }
1354
1355                 e->setUseAlphaChannel(true);
1356                 e->setImage(guiScalingImageButton(
1357                         Environment->getVideoDriver(), texture, geom.X, geom.Y));
1358                 e->setPressedImage(guiScalingImageButton(
1359                         Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1360                 e->setScaleImage(true);
1361                 e->setNotClipped(noclip);
1362                 e->setDrawBorder(drawborder);
1363
1364                 m_fields.push_back(spec);
1365                 return;
1366         }
1367
1368         errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'"  << std::endl;
1369 }
1370
1371 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1372 {
1373         std::vector<std::string> parts = split(element,';');
1374
1375         if (((parts.size() == 4) || (parts.size() == 6)) ||
1376                 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1377         {
1378                 std::vector<std::string> v_pos = split(parts[0],',');
1379                 std::string name = parts[1];
1380                 std::vector<std::string> buttons = split(parts[2],',');
1381                 std::string str_index = parts[3];
1382                 bool show_background = true;
1383                 bool show_border = true;
1384                 int tab_index = stoi(str_index) -1;
1385
1386                 MY_CHECKPOS("tabheader",0);
1387
1388                 if (parts.size() == 6) {
1389                         if (parts[4] == "true")
1390                                 show_background = false;
1391                         if (parts[5] == "false")
1392                                 show_border = false;
1393                 }
1394
1395                 FieldSpec spec(
1396                         name,
1397                         L"",
1398                         L"",
1399                         258+m_fields.size()
1400                 );
1401
1402                 spec.ftype = f_TabHeader;
1403
1404                 v2s32 pos(0,0);
1405                 pos.X += stof(v_pos[0]) * (float)spacing.X;
1406                 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1407                 v2s32 geom;
1408                 geom.X = DesiredRect.getWidth();
1409                 geom.Y = m_btn_height*2;
1410
1411                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1412                                 pos.Y+geom.Y);
1413
1414                 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1415                                 show_background, show_border, spec.fid);
1416                 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1417                                 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1418                 e->setTabHeight(m_btn_height*2);
1419
1420                 if (spec.fname == data->focused_fieldname) {
1421                         Environment->setFocus(e);
1422                 }
1423
1424                 e->setNotClipped(true);
1425
1426                 for (unsigned int i = 0; i < buttons.size(); i++) {
1427                         e->addTab(unescape_enriched(unescape_string(
1428                                 utf8_to_wide(buttons[i]))).c_str(), -1);
1429                 }
1430
1431                 if ((tab_index >= 0) &&
1432                                 (buttons.size() < INT_MAX) &&
1433                                 (tab_index < (int) buttons.size()))
1434                         e->setActiveTab(tab_index);
1435
1436                 m_fields.push_back(spec);
1437                 return;
1438         }
1439         errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1440                         << element << "'"  << std::endl;
1441 }
1442
1443 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1444 {
1445
1446         if (m_gamedef == 0) {
1447                 warningstream << "invalid use of item_image_button with m_gamedef==0"
1448                         << std::endl;
1449                 return;
1450         }
1451
1452         std::vector<std::string> parts = split(element,';');
1453
1454         if ((parts.size() == 5) ||
1455                 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1456         {
1457                 std::vector<std::string> v_pos = split(parts[0],',');
1458                 std::vector<std::string> v_geom = split(parts[1],',');
1459                 std::string item_name = parts[2];
1460                 std::string name = parts[3];
1461                 std::string label = parts[4];
1462
1463                 label = unescape_string(label);
1464                 item_name = unescape_string(item_name);
1465
1466                 MY_CHECKPOS("itemimagebutton",0);
1467                 MY_CHECKGEOM("itemimagebutton",1);
1468
1469                 v2s32 pos = padding;
1470                 pos.X += stof(v_pos[0]) * (float)spacing.X;
1471                 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1472                 v2s32 geom;
1473                 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1474                 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1475
1476                 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1477
1478                 if(!data->explicit_size)
1479                         warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
1480
1481                 IItemDefManager *idef = m_gamedef->idef();
1482                 ItemStack item;
1483                 item.deSerialize(item_name, idef);
1484
1485                 m_tooltips[name] =
1486                         TooltipSpec(item.getDefinition(idef).description,
1487                                                 m_default_tooltip_bgcolor,
1488                                                 m_default_tooltip_color);
1489
1490                 FieldSpec spec(
1491                         name,
1492                         utf8_to_wide(label),
1493                         utf8_to_wide(item_name),
1494                         258 + m_fields.size()
1495                 );
1496
1497                 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
1498
1499                 if (spec.fname == data->focused_fieldname) {
1500                         Environment->setFocus(e);
1501                 }
1502
1503                 spec.ftype = f_Button;
1504                 rect+=data->basepos-padding;
1505                 spec.rect=rect;
1506                 m_fields.push_back(spec);
1507                 pos = padding + AbsoluteRect.UpperLeftCorner;
1508                 pos.X += stof(v_pos[0]) * (float) spacing.X;
1509                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1510                 m_itemimages.push_back(ImageDrawSpec("", item_name, e, pos, geom));
1511                 m_static_texts.push_back(StaticTextSpec(utf8_to_wide(label), rect, e));
1512                 return;
1513         }
1514         errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'"  << std::endl;
1515 }
1516
1517 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1518 {
1519         std::vector<std::string> parts = split(element,';');
1520
1521         if ((parts.size() == 3) ||
1522                 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1523         {
1524                 std::vector<std::string> v_pos = split(parts[0],',');
1525                 std::vector<std::string> v_geom = split(parts[1],',');
1526
1527                 MY_CHECKPOS("box",0);
1528                 MY_CHECKGEOM("box",1);
1529
1530                 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1531                 pos.X += stof(v_pos[0]) * (float) spacing.X;
1532                 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1533
1534                 v2s32 geom;
1535                 geom.X = stof(v_geom[0]) * (float)spacing.X;
1536                 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1537
1538                 video::SColor tmp_color;
1539
1540                 if (parseColorString(parts[2], tmp_color, false)) {
1541                         BoxDrawSpec spec(pos, geom, tmp_color);
1542
1543                         m_boxes.push_back(spec);
1544                 }
1545                 else {
1546                         errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'  INVALID COLOR"  << std::endl;
1547                 }
1548                 return;
1549         }
1550         errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'"  << std::endl;
1551 }
1552
1553 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1554 {
1555         std::vector<std::string> parts = split(element,';');
1556
1557         if (((parts.size() == 1) || (parts.size() == 2)) ||
1558                 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1559         {
1560                 parseColorString(parts[0],m_bgcolor,false);
1561
1562                 if (parts.size() == 2) {
1563                         std::string fullscreen = parts[1];
1564                         m_bgfullscreen = is_yes(fullscreen);
1565                 }
1566                 return;
1567         }
1568         errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'"  << std::endl;
1569 }
1570
1571 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1572 {
1573         std::vector<std::string> parts = split(element,';');
1574
1575         if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1576                 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1577         {
1578                 parseColorString(parts[0], m_slotbg_n, false);
1579                 parseColorString(parts[1], m_slotbg_h, false);
1580
1581                 if (parts.size() >= 3) {
1582                         if (parseColorString(parts[2], m_slotbordercolor, false)) {
1583                                 m_slotborder = true;
1584                         }
1585                 }
1586                 if (parts.size() == 5) {
1587                         video::SColor tmp_color;
1588
1589                         if (parseColorString(parts[3], tmp_color, false))
1590                                 m_default_tooltip_bgcolor = tmp_color;
1591                         if (parseColorString(parts[4], tmp_color, false))
1592                                 m_default_tooltip_color = tmp_color;
1593                 }
1594                 return;
1595         }
1596         errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'"  << std::endl;
1597 }
1598
1599 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1600 {
1601         std::vector<std::string> parts = split(element,';');
1602         if (parts.size() == 2) {
1603                 std::string name = parts[0];
1604                 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1605                         m_default_tooltip_bgcolor, m_default_tooltip_color);
1606                 return;
1607         } else if (parts.size() == 4) {
1608                 std::string name = parts[0];
1609                 video::SColor tmp_color1, tmp_color2;
1610                 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1611                         m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1612                                 tmp_color1, tmp_color2);
1613                         return;
1614                 }
1615         }
1616         errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'"  << std::endl;
1617 }
1618
1619 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1620 {
1621         //some prechecks
1622         if (data == "")
1623                 return false;
1624
1625         std::vector<std::string> parts = split(data,'[');
1626
1627         if (parts.size() < 2) {
1628                 return false;
1629         }
1630
1631         if (parts[0] != "formspec_version") {
1632                 return false;
1633         }
1634
1635         if (is_number(parts[1])) {
1636                 m_formspec_version = mystoi(parts[1]);
1637                 return true;
1638         }
1639
1640         return false;
1641 }
1642
1643 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1644 {
1645         if (element == "")
1646                 return false;
1647
1648         std::vector<std::string> parts = split(element,'[');
1649
1650         if (parts.size() < 2)
1651                 return false;
1652
1653         std::string type = trim(parts[0]);
1654         std::string description = trim(parts[1]);
1655
1656         if (type != "size" && type != "invsize")
1657                 return false;
1658
1659         if (type == "invsize")
1660                 log_deprecated("Deprecated formspec element \"invsize\" is used");
1661
1662         parseSize(data, description);
1663
1664         return true;
1665 }
1666
1667 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1668 {
1669         //some prechecks
1670         if (element == "")
1671                 return;
1672
1673         std::vector<std::string> parts = split(element,'[');
1674
1675         // ugly workaround to keep compatibility
1676         if (parts.size() > 2) {
1677                 if (trim(parts[0]) == "image") {
1678                         for (unsigned int i=2;i< parts.size(); i++) {
1679                                 parts[1] += "[" + parts[i];
1680                         }
1681                 }
1682                 else { return; }
1683         }
1684
1685         if (parts.size() < 2) {
1686                 return;
1687         }
1688
1689         std::string type = trim(parts[0]);
1690         std::string description = trim(parts[1]);
1691
1692         if (type == "list") {
1693                 parseList(data,description);
1694                 return;
1695         }
1696
1697         if (type == "listring") {
1698                 parseListRing(data, description);
1699                 return;
1700         }
1701
1702         if (type == "checkbox") {
1703                 parseCheckbox(data,description);
1704                 return;
1705         }
1706
1707         if (type == "image") {
1708                 parseImage(data,description);
1709                 return;
1710         }
1711
1712         if (type == "item_image") {
1713                 parseItemImage(data,description);
1714                 return;
1715         }
1716
1717         if ((type == "button") || (type == "button_exit")) {
1718                 parseButton(data,description,type);
1719                 return;
1720         }
1721
1722         if (type == "background") {
1723                 parseBackground(data,description);
1724                 return;
1725         }
1726
1727         if (type == "tableoptions"){
1728                 parseTableOptions(data,description);
1729                 return;
1730         }
1731
1732         if (type == "tablecolumns"){
1733                 parseTableColumns(data,description);
1734                 return;
1735         }
1736
1737         if (type == "table"){
1738                 parseTable(data,description);
1739                 return;
1740         }
1741
1742         if (type == "textlist"){
1743                 parseTextList(data,description);
1744                 return;
1745         }
1746
1747         if (type == "dropdown"){
1748                 parseDropDown(data,description);
1749                 return;
1750         }
1751
1752         if (type == "pwdfield") {
1753                 parsePwdField(data,description);
1754                 return;
1755         }
1756
1757         if ((type == "field") || (type == "textarea")){
1758                 parseField(data,description,type);
1759                 return;
1760         }
1761
1762         if (type == "label") {
1763                 parseLabel(data,description);
1764                 return;
1765         }
1766
1767         if (type == "vertlabel") {
1768                 parseVertLabel(data,description);
1769                 return;
1770         }
1771
1772         if (type == "item_image_button") {
1773                 parseItemImageButton(data,description);
1774                 return;
1775         }
1776
1777         if ((type == "image_button") || (type == "image_button_exit")) {
1778                 parseImageButton(data,description,type);
1779                 return;
1780         }
1781
1782         if (type == "tabheader") {
1783                 parseTabHeader(data,description);
1784                 return;
1785         }
1786
1787         if (type == "box") {
1788                 parseBox(data,description);
1789                 return;
1790         }
1791
1792         if (type == "bgcolor") {
1793                 parseBackgroundColor(data,description);
1794                 return;
1795         }
1796
1797         if (type == "listcolors") {
1798                 parseListColors(data,description);
1799                 return;
1800         }
1801
1802         if (type == "tooltip") {
1803                 parseTooltip(data,description);
1804                 return;
1805         }
1806
1807         if (type == "scrollbar") {
1808                 parseScrollBar(data, description);
1809                 return;
1810         }
1811
1812         // Ignore others
1813         infostream
1814                 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1815                 <<std::endl;
1816 }
1817
1818 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1819 {
1820         /* useless to regenerate without a screensize */
1821         if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1822                 return;
1823         }
1824
1825         parserData mydata;
1826
1827         //preserve tables
1828         for (u32 i = 0; i < m_tables.size(); ++i) {
1829                 std::string tablename = m_tables[i].first.fname;
1830                 GUITable *table = m_tables[i].second;
1831                 mydata.table_dyndata[tablename] = table->getDynamicData();
1832         }
1833
1834         //set focus
1835         if (!m_focused_element.empty())
1836                 mydata.focused_fieldname = m_focused_element;
1837
1838         //preserve focus
1839         gui::IGUIElement *focused_element = Environment->getFocus();
1840         if (focused_element && focused_element->getParent() == this) {
1841                 s32 focused_id = focused_element->getID();
1842                 if (focused_id > 257) {
1843                         for (u32 i=0; i<m_fields.size(); i++) {
1844                                 if (m_fields[i].fid == focused_id) {
1845                                         mydata.focused_fieldname =
1846                                                 m_fields[i].fname;
1847                                         break;
1848                                 }
1849                         }
1850                 }
1851         }
1852
1853         // Remove children
1854         removeChildren();
1855
1856         for (u32 i = 0; i < m_tables.size(); ++i) {
1857                 GUITable *table = m_tables[i].second;
1858                 table->drop();
1859         }
1860
1861         mydata.size= v2s32(100,100);
1862         mydata.screensize = screensize;
1863
1864         // Base position of contents of form
1865         mydata.basepos = getBasePos();
1866
1867         /* Convert m_init_draw_spec to m_inventorylists */
1868
1869         m_inventorylists.clear();
1870         m_images.clear();
1871         m_backgrounds.clear();
1872         m_itemimages.clear();
1873         m_tables.clear();
1874         m_checkboxes.clear();
1875         m_scrollbars.clear();
1876         m_fields.clear();
1877         m_boxes.clear();
1878         m_tooltips.clear();
1879         m_inventory_rings.clear();
1880         m_static_texts.clear();
1881
1882         // Set default values (fits old formspec values)
1883         m_bgcolor = video::SColor(140,0,0,0);
1884         m_bgfullscreen = false;
1885
1886         m_slotbg_n = video::SColor(255,128,128,128);
1887         m_slotbg_h = video::SColor(255,192,192,192);
1888
1889         m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1890         m_default_tooltip_color = video::SColor(255,255,255,255);
1891
1892         m_slotbordercolor = video::SColor(200,0,0,0);
1893         m_slotborder = false;
1894
1895         // Add tooltip
1896         {
1897                 assert(m_tooltip_element == NULL);
1898                 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1899                 m_tooltip_element = addStaticText(Environment, L"",core::rect<s32>(0,0,110,18));
1900                 m_tooltip_element->enableOverrideColor(true);
1901                 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1902                 m_tooltip_element->setDrawBackground(true);
1903                 m_tooltip_element->setDrawBorder(true);
1904                 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1905                 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1906                 m_tooltip_element->setWordWrap(false);
1907                 //we're not parent so no autograb for this one!
1908                 m_tooltip_element->grab();
1909         }
1910
1911         std::vector<std::string> elements = split(m_formspec_string,']');
1912         unsigned int i = 0;
1913
1914         /* try to read version from first element only */
1915         if (elements.size() >= 1) {
1916                 if ( parseVersionDirect(elements[0]) ) {
1917                         i++;
1918                 }
1919         }
1920
1921         /* we need size first in order to calculate image scale */
1922         mydata.explicit_size = false;
1923         for (; i< elements.size(); i++) {
1924                 if (!parseSizeDirect(&mydata, elements[i])) {
1925                         break;
1926                 }
1927         }
1928
1929         if (mydata.explicit_size) {
1930                 // compute scaling for specified form size
1931                 if (m_lock) {
1932                         v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1933                         v2u32 delta = current_screensize - m_lockscreensize;
1934
1935                         if (current_screensize.Y > m_lockscreensize.Y)
1936                                 delta.Y /= 2;
1937                         else
1938                                 delta.Y = 0;
1939
1940                         if (current_screensize.X > m_lockscreensize.X)
1941                                 delta.X /= 2;
1942                         else
1943                                 delta.X = 0;
1944
1945                         offset = v2s32(delta.X,delta.Y);
1946
1947                         mydata.screensize = m_lockscreensize;
1948                 } else {
1949                         offset = v2s32(0,0);
1950                 }
1951
1952                 double gui_scaling = g_settings->getFloat("gui_scaling");
1953                 double screen_dpi = porting::getDisplayDensity() * 96;
1954
1955                 double use_imgsize;
1956                 if (m_lock) {
1957                         // In fixed-size mode, inventory image size
1958                         // is 0.53 inch multiplied by the gui_scaling
1959                         // config parameter.  This magic size is chosen
1960                         // to make the main menu (15.5 inventory images
1961                         // wide, including border) just fit into the
1962                         // default window (800 pixels wide) at 96 DPI
1963                         // and default scaling (1.00).
1964                         use_imgsize = 0.5555 * screen_dpi * gui_scaling;
1965                 } else {
1966                         // In variable-size mode, we prefer to make the
1967                         // inventory image size 1/15 of screen height,
1968                         // multiplied by the gui_scaling config parameter.
1969                         // If the preferred size won't fit the whole
1970                         // form on the screen, either horizontally or
1971                         // vertically, then we scale it down to fit.
1972                         // (The magic numbers in the computation of what
1973                         // fits arise from the scaling factors in the
1974                         // following stanza, including the form border,
1975                         // help text space, and 0.1 inventory slot spare.)
1976                         // However, a minimum size is also set, that
1977                         // the image size can't be less than 0.3 inch
1978                         // multiplied by gui_scaling, even if this means
1979                         // the form doesn't fit the screen.
1980                         double prefer_imgsize = mydata.screensize.Y / 15 *
1981                                                         gui_scaling;
1982                         double fitx_imgsize = mydata.screensize.X /
1983                                 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1984                         double fity_imgsize = mydata.screensize.Y /
1985                                 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1986                         double screen_dpi = porting::getDisplayDensity() * 96;
1987                         double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1988                         use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
1989                                 MYMIN(fitx_imgsize, fity_imgsize)));
1990                 }
1991
1992                 // Everything else is scaled in proportion to the
1993                 // inventory image size.  The inventory slot spacing
1994                 // is 5/4 image size horizontally and 15/13 image size
1995                 // vertically.  The padding around the form (incorporating
1996                 // the border of the outer inventory slots) is 3/8
1997                 // image size.  Font height (baseline to baseline)
1998                 // is 2/5 vertical inventory slot spacing, and button
1999                 // half-height is 7/8 of font height.
2000                 imgsize = v2s32(use_imgsize, use_imgsize);
2001                 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2002                 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2003                 m_btn_height = use_imgsize*15.0/13 * 0.35;
2004
2005                 m_font = g_fontengine->getFont();
2006
2007                 mydata.size = v2s32(
2008                         padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2009                         padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2010                 );
2011                 DesiredRect = mydata.rect = core::rect<s32>(
2012                                 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
2013                                 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
2014                                 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
2015                                 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
2016                 );
2017         } else {
2018                 // Non-size[] form must consist only of text fields and
2019                 // implicit "Proceed" button.  Use default font, and
2020                 // temporary form size which will be recalculated below.
2021                 m_font = g_fontengine->getFont();
2022                 m_btn_height = font_line_height(m_font) * 0.875;
2023                 DesiredRect = core::rect<s32>(
2024                         mydata.screensize.X/2 - 580/2,
2025                         mydata.screensize.Y/2 - 300/2,
2026                         mydata.screensize.X/2 + 580/2,
2027                         mydata.screensize.Y/2 + 300/2
2028                 );
2029         }
2030         recalculateAbsolutePosition(false);
2031         mydata.basepos = getBasePos();
2032         m_tooltip_element->setOverrideFont(m_font);
2033
2034         gui::IGUISkin* skin = Environment->getSkin();
2035         sanity_check(skin != NULL);
2036         gui::IGUIFont *old_font = skin->getFont();
2037         skin->setFont(m_font);
2038
2039         for (; i< elements.size(); i++) {
2040                 parseElement(&mydata, elements[i]);
2041         }
2042
2043         // If there are fields without explicit size[], add a "Proceed"
2044         // button and adjust size to fit all the fields.
2045         if (m_fields.size() && !mydata.explicit_size) {
2046                 mydata.rect = core::rect<s32>(
2047                                 mydata.screensize.X/2 - 580/2,
2048                                 mydata.screensize.Y/2 - 300/2,
2049                                 mydata.screensize.X/2 + 580/2,
2050                                 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2051                 );
2052                 DesiredRect = mydata.rect;
2053                 recalculateAbsolutePosition(false);
2054                 mydata.basepos = getBasePos();
2055
2056                 {
2057                         v2s32 pos = mydata.basepos;
2058                         pos.Y = ((m_fields.size()+2)*60);
2059
2060                         v2s32 size = DesiredRect.getSize();
2061                         mydata.rect =
2062                                         core::rect<s32>(size.X/2-70, pos.Y,
2063                                                         (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2064                         const wchar_t *text = wgettext("Proceed");
2065                         Environment->addButton(mydata.rect, this, 257, text);
2066                         delete[] text;
2067                 }
2068
2069         }
2070
2071         //set initial focus if parser didn't set it
2072         focused_element = Environment->getFocus();
2073         if (!focused_element
2074                         || !isMyChild(focused_element)
2075                         || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2076                 setInitialFocus();
2077
2078         skin->setFont(old_font);
2079 }
2080
2081 #ifdef __ANDROID__
2082 bool GUIFormSpecMenu::getAndroidUIInput()
2083 {
2084         /* no dialog shown */
2085         if (m_JavaDialogFieldName == "") {
2086                 return false;
2087         }
2088
2089         /* still waiting */
2090         if (porting::getInputDialogState() == -1) {
2091                 return true;
2092         }
2093
2094         std::string fieldname = m_JavaDialogFieldName;
2095         m_JavaDialogFieldName = "";
2096
2097         /* no value abort dialog processing */
2098         if (porting::getInputDialogState() != 0) {
2099                 return false;
2100         }
2101
2102         for(std::vector<FieldSpec>::iterator iter =  m_fields.begin();
2103                         iter != m_fields.end(); ++iter) {
2104
2105                 if (iter->fname != fieldname) {
2106                         continue;
2107                 }
2108                 IGUIElement* tochange = getElementFromId(iter->fid);
2109
2110                 if (tochange == 0) {
2111                         return false;
2112                 }
2113
2114                 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2115                         return false;
2116                 }
2117
2118                 std::string text = porting::getInputDialogValue();
2119
2120                 ((gui::IGUIEditBox*) tochange)->
2121                         setText(utf8_to_wide(text).c_str());
2122         }
2123         return false;
2124 }
2125 #endif
2126
2127 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2128 {
2129         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2130
2131         for(u32 i=0; i<m_inventorylists.size(); i++)
2132         {
2133                 const ListDrawSpec &s = m_inventorylists[i];
2134
2135                 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2136                         s32 item_i = i + s.start_item_i;
2137                         s32 x = (i%s.geom.X) * spacing.X;
2138                         s32 y = (i/s.geom.X) * spacing.Y;
2139                         v2s32 p0(x,y);
2140                         core::rect<s32> rect = imgrect + s.pos + p0;
2141                         if(rect.isPointInside(p))
2142                         {
2143                                 return ItemSpec(s.inventoryloc, s.listname, item_i);
2144                         }
2145                 }
2146         }
2147
2148         return ItemSpec(InventoryLocation(), "", -1);
2149 }
2150
2151 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
2152                 bool &item_hovered)
2153 {
2154         video::IVideoDriver* driver = Environment->getVideoDriver();
2155
2156         Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2157         if(!inv){
2158                 warningstream<<"GUIFormSpecMenu::drawList(): "
2159                                 <<"The inventory location "
2160                                 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2161                                 <<std::endl;
2162                 return;
2163         }
2164         InventoryList *ilist = inv->getList(s.listname);
2165         if(!ilist){
2166                 warningstream<<"GUIFormSpecMenu::drawList(): "
2167                                 <<"The inventory list \""<<s.listname<<"\" @ \""
2168                                 <<s.inventoryloc.dump()<<"\" doesn't exist"
2169                                 <<std::endl;
2170                 return;
2171         }
2172
2173         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2174
2175         for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2176         {
2177                 s32 item_i = i + s.start_item_i;
2178                 if(item_i >= (s32) ilist->getSize())
2179                         break;
2180                 s32 x = (i%s.geom.X) * spacing.X;
2181                 s32 y = (i/s.geom.X) * spacing.Y;
2182                 v2s32 p(x,y);
2183                 core::rect<s32> rect = imgrect + s.pos + p;
2184                 ItemStack item;
2185                 if(ilist)
2186                         item = ilist->getItem(item_i);
2187
2188                 bool selected = m_selected_item
2189                         && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2190                         && m_selected_item->listname == s.listname
2191                         && m_selected_item->i == item_i;
2192                 bool hovering = rect.isPointInside(m_pointer);
2193                 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2194                         (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
2195
2196                 if (phase == 0) {
2197                         if (hovering) {
2198                                 item_hovered = true;
2199                                 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2200                         } else {
2201                                 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2202                         }
2203                 }
2204
2205                 //Draw inv slot borders
2206                 if (m_slotborder) {
2207                         s32 x1 = rect.UpperLeftCorner.X;
2208                         s32 y1 = rect.UpperLeftCorner.Y;
2209                         s32 x2 = rect.LowerRightCorner.X;
2210                         s32 y2 = rect.LowerRightCorner.Y;
2211                         s32 border = 1;
2212                         driver->draw2DRectangle(m_slotbordercolor,
2213                                 core::rect<s32>(v2s32(x1 - border, y1 - border),
2214                                                                 v2s32(x2 + border, y1)), NULL);
2215                         driver->draw2DRectangle(m_slotbordercolor,
2216                                 core::rect<s32>(v2s32(x1 - border, y2),
2217                                                                 v2s32(x2 + border, y2 + border)), NULL);
2218                         driver->draw2DRectangle(m_slotbordercolor,
2219                                 core::rect<s32>(v2s32(x1 - border, y1),
2220                                                                 v2s32(x1, y2)), NULL);
2221                         driver->draw2DRectangle(m_slotbordercolor,
2222                                 core::rect<s32>(v2s32(x2, y1),
2223                                                                 v2s32(x2 + border, y2)), NULL);
2224                 }
2225
2226                 if(phase == 1)
2227                 {
2228                         // Draw item stack
2229                         if(selected)
2230                         {
2231                                 item.takeItem(m_selected_amount);
2232                         }
2233                         if(!item.empty())
2234                         {
2235                                 drawItemStack(driver, m_font, item,
2236                                         rect, &AbsoluteClippingRect, m_gamedef,
2237                                         rotation_kind);
2238                         }
2239
2240                         // Draw tooltip
2241                         std::wstring tooltip_text = L"";
2242                         if (hovering && !m_selected_item) {
2243                                 tooltip_text = utf8_to_wide(item.getDefinition(m_gamedef->idef()).description);
2244                         }
2245                         if (tooltip_text != L"") {
2246                                 std::vector<std::wstring> tt_rows = str_split(tooltip_text, L'\n');
2247                                 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2248                                 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2249                                 m_tooltip_element->setVisible(true);
2250                                 this->bringToFront(m_tooltip_element);
2251                                 setStaticText(m_tooltip_element, tooltip_text.c_str());
2252                                 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2253 #if IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2
2254                                 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2255 #else
2256                                 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2257 #endif
2258                                 v2u32 screenSize = driver->getScreenSize();
2259                                 int tooltip_offset_x = m_btn_height;
2260                                 int tooltip_offset_y = m_btn_height;
2261 #ifdef __ANDROID__
2262                                 tooltip_offset_x *= 3;
2263                                 tooltip_offset_y  = 0;
2264                                 if (m_pointer.X > (s32)screenSize.X / 2)
2265                                         tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2266 #endif
2267                                 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2268                                 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2269                                 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2270                                         tooltip_x = (s32)screenSize.X - tooltip_width  - m_btn_height;
2271                                 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2272                                         tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2273                                 m_tooltip_element->setRelativePosition(core::rect<s32>(
2274                                                 core::position2d<s32>(tooltip_x, tooltip_y),
2275                                                 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2276                         }
2277                 }
2278         }
2279 }
2280
2281 void GUIFormSpecMenu::drawSelectedItem()
2282 {
2283         video::IVideoDriver* driver = Environment->getVideoDriver();
2284
2285         if (!m_selected_item) {
2286                 drawItemStack(driver, m_font, ItemStack(),
2287                         core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2288                         NULL, m_gamedef, IT_ROT_DRAGGED);
2289                 return;
2290         }
2291
2292         Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2293         sanity_check(inv);
2294         InventoryList *list = inv->getList(m_selected_item->listname);
2295         sanity_check(list);
2296         ItemStack stack = list->getItem(m_selected_item->i);
2297         stack.count = m_selected_amount;
2298
2299         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2300         core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2301         drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef, IT_ROT_DRAGGED);
2302 }
2303
2304 void GUIFormSpecMenu::drawMenu()
2305 {
2306         if(m_form_src){
2307                 std::string newform = m_form_src->getForm();
2308                 if(newform != m_formspec_string){
2309                         m_formspec_string = newform;
2310                         regenerateGui(m_screensize_old);
2311                 }
2312         }
2313
2314         gui::IGUISkin* skin = Environment->getSkin();
2315         sanity_check(skin != NULL);
2316         gui::IGUIFont *old_font = skin->getFont();
2317         skin->setFont(m_font);
2318
2319         updateSelectedItem();
2320
2321         video::IVideoDriver* driver = Environment->getVideoDriver();
2322
2323         v2u32 screenSize = driver->getScreenSize();
2324         core::rect<s32> allbg(0, 0, screenSize.X ,      screenSize.Y);
2325         if (m_bgfullscreen)
2326                 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2327         else
2328                 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2329
2330         m_tooltip_element->setVisible(false);
2331
2332         /*
2333                 Draw backgrounds
2334         */
2335         for(u32 i=0; i<m_backgrounds.size(); i++)
2336         {
2337                 const ImageDrawSpec &spec = m_backgrounds[i];
2338                 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2339
2340                 if (texture != 0) {
2341                         // Image size on screen
2342                         core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2343                         // Image rectangle on screen
2344                         core::rect<s32> rect = imgrect + spec.pos;
2345
2346                         if (spec.clip) {
2347                                 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2348                                 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2349                                                                         AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2350                                                                         AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2351                                                                         AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2352                         }
2353
2354                         const video::SColor color(255,255,255,255);
2355                         const video::SColor colors[] = {color,color,color,color};
2356                         draw2DImageFilterScaled(driver, texture, rect,
2357                                 core::rect<s32>(core::position2d<s32>(0,0),
2358                                                 core::dimension2di(texture->getOriginalSize())),
2359                                 NULL/*&AbsoluteClippingRect*/, colors, true);
2360                 } else {
2361                         errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2362                         errorstream << "\t" << spec.name << std::endl;
2363                 }
2364         }
2365
2366         /*
2367                 Draw Boxes
2368         */
2369         for(u32 i=0; i<m_boxes.size(); i++)
2370         {
2371                 const BoxDrawSpec &spec = m_boxes[i];
2372
2373                 irr::video::SColor todraw = spec.color;
2374
2375                 todraw.setAlpha(140);
2376
2377                 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2378                                                         spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2379
2380                 driver->draw2DRectangle(todraw, rect, 0);
2381         }
2382
2383         /*
2384                 Call base class
2385         */
2386         gui::IGUIElement::draw();
2387
2388         /*
2389                 Draw images
2390         */
2391         for(u32 i=0; i<m_images.size(); i++)
2392         {
2393                 const ImageDrawSpec &spec = m_images[i];
2394                 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2395
2396                 if (texture != 0) {
2397                         const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2398                         // Image size on screen
2399                         core::rect<s32> imgrect;
2400
2401                         if (spec.scale)
2402                                 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2403                         else {
2404
2405                                 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2406                         }
2407                         // Image rectangle on screen
2408                         core::rect<s32> rect = imgrect + spec.pos;
2409                         const video::SColor color(255,255,255,255);
2410                         const video::SColor colors[] = {color,color,color,color};
2411                         draw2DImageFilterScaled(driver, texture, rect,
2412                                 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2413                                 NULL/*&AbsoluteClippingRect*/, colors, true);
2414                 }
2415                 else {
2416                         errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2417                         errorstream << "\t" << spec.name << std::endl;
2418                 }
2419         }
2420
2421         /*
2422                 Draw item images
2423         */
2424         for(u32 i=0; i<m_itemimages.size(); i++)
2425         {
2426                 if (m_gamedef == 0)
2427                         break;
2428
2429                 const ImageDrawSpec &spec = m_itemimages[i];
2430                 IItemDefManager *idef = m_gamedef->idef();
2431                 ItemStack item;
2432                 item.deSerialize(spec.item_name, idef);
2433                 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2434                 // Viewport rectangle on screen
2435                 core::rect<s32> rect = imgrect + spec.pos;
2436                 if (spec.parent_button && spec.parent_button->isPressed()) {
2437 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2438                         rect += core::dimension2d<s32>(
2439                                 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2440 #else
2441                         rect += core::dimension2d<s32>(
2442                                 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2443                                 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2444 #endif
2445                 }
2446                 drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
2447                                 m_gamedef, IT_ROT_NONE);
2448         }
2449
2450         /*
2451                 Draw items
2452                 Phase 0: Item slot rectangles
2453                 Phase 1: Item images; prepare tooltip
2454         */
2455         bool item_hovered = false;
2456         int start_phase = 0;
2457         for (int phase = start_phase; phase <= 1; phase++) {
2458                 for (u32 i = 0; i < m_inventorylists.size(); i++) {
2459                         drawList(m_inventorylists[i], phase, item_hovered);
2460                 }
2461         }
2462         if (!item_hovered) {
2463                 drawItemStack(driver, m_font, ItemStack(),
2464                         core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2465                         NULL, m_gamedef, IT_ROT_HOVERED);
2466         }
2467
2468 /* TODO find way to show tooltips on touchscreen */
2469 #ifndef HAVE_TOUCHSCREENGUI
2470         m_pointer = m_device->getCursorControl()->getPosition();
2471 #endif
2472
2473         /*
2474                 Draw static text elements
2475         */
2476         for (u32 i = 0; i < m_static_texts.size(); i++) {
2477                 const StaticTextSpec &spec = m_static_texts[i];
2478                 core::rect<s32> rect = spec.rect;
2479                 if (spec.parent_button && spec.parent_button->isPressed()) {
2480 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2481                         rect += core::dimension2d<s32>(
2482                                 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2483 #else
2484                         // Use image offset instead of text's because its a bit smaller
2485                         // and fits better, also TEXT_OFFSET_X is always 0
2486                         rect += core::dimension2d<s32>(
2487                                 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2488                                 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2489 #endif
2490                 }
2491                 video::SColor color(255, 255, 255, 255);
2492                 m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
2493         }
2494
2495         /*
2496                 Draw fields/buttons tooltips
2497         */
2498         gui::IGUIElement *hovered =
2499                         Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2500
2501         if (hovered != NULL) {
2502                 s32 id = hovered->getID();
2503
2504                 u32 delta = 0;
2505                 if (id == -1) {
2506                         m_old_tooltip_id = id;
2507                         m_old_tooltip = L"";
2508                 } else {
2509                         if (id == m_old_tooltip_id) {
2510                                 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2511                         } else {
2512                                 m_hovered_time = getTimeMs();
2513                                 m_old_tooltip_id = id;
2514                         }
2515                 }
2516
2517                 if (id != -1 && delta >= m_tooltip_show_delay) {
2518                         for(std::vector<FieldSpec>::iterator iter =  m_fields.begin();
2519                                         iter != m_fields.end(); ++iter) {
2520                                 if (iter->fid == id && m_tooltips[iter->fname].tooltip != L"") {
2521                                         if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2522                                                 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2523                                                 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2524                                                 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2525                                                 setStaticText(m_tooltip_element, m_tooltips[iter->fname].tooltip.c_str());
2526                                                 std::vector<std::wstring> tt_rows = str_split(m_tooltips[iter->fname].tooltip, L'\n');
2527                                                 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2528                                                 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2529                                                 int tooltip_offset_x = m_btn_height;
2530                                                 int tooltip_offset_y = m_btn_height;
2531 #ifdef __ANDROID__
2532                                                 tooltip_offset_x *= 3;
2533                                                 tooltip_offset_y  = 0;
2534                                                 if (m_pointer.X > (s32)screenSize.X / 2)
2535                                                         tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2536 #endif
2537                                                 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2538                                                 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2539                                                 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2540                                                         tooltip_x = (s32)screenSize.X - tooltip_width  - m_btn_height;
2541                                                 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2542                                                         tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2543                                                 m_tooltip_element->setRelativePosition(core::rect<s32>(
2544                                                 core::position2d<s32>(tooltip_x, tooltip_y),
2545                                                 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2546                                         }
2547                                         m_tooltip_element->setVisible(true);
2548                                         this->bringToFront(m_tooltip_element);
2549                                         break;
2550                                 }
2551                         }
2552                 }
2553         }
2554
2555         m_tooltip_element->draw();
2556
2557         /*
2558                 Draw dragged item stack
2559         */
2560         drawSelectedItem();
2561
2562         skin->setFont(old_font);
2563 }
2564
2565 void GUIFormSpecMenu::updateSelectedItem()
2566 {
2567         // If the selected stack has become empty for some reason, deselect it.
2568         // If the selected stack has become inaccessible, deselect it.
2569         // If the selected stack has become smaller, adjust m_selected_amount.
2570         ItemStack selected = verifySelectedItem();
2571
2572         // WARNING: BLACK MAGIC
2573         // See if there is a stack suited for our current guess.
2574         // If such stack does not exist, clear the guess.
2575         if(m_selected_content_guess.name != "" &&
2576                         selected.name == m_selected_content_guess.name &&
2577                         selected.count == m_selected_content_guess.count){
2578                 // Selected item fits the guess. Skip the black magic.
2579         }
2580         else if(m_selected_content_guess.name != ""){
2581                 bool found = false;
2582                 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2583                         const ListDrawSpec &s = m_inventorylists[i];
2584                         Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2585                         if(!inv)
2586                                 continue;
2587                         InventoryList *list = inv->getList(s.listname);
2588                         if(!list)
2589                                 continue;
2590                         for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2591                                 u32 item_i = i + s.start_item_i;
2592                                 if(item_i >= list->getSize())
2593                                         continue;
2594                                 ItemStack stack = list->getItem(item_i);
2595                                 if(stack.name == m_selected_content_guess.name &&
2596                                                 stack.count == m_selected_content_guess.count){
2597                                         found = true;
2598                                         infostream<<"Client: Changing selected content guess to "
2599                                                         <<s.inventoryloc.dump()<<" "<<s.listname
2600                                                         <<" "<<item_i<<std::endl;
2601                                         delete m_selected_item;
2602                                         m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2603                                         m_selected_amount = stack.count;
2604                                 }
2605                         }
2606                 }
2607                 if(!found){
2608                         infostream<<"Client: Discarding selected content guess: "
2609                                         <<m_selected_content_guess.getItemString()<<std::endl;
2610                         m_selected_content_guess.name = "";
2611                 }
2612         }
2613
2614         // If craftresult is nonempty and nothing else is selected, select it now.
2615         if(!m_selected_item)
2616         {
2617                 for(u32 i=0; i<m_inventorylists.size(); i++)
2618                 {
2619                         const ListDrawSpec &s = m_inventorylists[i];
2620                         if(s.listname == "craftpreview")
2621                         {
2622                                 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2623                                 InventoryList *list = inv->getList("craftresult");
2624                                 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2625                                 {
2626                                         m_selected_item = new ItemSpec;
2627                                         m_selected_item->inventoryloc = s.inventoryloc;
2628                                         m_selected_item->listname = "craftresult";
2629                                         m_selected_item->i = 0;
2630                                         m_selected_amount = 0;
2631                                         m_selected_dragging = false;
2632                                         break;
2633                                 }
2634                         }
2635                 }
2636         }
2637
2638         // If craftresult is selected, keep the whole stack selected
2639         if(m_selected_item && m_selected_item->listname == "craftresult")
2640         {
2641                 m_selected_amount = verifySelectedItem().count;
2642         }
2643 }
2644
2645 ItemStack GUIFormSpecMenu::verifySelectedItem()
2646 {
2647         // If the selected stack has become empty for some reason, deselect it.
2648         // If the selected stack has become inaccessible, deselect it.
2649         // If the selected stack has become smaller, adjust m_selected_amount.
2650         // Return the selected stack.
2651
2652         if(m_selected_item)
2653         {
2654                 if(m_selected_item->isValid())
2655                 {
2656                         Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2657                         if(inv)
2658                         {
2659                                 InventoryList *list = inv->getList(m_selected_item->listname);
2660                                 if(list && (u32) m_selected_item->i < list->getSize())
2661                                 {
2662                                         ItemStack stack = list->getItem(m_selected_item->i);
2663                                         if(m_selected_amount > stack.count)
2664                                                 m_selected_amount = stack.count;
2665                                         if(!stack.empty())
2666                                                 return stack;
2667                                 }
2668                         }
2669                 }
2670
2671                 // selection was not valid
2672                 delete m_selected_item;
2673                 m_selected_item = NULL;
2674                 m_selected_amount = 0;
2675                 m_selected_dragging = false;
2676         }
2677         return ItemStack();
2678 }
2679
2680 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2681 {
2682         if(m_text_dst)
2683         {
2684                 StringMap fields;
2685
2686                 if (quitmode == quit_mode_accept) {
2687                         fields["quit"] = "true";
2688                 }
2689
2690                 if (quitmode == quit_mode_cancel) {
2691                         fields["quit"] = "true";
2692                         m_text_dst->gotText(fields);
2693                         return;
2694                 }
2695
2696                 if (current_keys_pending.key_down) {
2697                         fields["key_down"] = "true";
2698                         current_keys_pending.key_down = false;
2699                 }
2700
2701                 if (current_keys_pending.key_up) {
2702                         fields["key_up"] = "true";
2703                         current_keys_pending.key_up = false;
2704                 }
2705
2706                 if (current_keys_pending.key_enter) {
2707                         fields["key_enter"] = "true";
2708                         current_keys_pending.key_enter = false;
2709                 }
2710
2711                 if (!current_field_enter_pending.empty()) {
2712                         fields["key_enter_field"] = current_field_enter_pending;
2713                         current_field_enter_pending = "";
2714                 }
2715
2716                 if (current_keys_pending.key_escape) {
2717                         fields["key_escape"] = "true";
2718                         current_keys_pending.key_escape = false;
2719                 }
2720
2721                 for(unsigned int i=0; i<m_fields.size(); i++) {
2722                         const FieldSpec &s = m_fields[i];
2723                         if(s.send) {
2724                                 std::string name = s.fname;
2725                                 if (s.ftype == f_Button) {
2726                                         fields[name] = wide_to_utf8(s.flabel);
2727                                 } else if (s.ftype == f_Table) {
2728                                         GUITable *table = getTable(s.fname);
2729                                         if (table) {
2730                                                 fields[name] = table->checkEvent();
2731                                         }
2732                                 }
2733                                 else if(s.ftype == f_DropDown) {
2734                                         // no dynamic cast possible due to some distributions shipped
2735                                         // without rtti support in irrlicht
2736                                         IGUIElement * element = getElementFromId(s.fid);
2737                                         gui::IGUIComboBox *e = NULL;
2738                                         if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2739                                                 e = static_cast<gui::IGUIComboBox*>(element);
2740                                         }
2741                                         s32 selected = e->getSelected();
2742                                         if (selected >= 0) {
2743                                                 std::vector<std::string> *dropdown_values =
2744                                                         getDropDownValues(s.fname);
2745                                                 if (dropdown_values && selected < (s32)dropdown_values->size()) {
2746                                                         fields[name] = (*dropdown_values)[selected];
2747                                                 }
2748                                         }
2749                                 }
2750                                 else if (s.ftype == f_TabHeader) {
2751                                         // no dynamic cast possible due to some distributions shipped
2752                                         // without rtti support in irrlicht
2753                                         IGUIElement * element = getElementFromId(s.fid);
2754                                         gui::IGUITabControl *e = NULL;
2755                                         if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2756                                                 e = static_cast<gui::IGUITabControl*>(element);
2757                                         }
2758
2759                                         if (e != 0) {
2760                                                 std::stringstream ss;
2761                                                 ss << (e->getActiveTab() +1);
2762                                                 fields[name] = ss.str();
2763                                         }
2764                                 }
2765                                 else if (s.ftype == f_CheckBox) {
2766                                         // no dynamic cast possible due to some distributions shipped
2767                                         // without rtti support in irrlicht
2768                                         IGUIElement * element = getElementFromId(s.fid);
2769                                         gui::IGUICheckBox *e = NULL;
2770                                         if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2771                                                 e = static_cast<gui::IGUICheckBox*>(element);
2772                                         }
2773
2774                                         if (e != 0) {
2775                                                 if (e->isChecked())
2776                                                         fields[name] = "true";
2777                                                 else
2778                                                         fields[name] = "false";
2779                                         }
2780                                 }
2781                                 else if (s.ftype == f_ScrollBar) {
2782                                         // no dynamic cast possible due to some distributions shipped
2783                                         // without rtti support in irrlicht
2784                                         IGUIElement * element = getElementFromId(s.fid);
2785                                         gui::IGUIScrollBar *e = NULL;
2786                                         if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2787                                                 e = static_cast<gui::IGUIScrollBar*>(element);
2788                                         }
2789
2790                                         if (e != 0) {
2791                                                 std::stringstream os;
2792                                                 os << e->getPos();
2793                                                 if (s.fdefault == L"Changed")
2794                                                         fields[name] = "CHG:" + os.str();
2795                                                 else
2796                                                         fields[name] = "VAL:" + os.str();
2797                                         }
2798                                 }
2799                                 else
2800                                 {
2801                                         IGUIElement* e = getElementFromId(s.fid);
2802                                         if(e != NULL) {
2803                                                 fields[name] = wide_to_utf8(e->getText());
2804                                         }
2805                                 }
2806                         }
2807                 }
2808
2809                 m_text_dst->gotText(fields);
2810         }
2811 }
2812
2813 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2814 {
2815         while(tocheck != NULL) {
2816                 if (tocheck == parent) {
2817                         return true;
2818                 }
2819                 tocheck = tocheck->getParent();
2820         }
2821         return false;
2822 }
2823
2824 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2825 {
2826         // The IGUITabControl renders visually using the skin's selected
2827         // font, which we override for the duration of form drawing,
2828         // but computes tab hotspots based on how it would have rendered
2829         // using the font that is selected at the time of button release.
2830         // To make these two consistent, temporarily override the skin's
2831         // font while the IGUITabControl is processing the event.
2832         if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2833                         event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2834                 s32 x = event.MouseInput.X;
2835                 s32 y = event.MouseInput.Y;
2836                 gui::IGUIElement *hovered =
2837                         Environment->getRootGUIElement()->getElementFromPoint(
2838                                 core::position2d<s32>(x, y));
2839                 if (hovered && isMyChild(hovered) &&
2840                                 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2841                         gui::IGUISkin* skin = Environment->getSkin();
2842                         sanity_check(skin != NULL);
2843                         gui::IGUIFont *old_font = skin->getFont();
2844                         skin->setFont(m_font);
2845                         bool retval = hovered->OnEvent(event);
2846                         skin->setFont(old_font);
2847                         return retval;
2848                 }
2849         }
2850
2851         // Fix Esc/Return key being eaten by checkboxen and tables
2852         if(event.EventType==EET_KEY_INPUT_EVENT) {
2853                 KeyPress kp(event.KeyInput);
2854                 if (kp == EscapeKey || kp == CancelKey
2855                                 || kp == getKeySetting("keymap_inventory")
2856                                 || event.KeyInput.Key==KEY_RETURN) {
2857                         gui::IGUIElement *focused = Environment->getFocus();
2858                         if (focused && isMyChild(focused) &&
2859                                         (focused->getType() == gui::EGUIET_LIST_BOX ||
2860                                          focused->getType() == gui::EGUIET_CHECK_BOX)) {
2861                                 OnEvent(event);
2862                                 return true;
2863                         }
2864                 }
2865         }
2866         // Mouse wheel events: send to hovered element instead of focused
2867         if(event.EventType==EET_MOUSE_INPUT_EVENT
2868                         && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2869                 s32 x = event.MouseInput.X;
2870                 s32 y = event.MouseInput.Y;
2871                 gui::IGUIElement *hovered =
2872                         Environment->getRootGUIElement()->getElementFromPoint(
2873                                 core::position2d<s32>(x, y));
2874                 if (hovered && isMyChild(hovered)) {
2875                         hovered->OnEvent(event);
2876                         return true;
2877                 }
2878         }
2879
2880         if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2881                 s32 x = event.MouseInput.X;
2882                 s32 y = event.MouseInput.Y;
2883                 gui::IGUIElement *hovered =
2884                         Environment->getRootGUIElement()->getElementFromPoint(
2885                                 core::position2d<s32>(x, y));
2886                 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2887                         m_old_tooltip_id = -1;
2888                         m_old_tooltip = L"";
2889                 }
2890                 if (!isChild(hovered,this)) {
2891                         if (DoubleClickDetection(event)) {
2892                                 return true;
2893                         }
2894                 }
2895         }
2896
2897         #ifdef __ANDROID__
2898         // display software keyboard when clicking edit boxes
2899         if (event.EventType == EET_MOUSE_INPUT_EVENT
2900                         && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2901                 gui::IGUIElement *hovered =
2902                         Environment->getRootGUIElement()->getElementFromPoint(
2903                                 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2904                 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2905                         bool retval = hovered->OnEvent(event);
2906                         if (retval) {
2907                                 Environment->setFocus(hovered);
2908                         }
2909                         m_JavaDialogFieldName = getNameByID(hovered->getID());
2910                         std::string message   = gettext("Enter ");
2911                         std::string label     = wide_to_utf8(getLabelByID(hovered->getID()));
2912                         if (label == "") {
2913                                 label = "text";
2914                         }
2915                         message += gettext(label) + ":";
2916
2917                         /* single line text input */
2918                         int type = 2;
2919
2920                         /* multi line text input */
2921                         if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2922                                 type = 1;
2923                         }
2924
2925                         /* passwords are always single line */
2926                         if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2927                                 type = 3;
2928                         }
2929
2930                         porting::showInputDialog(gettext("ok"), "",
2931                                         wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
2932                                         type);
2933                         return retval;
2934                 }
2935         }
2936
2937         if (event.EventType == EET_TOUCH_INPUT_EVENT)
2938         {
2939                 SEvent translated;
2940                 memset(&translated, 0, sizeof(SEvent));
2941                 translated.EventType   = EET_MOUSE_INPUT_EVENT;
2942                 gui::IGUIElement* root = Environment->getRootGUIElement();
2943
2944                 if (!root) {
2945                         errorstream
2946                         << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2947                         << std::endl;
2948                         return false;
2949                 }
2950                 gui::IGUIElement* hovered = root->getElementFromPoint(
2951                         core::position2d<s32>(
2952                                         event.TouchInput.X,
2953                                         event.TouchInput.Y));
2954
2955                 translated.MouseInput.X = event.TouchInput.X;
2956                 translated.MouseInput.Y = event.TouchInput.Y;
2957                 translated.MouseInput.Control = false;
2958
2959                 bool dont_send_event = false;
2960
2961                 if (event.TouchInput.touchedCount == 1) {
2962                         switch (event.TouchInput.Event) {
2963                                 case ETIE_PRESSED_DOWN:
2964                                         m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2965                                         translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2966                                         translated.MouseInput.ButtonStates = EMBSM_LEFT;
2967                                         m_down_pos = m_pointer;
2968                                         break;
2969                                 case ETIE_MOVED:
2970                                         m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2971                                         translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2972                                         translated.MouseInput.ButtonStates = EMBSM_LEFT;
2973                                         break;
2974                                 case ETIE_LEFT_UP:
2975                                         translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2976                                         translated.MouseInput.ButtonStates = 0;
2977                                         hovered = root->getElementFromPoint(m_down_pos);
2978                                         /* we don't have a valid pointer element use last
2979                                          * known pointer pos */
2980                                         translated.MouseInput.X = m_pointer.X;
2981                                         translated.MouseInput.Y = m_pointer.Y;
2982
2983                                         /* reset down pos */
2984                                         m_down_pos = v2s32(0,0);
2985                                         break;
2986                                 default:
2987                                         dont_send_event = true;
2988                                         //this is not supposed to happen
2989                                         errorstream
2990                                         << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2991                                         << event.TouchInput.Event << std::endl;
2992                         }
2993                 } else if ( (event.TouchInput.touchedCount == 2) &&
2994                                 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2995                         hovered = root->getElementFromPoint(m_down_pos);
2996
2997                         translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
2998                         translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
2999                         translated.MouseInput.X = m_pointer.X;
3000                         translated.MouseInput.Y = m_pointer.Y;
3001
3002                         if (hovered) {
3003                                 hovered->OnEvent(translated);
3004                         }
3005
3006                         translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
3007                         translated.MouseInput.ButtonStates = EMBSM_LEFT;
3008
3009
3010                         if (hovered) {
3011                                 hovered->OnEvent(translated);
3012                         }
3013                         dont_send_event = true;
3014                 }
3015                 /* ignore unhandled 2 touch events ... accidental moving for example */
3016                 else if (event.TouchInput.touchedCount == 2) {
3017                         dont_send_event = true;
3018                 }
3019                 else if (event.TouchInput.touchedCount > 2) {
3020                         errorstream
3021                         << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
3022                         << event.TouchInput.touchedCount << " ignoring them" << std::endl;
3023                 }
3024
3025                 if (dont_send_event) {
3026                         return true;
3027                 }
3028
3029                 /* check if translated event needs to be preprocessed again */
3030                 if (preprocessEvent(translated)) {
3031                         return true;
3032                 }
3033                 if (hovered) {
3034                         grab();
3035                         bool retval = hovered->OnEvent(translated);
3036
3037                         if (event.TouchInput.Event == ETIE_LEFT_UP) {
3038                                 /* reset pointer */
3039                                 m_pointer = v2s32(0,0);
3040                         }
3041                         drop();
3042                         return retval;
3043                 }
3044         }
3045         #endif
3046
3047         if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3048                 /* TODO add a check like:
3049                 if (event.JoystickEvent != joystick_we_listen_for)
3050                         return false;
3051                 */
3052                 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3053                 if (handled) {
3054                         if (m_joystick->wasKeyDown(KeyType::ESC)) {
3055                                 tryClose();
3056                         } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3057                                 if (m_allowclose) {
3058                                         acceptInput(quit_mode_accept);
3059                                         quitMenu();
3060                                 }
3061                         }
3062                 }
3063                 return handled;
3064         }
3065
3066         return false;
3067 }
3068
3069 /******************************************************************************/
3070 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3071 {
3072         /* The following code is for capturing double-clicks of the mouse button
3073          * and translating the double-click into an EET_KEY_INPUT_EVENT event
3074          * -- which closes the form -- under some circumstances.
3075          *
3076          * There have been many github issues reporting this as a bug even though it
3077          * was an intended feature.  For this reason, remapping the double-click as
3078          * an ESC must be explicitly set when creating this class via the
3079          * /p remap_dbl_click parameter of the constructor.
3080          */
3081
3082         if (!m_remap_dbl_click)
3083                 return false;
3084
3085         if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3086                 m_doubleclickdetect[0].pos  = m_doubleclickdetect[1].pos;
3087                 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3088
3089                 m_doubleclickdetect[1].pos  = m_pointer;
3090                 m_doubleclickdetect[1].time = getTimeMs();
3091         }
3092         else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3093                 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
3094                 if (delta > 400) {
3095                         return false;
3096                 }
3097
3098                 double squaredistance =
3099                                 m_doubleclickdetect[0].pos
3100                                 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3101
3102                 if (squaredistance > (30*30)) {
3103                         return false;
3104                 }
3105
3106                 SEvent* translated = new SEvent();
3107                 assert(translated != 0);
3108                 //translate doubleclick to escape
3109                 memset(translated, 0, sizeof(SEvent));
3110                 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3111                 translated->KeyInput.Key         = KEY_ESCAPE;
3112                 translated->KeyInput.Control     = false;
3113                 translated->KeyInput.Shift       = false;
3114                 translated->KeyInput.PressedDown = true;
3115                 translated->KeyInput.Char        = 0;
3116                 OnEvent(*translated);
3117
3118                 // no need to send the key up event as we're already deleted
3119                 // and no one else did notice this event
3120                 delete translated;
3121                 return true;
3122         }
3123
3124         return false;
3125 }
3126
3127 void GUIFormSpecMenu::tryClose()
3128 {
3129         if (m_allowclose) {
3130                 doPause = false;
3131                 acceptInput(quit_mode_cancel);
3132                 quitMenu();
3133         } else {
3134                 m_text_dst->gotText(L"MenuQuit");
3135         }
3136 }
3137
3138 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3139 {
3140         if (event.EventType==EET_KEY_INPUT_EVENT) {
3141                 KeyPress kp(event.KeyInput);
3142                 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3143                                 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3144                         tryClose();
3145                         return true;
3146                 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3147                                 (kp == getKeySetting("keymap_screenshot"))) {
3148                         m_client->makeScreenshot(m_device);
3149                 }
3150                 if (event.KeyInput.PressedDown &&
3151                         (event.KeyInput.Key==KEY_RETURN ||
3152                          event.KeyInput.Key==KEY_UP ||
3153                          event.KeyInput.Key==KEY_DOWN)
3154                         ) {
3155                         switch (event.KeyInput.Key) {
3156                                 case KEY_RETURN:
3157                                         current_keys_pending.key_enter = true;
3158                                         break;
3159                                 case KEY_UP:
3160                                         current_keys_pending.key_up = true;
3161                                         break;
3162                                 case KEY_DOWN:
3163                                         current_keys_pending.key_down = true;
3164                                         break;
3165                                 break;
3166                                 default:
3167                                         //can't happen at all!
3168                                         FATAL_ERROR("Reached a source line that can't ever been reached");
3169                                         break;
3170                         }
3171                         if (current_keys_pending.key_enter && m_allowclose) {
3172                                 acceptInput(quit_mode_accept);
3173                                 quitMenu();
3174                         } else {
3175                                 acceptInput();
3176                         }
3177                         return true;
3178                 }
3179
3180         }
3181
3182         /* Mouse event other than movement, or crossing the border of inventory
3183           field while holding right mouse button
3184          */
3185         if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3186                         (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3187                          (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3188                           event.MouseInput.isRightPressed() &&
3189                           getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3190
3191                 // Get selected item and hovered/clicked item (s)
3192
3193                 m_old_tooltip_id = -1;
3194                 updateSelectedItem();
3195                 ItemSpec s = getItemAtPos(m_pointer);
3196
3197                 Inventory *inv_selected = NULL;
3198                 Inventory *inv_s = NULL;
3199                 InventoryList *list_s = NULL;
3200
3201                 if (m_selected_item) {
3202                         inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3203                         sanity_check(inv_selected);
3204                         sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3205                 }
3206
3207                 u32 s_count = 0;
3208
3209                 if (s.isValid())
3210                 do { // breakable
3211                         inv_s = m_invmgr->getInventory(s.inventoryloc);
3212
3213                         if (!inv_s) {
3214                                 errorstream << "InventoryMenu: The selected inventory location "
3215                                                 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3216                                                 << std::endl;
3217                                 s.i = -1;  // make it invalid again
3218                                 break;
3219                         }
3220
3221                         list_s = inv_s->getList(s.listname);
3222                         if (list_s == NULL) {
3223                                 verbosestream << "InventoryMenu: The selected inventory list \""
3224                                                 << s.listname << "\" does not exist" << std::endl;
3225                                 s.i = -1;  // make it invalid again
3226                                 break;
3227                         }
3228
3229                         if ((u32)s.i >= list_s->getSize()) {
3230                                 infostream << "InventoryMenu: The selected inventory list \""
3231                                                 << s.listname << "\" is too small (i=" << s.i << ", size="
3232                                                 << list_s->getSize() << ")" << std::endl;
3233                                 s.i = -1;  // make it invalid again
3234                                 break;
3235                         }
3236
3237                         s_count = list_s->getItem(s.i).count;
3238                 } while(0);
3239
3240                 bool identical = (m_selected_item != NULL) && s.isValid() &&
3241                         (inv_selected == inv_s) &&
3242                         (m_selected_item->listname == s.listname) &&
3243                         (m_selected_item->i == s.i);
3244
3245                 // buttons: 0 = left, 1 = right, 2 = middle
3246                 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3247                 int button = 0;
3248                 int updown = 2;
3249                 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3250                         { button = 0; updown = 0; }
3251                 else if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3252                         { button = 1; updown = 0; }
3253                 else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3254                         { button = 2; updown = 0; }
3255                 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3256                         { button = 0; updown = 1; }
3257                 else if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3258                         { button = 1; updown = 1; }
3259                 else if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3260                         { button = 2; updown = 1; }
3261                 else if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
3262                         { updown = -1;}
3263
3264                 // Set this number to a positive value to generate a move action
3265                 // from m_selected_item to s.
3266                 u32 move_amount = 0;
3267
3268                 // Set this number to a positive value to generate a move action
3269                 // from s to the next inventory ring.
3270                 u32 shift_move_amount = 0;
3271
3272                 // Set this number to a positive value to generate a drop action
3273                 // from m_selected_item.
3274                 u32 drop_amount = 0;
3275
3276                 // Set this number to a positive value to generate a craft action at s.
3277                 u32 craft_amount = 0;
3278
3279                 if (updown == 0) {
3280                         // Some mouse button has been pressed
3281
3282                         //infostream<<"Mouse button "<<button<<" pressed at p=("
3283                         //      <<p.X<<","<<p.Y<<")"<<std::endl;
3284
3285                         m_selected_dragging = false;
3286
3287                         if (s.isValid() && s.listname == "craftpreview") {
3288                                 // Craft preview has been clicked: craft
3289                                 craft_amount = (button == 2 ? 10 : 1);
3290                         } else if (m_selected_item == NULL) {
3291                                 if (s_count != 0) {
3292                                         // Non-empty stack has been clicked: select or shift-move it
3293                                         m_selected_item = new ItemSpec(s);
3294
3295                                         u32 count;
3296                                         if (button == 1)  // right
3297                                                 count = (s_count + 1) / 2;
3298                                         else if (button == 2)  // middle
3299                                                 count = MYMIN(s_count, 10);
3300                                         else  // left
3301                                                 count = s_count;
3302
3303                                         if (!event.MouseInput.Shift) {
3304                                                 // no shift: select item
3305                                                 m_selected_amount = count;
3306                                                 m_selected_dragging = true;
3307                                                 m_rmouse_auto_place = false;
3308                                         } else {
3309                                                 // shift pressed: move item
3310                                                 if (button != 1)
3311                                                         shift_move_amount = count;
3312                                                 else // count of 1 at left click like after drag & drop
3313                                                         shift_move_amount = 1;
3314                                         }
3315                                 }
3316                         } else { // m_selected_item != NULL
3317                                 assert(m_selected_amount >= 1);
3318
3319                                 if (s.isValid()) {
3320                                         // Clicked a slot: move
3321                                         if (button == 1)  // right
3322                                                 move_amount = 1;
3323                                         else if (button == 2)  // middle
3324                                                 move_amount = MYMIN(m_selected_amount, 10);
3325                                         else  // left
3326                                                 move_amount = m_selected_amount;
3327
3328                                         if (identical) {
3329                                                 if (move_amount >= m_selected_amount)
3330                                                         m_selected_amount = 0;
3331                                                 else
3332                                                         m_selected_amount -= move_amount;
3333                                                 move_amount = 0;
3334                                         }
3335                                 }
3336                                 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3337                                         // Clicked outside of the window: drop
3338                                         if (button == 1)  // right
3339                                                 drop_amount = 1;
3340                                         else if (button == 2)  // middle
3341                                                 drop_amount = MYMIN(m_selected_amount, 10);
3342                                         else  // left
3343                                                 drop_amount = m_selected_amount;
3344                                 }
3345                         }
3346                 }
3347                 else if (updown == 1) {
3348                         // Some mouse button has been released
3349
3350                         //infostream<<"Mouse button "<<button<<" released at p=("
3351                         //      <<p.X<<","<<p.Y<<")"<<std::endl;
3352
3353                         if (m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3354                                 if (!identical) {
3355                                         // Dragged to different slot: move all selected
3356                                         move_amount = m_selected_amount;
3357                                 }
3358                         } else if (m_selected_item != NULL && m_selected_dragging &&
3359                                         !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3360                                 // Dragged outside of window: drop all selected
3361                                 drop_amount = m_selected_amount;
3362                         }
3363
3364                         m_selected_dragging = false;
3365                         // Keep count of how many times right mouse button has been
3366                         // clicked. One click is drag without dropping. Click + release
3367                         // + click changes to drop one item when moved mode
3368                         if (button == 1 && m_selected_item != NULL)
3369                                 m_rmouse_auto_place = !m_rmouse_auto_place;
3370                 } else if (updown == -1) {
3371                         // Mouse has been moved and rmb is down and mouse pointer just
3372                         // entered a new inventory field (checked in the entry-if, this
3373                         // is the only action here that is generated by mouse movement)
3374                         if (m_selected_item != NULL && s.isValid()) {
3375                                 // Move 1 item
3376                                 // TODO: middle mouse to move 10 items might be handy
3377                                 if (m_rmouse_auto_place) {
3378                                         // Only move an item if the destination slot is empty
3379                                         // or contains the same item type as what is going to be
3380                                         // moved
3381                                         InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3382                                         InventoryList *list_to = list_s;
3383                                         assert(list_from && list_to);
3384                                         ItemStack stack_from = list_from->getItem(m_selected_item->i);
3385                                         ItemStack stack_to = list_to->getItem(s.i);
3386                                         if (stack_to.empty() || stack_to.name == stack_from.name)
3387                                                 move_amount = 1;
3388                                 }
3389                         }
3390                 }
3391
3392                 // Possibly send inventory action to server
3393                 if (move_amount > 0) {
3394                         // Send IACTION_MOVE
3395
3396                         assert(m_selected_item && m_selected_item->isValid());
3397                         assert(s.isValid());
3398
3399                         assert(inv_selected && inv_s);
3400                         InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3401                         InventoryList *list_to = list_s;
3402                         assert(list_from && list_to);
3403                         ItemStack stack_from = list_from->getItem(m_selected_item->i);
3404                         ItemStack stack_to = list_to->getItem(s.i);
3405
3406                         // Check how many items can be moved
3407                         move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3408                         ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3409                         // If source stack cannot be added to destination stack at all,
3410                         // they are swapped
3411                         if ((leftover.count == stack_from.count) &&
3412                                         (leftover.name == stack_from.name)) {
3413                                 m_selected_amount = stack_to.count;
3414                                 // In case the server doesn't directly swap them but instead
3415                                 // moves stack_to somewhere else, set this
3416                                 m_selected_content_guess = stack_to;
3417                                 m_selected_content_guess_inventory = s.inventoryloc;
3418                         }
3419                         // Source stack goes fully into destination stack
3420                         else if (leftover.empty()) {
3421                                 m_selected_amount -= move_amount;
3422                                 m_selected_content_guess = ItemStack(); // Clear
3423                         }
3424                         // Source stack goes partly into destination stack
3425                         else {
3426                                 move_amount -= leftover.count;
3427                                 m_selected_amount -= move_amount;
3428                                 m_selected_content_guess = ItemStack(); // Clear
3429                         }
3430
3431                         infostream << "Handing IACTION_MOVE to manager" << std::endl;
3432                         IMoveAction *a = new IMoveAction();
3433                         a->count = move_amount;
3434                         a->from_inv = m_selected_item->inventoryloc;
3435                         a->from_list = m_selected_item->listname;
3436                         a->from_i = m_selected_item->i;
3437                         a->to_inv = s.inventoryloc;
3438                         a->to_list = s.listname;
3439                         a->to_i = s.i;
3440                         m_invmgr->inventoryAction(a);
3441                 } else if (shift_move_amount > 0) {
3442                         u32 mis = m_inventory_rings.size();
3443                         u32 i = 0;
3444                         for (; i < mis; i++) {
3445                                 const ListRingSpec &sp = m_inventory_rings[i];
3446                                 if (sp.inventoryloc == s.inventoryloc
3447                                                 && sp.listname == s.listname)
3448                                         break;
3449                         }
3450                         do {
3451                                 if (i >= mis) // if not found
3452                                         break;
3453                                 u32 to_inv_ind = (i + 1) % mis;
3454                                 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3455                                 InventoryList *list_from = list_s;
3456                                 if (!s.isValid())
3457                                         break;
3458                                 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3459                                 if (!inv_to)
3460                                         break;
3461                                 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3462                                 if (!list_to)
3463                                         break;
3464                                 ItemStack stack_from = list_from->getItem(s.i);
3465                                 assert(shift_move_amount <= stack_from.count);
3466                                 if (m_client->getProtoVersion() >= 25) {
3467                                         infostream << "Handing IACTION_MOVE to manager" << std::endl;
3468                                         IMoveAction *a = new IMoveAction();
3469                                         a->count = shift_move_amount;
3470                                         a->from_inv = s.inventoryloc;
3471                                         a->from_list = s.listname;
3472                                         a->from_i = s.i;
3473                                         a->to_inv = to_inv_sp.inventoryloc;
3474                                         a->to_list = to_inv_sp.listname;
3475                                         a->move_somewhere = true;
3476                                         m_invmgr->inventoryAction(a);
3477                                 } else {
3478                                         // find a place (or more than one) to add the new item
3479                                         u32 ilt_size = list_to->getSize();
3480                                         ItemStack leftover;
3481                                         for (u32 slot_to = 0; slot_to < ilt_size
3482                                                         && shift_move_amount > 0; slot_to++) {
3483                                                 list_to->itemFits(slot_to, stack_from, &leftover);
3484                                                 if (leftover.count < stack_from.count) {
3485                                                         infostream << "Handing IACTION_MOVE to manager" << std::endl;
3486                                                         IMoveAction *a = new IMoveAction();
3487                                                         a->count = MYMIN(shift_move_amount,
3488                                                                 (u32) (stack_from.count - leftover.count));
3489                                                         shift_move_amount -= a->count;
3490                                                         a->from_inv = s.inventoryloc;
3491                                                         a->from_list = s.listname;
3492                                                         a->from_i = s.i;
3493                                                         a->to_inv = to_inv_sp.inventoryloc;
3494                                                         a->to_list = to_inv_sp.listname;
3495                                                         a->to_i = slot_to;
3496                                                         m_invmgr->inventoryAction(a);
3497                                                         stack_from = leftover;
3498                                                 }
3499                                         }
3500                                 }
3501                         } while (0);
3502                 } else if (drop_amount > 0) {
3503                         m_selected_content_guess = ItemStack(); // Clear
3504
3505                         // Send IACTION_DROP
3506
3507                         assert(m_selected_item && m_selected_item->isValid());
3508                         assert(inv_selected);
3509                         InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3510                         assert(list_from);
3511                         ItemStack stack_from = list_from->getItem(m_selected_item->i);
3512
3513                         // Check how many items can be dropped
3514                         drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3515                         assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3516                         m_selected_amount -= drop_amount;
3517
3518                         infostream << "Handing IACTION_DROP to manager" << std::endl;
3519                         IDropAction *a = new IDropAction();
3520                         a->count = drop_amount;
3521                         a->from_inv = m_selected_item->inventoryloc;
3522                         a->from_list = m_selected_item->listname;
3523                         a->from_i = m_selected_item->i;
3524                         m_invmgr->inventoryAction(a);
3525                 } else if (craft_amount > 0) {
3526                         m_selected_content_guess = ItemStack(); // Clear
3527
3528                         // Send IACTION_CRAFT
3529
3530                         assert(s.isValid());
3531                         assert(inv_s);
3532
3533                         infostream << "Handing IACTION_CRAFT to manager" << std::endl;
3534                         ICraftAction *a = new ICraftAction();
3535                         a->count = craft_amount;
3536                         a->craft_inv = s.inventoryloc;
3537                         m_invmgr->inventoryAction(a);
3538                 }
3539
3540                 // If m_selected_amount has been decreased to zero, deselect
3541                 if (m_selected_amount == 0) {
3542                         delete m_selected_item;
3543                         m_selected_item = NULL;
3544                         m_selected_amount = 0;
3545                         m_selected_dragging = false;
3546                         m_selected_content_guess = ItemStack();
3547                 }
3548                 m_old_pointer = m_pointer;
3549         }
3550         if (event.EventType == EET_GUI_EVENT) {
3551
3552                 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3553                                 && isVisible()) {
3554                         // find the element that was clicked
3555                         for (unsigned int i=0; i<m_fields.size(); i++) {
3556                                 FieldSpec &s = m_fields[i];
3557                                 if ((s.ftype == f_TabHeader) &&
3558                                                 (s.fid == event.GUIEvent.Caller->getID())) {
3559                                         s.send = true;
3560                                         acceptInput();
3561                                         s.send = false;
3562                                         return true;
3563                                 }
3564                         }
3565                 }
3566                 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3567                                 && isVisible()) {
3568                         if (!canTakeFocus(event.GUIEvent.Element)) {
3569                                 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3570                                                 <<std::endl;
3571                                 // Returning true disables focus change
3572                                 return true;
3573                         }
3574                 }
3575                 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3576                                 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3577                                 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3578                                 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3579                         unsigned int btn_id = event.GUIEvent.Caller->getID();
3580
3581                         if (btn_id == 257) {
3582                                 if (m_allowclose) {
3583                                         acceptInput(quit_mode_accept);
3584                                         quitMenu();
3585                                 } else {
3586                                         acceptInput();
3587                                         m_text_dst->gotText(L"ExitButton");
3588                                 }
3589                                 // quitMenu deallocates menu
3590                                 return true;
3591                         }
3592
3593                         // find the element that was clicked
3594                         for (u32 i = 0; i < m_fields.size(); i++) {
3595                                 FieldSpec &s = m_fields[i];
3596                                 // if its a button, set the send field so
3597                                 // lua knows which button was pressed
3598                                 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3599                                                 (s.fid == event.GUIEvent.Caller->getID())) {
3600                                         s.send = true;
3601                                         if (s.is_exit) {
3602                                                 if (m_allowclose) {
3603                                                         acceptInput(quit_mode_accept);
3604                                                         quitMenu();
3605                                                 } else {
3606                                                         m_text_dst->gotText(L"ExitButton");
3607                                                 }
3608                                                 return true;
3609                                         } else {
3610                                                 acceptInput(quit_mode_no);
3611                                                 s.send = false;
3612                                                 return true;
3613                                         }
3614                                 } else if ((s.ftype == f_DropDown) &&
3615                                                 (s.fid == event.GUIEvent.Caller->getID())) {
3616                                         // only send the changed dropdown
3617                                         for (u32 i = 0; i < m_fields.size(); i++) {
3618                                                 FieldSpec &s2 = m_fields[i];
3619                                                 if (s2.ftype == f_DropDown) {
3620                                                         s2.send = false;
3621                                                 }
3622                                         }
3623                                         s.send = true;
3624                                         acceptInput(quit_mode_no);
3625
3626                                         // revert configuration to make sure dropdowns are sent on
3627                                         // regular button click
3628                                         for (u32 i = 0; i < m_fields.size(); i++) {
3629                                                 FieldSpec &s2 = m_fields[i];
3630                                                 if (s2.ftype == f_DropDown) {
3631                                                         s2.send = true;
3632                                                 }
3633                                         }
3634                                         return true;
3635                                 } else if ((s.ftype == f_ScrollBar) &&
3636                                                 (s.fid == event.GUIEvent.Caller->getID())) {
3637                                         s.fdefault = L"Changed";
3638                                         acceptInput(quit_mode_no);
3639                                         s.fdefault = L"";
3640                                 }
3641                         }
3642                 }
3643
3644                 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3645                         if (event.GUIEvent.Caller->getID() > 257) {
3646                                 bool close_on_enter = true;
3647                                 for (u32 i = 0; i < m_fields.size(); i++) {
3648                                         FieldSpec &s = m_fields[i];
3649                                         if (s.ftype == f_Unknown &&
3650                                                         s.fid == event.GUIEvent.Caller->getID()) {
3651                                                 current_field_enter_pending = s.fname;
3652                                                 close_on_enter = s.close_on_enter;
3653                                                 break;
3654                                         }
3655                                 }
3656
3657                                 if (m_allowclose && close_on_enter) {
3658                                         current_keys_pending.key_enter = true;
3659                                         acceptInput(quit_mode_accept);
3660                                         quitMenu();
3661                                 } else {
3662                                         current_keys_pending.key_enter = true;
3663                                         acceptInput();
3664                                 }
3665                                 // quitMenu deallocates menu
3666                                 return true;
3667                         }
3668                 }
3669
3670                 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3671                         int current_id = event.GUIEvent.Caller->getID();
3672                         if (current_id > 257) {
3673                                 // find the element that was clicked
3674                                 for (u32 i = 0; i < m_fields.size(); i++) {
3675                                         FieldSpec &s = m_fields[i];
3676                                         // if it's a table, set the send field
3677                                         // so lua knows which table was changed
3678                                         if ((s.ftype == f_Table) && (s.fid == current_id)) {
3679                                                 s.send = true;
3680                                                 acceptInput();
3681                                                 s.send=false;
3682                                         }
3683                                 }
3684                                 return true;
3685                         }
3686                 }
3687         }
3688
3689         return Parent ? Parent->OnEvent(event) : false;
3690 }
3691
3692 /**
3693  * get name of element by element id
3694  * @param id of element
3695  * @return name string or empty string
3696  */
3697 std::string GUIFormSpecMenu::getNameByID(s32 id)
3698 {
3699         for(std::vector<FieldSpec>::iterator iter =  m_fields.begin();
3700                                 iter != m_fields.end(); ++iter) {
3701                 if (iter->fid == id) {
3702                         return iter->fname;
3703                 }
3704         }
3705         return "";
3706 }
3707
3708 /**
3709  * get label of element by id
3710  * @param id of element
3711  * @return label string or empty string
3712  */
3713 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3714 {
3715         for(std::vector<FieldSpec>::iterator iter =  m_fields.begin();
3716                                 iter != m_fields.end(); ++iter) {
3717                 if (iter->fid == id) {
3718                         return iter->flabel;
3719                 }
3720         }
3721         return L"";
3722 }