Add a MSVC / Windows compatible snprintf function (#7353)
[oweals/minetest.git] / src / gui / guiConfirmRegistration.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 srifqi, Muhammad Rifqi Priyo Susanto
4                 <muhammadrifqipriyosusanto@gmail.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "guiConfirmRegistration.h"
22 #include "client.h"
23 #include <IGUICheckBox.h>
24 #include <IGUIButton.h>
25 #include <IGUIStaticText.h>
26 #include <IGUIFont.h>
27 #include "intlGUIEditBox.h"
28 #include "porting.h"
29
30 #include "gettext.h"
31
32 // Continuing from guiPasswordChange.cpp
33 const int ID_confirmPassword = 262;
34 const int ID_confirm = 263;
35 const int ID_message = 264;
36 const int ID_cancel = 265;
37
38 GUIConfirmRegistration::GUIConfirmRegistration(gui::IGUIEnvironment *env,
39                 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr, Client *client,
40                 const std::string &playername, const std::string &password,
41                 const std::string &address, bool *aborted) :
42                 GUIModalMenu(env, parent, id, menumgr),
43                 m_client(client), m_playername(playername), m_password(password),
44                 m_address(address), m_aborted(aborted)
45 {
46 }
47
48 GUIConfirmRegistration::~GUIConfirmRegistration()
49 {
50         removeChildren();
51 }
52
53 void GUIConfirmRegistration::removeChildren()
54 {
55         const core::list<gui::IGUIElement *> &children = getChildren();
56         core::list<gui::IGUIElement *> children_copy;
57         for (gui::IGUIElement *i : children)
58                 children_copy.push_back(i);
59         for (gui::IGUIElement *i : children_copy)
60                 i->remove();
61 }
62 void GUIConfirmRegistration::regenerateGui(v2u32 screensize)
63 {
64         acceptInput();
65         removeChildren();
66
67         /*
68                 Calculate new sizes and positions
69         */
70         core::rect<s32> rect(screensize.X / 2 - 600 / 2, screensize.Y / 2 - 360 / 2,
71                         screensize.X / 2 + 600 / 2, screensize.Y / 2 + 360 / 2);
72
73         DesiredRect = rect;
74         recalculateAbsolutePosition(false);
75
76         v2s32 size = rect.getSize();
77         v2s32 topleft_client(0, 0);
78
79         const wchar_t *text;
80
81         /*
82                 Add stuff
83         */
84         s32 ypos = 30;
85         {
86                 std::string address = m_address;
87                 if (address.empty())
88                         address = "localhost";
89                 core::rect<s32> rect2(0, 0, 540, 180);
90                 rect2 += topleft_client + v2s32(30, ypos);
91                 static const std::string info_text_template = strgettext(
92                                 "You are about to join the server at %1$s with the "
93                                 "name \"%2$s\" for the first time. If you proceed, a "
94                                 "new account using your credentials will be created "
95                                 "on this server.\n"
96                                 "Please retype your password and click Register and "
97                                 "Join to confirm account creation or click Cancel to "
98                                 "abort.");
99                 char info_text_buf[1024];
100                 porting::mt_snprintf(info_text_buf, sizeof(info_text_buf),
101                                 info_text_template.c_str(), address.c_str(),
102                                 m_playername.c_str());
103
104                 wchar_t *info_text_buf_wide = utf8_to_wide_c(info_text_buf);
105                 gui::IGUIEditBox *e = new gui::intlGUIEditBox(info_text_buf_wide, true,
106                                 Environment, this, ID_message, rect2, false, true);
107                 delete[] info_text_buf_wide;
108                 e->drop();
109                 e->setMultiLine(true);
110                 e->setWordWrap(true);
111                 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
112         }
113
114         ypos += 210;
115         {
116                 core::rect<s32> rect2(0, 0, 540, 30);
117                 rect2 += topleft_client + v2s32(30, ypos);
118                 gui::IGUIEditBox *e = Environment->addEditBox(m_pass_confirm.c_str(),
119                                 rect2, true, this, ID_confirmPassword);
120                 e->setPasswordBox(true);
121         }
122
123         ypos += 60;
124         {
125                 core::rect<s32> rect2(0, 0, 230, 35);
126                 rect2 = rect2 + v2s32(size.X / 2 - 220, ypos);
127                 text = wgettext("Register and Join");
128                 Environment->addButton(rect2, this, ID_confirm, text);
129                 delete[] text;
130         }
131         {
132                 core::rect<s32> rect2(0, 0, 120, 35);
133                 rect2 = rect2 + v2s32(size.X / 2 + 70, ypos);
134                 text = wgettext("Cancel");
135                 Environment->addButton(rect2, this, ID_cancel, text);
136                 delete[] text;
137         }
138         {
139                 core::rect<s32> rect2(0, 0, 200, 20);
140                 rect2 += topleft_client + v2s32(30, ypos - 40);
141                 text = wgettext("Passwords do not match!");
142                 IGUIElement *e = Environment->addStaticText(
143                                 text, rect2, false, true, this, ID_message);
144                 e->setVisible(false);
145                 delete[] text;
146         }
147 }
148
149 void GUIConfirmRegistration::drawMenu()
150 {
151         gui::IGUISkin *skin = Environment->getSkin();
152         if (!skin)
153                 return;
154         video::IVideoDriver *driver = Environment->getVideoDriver();
155
156         video::SColor bgcolor(140, 0, 0, 0);
157         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
158
159         gui::IGUIElement::draw();
160 }
161
162 void GUIConfirmRegistration::closeMenu(bool goNext)
163 {
164         if (goNext) {
165                 m_client->confirmRegistration();
166         } else {
167                 *m_aborted = true;
168                 infostream << "Connect aborted [Escape]" << std::endl;
169         }
170         quitMenu();
171 }
172
173 void GUIConfirmRegistration::acceptInput()
174 {
175         gui::IGUIElement *e;
176         e = getElementFromId(ID_confirmPassword);
177         if (e)
178                 m_pass_confirm = e->getText();
179 }
180
181 bool GUIConfirmRegistration::processInput()
182 {
183         std::wstring m_password_ws = narrow_to_wide(m_password);
184         if (m_password_ws != m_pass_confirm) {
185                 gui::IGUIElement *e = getElementFromId(ID_message);
186                 if (e)
187                         e->setVisible(true);
188                 return false;
189         }
190         return true;
191 }
192
193 bool GUIConfirmRegistration::OnEvent(const SEvent &event)
194 {
195         if (event.EventType == EET_KEY_INPUT_EVENT) {
196                 if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
197                         closeMenu(false);
198                         return true;
199                 }
200                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
201                         acceptInput();
202                         if (processInput())
203                                 closeMenu(true);
204                         return true;
205                 }
206         }
207
208         if (event.EventType != EET_GUI_EVENT)
209                 return Parent ? Parent->OnEvent(event) : false;
210
211         if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST && isVisible()) {
212                 if (!canTakeFocus(event.GUIEvent.Element)) {
213                         dstream << "GUIConfirmRegistration: Not allowing focus "
214                                    "change."
215                                 << std::endl;
216                         // Returning true disables focus change
217                         return true;
218                 }
219         } else if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
220                 switch (event.GUIEvent.Caller->getID()) {
221                 case ID_confirm:
222                         acceptInput();
223                         if (processInput())
224                                 closeMenu(true);
225                         return true;
226                 case ID_cancel:
227                         closeMenu(false);
228                         return true;
229                 }
230         } else if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
231                 switch (event.GUIEvent.Caller->getID()) {
232                 case ID_confirmPassword:
233                         acceptInput();
234                         if (processInput())
235                                 closeMenu(true);
236                         return true;
237                 }
238         }
239
240         return false;
241 }