GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/popuplist.cpp Lines: 55 99 55.6 %
Date: 2017-11-29 Branches: 33 96 34.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2017  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
188
PopupList::PopupList(DropDown *const widget,
35
                     ListModel *const listModel,
36
                     const bool extended,
37
188
                     const Modal modal) :
38
    Popup("PopupList", "popuplist.xml"),
39
    FocusListener(),
40
    mListModel(listModel),
41




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



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

188
    mScrollArea(new ScrollArea(this, mListBox, Opaque_false)),
46
    mDropDown(widget),
47
    mPressedIndex(-2),
48


2632
    mModal(modal)
49
{
50
376
    mListBox->setMouseConsume(false);
51
376
    mScrollArea->setMouseConsume(false);
52
188
    mAllowLogic = false;
53
188
    setFocusable(true);
54
55
376
    mListBox->setDistributeMousePressed(true);
56
188
    mScrollArea->setPosition(mPadding, mPadding);
57
188
}
58
59
188
void PopupList::postInit()
60
{
61
376
    Popup::postInit();
62
188
    add(mScrollArea);
63
64
188
    if (gui != nullptr)
65
188
        gui->addGlobalFocusListener(this);
66
67
188
    addKeyListener(mDropDown);
68
188
    addMouseListener(this);
69
188
    adjustSize();
70
188
}
71
72
752
PopupList::~PopupList()
73
{
74
188
    if (mParent != nullptr)
75
188
        mParent->removeFocusListener(this);
76
188
    if (gui != nullptr)
77
188
        gui->removeGlobalFocusListener(this);
78
188
    removeKeyListener(mDropDown);
79
376
}
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
188
void PopupList::widgetResized(const Event &event)
101
{
102
188
    Popup::widgetResized(event);
103
188
    adjustSize();
104
188
}
105
106
386
void PopupList::setSelected(const int selected)
107
{
108
386
    if (mListBox == nullptr)
109
        return;
110
111
386
    mListBox->setSelected(selected);
112
}
113
114
410
int PopupList::getSelected() const
115
{
116
410
    if (mListBox == nullptr)
117
        return -1;
118
119
820
    return mListBox->getSelected();
120
}
121
122
188
void PopupList::setListModel(ListModel *const model)
123
{
124
188
    if (mListBox != nullptr)
125
188
        mListBox->setListModel(model);
126
188
    mListModel = model;
127
188
}
128
129
564
void PopupList::adjustSize()
130
{
131
564
    const int pad2 = 2 * mPadding;
132
564
    const int width = mDimension.width - pad2;
133
564
    mScrollArea->setWidth(width);
134
564
    mScrollArea->setHeight(mDimension.height - pad2);
135
564
    mListBox->adjustSize();
136
564
    mListBox->setWidth(width);
137
564
}
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
8
void PopupList::focusGained(const Event& event)
166
{
167
8
    const Widget *const source = event.getSource();
168

8
    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
}