GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/textselectdialog.cpp Lines: 51 77 66.2 %
Date: 2017-11-29 Branches: 25 72 34.7 %

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/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
2
TextSelectDialog::TextSelectDialog(const std::string &name,
44
                                   const std::string &selectButton,
45
2
                                   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

16
    mTag(0)
59
{
60
2
}
61
62
2
void TextSelectDialog::postInit()
63
{
64
2
    Window::postInit();
65
10
    setWindowName("TextSelectDialog");
66
2
    setResizable(true);
67
2
    setCloseButton(mAllowQuit == AllowQuit_true);
68
2
    setStickyButtonLock(true);
69
2
    setMinWidth(260);
70
2
    setMinHeight(220);
71
2
    setDefaultSize(260, 230, ImagePosition::CENTER);
72
73
2
    if (setupWindow != nullptr)
74
        setupWindow->registerWindowForReset(this);
75
76
10
    setActionEventId("OK");
77
2
    mModel = new NamesModel;
78


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

8
        fromBool(getOptionBool("showbackground"), Opaque),
84

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

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

6
}