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