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