GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/listbox.h Lines: 7 8 87.5 %
Date: 2021-03-17 Branches: 0 0 0.0 %

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-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
/*      _______   __   __   __   ______   __   __   _______   __   __
25
 *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
26
 *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
27
 *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
28
 *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
29
 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
30
 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
31
 *
32
 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
33
 *
34
 *
35
 * Per Larsson a.k.a finalman
36
 * Olof Naessén a.k.a jansem/yakslem
37
 *
38
 * Visit: http://guichan.sourceforge.net
39
 *
40
 * License: (BSD)
41
 * Redistribution and use in source and binary forms, with or without
42
 * modification, are permitted provided that the following conditions
43
 * are met:
44
 * 1. Redistributions of source code must retain the above copyright
45
 *    notice, this list of conditions and the following disclaimer.
46
 * 2. Redistributions in binary form must reproduce the above copyright
47
 *    notice, this list of conditions and the following disclaimer in
48
 *    the documentation and/or other materials provided with the
49
 *    distribution.
50
 * 3. Neither the name of Guichan nor the names of its contributors may
51
 *    be used to endorse or promote products derived from this software
52
 *    without specific prior written permission.
53
 *
54
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
60
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65
 */
66
67
#ifndef GUI_WIDGETS_LISTBOX_H
68
#define GUI_WIDGETS_LISTBOX_H
69
70
#include "gui/widgets/widget.h"
71
72
#include "listeners/keylistener.h"
73
#include "listeners/mouselistener.h"
74
75
#include "localconsts.h"
76
77
class Skin;
78
class KeyEvent;
79
class ListModel;
80
class MouseEvent;
81
class SelectionListener;
82
class Widget2;
83
84
/**
85
 * A list box, meant to be used inside a scroll area. Same as the Guichan list
86
 * box except this one doesn't have a background, instead completely relying
87
 * on the scroll area. It also adds selection listener functionality.
88
 *
89
 * \ingroup GUI
90
 */
91
class ListBox notfinal : public Widget,
92
                         public MouseListener,
93
                         public KeyListener
