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