Translation to Portuguese of Brazil for Minetest
[oweals/minetest.git] / src / guiConfirmMenu.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 #include "guiConfirmMenu.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 enum
33 {
34         GUI_ID_YES = 101,
35         GUI_ID_NO,
36 };
37
38 GUIConfirmMenu::GUIConfirmMenu(gui::IGUIEnvironment* env,
39                 gui::IGUIElement* parent, s32 id,
40                 IMenuManager *menumgr,
41                 ConfirmDest *dest,
42                 std::wstring message_text
43 ):
44         GUIModalMenu(env, parent, id, menumgr),
45         m_dest(dest),
46         m_message_text(message_text)
47 {
48 }
49
50 GUIConfirmMenu::~GUIConfirmMenu()
51 {
52         removeChildren();
53         if(m_dest)
54                 delete m_dest;
55 }
56
57 void GUIConfirmMenu::removeChildren()
58 {
59         const core::list<gui::IGUIElement*> &children = getChildren();
60         core::list<gui::IGUIElement*> children_copy;
61         for(core::list<gui::IGUIElement*>::ConstIterator
62                         i = children.begin(); i != children.end(); i++)
63         {
64                 children_copy.push_back(*i);
65         }
66         for(core::list<gui::IGUIElement*>::Iterator
67                         i = children_copy.begin();
68                         i != children_copy.end(); i++)
69         {
70                 (*i)->remove();
71         }
72 }
73
74 void GUIConfirmMenu::regenerateGui(v2u32 screensize)
75 {
76         /*
77                 Remove stuff
78         */
79         removeChildren();
80         
81         /*
82                 Calculate new sizes and positions
83         */
84         core::rect<s32> rect(
85                         screensize.X/2 - 580/2,
86                         screensize.Y/2 - 300/2,
87                         screensize.X/2 + 580/2,
88                         screensize.Y/2 + 300/2
89         );
90         
91         DesiredRect = rect;
92         recalculateAbsolutePosition(false);
93
94         v2s32 size = rect.getSize();
95
96         gui::IGUISkin *skin = Environment->getSkin();
97         gui::IGUIFont *font = skin->getFont();
98         s32 msg_h = font->getDimension(m_message_text.c_str()).Height;
99         s32 msg_w = font->getDimension(m_message_text.c_str()).Width;
100         if(msg_h > 200)
101                 msg_h = 200;
102         if(msg_w > 540)
103                 msg_w = 540;
104
105         /*
106                 Add stuff
107         */
108         {
109                 core::rect<s32> rect(0, 0, msg_w, msg_h);
110                 rect += v2s32(size.X/2-msg_w/2, size.Y/2-30/2 - msg_h/2);
111                 Environment->addStaticText(m_message_text.c_str(),
112                         rect, false, true, this, -1);
113         }
114         changeCtype("");
115         int bw = 100;
116         {
117                 core::rect<s32> rect(0, 0, bw, 30);
118                 rect = rect + v2s32(size.X/2-bw/2-(bw/2+5), size.Y/2-30/2+5 + msg_h/2);
119                 wchar_t* text = wgettext("Yes");
120                 Environment->addButton(rect, this, GUI_ID_YES,
121                                 text);
122                 delete[] text;
123         }
124         {
125                 core::rect<s32> rect(0, 0, bw, 30);
126                 rect = rect + v2s32(size.X/2-bw/2+(bw/2+5), size.Y/2-30/2+5 + msg_h/2);
127                 wchar_t* text = wgettext("No");
128                 Environment->addButton(rect, this, GUI_ID_NO,
129                         text);
130                 delete[] text;
131         }
132         changeCtype("C");
133 }
134
135 void GUIConfirmMenu::drawMenu()
136 {
137         gui::IGUISkin* skin = Environment->getSkin();
138         if (!skin)
139                 return;
140         video::IVideoDriver* driver = Environment->getVideoDriver();
141         
142         video::SColor bgcolor(140,0,0,0);
143         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
144
145         gui::IGUIElement::draw();
146 }
147
148 void GUIConfirmMenu::acceptInput(bool answer)
149 {
150         if(m_dest)
151                 m_dest->answer(answer);
152 }
153
154 bool GUIConfirmMenu::OnEvent(const SEvent& event)
155 {
156         if(event.EventType==EET_KEY_INPUT_EVENT)
157         {
158                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
159                 {
160                         acceptInput(false);
161                         quitMenu();
162                         return true;
163                 }
164                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
165                 {
166                         acceptInput(true);
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<<"GUIConfirmMenu: 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 GUI_ID_YES:
189                                 acceptInput(true);
190                                 quitMenu();
191                                 // quitMenu deallocates menu
192                                 return true;
193                         case GUI_ID_NO:
194                                 acceptInput(false);
195                                 quitMenu();
196                                 // quitMenu deallocates menu
197                                 return true;
198                         }
199                 }
200         }
201
202         return Parent ? Parent->OnEvent(event) : false;
203 }
204