94
{
95
    public:
96
        /**
97
         * Constructor.
98
         */
99
        ListBox(const Widget2 *const widget,
100
                ListModel *const listModel,
101
                const std::string &skin);
102
103
        A_DELETE_COPY(ListBox)
104
105
        ~ListBox() override;
106
107
        void postInit() override;
108
109
        /**
110
         * Draws the list box.
111
         */
112
        void draw(Graphics *const graphics) override A_NONNULL(2);
113
114
        void safeDraw(Graphics *const graphics) override A_NONNULL(2);
115
116
        /**
117
         * Update the alpha value to the graphic components.
118
         */
119
        void updateAlpha();
120
121
        // Inherited from KeyListener
122
123
        void keyPressed(KeyEvent& event) override final;
124
125
        // Inherited from MouseListener
126
127
        void mouseWheelMovedUp(MouseEvent& event) override final;
128
129
        void mouseWheelMovedDown(MouseEvent& event) override final;
130
131
        void mousePressed(MouseEvent &event) override;
132
133
        void mouseReleased(MouseEvent &event) override;
134
135
        void mouseReleased1(const MouseEvent &event);
136
137
        void mouseDragged(MouseEvent &event) override;
138
139
        void refocus();
140
141
        void setDistributeMousePressed(const bool b) noexcept2
142
105
        { mDistributeMousePressed = b; }
143
144
        virtual void adjustSize();
145
146
        void logic() override final;
147
148
        virtual int getSelectionByMouse(const int y) const;
149
150
        void setCenter(const bool b) noexcept2
151
2
        { mCenterText = b; }
152
153
        int getPressedIndex() const noexcept2 A_WARN_UNUSED
154
        { return mPressedIndex; }
155
156
886
        virtual unsigned int getRowHeight() const A_WARN_UNUSED
157
886
        { return mRowHeight; }
158
159
        void setRowHeight(unsigned int n) noexcept2
160
2
        { mRowHeight = n; }
161
162
        /**
163
         * Gets the selected item as an index in the list model.
164
         *
165
         * @return the selected item as an index in the list model.
166
         * @see setSelected
167
         */
168
        int getSelected() const noexcept2 A_WARN_UNUSED
169
216
        { return mSelected; }
170
171
         /**
172
         * Sets the selected item. The selected item is represented by
173
         * an index from the list model.
174
         *
175
         * @param selected the selected item as an index from the list model.
176
         * @see getSelected
177
         */
178
        void setSelected(const int selected);
179
180
        /**
181
         * Sets the list model to use.
182
         *
183
         * @param listModel the list model to use.
184
         * @see getListModel
185
         */
186
        void setListModel(ListModel *listModel);
187
188
        /**
189
         * Gets the list model used.
190
         *
191
         * @return the list model used.
192
         * @see setListModel
193
         */
194
        ListModel *getListModel() const noexcept2 A_WARN_UNUSED
195
        { return mListModel; }
196
197
        /**
198
         * Checks whether the list box wraps when selecting items with a
199
         * keyboard.
200
         *
201
         * Wrapping means that the selection of items will be wrapped. That is,
202
         * if the first item is selected and up is pressed, the last item will
203
         * get selected. If the last item is selected and down is pressed, the
204
         * first item will get selected.
205
         *
206
         * @return true if wrapping is enabled, fasle otherwise.
207
         * @see setWrappingEnabled
208
         */
209
        bool isWrappingEnabled() const noexcept2 A_WARN_UNUSED
210
        { return mWrappingEnabled; }
211
212
        /**
213
         * Sets the list box to wrap or not when selecting items with a
214
         * keyboard.
215
         *
216
         * Wrapping means that the selection of items will be wrapped. That is,
217
         * if the first item is selected and up is pressed, the last item will
218
         * get selected. If the last item is selected and down is pressed, the
219
         * first item will get selected.
220
         *
221
         * @see isWrappingEnabled
222
         */
223
        void setWrappingEnabled(const bool wrappingEnabled) noexcept2
224
1
        { mWrappingEnabled = wrappingEnabled; }
225
226
        /**
227
         * Adds a selection listener to the list box. When the selection
228
         * changes an event will be sent to all selection listeners of the
229
         * list box.
230
         *
231
         * If you delete your selection listener, be sure to also remove it
232
         * using removeSelectionListener().
233
         *
234
         * @param selectionListener The selection listener to add.
235
         */
236
        void addSelectionListener(SelectionListener *const selectionListener);
237
238
        /**
239
         * Removes a selection listener from the list box.
240
         *
241
         * @param selectionListener The selection listener to remove.
242
         */
243
        void removeSelectionListener(SelectionListener *const
244
                                     selectionListener);
245
246
        /**
247
         * Distributes a value changed event to all selection listeners
248
         * of the list box.
249
         */
250
        void distributeValueChangedEvent();
251
252
    protected:
253
        /**
254
         * The selected item as an index in the list model.
255
         */
256
        int mSelected;
257
258
        /**
259
         * The list model to use.
260
         */
261
        ListModel *mListModel;
262
263
        /**
264
         * True if wrapping is enabled, false otherwise.
265
         */
266
        bool mWrappingEnabled;
267
268
        /**
269
         * Typdef.
270
         */
271
        typedef std::list<SelectionListener*> SelectionListenerList;
272
273
        /**
274
         * The selection listeners of the list box.
275
         */
276
        SelectionListenerList mSelectionListeners;
277
278
        /**
279
         * Typedef.
280
         */
281
        typedef SelectionListenerList::iterator SelectionListenerIterator;
282
283
        Color mHighlightColor;
284
        Color mForegroundSelectedColor;
285
        Color mForegroundSelectedColor2;
286
        int mOldSelected;
287
        int mPadding;
288
        int mPressedIndex;
289
        unsigned int mRowHeight;
290
        int mItemPadding;
291
        Skin *mSkin;
292
        static float mAlpha;
293
        bool mDistributeMousePressed;
294
        bool mCenterText;
295
};
296
297
#endif  // GUI_WIDGETS_LISTBOX_H