GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/confirmdialog.cpp Lines: 47 66 71.2 %
Date: 2021-03-17 Branches: 25 76 32.9 %

Line Branch Exec Source
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/confirmdialog.h"
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
1
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
1
                             Window *const parent) :
43
    Window(title, modal, parent, "confirm.xml"),
44
    ActionListener(),
45
    // TRANSLATORS: confirm dialog button
46
1
    mYesMsg(_("Yes")),
47
    // TRANSLATORS: confirm dialog button
48
1
    mNoMsg(_("No")),
49

1
    mTextBox(new TextBox(this)),
50

10
    mIgnore(ignore)
51
{
52
1
    mTextBox->setEditable(false);
53
2
    mTextBox->setOpaque(Opaque_false);
54
1
    mTextBox->setTextWrapped(msg, 260);
55
1
    soundManager.playGuiSound(soundEvent);
56
1
}
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"),
67
    ActionListener(),
68
    mYesMsg(yesMsg),
69
    mNoMsg(noMsg),
70
    mTextBox(new TextBox(this)),
71
    mIgnore(ignore)
72
{
73
    mTextBox->setEditable(false);
74
    mTextBox->setOpaque(Opaque_false);
75
    mTextBox->setTextWrapped(msg, 260);
76
    soundManager.playGuiSound(soundEvent);
77
}
78
79
1
void ConfirmDialog::postInit()
80
{
81
1
    Window::postInit();
82
    Button *const yesButton = new Button(this,
83
        mYesMsg,
84
        "yes",
85
        BUTTON_SKIN,
86

4
        this);
87
    Button *const noButton = new Button(this,
88
        mNoMsg,
89
        "no",
90
        BUTTON_SKIN,
91

4
        this);
92



2
    Button *const ignoreButton = mIgnore ? new Button(
93
        // TRANSLATORS: confirm dialog button
94
        this, _("Ignore"), "ignore", BUTTON_SKIN, this) : nullptr;
95
96
2
    const int numRows = mTextBox->getNumberOfRows();
97
3
    int inWidth = yesButton->getWidth() + noButton->getWidth() +
98
1
        (2 * mPadding);
99
100
1
    if (ignoreButton != nullptr)
101
        inWidth += ignoreButton->getWidth();
102
103
1
    const int fontHeight = getFont()->getHeight();
104
1
    const int height = numRows * fontHeight;
105
2
    int width = getFont()->getWidth(getCaption()) + mCaptionOffsetX;
106
107
2
    if (width < mTextBox->getMinWidth())
108
        width = mTextBox->getMinWidth();
109
1
    if (width < inWidth)
110
1
        width = inWidth;
111
112
1
    int windowWidth = width;
113
1
    if (windowWidth < mTextBox->getMinWidth() + fontHeight)
114
    {
115
        windowWidth = mTextBox->getMinWidth() + fontHeight;
116
    }
117
1
    setContentSize(windowWidth,
118
3
        height + fontHeight + noButton->getHeight());
119
1
    mTextBox->setPosition(mPadding, mPadding);
120
121
4
    const int buttonPadding = getOption("buttonPadding", 8);
122
1
    yesButton->setPosition((width - inWidth) / 2, height + buttonPadding);
123
4
    noButton->setPosition(yesButton->getX() + yesButton->getWidth()
124
2
        + (2 * mPadding), height + buttonPadding);
125
1
    if (ignoreButton != nullptr)
126
    {
127
        ignoreButton->setPosition(noButton->getX() + noButton->getWidth()
128
            + (2 * mPadding), height + buttonPadding);
129
    }
130
131
1
    add(mTextBox);
132
1
    add(yesButton);
133
1
    add(noButton);
134
135

1
    if (mIgnore && (ignoreButton != nullptr))
136
        add(ignoreButton);
137
138
2
    if (getParent() != nullptr)
139
    {
140
1
        center();
141
2
        getParent()->moveToTop(this);
142
    }
143
1
    setVisible(Visible_true);
144
1
    yesButton->requestFocus();
145
1
}
146
147
void ConfirmDialog::action(const ActionEvent &event)
148
{
149
    setActionEventId(event.getId());
150
    distributeActionEvent();
151
    scheduleDelete();
152

3
}