ManaPlus
pincodedialog.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2018 The ManaPlus Developers
4  *
5  * This file is part of The ManaPlus Client.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
22 
23 #include "pincodemanager.h"
24 
25 #include "input/keyboardconfig.h"
26 
27 #include "gui/widgets/button.h"
28 #include "gui/widgets/label.h"
30 #include "gui/widgets/pincode.h"
31 
32 #include "utils/gettext.h"
33 
34 #include "gui/fonts/font.h"
35 
36 #include "debug.h"
37 
39 
40 PincodeDialog::PincodeDialog(const std::string &restrict title,
41  const std::string &restrict msg,
42  uint32_t seed,
43  Window *const parent) :
44  Window(title, Modal_true, parent, "pincode.xml"),
46  mPasswordField(new PasswordField(this, std::string())),
47  mPincode(new Pincode(this, mPasswordField)),
48  // TRANSLATORS: text dialog button
49  mOkButton(new Button(this, _("OK"), "OK", BUTTON_SKIN, this)),
50  mEnabledKeyboard(keyboard.isEnabled())
51 {
52  setStickyButtonLock(true);
53 
54  keyboard.setEnabled(false);
55 
56  Label *const textLabel = new Label(this, msg);
57  Button *const cancelButton = new Button(this,
58  // TRANSLATORS: text dialog button
59  _("Cancel"),
60  "CANCEL",
62  this);
63 
64  place(0, 0, textLabel, 4, 1);
65  place(0, 1, mPincode, 4, 1);
66  place(0, 2, mPasswordField, 4, 1);
67  place(2, 3, mOkButton, 1, 1);
68  place(3, 3, cancelButton, 1, 1);
69 
70  mPasswordField->setEnabled(false);
71  mPasswordField->setActionEventId("pincode");
73 
74  mPincode->shuffle(seed);
75  int width = getFont()->getWidth(title);
76  if (width < textLabel->getWidth())
77  width = textLabel->getWidth();
78  if (width < mPincode->getWidth())
79  width = mPincode->getWidth();
80  reflowLayout(CAST_S32(width + 20), 0);
81  updateButtons();
82 }
83 
85 {
87  if (getParent() != nullptr)
88  {
90  getParent()->moveToTop(this);
91  }
95 
96  instances++;
97 }
98 
100 {
101  instances--;
103 }
104 
106 {
107  const std::string &eventId = event.getId();
108  if (eventId == "pincode")
109  {
110  updateButtons();
111  return;
112  }
113  if (eventId == "CANCEL")
115 
117  close();
118 }
119 
120 const std::string &PincodeDialog::getMsg() const
121 {
122  return mPasswordField->getText();
123 }
124 
126 {
129  scheduleDelete();
130 }
131 
133 {
134  if (mPasswordField->getText().size() == 4)
135  {
136  mOkButton->setEnabled(true);
137  }
138  else
139  {
140  mOkButton->setEnabled(false);
141  }
142 }
Font * getFont() const
Definition: widget.cpp:330
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1379
#define _(s)
Definition: gettext.h:34
int getWidth() const
Definition: widget.h:220
Button * mOkButton
Definition: pincodedialog.h:63
void requestModalFocus()
Definition: widget.cpp:379
const bool Visible_true
Definition: visible.h:29
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
Definition: window.h:98
Definition: button.h:96
bool msg(InputEvent &event)
Definition: chat.cpp:38
const std::string & getActionEventId() const
Definition: widget.h:604
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
void action(const ActionEvent &event)
bool mEnabledKeyboard
Definition: pincodedialog.h:64
Pincode * mPincode
Definition: pincodedialog.h:62
void distributeActionEvent()
Definition: widget.cpp:492
const std::string & getMsg() const
#define new
Definition: debug_new.h:147
void shuffle(uint32_t seed) const
Definition: pincode.cpp:179
#define CAST_S32
Definition: cast.h:29
virtual void requestFocus()
Definition: widget.cpp:203
const std::string BUTTON_SKIN
Definition: button.h:88
Definition: label.h:87
void setEnabled(const bool enabled)
Definition: widget.h:351
virtual void scheduleDelete()
Definition: window.cpp:826
virtual void moveToTop(Widget *widget)
Definition: widget.h:866
Widget * getParent() const
Definition: widget.h:201
virtual void setVisible(Visible visible)
Definition: window.cpp:773
PincodeManager pincodeManager
void clearDialog(const PincodeDialog *const PincodeDialog)
int getWidth(const std::string &text) const
Definition: font.cpp:333
static int instances
Definition: pincodedialog.h:59
#define restrict
Definition: localconsts.h:156
KeyboardConfig keyboard
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:767
PincodeDialog(const std::string &title, const std::string &msg, uint32_t seed, Window *const parent)
PasswordField * mPasswordField
Definition: pincodedialog.h:61
void setLocationRelativeTo(const Widget *const widget)
Definition: window.cpp:508
const std::string & getText() const
Definition: textfield.h:223
void setEnabled(const bool flag)
void reflowLayout(int w, int h)
Definition: window.cpp:1391
void postInit()
Definition: window.cpp:248