GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/textdialog.cpp Lines: 34 56 60.7 %
Date: 2017-11-29 Branches: 30 78 38.5 %

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/textdialog.h"
24
25
#include "input/keyboardconfig.h"
26
27
#include "gui/widgets/button.h"
28
#include "gui/widgets/label.h"
29
#include "gui/widgets/passwordfield.h"
30
31
#include "utils/gettext.h"
32
33
#include "gui/fonts/font.h"
34
35
#include "debug.h"
36
37
int TextDialog::instances = 0;
38
39
namespace
40
{
41
2
    const std::string emptyStr;
42
}  // namespace
43
44
2
TextDialog::TextDialog(const std::string &restrict title,
45
                       const std::string &restrict msg,
46
                       Window *const parent,
47
2
                       const bool isPassword) :
48
    Window(title, Modal_true, parent, "textdialog.xml"),
49
    ActionListener(),
50
    mTextField(nullptr),
51
    mPasswordField(nullptr),
52
    // TRANSLATORS: text dialog button
53

4
    mOkButton(new Button(this, _("OK"), "OK", this)),
54


26
    mEnabledKeyboard(keyboard.isEnabled())
55
{
56
2
    keyboard.setEnabled(false);
57
58

2
    Label *const textLabel = new Label(this, msg);
59
    // TRANSLATORS: text dialog button
60


14
    Button *const cancelButton = new Button(this, _("Cancel"), "CANCEL", this);
61
62
2
    place(0, 0, textLabel, 4);
63
2
    if (isPassword)
64
    {
65
        mPasswordField = new PasswordField(this);
66
        place(0, 1, mPasswordField, 4);
67
    }
68
    else
69
    {
70


14
        mTextField = new TextField(this);
71
2
        place(0, 1, mTextField, 4);
72
    }
73
2
    place(2, 2, mOkButton);
74
2
    place(3, 2, cancelButton);
75
76

2
    int width = getFont()->getWidth(title);
77
4
    if (width < textLabel->getWidth())
78
        width = textLabel->getWidth();
79
2
    reflowLayout(CAST_S32(width + 20));
80
2
}
81
82
2
void TextDialog::postInit()
83
{
84
2
    Window::postInit();
85
4
    if (getParent() != nullptr)
86
    {
87
2
        setLocationRelativeTo(getParent());
88
4
        getParent()->moveToTop(this);
89
    }
90
2
    setVisible(Visible_true);
91
2
    requestModalFocus();
92
2
    if (mPasswordField != nullptr)
93
        mPasswordField->requestFocus();
94
2
    else if (mTextField != nullptr)
95
2
        mTextField->requestFocus();
96
97
2
    instances++;
98
2
}
99
100
6
TextDialog::~TextDialog()
101
{
102
2
    instances--;
103
2
}
104
105
void TextDialog::action(const ActionEvent &event)
106
{
107
    if (event.getId() == "CANCEL")
108
        setActionEventId("~" + getActionEventId());
109
110
    distributeActionEvent();
111
    close();
112
}
113
114
const std::string &TextDialog::getText() const
115
{
116
    if (mTextField != nullptr)
117
        return mTextField->getText();
118
    else if (mPasswordField != nullptr)
119
        return mPasswordField->getText();
120
    return emptyStr;
121
}
122
123
void TextDialog::setText(const std::string &text)
124
{
125
    if (mTextField != nullptr)
126
        mTextField->setText(text);
127
    else if (mPasswordField != nullptr)
128
        mPasswordField->setText(text);
129
}
130
131
void TextDialog::close()
132
{
133
    keyboard.setEnabled(mEnabledKeyboard);
134
    scheduleDelete();
135

6
}