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

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

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

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

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

3
}