GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/confirmdialog.cpp Lines: 45 56 80.4 %
Date: 2017-11-29 Branches: 28 70 40.0 %

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-2017  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

2
    mTextBox(new TextBox(this)),
45

12
    mIgnore(ignore)
46
{
47
4
    mTextBox->setEditable(false);
48
4
    mTextBox->setOpaque(Opaque_false);
49
2
    mTextBox->setTextWrapped(msg, 260);
50
2
    soundManager.playGuiSound(soundEvent);
51
2
}
52
53
2
void ConfirmDialog::postInit()
54
{
55
2
    Window::postInit();
56
    // TRANSLATORS: confirm dialog button
57


14
    Button *const yesButton = new Button(this, _("Yes"), "yes", this);
58
    // TRANSLATORS: confirm dialog button
59


14
    Button *const noButton = new Button(this, _("No"), "no", this);
60



8
    Button *const ignoreButton = mIgnore ? new Button(
61
        // TRANSLATORS: confirm dialog button
62
        this, _("Ignore"), "ignore", this) : nullptr;
63
64
4
    const int numRows = mTextBox->getNumberOfRows();
65
6
    int inWidth = yesButton->getWidth() + noButton->getWidth() +
66
2
        (2 * mPadding);
67
68
2
    if (ignoreButton != nullptr)
69
        inWidth += ignoreButton->getWidth();
70
71
2
    const int fontHeight = getFont()->getHeight();
72
2
    const int height = numRows * fontHeight;
73
4
    int width = getFont()->getWidth(getCaption()) + mCaptionOffsetX;
74
75
4
    if (width < mTextBox->getMinWidth())
76
        width = mTextBox->getMinWidth();
77
2
    if (width < inWidth)
78
2
        width = inWidth;
79
80
2
    int windowWidth = width;
81
2
    if (windowWidth < mTextBox->getMinWidth() + fontHeight)
82
    {
83
        windowWidth = mTextBox->getMinWidth() + fontHeight;
84
    }
85
2
    setContentSize(windowWidth,
86
4
        height + fontHeight + noButton->getHeight());
87
2
    mTextBox->setPosition(mPadding, mPadding);
88
89

8
    const int buttonPadding = getOption("buttonPadding", 8);
90
2
    yesButton->setPosition((width - inWidth) / 2, height + buttonPadding);
91
8
    noButton->setPosition(yesButton->getX() + yesButton->getWidth()
92
2
        + (2 * mPadding), height + buttonPadding);
93
2
    if (ignoreButton != nullptr)
94
    {
95
        ignoreButton->setPosition(noButton->getX() + noButton->getWidth()
96
            + (2 * mPadding), height + buttonPadding);
97
    }
98
99
2
    add(mTextBox);
100
2
    add(yesButton);
101
2
    add(noButton);
102
103

2
    if (mIgnore && (ignoreButton != nullptr))
104
        add(ignoreButton);
105
106
4
    if (getParent() != nullptr)
107
    {
108
2
        center();
109
4
        getParent()->moveToTop(this);
110
    }
111
2
    setVisible(Visible_true);
112
2
    yesButton->requestFocus();
113
2
}
114
115
void ConfirmDialog::action(const ActionEvent &event)
116
{
117
    setActionEventId(event.getId());
118
    distributeActionEvent();
119
    scheduleDelete();
120

6
}