GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/confirmdialog.cpp Lines: 47 66 71.2 %
Date: 2018-05-24 20:11:55 Branches: 28 82 34.1 %

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-2018  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "gui/windows/confirmdialog.h"
24
25
#include "soundmanager.h"
26
27
#include "gui/fonts/font.h"
28
29
#include "gui/widgets/button.h"
30
#include "gui/widgets/textbox.h"
31
32
#include "utils/gettext.h"
33
34
#include "debug.h"
35
36
2
ConfirmDialog::ConfirmDialog(const std::string &restrict title,
37
                             const std::string &restrict msg,
38
                             const std::string &restrict soundEvent,
39
                             const bool ignore,
40
                             const Modal modal,
41
2
                             Window *const parent) :
42
    Window(title, modal, parent, "confirm.xml"),
43
    ActionListener(),
44
    // TRANSLATORS: confirm dialog button
45
2
    mYesMsg(_("Yes")),
46
    // TRANSLATORS: confirm dialog button
47
2
    mNoMsg(_("No")),
48

2
    mTextBox(new TextBox(this)),
49


20
    mIgnore(ignore)
50
{
51
4
    mTextBox->setEditable(false);
52
4
    mTextBox->setOpaque(Opaque_false);
53
2
    mTextBox->setTextWrapped(msg, 260);
54
2
    soundManager.playGuiSound(soundEvent);
55
2
}
56
57
ConfirmDialog::ConfirmDialog(const std::string &restrict title,
58
                             const std::string &restrict msg,
59
                             const std::string &restrict yesMsg,
60
                             const std::string &restrict noMsg,
61
                             const std::string &restrict soundEvent,
62
                             const bool ignore,
63
                             const Modal modal,
64
                             Window *const parent) :
65
    Window(title, modal, parent, "confirm.xml"),
66
    ActionListener(),
67
    mYesMsg(yesMsg),
68
    mNoMsg(noMsg),
69
    mTextBox(new TextBox(this)),
70
    mIgnore(ignore)
71
{
72
    mTextBox->setEditable(false);
73
    mTextBox->setOpaque(Opaque_false);
74
    mTextBox->setTextWrapped(msg, 260);
75
    soundManager.playGuiSound(soundEvent);
76
}
77
78
2
void ConfirmDialog::postInit()
79
{
80
2
    Window::postInit();
81
    Button *const yesButton = new Button(this,
82
        mYesMsg,
83
        "yes",
84
        BUTTON_SKIN,
85

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

8
        this);
91



8
    Button *const ignoreButton = mIgnore ? new Button(
92
        // TRANSLATORS: confirm dialog button
93
        this, _("Ignore"), "ignore", BUTTON_SKIN, this) : nullptr;
94
95
4
    const int numRows = mTextBox->getNumberOfRows();
96
6
    int inWidth = yesButton->getWidth() + noButton->getWidth() +
97
2
        (2 * mPadding);
98
99
2
    if (ignoreButton != nullptr)
100
        inWidth += ignoreButton->getWidth();
101
102
2
    const int fontHeight = getFont()->getHeight();
103
2
    const int height = numRows * fontHeight;
104
4
    int width = getFont()->getWidth(getCaption()) + mCaptionOffsetX;
105
106
4
    if (width < mTextBox->getMinWidth())
107
        width = mTextBox->getMinWidth();
108
2
    if (width < inWidth)
109
2
        width = inWidth;
110
111
2
    int windowWidth = width;
112
2
    if (windowWidth < mTextBox->getMinWidth() + fontHeight)
113
    {
114
        windowWidth = mTextBox->getMinWidth() + fontHeight;
115
    }
116
2
    setContentSize(windowWidth,
117
4
        height + fontHeight + noButton->getHeight());
118
2
    mTextBox->setPosition(mPadding, mPadding);
119
120

8
    const int buttonPadding = getOption("buttonPadding", 8);
121
2
    yesButton->setPosition((width - inWidth) / 2, height + buttonPadding);
122
8
    noButton->setPosition(yesButton->getX() + yesButton->getWidth()
123
2
        + (2 * mPadding), height + buttonPadding);
124
2
    if (ignoreButton != nullptr)
125
    {
126
        ignoreButton->setPosition(noButton->getX() + noButton->getWidth()
127
            + (2 * mPadding), height + buttonPadding);
128
    }
129
130
2
    add(mTextBox);
131
2
    add(yesButton);
132
2
    add(noButton);
133
134

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

6
}