GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/textdialog.cpp Lines: 38 60 63.3 %
Date: 2021-03-17 Branches: 27 70 38.6 %

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

2
    mOkButton(new Button(this, _("OK"), "OK", BUTTON_SKIN, this)),
55

13
    mEnabledKeyboard(keyboard.isEnabled())
56
{
57
1
    keyboard.setEnabled(false);
58
59

1
    Label *const textLabel = new Label(this, msg);
60
    Button *const cancelButton = new Button(this,
61
        // TRANSLATORS: text dialog button
62
1
        _("Cancel"),
63
        "CANCEL",
64
        BUTTON_SKIN,
65


6
        this);
66
67
1
    place(0, 0, textLabel, 4, 1);
68
1
    if (isPassword)
69
    {
70
        mPasswordField = new PasswordField(this, std::string());
71
        place(0, 1, mPasswordField, 4, 1);
72
    }
73
    else
74
    {
75
1
        mTextField = new TextField(this,
76
2
            std::string(),
77
            LoseFocusOnTab_true,
78
            nullptr,
79
2
            std::string(),
80

2
            false);
81
1
        place(0, 1, mTextField, 4, 1);
82
    }
83
1
    place(2, 2, mOkButton, 1, 1);
84
1
    place(3, 2, cancelButton, 1, 1);
85
86

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

3
}