Use a more standard order of yes/no/cancel/whatever buttons in dialogs
[oweals/minetest.git] / src / guiConfirmMenu.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2012 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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         /*
97                 Add stuff
98         */
99         {
100                 core::rect<s32> rect(0, 0, 300, 20);
101                 rect += v2s32(size.X/2-300/2, size.Y/2-30/2-25);
102                 Environment->addStaticText(m_message_text.c_str(),
103                         rect, false, true, this, -1);
104         }
105         changeCtype("");
106         int bw = 100;
107         {
108                 core::rect<s32> rect(0, 0, bw, 30);
109                 rect = rect + v2s32(size.X/2-bw/2-(bw/2+5), size.Y/2-30/2+25);
110                 Environment->addButton(rect, this, GUI_ID_YES,
111                         wgettext("Yes"));
112         }
113         {
114                 core::rect<s32> rect(0, 0, bw, 30);
115                 rect = rect + v2s32(size.X/2-bw/2+(bw/2+5), size.Y/2-30/2+25);
116                 Environment->addButton(rect, this, GUI_ID_NO,
117                         wgettext("No"));
118         }
119         changeCtype("C");
120 }
121
122 void GUIConfirmMenu::drawMenu()
123 {
124         gui::IGUISkin* skin = Environment->getSkin();
125         if (!skin)
126                 return;
127         video::IVideoDriver* driver = Environment->getVideoDriver();
128         
129         video::SColor bgcolor(140,0,0,0);
130         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
131
132         gui::IGUIElement::draw();
133 }
134
135 void GUIConfirmMenu::acceptInput(bool answer)
136 {
137         if(m_dest)
138                 m_dest->answer(answer);
139 }
140
141 bool GUIConfirmMenu::OnEvent(const SEvent& event)
142 {
143         if(event.EventType==EET_KEY_INPUT_EVENT)
144         {
145                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
146                 {
147                         acceptInput(false);
148                         quitMenu();
149                         return true;
150                 }
151                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
152                 {
153                         acceptInput(true);
154                         quitMenu();
155                         return true;
156                 }
157         }
158         if(event.EventType==EET_GUI_EVENT)
159         {
160                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
161                                 && isVisible())
162                 {
163                         if(!canTakeFocus(event.GUIEvent.Element))
164                         {
165                                 dstream<<"GUIConfirmMenu: Not allowing focus change."
166                                                 <<std::endl;
167                                 // Returning true disables focus change
168                                 return true;
169                         }
170                 }
171                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
172                 {
173                         switch(event.GUIEvent.Caller->getID())
174                         {
175                         case GUI_ID_YES:
176                                 acceptInput(true);
177                                 quitMenu();
178                                 // quitMenu deallocates menu
179                                 return true;
180                         case GUI_ID_NO:
181                                 acceptInput(false);
182                                 quitMenu();
183                                 // quitMenu deallocates menu
184                                 return true;
185                         }
186                 }
187         }
188
189         return Parent ? Parent->OnEvent(event) : false;
190 }
191