ManaPlus
pincodedialog.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
23 
24 #include "pincodemanager.h"
25 
26 #include "gui/widgets/button.h"
27 #include "gui/widgets/label.h"
29 #include "gui/widgets/pincode.h"
30 
31 #include "utils/gettext.h"
32 
33 #include "gui/fonts/font.h"
34 
35 #include "debug.h"
36 
38 
39 PincodeDialog::PincodeDialog(const std::string &restrict title,
40  const std::string &restrict msg,
41  uint32_t seed,
42  Window *const parent) :
43  Window(title, Modal_true, parent, "pincode.xml"),
45  mPasswordField(new PasswordField(this, std::string())),
46  mPincode(new Pincode(this, mPasswordField)),
47  // TRANSLATORS: text dialog button
48  mOkButton(new Button(this, _("OK"), "OK", BUTTON_SKIN, this))
49 {
50  setStickyButtonLock(true);
51 
52  Label *const textLabel = new Label(this, msg);
53  Button *const cancelButton = new Button(this,
54  // TRANSLATORS: text dialog button
55  _("Cancel"),
56  "CANCEL",
58  this);
59 
60  place(0, 0, textLabel, 4, 1);
61  place(0, 1, mPincode, 4, 1);
62  place(0, 2, mPasswordField, 4, 1);
63  place(2, 3, mOkButton, 1, 1);
64  place(3, 3, cancelButton, 1, 1);
65 
66  mPasswordField->setEnabled(false);
67  mPasswordField->setActionEventId("pincode");
69 
70  mPincode->shuffle(seed);
71  int width = getFont()->getWidth(title);
72  if (width < textLabel->getWidth())
73  width = textLabel->getWidth();
74  if (width < mPincode->getWidth())
75  width = mPincode->getWidth();
76  reflowLayout(CAST_S32(width + 20), 0);
77  updateButtons();
78 }
79 
81 {
83  if (getParent() != nullptr)
84  {
86  getParent()->moveToTop(this);
87  }
91 
92  instances++;
93 }
94 
96 {
97  instances--;
99 }
100 
102 {
103  const std::string &eventId = event.getId();
104  if (eventId == "pincode")
105  {
106  updateButtons();
107  return;
108  }
109  if (eventId == "CANCEL")
111 
113  close();
114 }
115 
116 const std::string &PincodeDialog::getMsg() const
117 {
118  return mPasswordField->getText();
119 }
120 
122 {
124  scheduleDelete();
125 }
126 
128 {
129  if (mPasswordField->getText().size() == 4)
130  {
131  mOkButton->setEnabled(true);
132  }
133  else
134  {
135  mOkButton->setEnabled(false);
136  }
137 }
const std::string BUTTON_SKIN
Definition: button.h:89
#define CAST_S32
Definition: cast.h:30
Definition: button.h:102
int getWidth(const std::string &text) const
Definition: font.cpp:334
Definition: label.h:91
const std::string & getMsg() const
Button * mOkButton
Definition: pincodedialog.h:64
static int instances
Definition: pincodedialog.h:60
Pincode * mPincode
Definition: pincodedialog.h:63
PasswordField * mPasswordField
Definition: pincodedialog.h:62
void action(const ActionEvent &event)
PincodeDialog(const std::string &title, const std::string &msg, uint32_t seed, Window *const parent)
void clearDialog(const PincodeDialog *const PincodeDialog)
void shuffle(uint32_t seed) const
Definition: pincode.cpp:180
const std::string & getText() const
Definition: textfield.h:224
void distributeActionEvent()
Definition: widget.cpp:493
void setEnabled(const bool enabled)
Definition: widget.h:352
void requestModalFocus()
Definition: widget.cpp:380
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
virtual void moveToTop(Widget *widget)
Definition: widget.h:867
virtual void requestFocus()
Definition: widget.cpp:204
Font * getFont() const
Definition: widget.cpp:331
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
Widget * getParent() const
Definition: widget.h:202
const std::string & getActionEventId() const
Definition: widget.h:605
int getWidth() const
Definition: widget.h:221
Definition: window.h:102
virtual void setVisible(Visible visible)
Definition: window.cpp:778
void reflowLayout(int w, int h)
Definition: window.cpp:1396
void postInit()
Definition: window.cpp:249
virtual void scheduleDelete()
Definition: window.cpp:831
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:772
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1384
void setLocationRelativeTo(const Widget *const widget)
Definition: window.cpp:509
#define new
Definition: debug_new.h:147
#define _(s)
Definition: gettext.h:35
#define restrict
Definition: localconsts.h:165
const bool Modal_true
Definition: modal.h:30
bool msg(InputEvent &event)
Definition: chat.cpp:39
PincodeManager pincodeManager
const bool Visible_true
Definition: visible.h:30