GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/listbox.cpp Lines: 75 179 41.9 %
Date: 2017-11-29 Branches: 29 129 22.5 %

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
/*      _______   __   __   __   ______   __   __   _______   __   __
24
 *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
25
 *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
26
 *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
27
 *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
28
 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
29
 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
30
 *
31
 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
32
 *
33
 *
34
 * Per Larsson a.k.a finalman
35
 * Olof Naessén a.k.a jansem/yakslem
36
 *
37
 * Visit: http://guichan.sourceforge.net
38
 *
39
 * License: (BSD)
40
 * Redistribution and use in source and binary forms, with or without
41
 * modification, are permitted provided that the following conditions
42
 * are met:
43
 * 1. Redistributions of source code must retain the above copyright
44
 *    notice, this list of conditions and the following disclaimer.
45
 * 2. Redistributions in binary form must reproduce the above copyright
46
 *    notice, this list of conditions and the following disclaimer in
47
 *    the documentation and/or other materials provided with the
48
 *    distribution.
49
 * 3. Neither the name of Guichan nor the names of its contributors may
50
 *    be used to endorse or promote products derived from this software
51
 *    without specific prior written permission.
52
 *
53
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
57
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
59
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64
 */
65
66
#include "gui/widgets/listbox.h"
67
68
#include "settings.h"
69
70
#include "gui/focushandler.h"
71
#include "gui/gui.h"
72
#include "gui/skin.h"
73
74
#include "gui/fonts/font.h"
75
76
#include "gui/models/listmodel.h"
77
78
#include "listeners/selectionlistener.h"
79
80
#include "utils/foreach.h"
81
82
#include "render/graphics.h"
83
84
#include "debug.h"
85
86
float ListBox::mAlpha = 1.0;
87
88
250
ListBox::ListBox(const Widget2 *const widget,
89
                 ListModel *const listModel,
90
250
                 const std::string &skin) :
91
    Widget(widget),
92
    MouseListener(),
93
    KeyListener(),
94
    mSelected(-1),
95
    mListModel(listModel),
96
    mWrappingEnabled(false),
97
    mSelectionListeners(),
98
500
    mHighlightColor(getThemeColor(ThemeColorId::HIGHLIGHT)),
99
500
    mForegroundSelectedColor(getThemeColor(ThemeColorId::LISTBOX_SELECTED)),
100
    mForegroundSelectedColor2(getThemeColor(
101
500
        ThemeColorId::LISTBOX_SELECTED_OUTLINE)),
102
    mOldSelected(-1),
103
    mPadding(0),
104
    mPressedIndex(-2),
105
    mRowHeight(0),
106
    mItemPadding(1),
107
    mSkin(nullptr),
108
    mDistributeMousePressed(true),
109
1750
    mCenterText(false)
