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