ManaPlus
confirmdialog.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 
25 
26 #include "soundmanager.h"
27 
28 #include "gui/fonts/font.h"
29 
30 #include "gui/widgets/button.h"
31 #include "gui/widgets/textbox.h"
32 
33 #include "utils/gettext.h"
34 
35 #include "debug.h"
36 
37 ConfirmDialog::ConfirmDialog(const std::string &restrict title,
38  const std::string &restrict msg,
39  const std::string &restrict soundEvent,
40  const bool ignore,
41  const Modal modal,
42  Window *const parent) :
43  Window(title, modal, parent, "confirm.xml"),
45  // TRANSLATORS: confirm dialog button
46  mYesMsg(_("Yes")),
47  // TRANSLATORS: confirm dialog button
48  mNoMsg(_("No")),
49  mTextBox(new TextBox(this)),
50  mIgnore(ignore)
51 {
52  mTextBox->setEditable(false);
55  soundManager.playGuiSound(soundEvent);
56 }
57 
58 ConfirmDialog::ConfirmDialog(const std::string &restrict title,
59  const std::string &restrict msg,
60  const std::string &restrict yesMsg,
61  const std::string &restrict noMsg,
62  const std::string &restrict soundEvent,
63  const bool ignore,
64  const Modal modal,
65  Window *const parent) :
66  Window(title, modal, parent, "confirm.xml"),
68  mYesMsg(yesMsg),
69  mNoMsg(noMsg),
70  mTextBox(new TextBox(this)),
71  mIgnore(ignore)
72 {
73  mTextBox->setEditable(false);
76  soundManager.playGuiSound(soundEvent);
77 }
78 
80 {
82  Button *const yesButton = new Button(this,
83  mYesMsg,
84  "yes",
86  this);
87  Button *const noButton = new Button(this,
88  mNoMsg,
89  "no",
91  this);
92  Button *const ignoreButton = mIgnore ? new Button(
93  // TRANSLATORS: confirm dialog button
94  this, _("Ignore"), "ignore", BUTTON_SKIN, this) : nullptr;
95 
96  const int numRows = mTextBox->getNumberOfRows();
97  int inWidth = yesButton->getWidth() + noButton->getWidth() +
98  (2 * mPadding);
99 
100  if (ignoreButton != nullptr)
101  inWidth += ignoreButton->getWidth();
102 
103  const int fontHeight = getFont()->getHeight();
104  const int height = numRows * fontHeight;
105  int width = getFont()->getWidth(getCaption()) + mCaptionOffsetX;
106 
107  if (width < mTextBox->getMinWidth())
108  width = mTextBox->getMinWidth();
109  if (width < inWidth)
110  width = inWidth;
111 
112  int windowWidth = width;
113  if (windowWidth < mTextBox->getMinWidth() + fontHeight)
114  {
115  windowWidth = mTextBox->getMinWidth() + fontHeight;
116  }
117  setContentSize(windowWidth,
118  height + fontHeight + noButton->getHeight());
120 
121  const int buttonPadding = getOption("buttonPadding", 8);
122  yesButton->setPosition((width - inWidth) / 2, height + buttonPadding);
123  noButton->setPosition(yesButton->getX() + yesButton->getWidth()
124  + (2 * mPadding), height + buttonPadding);
125  if (ignoreButton != nullptr)
126  {
127  ignoreButton->setPosition(noButton->getX() + noButton->getWidth()
128  + (2 * mPadding), height + buttonPadding);
129  }
130 
131  add(mTextBox);
132  add(yesButton);
133  add(noButton);
134 
135  if (mIgnore && (ignoreButton != nullptr))
136  add(ignoreButton);
137 
138  if (getParent() != nullptr)
139  {
140  center();
141  getParent()->moveToTop(this);
142  }
144  yesButton->requestFocus();
145 }
146 
148 {
149  setActionEventId(event.getId());
151  scheduleDelete();
152 }
const std::string BUTTON_SKIN
Definition: button.h:89
const std::string & getId() const
Definition: actionevent.h:122
virtual void add(Widget *const widget)
Definition: button.h:102
ConfirmDialog(const std::string &title, const std::string &msg, const std::string &soundEvent, const bool ignore, const Modal modal, Window *const parent)
std::string mYesMsg
Definition: confirmdialog.h:80
TextBox * mTextBox
Definition: confirmdialog.h:82
void action(const ActionEvent &event)
std::string mNoMsg
Definition: confirmdialog.h:81
int getHeight() const
Definition: font.cpp:362
int getWidth(const std::string &text) const
Definition: font.cpp:334
void playGuiSound(const std::string &name)
void setEditable(const bool editable)
Definition: textbox.cpp:650
void setOpaque(const Opaque opaque)
Definition: textbox.h:273
unsigned int getNumberOfRows() const
Definition: textbox.h:162
int getMinWidth() const
Definition: textbox.h:108
void setTextWrapped(const std::string &text, const int minDimension)
Definition: textbox.cpp:109
void distributeActionEvent()
Definition: widget.cpp:493
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 setPosition(const int x, const int y)
Definition: widget.cpp:161
Widget * getParent() const
Definition: widget.h:202
int getX() const
Definition: widget.h:269
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
Definition: window.h:102
void center()
Definition: window.cpp:1417
virtual void setVisible(Visible visible)
Definition: window.cpp:778
void setContentSize(int width, int height)
Definition: window.cpp:492
int getOption(const std::string &name, const int def) const
Definition: window.cpp:1454
int getMinWidth() const
Definition: window.h:205
void postInit()
Definition: window.cpp:249
virtual void scheduleDelete()
Definition: window.cpp:831
int mPadding
Definition: window.h:618
const std::string & getCaption() const
Definition: window.h:540
int mCaptionOffsetX
Definition: window.h:654
#define new
Definition: debug_new.h:147
#define _(s)
Definition: gettext.h:35
#define restrict
Definition: localconsts.h:165
bool Modal
Definition: modal.h:30
bool msg(InputEvent &event)
Definition: chat.cpp:39
const bool Opaque_false
Definition: opaque.h:30
SoundManager soundManager
const bool Visible_true
Definition: visible.h:30