GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/textselectdialog.cpp Lines: 51 79 64.6 %
Date: 2021-03-17 Branches: 20 64 31.3 %

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/textselectdialog.h"
25
26
#include "enums/gui/layouttype.h"
27
28
#include "gui/windows/setupwindow.h"
29
30
#include "gui/models/namesmodel.h"
31
32
#include "gui/widgets/button.h"
33
#include "gui/widgets/containerplacer.h"
34
#include "gui/widgets/createwidget.h"
35
#include "gui/widgets/layout.h"
36
#include "gui/widgets/listbox.h"
37
#include "gui/widgets/scrollarea.h"
38
39
#include "utils/delete2.h"
40
#include "utils/gettext.h"
41
42
#include "debug.h"
43
44
1
TextSelectDialog::TextSelectDialog(const std::string &name,
45
                                   const std::string &selectButton,
46
1
                                   const AllowQuit allowQuit) :
47
    // TRANSLATORS: sell dialog name
48
    Window(name, Modal_false, nullptr, "sell.xml"),
49
    ActionListener(),
50
    SelectionListener(),
51
    mSelectButtonName(selectButton),
52
    mText(),
53
    mSelectButton(nullptr),
54
    mQuitButton(nullptr),
55
    mItemList(nullptr),
56
    mScrollArea(nullptr),
57
    mModel(nullptr),
58
    mAllowQuit(allowQuit),
59
8
    mTag(0)
60
{
61
1
}
62
63
1
void TextSelectDialog::postInit()
64
{
65
1
    Window::postInit();
66
5
    setWindowName("TextSelectDialog");
67
1
    setResizable(true);
68
1
    setCloseButton(mAllowQuit == AllowQuit_true);
69
1
    setStickyButtonLock(true);
70
1
    setMinWidth(260);
71
1
    setMinHeight(220);
72
1
    setDefaultSize(260, 230, ImagePosition::CENTER, 0, 0);
73
74
1
    if (setupWindow != nullptr)
75
        setupWindow->registerWindowForReset(this);
76
77
5
    setActionEventId("OK");
78
1
    mModel = new NamesModel;
79

4
    mItemList = CREATEWIDGETR(ListBox,
80
        this,
81
        mModel,
82
        "listbox.xml");
83
2
    mScrollArea = new ScrollArea(this, mItemList,
84

4
        fromBool(getOptionBool("showbackground", false), Opaque),
85

5
        "sell_background.xml");
86
1
    mScrollArea->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
87
88
1
    mSelectButton = new Button(this,
89
        mSelectButtonName,
90
        "select",
91
        BUTTON_SKIN,
92

4
        this);
93
1
    if (mAllowQuit == AllowQuit_true)
94
    {
95
        mQuitButton = new Button(this,
96
            // TRANSLATORS: sell dialog button
97
            _("Quit"),
98
            "quit",
99
            BUTTON_SKIN,
100
            this);
101
    }
102
103
2
    mSelectButton->setEnabled(false);
104
105
2
    mItemList->setDistributeMousePressed(false);
106
1
    mItemList->addSelectionListener(this);
107
5
    mItemList->setActionEventId("item");
108
1
    mItemList->addActionListener(this);
109
110
1
    ContainerPlacer placer(nullptr, nullptr);
111
1
    placer = getPlacer(0, 0);
112
113
2
    placer(0, 0, mScrollArea, 8, 5).setPadding(3);
114
1
    if (mQuitButton != nullptr)
115
    {
116
        placer(6, 5, mSelectButton, 1, 1);
117
        placer(7, 5, mQuitButton, 1, 1);
118
    }
119
    else
120
    {
121
1
        placer(7, 5, mSelectButton, 1, 1);
122
    }
123
124
1
    Layout &layout = getLayout();
125
1
    layout.setRowHeight(0, LayoutType::SET);
126
127
1
    center();
128
1
    loadWindowState();
129
130
1
    setVisible(Visible_true);
131
2
    enableVisibleSound(true);
132
1
}
133
134
7
TextSelectDialog::~TextSelectDialog()
135
{
136
1
    delete2(mModel)
137
2
}
138
139
void TextSelectDialog::action(const ActionEvent &event)
140
{
141
    const std::string &eventId = event.getId();
142
143
    if (eventId == "quit")
144
    {
145
        close();
146
        return;
147
    }
148
149
    const int selectedItem = mItemList->getSelected();
150
151
    // The following actions require a valid item selection
152
    if (selectedItem == -1 ||
153
        selectedItem >= mModel->getNumberOfElements())
154
    {
155
        return;
156
    }
157
    else if (eventId == "select")
158
    {
159
        const int index = mItemList->getSelected();
160
        if (index < 0 || index >= CAST_S32(mModel->size()))
161
            return;
162
        mText = mModel->getElementAt(index);
163
        distributeActionEvent();
164
        close();
165
    }
166
}
167
168
void TextSelectDialog::updateButtonsAndLabels()
169
{
170
    mSelectButton->setEnabled(mItemList->getSelected() > -1);
171
}
172
173
void TextSelectDialog::valueChanged(const SelectionEvent &event A_UNUSED)
174
{
175
    updateButtonsAndLabels();
176
}
177
178
1
void TextSelectDialog::setVisible(Visible visible)
179
{
180
1
    Window::setVisible(visible);
181
182
1
    if (visible == Visible_true)
183
    {
184
1
        if (mItemList != nullptr)
185
1
            mItemList->requestFocus();
186
    }
187
    else
188
    {
189
        scheduleDelete();
190
    }
191
1
}
192
193
void TextSelectDialog::addText(const std::string &text)
194
{
195
    if (text.empty())
196
        return;
197
    mModel->add(text);
198

3
}