Add cancel button to password change menu. (#5720)
[oweals/minetest.git] / src / guiPasswordChange.cpp
1 /*
2 Part of Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013 Ciaran Gultnieks <ciaran@ciarang.com>
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include "guiPasswordChange.h"
20 #include "debug.h"
21 #include "serialization.h"
22 #include <string>
23 #include <IGUICheckBox.h>
24 #include <IGUIEditBox.h>
25 #include <IGUIButton.h>
26 #include <IGUIStaticText.h>
27 #include <IGUIFont.h>
28
29 #include "gettext.h"
30
31 const int ID_oldPassword = 256;
32 const int ID_newPassword1 = 257;
33 const int ID_newPassword2 = 258;
34 const int ID_change = 259;
35 const int ID_message = 260;
36 const int ID_cancel = 261;
37
38 GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
39                 gui::IGUIElement* parent, s32 id,
40                 IMenuManager *menumgr,
41                 Client* client
42 ):
43         GUIModalMenu(env, parent, id, menumgr),
44         m_client(client)
45 {
46 }
47
48 GUIPasswordChange::~GUIPasswordChange()
49 {
50         removeChildren();
51 }
52
53 void GUIPasswordChange::removeChildren()
54 {
55         {
56                 gui::IGUIElement *e = getElementFromId(ID_oldPassword);
57                 if (e != NULL)
58                         e->remove();
59         }
60         {
61                 gui::IGUIElement *e = getElementFromId(ID_newPassword1);
62                 if (e != NULL)
63                         e->remove();
64         }
65         {
66                 gui::IGUIElement *e = getElementFromId(ID_newPassword2);
67                 if (e != NULL)
68                         e->remove();
69         }
70         {
71                 gui::IGUIElement *e = getElementFromId(ID_change);
72                 if (e != NULL)
73                         e->remove();
74         }
75         {
76                 gui::IGUIElement *e = getElementFromId(ID_cancel);
77                 if (e != NULL)
78                         e->remove();
79         }
80 }
81
82 void GUIPasswordChange::regenerateGui(v2u32 screensize)
83 {
84         /*
85                 Remove stuff
86         */
87         removeChildren();
88
89         /*
90                 Calculate new sizes and positions
91         */
92         core::rect<s32> rect(
93                         screensize.X/2 - 580/2,
94                         screensize.Y/2 - 300/2,
95                         screensize.X/2 + 580/2,
96                         screensize.Y/2 + 300/2
97         );
98
99         DesiredRect = rect;
100         recalculateAbsolutePosition(false);
101
102         v2s32 size = rect.getSize();
103         v2s32 topleft_client(40, 0);
104
105         const wchar_t *text;
106
107         /*
108                 Add stuff
109         */
110         s32 ypos = 50;
111         {
112                 core::rect<s32> rect(0, 0, 150, 20);
113                 rect += topleft_client + v2s32(25, ypos + 6);
114                 text = wgettext("Old Password");
115                 Environment->addStaticText(text, rect, false, true, this, -1);
116                 delete[] text;
117         }
118         {
119                 core::rect<s32> rect(0, 0, 230, 30);
120                 rect += topleft_client + v2s32(160, ypos);
121                 gui::IGUIEditBox *e = Environment->addEditBox(
122                                 L"", rect, true, this, ID_oldPassword);
123                 Environment->setFocus(e);
124                 e->setPasswordBox(true);
125         }
126         ypos += 50;
127         {
128                 core::rect<s32> rect(0, 0, 150, 20);
129                 rect += topleft_client + v2s32(25, ypos + 6);
130                 text = wgettext("New Password");
131                 Environment->addStaticText(text, rect, false, true, this, -1);
132                 delete[] text;
133         }
134         {
135                 core::rect<s32> rect(0, 0, 230, 30);
136                 rect += topleft_client + v2s32(160, ypos);
137                 gui::IGUIEditBox *e = Environment->addEditBox(
138                                 L"", rect, true, this, ID_newPassword1);
139                 e->setPasswordBox(true);
140         }
141         ypos += 50;
142         {
143                 core::rect<s32> rect(0, 0, 150, 20);
144                 rect += topleft_client + v2s32(25, ypos + 6);
145                 text = wgettext("Confirm Password");
146                 Environment->addStaticText(text, rect, false, true, this, -1);
147                 delete[] text;
148         }
149         {
150                 core::rect<s32> rect(0, 0, 230, 30);
151                 rect += topleft_client + v2s32(160, ypos);
152                 gui::IGUIEditBox *e = Environment->addEditBox(
153                                 L"", rect, true, this, ID_newPassword2);
154                 e->setPasswordBox(true);
155         }
156
157         ypos += 50;
158         {
159                 core::rect<s32> rect(0, 0, 100, 30);
160                 rect = rect + v2s32(size.X / 4 + 56, ypos);
161                 text = wgettext("Change");
162                 Environment->addButton(rect, this, ID_change, text);
163                 delete[] text;
164         }
165         {
166                 core::rect<s32> rect(0, 0, 100, 30);
167                 rect = rect + v2s32(size.X / 4 + 185, ypos);
168                 text = wgettext("Cancel");
169                 Environment->addButton(rect, this, ID_cancel, text);
170                 delete[] text;
171         }
172
173         ypos += 50;
174         {
175                 core::rect<s32> rect(0, 0, 300, 20);
176                 rect += topleft_client + v2s32(35, ypos);
177                 text = wgettext("Passwords do not match!");
178                 IGUIElement *e =
179                         Environment->addStaticText(
180                         text, rect, false, true, this, ID_message);
181                 e->setVisible(false);
182                 delete[] text;
183         }
184 }
185
186 void GUIPasswordChange::drawMenu()
187 {
188         gui::IGUISkin *skin = Environment->getSkin();
189         if (!skin)
190                 return;
191         video::IVideoDriver *driver = Environment->getVideoDriver();
192
193         video::SColor bgcolor(140, 0, 0, 0);
194         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
195
196         gui::IGUIElement::draw();
197 }
198
199 bool GUIPasswordChange::acceptInput()
200 {
201         std::wstring oldpass;
202         std::wstring newpass;
203         gui::IGUIElement *e;
204         e = getElementFromId(ID_oldPassword);
205         if (e != NULL)
206                 oldpass = e->getText();
207         e = getElementFromId(ID_newPassword1);
208         if (e != NULL)
209                 newpass = e->getText();
210         e = getElementFromId(ID_newPassword2);
211         if (e != NULL && newpass != e->getText()) {
212                 e = getElementFromId(ID_message);
213                 if (e != NULL)
214                         e->setVisible(true);
215                 return false;
216         }
217         m_client->sendChangePassword(wide_to_utf8(oldpass), wide_to_utf8(newpass));
218         return true;
219 }
220
221 bool GUIPasswordChange::OnEvent(const SEvent &event)
222 {
223         if (event.EventType == EET_KEY_INPUT_EVENT) {
224                 if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
225                         quitMenu();
226                         return true;
227                 }
228                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
229                         if (acceptInput())
230                                 quitMenu();
231                         return true;
232                 }
233         }
234         if (event.EventType == EET_GUI_EVENT) {
235                 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST &&
236                                 isVisible()) {
237                         if (!canTakeFocus(event.GUIEvent.Element)) {
238                                 dstream << "GUIPasswordChange: Not allowing focus change."
239                                         << std::endl;
240                                 // Returning true disables focus change
241                                 return true;
242                         }
243                 }
244                 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
245                         switch (event.GUIEvent.Caller->getID()) {
246                         case ID_change:
247                                 if (acceptInput())
248                                         quitMenu();
249                                 return true;
250                         case ID_cancel:
251                                 quitMenu();
252                                 return true;
253                         }
254                 }
255                 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
256                         switch (event.GUIEvent.Caller->getID()) {
257                         case ID_oldPassword:
258                         case ID_newPassword1:
259                         case ID_newPassword2:
260                                 if (acceptInput())
261                                         quitMenu();
262                                 return true;
263                         }
264                 }
265         }
266
267         return Parent ? Parent->OnEvent(event) : false;
268 }