Switch the license to be LGPLv2/later, with small parts still remaining as GPLv2...
[oweals/minetest.git] / src / guiTextInputMenu.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 #include "guiTextInputMenu.h"
21 #include "debug.h"
22 #include "serialization.h"
23 #include <string>
24 #include <IGUICheckBox.h>
25 #include <IGUIEditBox.h>
26 #include <IGUIButton.h>
27 #include <IGUIStaticText.h>
28 #include <IGUIFont.h>
29
30 #include "gettext.h"
31
32 GUITextInputMenu::GUITextInputMenu(gui::IGUIEnvironment* env,
33                 gui::IGUIElement* parent, s32 id,
34                 IMenuManager *menumgr,
35                 TextDest *dest,
36                 std::wstring initial_text
37 ):
38         GUIModalMenu(env, parent, id, menumgr),
39         m_dest(dest),
40         m_initial_text(initial_text)
41 {
42 }
43
44 GUITextInputMenu::~GUITextInputMenu()
45 {
46         removeChildren();
47         if(m_dest)
48                 delete m_dest;
49 }
50
51 void GUITextInputMenu::removeChildren()
52 {
53         {
54                 gui::IGUIElement *e = getElementFromId(256);
55                 if(e != NULL)
56                         e->remove();
57         }
58         {
59                 gui::IGUIElement *e = getElementFromId(257);
60                 if(e != NULL)
61                         e->remove();
62         }
63 }
64
65 void GUITextInputMenu::regenerateGui(v2u32 screensize)
66 {
67         std::wstring text;
68
69         {
70                 gui::IGUIElement *e = getElementFromId(256);
71                 if(e != NULL)
72                 {
73                         text = e->getText();
74                 }
75                 else
76                 {
77                         text = m_initial_text;
78                         m_initial_text = L"";
79                 }
80         }
81
82         /*
83                 Remove stuff
84         */
85         removeChildren();
86         
87         /*
88                 Calculate new sizes and positions
89         */
90         core::rect<s32> rect(
91                         screensize.X/2 - 580/2,
92                         screensize.Y/2 - 300/2,
93                         screensize.X/2 + 580/2,
94                         screensize.Y/2 + 300/2
95         );
96         
97         DesiredRect = rect;
98         recalculateAbsolutePosition(false);
99
100         v2s32 size = rect.getSize();
101
102         /*
103                 Add stuff
104         */
105         {
106                 core::rect<s32> rect(0, 0, 300, 30);
107                 rect = rect + v2s32(size.X/2-300/2, size.Y/2-30/2-25);
108                 gui::IGUIElement *e = 
109                 Environment->addEditBox(text.c_str(), rect, true, this, 256);
110                 Environment->setFocus(e);
111
112                 irr::SEvent evt;
113                 evt.EventType = EET_KEY_INPUT_EVENT;
114                 evt.KeyInput.Key = KEY_END;
115                 evt.KeyInput.PressedDown = true;
116                 e->OnEvent(evt);
117         }
118         changeCtype("");
119         {
120                 core::rect<s32> rect(0, 0, 140, 30);
121                 rect = rect + v2s32(size.X/2-140/2, size.Y/2-30/2+25);
122                 Environment->addButton(rect, this, 257,
123                         wgettext("Proceed"));
124         }
125         changeCtype("C");
126 }
127
128 void GUITextInputMenu::drawMenu()
129 {
130         gui::IGUISkin* skin = Environment->getSkin();
131         if (!skin)
132                 return;
133         video::IVideoDriver* driver = Environment->getVideoDriver();
134         
135         video::SColor bgcolor(140,0,0,0);
136         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
137
138         gui::IGUIElement::draw();
139 }
140
141 void GUITextInputMenu::acceptInput()
142 {
143         if(m_dest)
144         {
145                 gui::IGUIElement *e = getElementFromId(256);
146                 if(e != NULL)
147                 {
148                         m_dest->gotText(e->getText());
149                 }
150                 delete m_dest;
151                 m_dest = NULL;
152         }
153 }
154
155 bool GUITextInputMenu::OnEvent(const SEvent& event)
156 {
157         if(event.EventType==EET_KEY_INPUT_EVENT)
158         {
159                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
160                 {
161                         quitMenu();
162                         return true;
163                 }
164                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
165                 {
166                         acceptInput();
167                         quitMenu();
168                         return true;
169                 }
170         }
171         if(event.EventType==EET_GUI_EVENT)
172         {
173                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
174                                 && isVisible())
175                 {
176                         if(!canTakeFocus(event.GUIEvent.Element))
177                         {
178                                 dstream<<"GUITextInputMenu: Not allowing focus change."
179                                                 <<std::endl;
180                                 // Returning true disables focus change
181                                 return true;
182                         }
183                 }
184                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
185                 {
186                         switch(event.GUIEvent.Caller->getID())
187                         {
188                         case 257:
189                                 acceptInput();
190                                 quitMenu();
191                                 // quitMenu deallocates menu
192                                 return true;
193                         }
194                 }
195                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
196                 {
197                         switch(event.GUIEvent.Caller->getID())
198                         {
199                         case 256:
200                                 acceptInput();
201                                 quitMenu();
202                                 // quitMenu deallocates menu
203                                 return true;
204                         }
205                 }
206         }
207
208         return Parent ? Parent->OnEvent(event) : false;
209 }
210