GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/popuplist.cpp Lines: 55 99 55.6 %
Date: 2018-05-19 03:07:18 Branches: 32 94 34.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2018  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "gui/widgets/popuplist.h"
22
23
#include "gui/gui.h"
24
25
#include "gui/widgets/createwidget.h"
26
#include "gui/widgets/dropdown.h"
27
#include "gui/widgets/extendedlistbox.h"
28
#include "gui/widgets/scrollarea.h"
29
30
#include "render/graphics.h"
31
32
#include "debug.h"
33
34
190
PopupList::PopupList(DropDown *const widget,
35
                     ListModel *const listModel,
36
                     const bool isExtended,
37
190
                     const Modal modal) :
38
    Popup("PopupList", "popuplist.xml"),
39
    FocusListener(),
40
    mListModel(listModel),
41




760
    mListBox(isExtended ? CREATEWIDGETR(ExtendedListBox,
42
        widget, listModel, "extendedlistbox.xml", 0) :
43



752
        CREATEWIDGETR(ListBox,
44
        widget, listModel, "popuplistbox.xml")),
45

380
    mScrollArea(new ScrollArea(this, mListBox, Opaque_false, std::string())),
46
    mDropDown(widget),
47
    mPressedIndex(-2),
48

2090
    mModal(modal)
49
{
50
380
    mListBox->setMouseConsume(false);
51
380
    mScrollArea->setMouseConsume(false);
52
190
    mAllowLogic = false;
53
190
    setFocusable(true);
54
55
380
    mListBox->setDistributeMousePressed(true);
56
190
    mScrollArea->setPosition(mPadding, mPadding);
57
190
}
58
59
190
void PopupList::postInit()
60
{
61
380
    Popup::postInit();
62
190
    add(mScrollArea);
63
64
190
    if (gui != nullptr)
65
190
        gui->addGlobalFocusListener(this);
66
67
190
    addKeyListener(mDropDown);
68
190
    addMouseListener(this);
69
190
    adjustSize();
70
190
}
71
72
760
PopupList::~PopupList()
73
{
74
190
    if (mParent != nullptr)
75
190
        mParent->removeFocusListener(this);
76
190
    if (gui != nullptr)
77
190
        gui->removeGlobalFocusListener(this);
78
190
    removeKeyListener(mDropDown);
79
380
}
80
81
void PopupList::show(int x, int y)
82
{
83
    int len = mListBox->getHeight() + 8;
84
    if (len > 250)
85
        len = 250;
86
    setContentSize(mListBox->getWidth() + 8, len);
87
    const int width = mDimension.width;
88
    const int height = mDimension.height;
89
    if (mainGraphics->mWidth < (x + width + 5))
90
        x = mainGraphics->mWidth - width;
91
    if (mainGraphics->mHeight < (y + height + 5))
92
        y = mainGraphics->mHeight - height;
93
    setPosition(x, y);
94
    setVisible(Visible_true);
95
    requestMoveToTop();
96
    if (mModal == Modal_true)
97
        requestModalFocus();
98
}
99
100
190
void PopupList::widgetResized(const Event &event)
101
{
102
190
    Popup::widgetResized(event);
103
190
    adjustSize();
104
190
}
105
106
390
void PopupList::setSelected(const int selected)
107
{
108
390
    if (mListBox == nullptr)
109
        return;
110
111
390
    mListBox->setSelected(selected);
112
}
113
114
418
int PopupList::getSelected() const
115
{
116
418
    if (mListBox == nullptr)
117
        return -1;
118
119
836
    return mListBox->getSelected();
120
}
121
122
190
void PopupList::setListModel(ListModel *const model)
123
{
124
190
    if (mListBox != nullptr)
125
190
        mListBox->setListModel(model);
126
190
    mListModel = model;
127
190
}
128
129
570
void PopupList::adjustSize()
130
{
131
570
    const int pad2 = 2 * mPadding;
132
570
    const int width = mDimension.width - pad2;
133
570
    mScrollArea->setWidth(width);
134
570
    mScrollArea->setHeight(mDimension.height - pad2);
135
570
    mListBox->adjustSize();
136
570
    mListBox->setWidth(width);
137
570
}
138
139
void PopupList::mousePressed(MouseEvent& event)
140
{
141
    mPressedIndex = mListBox->getSelectionByMouse(
142
        event.getY() + mPadding);
143
    event.consume();
144
}
145
146
void PopupList::mouseReleased(MouseEvent& event)
147
{
148
    if (mPressedIndex != mListBox->getSelectionByMouse(
149
        event.getY() + mPadding))
150
    {
151
        mPressedIndex = -2;
152
        return;
153
    }
154
155
    mPressedIndex = -2;
156
    if (event.getSource() == mScrollArea)
157
        return;
158
    if (mDropDown != nullptr)
159
        mDropDown->updateSelection();
160
    setVisible(Visible_false);
161
    if (mModal == Modal_true)
162
        releaseModalFocus();
163
}
164
165
10
void PopupList::focusGained(const Event& event)
166
{
167
10
    const Widget *const source = event.getSource();
168

10
    if (mVisible == Visible_false ||
169
        source == this ||
170
        source == mListBox ||
171
        source == mScrollArea ||
172
        source == mDropDown)
173
    {
174
        return;
175
    }
176
177
    if (mDropDown != nullptr)
178
        mDropDown->updateSelection();
179
    setVisible(Visible_false);
180
    if (mModal == Modal_true)
181
        releaseModalFocus();
182
}
183
184
void PopupList::focusLost(const Event& event A_UNUSED)
185
{
186
    if (mDropDown != nullptr)
187
        mDropDown->updateSelection();
188
}