ManaPlus
textdialog.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "gui/windows/textdialog.h"
25 
26 #include "input/keyboardconfig.h"
27 
28 #include "gui/widgets/button.h"
29 #include "gui/widgets/label.h"
31 
32 #include "utils/gettext.h"
33 
34 #include "gui/fonts/font.h"
35 
36 #include "debug.h"
37 
38 int TextDialog::instances = 0;
39 
40 namespace
41 {
42  const std::string emptyStr;
43 } // namespace
44 
45 TextDialog::TextDialog(const std::string &restrict title,
46  const std::string &restrict msg,
47  Window *const parent,
48  const bool isPassword) :
49  Window(title, Modal_true, parent, "textdialog.xml"),
51  mTextField(nullptr),
52  mPasswordField(nullptr),
53  // TRANSLATORS: text dialog button
54  mOkButton(new Button(this, _("OK"), "OK", BUTTON_SKIN, this)),
55  mEnabledKeyboard(keyboard.isEnabled())
56 {
57  keyboard.setEnabled(false);
58 
59  Label *const textLabel = new Label(this, msg);
60  Button *const cancelButton = new Button(this,
61  // TRANSLATORS: text dialog button
62  _("Cancel"),
63  "CANCEL",
65  this);
66 
67  place(0, 0, textLabel, 4, 1);
68  if (isPassword)
69  {
70  mPasswordField = new PasswordField(this, std::string());
71  place(0, 1, mPasswordField, 4, 1);
72  }
73  else
74  {
75  mTextField = new TextField(this,
76  std::string(),
78  nullptr,
79  std::string(),
80  false);
81  place(0, 1, mTextField, 4, 1);
82  }
83  place(2, 2, mOkButton, 1, 1);
84  place(3, 2, cancelButton, 1, 1);
85 
86  int width = getFont()->getWidth(title);
87  if (width < textLabel->getWidth())
88  width = textLabel->getWidth();
89  reflowLayout(CAST_S32(width + 20), 0);
90 }
91 
93 {
95  if (getParent() != nullptr)
96  {
98  getParent()->moveToTop(this);
99  }
102  if (mPasswordField != nullptr)
104  else if (mTextField != nullptr)
106 
107  instances++;
108 }
109 
111 {
112  instances--;
113 }
114 
115 void TextDialog::action(const ActionEvent &event)
116 {
117  if (event.getId() == "CANCEL")
119 
121  close();
122 }
123 
124 const std::string &TextDialog::getText() const
125 {
126  if (mTextField != nullptr)
127  return mTextField->getText();
128  else if (mPasswordField != nullptr)
129  return mPasswordField->getText();
130  return emptyStr;
131 }
132 
133 void TextDialog::setText(const std::string &text)
134 {
135  if (mTextField != nullptr)
136  mTextField->setText(text);
137  else if (mPasswordField != nullptr)
138  mPasswordField->setText(text);
139 }
140 
142 {
144  scheduleDelete();
145 }
const std::string BUTTON_SKIN
Definition: button.h:89
#define CAST_S32
Definition: cast.h:30
const std::string & getId() const
Definition: actionevent.h:122
Definition: button.h:102
int getWidth(const std::string &text) const
Definition: font.cpp:334
void setEnabled(const bool flag)
Definition: label.h:91
static int instances
Definition: textdialog.h:78
const std::string & getText() const
Definition: textdialog.cpp:124
void postInit()
Definition: textdialog.cpp:92
TextDialog(const std::string &title, const std::string &msg, Window *const parent, const bool isPassword)
Definition: textdialog.cpp:45
void action(const ActionEvent &event)
Definition: textdialog.cpp:115
PasswordField * mPasswordField
Definition: textdialog.h:81
void close()
Definition: textdialog.cpp:141
bool mEnabledKeyboard
Definition: textdialog.h:83
void setText(const std::string &text)
Definition: textdialog.cpp:133
Button * mOkButton
Definition: textdialog.h:82
TextField * mTextField
Definition: textdialog.h:80
const std::string & getText() const
Definition: textfield.h:224
void setText(const std::string &text)
Definition: textfield.cpp:803
void distributeActionEvent()
Definition: widget.cpp:493
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
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
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
KeyboardConfig keyboard
#define restrict
Definition: localconsts.h:165
#define nullptr
Definition: localconsts.h:45
const bool LoseFocusOnTab_true
const bool Modal_true
Definition: modal.h:30
bool msg(InputEvent &event)
Definition: chat.cpp:39
const bool Visible_true
Definition: visible.h:30