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 "gui/widgets/button.h"
26 #include "gui/widgets/label.h"
28 #include "gui/widgets/pincode.h"
29 
30 #include "utils/gettext.h"
31 
32 #include "gui/fonts/font.h"
33 
34 #include "debug.h"
35 
37 
38 PincodeDialog::PincodeDialog(const std::string &restrict title,
39  const std::string &restrict msg,
40  uint32_t seed,
41  Window *const parent) :
42  Window(title, Modal_true, parent, "pincode.xml"),
44  mPasswordField(new PasswordField(this, std::string())),
45  mPincode(new Pincode(this, mPasswordField)),
46  // TRANSLATORS: text dialog button
47  mOkButton(new Button(this, _("OK"), "OK", BUTTON_SKIN, this))
48 {
49  setStickyButtonLock(true);
50 
51  Label *const textLabel = new Label(this, msg);
52  Button *const cancelButton = new Button(this,
53  // TRANSLATORS: text dialog button
54  _("Cancel"),
55  "CANCEL",
57  this);
58 
59  place(0, 0, textLabel, 4, 1);
60  place(0, 1, mPincode, 4, 1);
61  place(0, 2, mPasswordField, 4, 1);
62  place(2, 3, mOkButton, 1, 1);
63  place(3, 3, cancelButton, 1, 1);
64 
65  mPasswordField->setEnabled(false);
66  mPasswordField->setActionEventId("pincode");
68 
69  mPincode->shuffle(seed);
70  int width = getFont()->getWidth(title);
71  if (width < textLabel->getWidth())
72  width = textLabel->getWidth();
73  if (width < mPincode->getWidth())
74  width = mPincode->getWidth();
75  reflowLayout(CAST_S32(width + 20), 0);
76  updateButtons();
77 }
78 
80 {
82  if (getParent() != nullptr)
83  {
85  getParent()->moveToTop(this);
86  }
90 
91  instances++;
92 }
93 
95 {
96  instances--;
98 }
99 
101 {
102  const std::string &eventId = event.getId();
103  if (eventId == "pincode")
104  {
105  updateButtons();
106  return;
107  }
108  if (eventId == "CANCEL")
110 
112  close();
113 }
114 
115 const std::string &PincodeDialog::getMsg() const
116 {
117  return mPasswordField->getText();
118 }
119 
121 {
123  scheduleDelete();
124 }
125 
127 {
128  if (mPasswordField->getText().size() == 4)
129  {
130  mOkButton->setEnabled(true);
131  }
132  else
133  {
134  mOkButton->setEnabled(false);
135  }
136 }
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)
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
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 reflowLayout(int w, int h)
Definition: window.cpp:1391
void postInit()
Definition: window.cpp:248