ManaPlus
okdialog.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/okdialog.h"
25 
26 #include "soundmanager.h"
27 
28 #include "const/sound.h"
29 
30 #include "gui/widgets/button.h"
31 #include "gui/widgets/textbox.h"
32 
33 #include "gui/fonts/font.h"
34 
35 #include "debug.h"
36 
37 OkDialog::OkDialog(const std::string &restrict title,
38  const std::string &restrict msg,
39  const std::string &restrict button,
40  const DialogTypeT soundEvent,
41  const Modal modal,
42  const ShowCenter showCenter,
43  Window *const parent,
44  const int minWidth) :
45  Window(title, modal, parent, "ok.xml"),
47  mTextBox(new TextBox(this))
48 {
49  mTextBox->setEditable(false);
51  mTextBox->setTextWrapped(msg, minWidth);
52 
53  // TRANSLATORS: ok dialog button
54  Button *const okButton = new Button(this,
55  button,
56  "ok",
58  this);
59 
60  int width = getFont()->getWidth(title);
61  if (width < mTextBox->getMinWidth())
62  width = mTextBox->getMinWidth();
63  if (width < okButton->getWidth())
64  width = okButton->getWidth();
65 
66  if (mTextBox->getWidth() > width)
67  width = mTextBox->getWidth();
68  if (okButton->getWidth() > width)
69  width = okButton->getWidth();
70  setContentSize(width, mTextBox->getHeight() + okButton->getHeight()
71  + getOption("buttonPadding", 8));
72  mTextBox->setPosition((width - mTextBox->getWidth()) / 2, 0);
73  okButton->setPosition((width - okButton->getWidth()) / 2,
74  mTextBox->getHeight() + getOption("buttonPadding", 8));
75 
76  // +++ virtual method call
77  add(mTextBox);
78  add(okButton);
79 
80  if (showCenter == ShowCenter_true)
81  center();
82  else
84  // +++ virtual method call
86  okButton->requestFocus();
87 
88  if (soundEvent == DialogType::OK)
90  else if (soundEvent == DialogType::ERROR)
92 }
93 
94 void OkDialog::action(const ActionEvent &event)
95 {
96  setActionEventId(event.getId());
99 }
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
int getWidth(const std::string &text) const
Definition: font.cpp:334
void action(const ActionEvent &event)
Definition: okdialog.cpp:94
TextBox * mTextBox
Definition: okdialog.h:70
OkDialog(const std::string &title, const std::string &msg, const std::string &button, const DialogTypeT soundEvent, const Modal modal, const ShowCenter showCenter, Window *const parent, const int minWidth)
Definition: okdialog.cpp:37
void playGuiSound(const std::string &name)
void setEditable(const bool editable)
Definition: textbox.cpp:650
void setOpaque(const Opaque opaque)
Definition: textbox.h:273
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 requestFocus()
Definition: widget.cpp:204
Font * getFont() const
Definition: widget.cpp:331
void setPosition(const int x, const int y)
Definition: widget.cpp:161
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
Definition: window.h:102
void center()
Definition: window.cpp:1417
void centerHorisontally()
Definition: window.cpp:1422
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
virtual void scheduleDelete()
Definition: window.cpp:831
#define new
Definition: debug_new.h:147
DialogType ::T DialogTypeT
Definition: dialogtype.h:37
#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
const bool ShowCenter_true
Definition: showcenter.h:30
bool ShowCenter
Definition: showcenter.h:30
static const std::string SOUND_INFO
Definition: sound.h:27
static const std::string SOUND_ERROR
Definition: sound.h:28
SoundManager soundManager
const bool Visible_true
Definition: visible.h:30