110
{
111
250
    setWidth(100);
112
250
    setFocusable(true);
113
250
    addMouseListener(this);
114
250
    addKeyListener(this);
115
116
500
    mForegroundColor = getThemeColor(ThemeColorId::LISTBOX);
117
500
    mForegroundColor2 = getThemeColor(ThemeColorId::LISTBOX_OUTLINE);
118
119
250
    if (theme != nullptr)
120

1250
        mSkin = theme->load(skin, "listbox.xml");
121
122
250
    if (mSkin != nullptr)
123
    {
124
500
        mPadding = mSkin->getPadding();
125

1000
        mItemPadding = mSkin->getOption("itemPadding");
126
    }
127
128
250
    const Font *const font = getFont();
129
250
    mRowHeight = CAST_U32(
130
250
        font->getHeight() + 2 * mItemPadding);
131
250
}
132
133
250
void ListBox::postInit()
134
{
135
250
    adjustSize();
136
250
}
137
138
1454
ListBox::~ListBox()
139
{
140
250
    if (gui != nullptr)
141
250
        gui->removeDragged(this);
142
143
250
    if (theme != nullptr)
144
250
        theme->unload(mSkin);
145
454
}
146
147
4
void ListBox::updateAlpha()
148
{
149
    const float alpha = std::max(settings.guiAlpha,
150
24
        theme->getMinimumOpacity());
151
152

8
    if (mAlpha != alpha)
153
        mAlpha = alpha;
154
4
}
155
156
4
void ListBox::draw(Graphics *const graphics)
157
{
158
4
    if (mListModel == nullptr)
159
        return;
160
161
    BLOCK_START("ListBox::draw")
162
4
    updateAlpha();
163
164
4
    mHighlightColor.a = CAST_U32(mAlpha * 255.0F);
165
4
    graphics->setColor(mHighlightColor);
166
4
    Font *const font = getFont();
167
4
    const int rowHeight = CAST_S32(getRowHeight());
168
4
    const int width = mDimension.width;
169
170
4
    if (mCenterText)
171
    {
172
        // Draw filled rectangle around the selected list element
173
        if (mSelected >= 0)
174
        {
175
            graphics->fillRectangle(Rect(mPadding,
176
                rowHeight * mSelected + mPadding,
177
                mDimension.width - 2 * mPadding, rowHeight));
178
179
            const std::string str = mListModel->getElementAt(mSelected);
180
            font->drawString(graphics,
181
                mForegroundSelectedColor,
182
                mForegroundSelectedColor2,
183
                str,
184
                (width - font->getWidth(str)) / 2,
185
                mSelected * rowHeight + mPadding + mItemPadding);
186
        }
187
        // Draw the list elements
188
        const int sz = mListModel->getNumberOfElements();
189
        for (int i = 0, y = mPadding + mItemPadding;
190
             i < sz; ++i, y += rowHeight)
191
        {
192
            if (i != mSelected)
193
            {
194
                const std::string str = mListModel->getElementAt(i);
195
                font->drawString(graphics,
196
                    mForegroundColor,
197
                    mForegroundColor2,
198
                    str,
199
                    (width - font->getWidth(str)) / 2, y);
200
            }
201
        }
202
    }
203
    else
204
    {
205
        // Draw filled rectangle around the selected list element
206
4
        if (mSelected >= 0)
207
        {
208
            graphics->fillRectangle(Rect(mPadding,
209
                rowHeight * mSelected + mPadding,
210
                mDimension.width - 2 * mPadding, rowHeight));
211
212
            const std::string str = mListModel->getElementAt(mSelected);
213
            font->drawString(graphics,
214
                mForegroundSelectedColor,
215
                mForegroundSelectedColor2,
216
                str,
217
                mPadding,
218
                mSelected * rowHeight + mPadding + mItemPadding);
219
        }
220
        // Draw the list elements
221
4
        const int sz = mListModel->getNumberOfElements();
222
4
        for (int i = 0, y = mPadding + mItemPadding; i < sz;
223
             ++i, y += rowHeight)
224
        {
225
            if (i != mSelected)
226
            {
227
                const std::string str = mListModel->getElementAt(i);
228
                font->drawString(graphics,
229
                    mForegroundColor,
230
                    mForegroundColor2,
231
                    str,
232
                    mPadding, y);
233
            }
234
        }
235
    }
236
    BLOCK_END("ListBox::draw")
237
}
238
239
void ListBox::keyPressed(KeyEvent &event)
240
{
241
    const InputActionT action = event.getActionId();
242
    if (action == InputAction::GUI_SELECT)
243
    {
244
        distributeActionEvent();
245
        event.consume();
246
    }
247
    else if (action == InputAction::GUI_UP)
248
    {
249
        if (mSelected > 0)
250
        {
251
            setSelected(mSelected - 1);
252
        }
253
        else if (mSelected == 0 &&
254
                 mWrappingEnabled &&
255
                 getListModel() != nullptr)
256
        {
257
            setSelected(getListModel()->getNumberOfElements() - 1);
258
        }
259
        event.consume();
260
    }
261
    else if (action == InputAction::GUI_DOWN)
262
    {
263
        const int num = getListModel()->getNumberOfElements() - 1;
264
        if (mSelected < num)
265
            setSelected(mSelected + 1);
266
        else if (mSelected == num && mWrappingEnabled)
267
            setSelected(0);
268
        event.consume();
269
    }
270
    else if (action == InputAction::GUI_HOME)
271
    {
272
        setSelected(0);
273
        event.consume();
274
    }
275
    else if (action == InputAction::GUI_END && (getListModel() != nullptr))
276
    {
277
        setSelected(getListModel()->getNumberOfElements() - 1);
278
        event.consume();
279
    }
280
}
281
282
void ListBox::safeDraw(Graphics *const graphics)
283
{
284
    ListBox::draw(graphics);
285
}
286
287
// Don't do anything on scrollwheel. ScrollArea will deal with that.
288
289
void ListBox::mouseWheelMovedUp(MouseEvent &event A_UNUSED)
290
{
291
}
292
293
void ListBox::mouseWheelMovedDown(MouseEvent &event A_UNUSED)
294
{
295
}
296
297
void ListBox::mousePressed(MouseEvent &event)
298
{
299
    mPressedIndex = getSelectionByMouse(event.getY());
300
    if (mMouseConsume && mPressedIndex != -1)
301
        event.consume();
302
}
303
304
void ListBox::mouseReleased(MouseEvent &event)
305
{
306
    if (mPressedIndex != getSelectionByMouse(event.getY()))
307
        return;
308
309
    if (mDistributeMousePressed)
310
    {
311
        mouseReleased1(event);
312
    }
313
    else
314
    {
315
        switch (event.getClickCount())
316
        {
317
            case 1:
318
                mouseDragged(event);
319
                mOldSelected = mSelected;
320
                break;
321
            case 2:
322
                if (gui != nullptr)
323
                    gui->resetClickCount();
324
                if (mOldSelected == mSelected)
325
                    mouseReleased1(event);
326
                else
327
                    mouseDragged(event);
328
                mOldSelected = mSelected;
329
                break;
330
            default:
331
                mouseDragged(event);
332
                mOldSelected = mSelected;
333
                break;
334
        }
335
    }
336
    mPressedIndex = -2;
337
}
338
339
void ListBox::mouseReleased1(const MouseEvent &event)
340
{
341
    if (event.getButton() == MouseButton::LEFT)
342
    {
343
        setSelected(std::max(0, getSelectionByMouse(event.getY())));
344
        distributeActionEvent();
345
    }
346
}
347
348
void ListBox::mouseDragged(MouseEvent &event)
349
{
350
    if (event.getButton() != MouseButton::LEFT || getRowHeight() == 0)
351
        return;
352
353
    // Make list selection update on drag, but guard against negative y
354
    if (getRowHeight() != 0u)
355
        setSelected(std::max(0, getSelectionByMouse(event.getY())));
356
}
357
358
void ListBox::refocus()
359
{
360
    if (mFocusHandler == nullptr)
361
        return;
362
363
    if (isFocusable())
364
        mFocusHandler->requestFocus(this);
365
}
366
367
980
void ListBox::adjustSize()
368
{
369
    BLOCK_START("ListBox::adjustSize")
370
980
    if (mListModel != nullptr)
371
    {
372
2940
        setHeight(CAST_S32(getRowHeight()) *
373
1960
            mListModel->getNumberOfElements() + 2 * mPadding);
374
    }
375
    BLOCK_END("ListBox::adjustSize")
376
980
}
377
378
void ListBox::logic()
379
{
380
    BLOCK_START("ListBox::logic")
381
    adjustSize();
382
    BLOCK_END("ListBox::logic")
383
}
384
385
int ListBox::getSelectionByMouse(const int y) const
386
{
387
    if (y < mPadding)
388
        return -1;
389
    return (y - mPadding) / CAST_S32(getRowHeight());
390
}
391
392
398
void ListBox::setSelected(const int selected)
393
{
394
398
    if (mListModel == nullptr)
395
    {
396
        mSelected = -1;
397
    }
398
    else
399
    {
400
398
        if (selected < 0)
401
            mSelected = -1;
402
398
        else if (selected >= mListModel->getNumberOfElements())
403
12
            mSelected = mListModel->getNumberOfElements() - 1;
404
        else
405
386
            mSelected = selected;
406
    }
407
408
796
    Rect scroll;
409
410
398
    if (mSelected < 0)
411
        scroll.y = 0;
412
    else
413
386
        scroll.y = CAST_S32(getRowHeight()) * mSelected;
414
415
398
    scroll.height = CAST_S32(getRowHeight());
416
398
    showPart(scroll);
417
418
398
    distributeValueChangedEvent();
419
398
}
420
421
188
void ListBox::setListModel(ListModel *const listModel)
422
{
423
188
    mSelected = -1;
424
188
    mListModel = listModel;
425
188
    adjustSize();
426
188
}
427
428
30
void ListBox::addSelectionListener(SelectionListener *const selectionListener)
429
{
430
60
    mSelectionListeners.push_back(selectionListener);
431
30
}
432
433
void ListBox::removeSelectionListener(SelectionListener *const
434
                                      selectionListener)
435
{
436
    mSelectionListeners.remove(selectionListener);
437
}
438
439
void ListBox::distributeValueChangedEvent()
440
{
441

1194
    FOR_EACH (SelectionListenerIterator, iter, mSelectionListeners)
442
    {
443
24
        SelectionEvent event(this);
444
8
        (*iter)->valueChanged(event);
445
    }
446
}