GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/textdialog.cpp Lines: 38 60 63.3 %
Date: 2018-07-14 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-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/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
1
    const std::string emptyStr;
42
}  // namespace
43
44
1
TextDialog::TextDialog(const std::string &restrict title,
45
                       const std::string &restrict msg,
46
                       Window *const parent,
47
1
                       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

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

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

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


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

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

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

3
}