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