GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/listbox.h Lines: 7 8 87.5 %
Date: 2017-11-29 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-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
#ifndef GUI_WIDGETS_LISTBOX_H
67
#define GUI_WIDGETS_LISTBOX_H
68
69
#include "gui/widgets/widget.h"
70
71
#include "listeners/keylistener.h"
72
#include "listeners/mouselistener.h"
73
74
#include "localconsts.h"
75
76
class Skin;
77
class KeyEvent;
78
class ListModel;
79
class MouseEvent;
80
class SelectionListener;
81
class Widget2;
82
83
/**
84
 * A list box, meant to be used inside a scroll area. Same as the Guichan list
85
 * box except this one doesn't have a background, instead completely relying
86
 * on the scroll area. It also adds selection listener functionality.
87
 *
88
 * \ingroup GUI
89
 */
90
class ListBox notfinal : public Widget,
91
                         public MouseListener,
92
                         public KeyListener
93
{
94
    public:
95
        /**
96
         * Constructor.
97
         */
98
        ListBox(const Widget2 *const widget,
99
                ListModel *const listModel,
100
                const std::string &skin);
101
102
        A_DELETE_COPY(ListBox)
103
104
        virtual ~ListBox();
105
106
        void postInit() override;
107
108
        /**
109
         * Draws the list box.
110
         */
111
        void draw(Graphics *const graphics) override A_NONNULL(2);
112
113
        void safeDraw(Graphics *const graphics) override A_NONNULL(2);
114
115
        /**
116
         * Update the alpha value to the graphic components.
117
         */
118
        void updateAlpha();
119
120
        // Inherited from KeyListener
121
122
        void keyPressed(KeyEvent& event) override final;
123
124
        // Inherited from MouseListener
125
126
        void mouseWheelMovedUp(MouseEvent& event) override final A_CONST;
127
128
        void mouseWheelMovedDown(MouseEvent& event) override final A_CONST;
129
130
        void mousePressed(MouseEvent &event) override;
131
132
        void mouseReleased(MouseEvent &event) override;
133
134
        void mouseReleased1(const MouseEvent &event);
135
136
        void mouseDragged(MouseEvent &event) override;
137
138
        void refocus();
139
140
        void setDistributeMousePressed(const bool b) noexcept2
141
208
        { mDistributeMousePressed = b; }
142
143
        virtual void adjustSize();
144
145
        void logic() override final;
146
147
        virtual int getSelectionByMouse(const int y) const;
148
149
        void setCenter(const bool b) noexcept2
150
4
        { mCenterText = b; }
151
152
        int getPressedIndex() const noexcept2 A_WARN_UNUSED
153
        { return mPressedIndex; }
154
155
1750
        virtual unsigned int getRowHeight() const A_WARN_UNUSED
156
1750
        { return mRowHeight; }
157
158
        void setRowHeight(unsigned int n) noexcept2
159
4
        { mRowHeight = n; }
160
161
        /**
162
         * Gets the selected item as an index in the list model.
163
         *
164
         * @return the selected item as an index in the list model.
165
         * @see setSelected
166
         */
167
        int getSelected() const noexcept2 A_WARN_UNUSED
168
424
        { return mSelected; }
169
170
         /**
171
         * Sets the selected item. The selected item is represented by
172
         * an index from the list model.
173
         *
174
         * @param selected the selected item as an index from the list model.
175
         * @see getSelected
176
         */
177
        void setSelected(const int selected);
178
179
        /**
180
         * Sets the list model to use.
181
         *
182
         * @param listModel the list model to use.
183
         * @see getListModel
184
         */
185
        void setListModel(ListModel *listModel);
186
187
        /**
188
         * Gets the list model used.
189
         *
190
         * @return the list model used.
191
         * @see setListModel
192
         */
193
        ListModel *getListModel() const noexcept2 A_WARN_UNUSED
194
        { return mListModel; }
195
196
        /**
197
         * Checks whether the list box wraps when selecting items with a
198
         * keyboard.
199
         *
200
         * Wrapping means that the selection of items will be wrapped. That is,
201
         * if the first item is selected and up is pressed, the last item will
202
         * get selected. If the last item is selected and down is pressed, the
203
         * first item will get selected.
204
         *
205
         * @return true if wrapping is enabled, fasle otherwise.
206
         * @see setWrappingEnabled
207
         */
208
        bool isWrappingEnabled() const noexcept2 A_WARN_UNUSED
209
        { return mWrappingEnabled; }
210
211
        /**
212
         * Sets the list box to wrap or not when selecting items with a
213
         * keyboard.
214
         *
215
         * Wrapping means that the selection of items will be wrapped. That is,
216
         * if the first item is selected and up is pressed, the last item will
217
         * get selected. If the last item is selected and down is pressed, the
218
         * first item will get selected.
219
         *
220
         * @see isWrappingEnabled
221
         */
222
        void setWrappingEnabled(const bool wrappingEnabled) noexcept2
223
2
        { mWrappingEnabled = wrappingEnabled; }
224
225
        /**
226
         * Adds a selection listener to the list box. When the selection
227
         * changes an event will be sent to all selection listeners of the
228
         * list box.
229
         *
230
         * If you delete your selection listener, be sure to also remove it
231
         * using removeSelectionListener().
232
         *
233
         * @param selectionListener The selection listener to add.
234
         */
235
        void addSelectionListener(SelectionListener *const selectionListener);
236
237
        /**
238
         * Removes a selection listener from the list box.
239
         *
240
         * @param selectionListener The selection listener to remove.
241
         */
242
        void removeSelectionListener(SelectionListener *const
243
                                     selectionListener);
244
245
        /**
246
         * Distributes a value changed event to all selection listeners
247
         * of the list box.
248
         */
249
        void distributeValueChangedEvent();
250
251
    protected:
252
        /**
253
         * The selected item as an index in the list model.
254
         */
255
        int mSelected;
256
257
        /**
258
         * The list model to use.
259
         */
260
        ListModel *mListModel;
261
262
        /**
263
         * True if wrapping is enabled, false otherwise.
264
         */
265
        bool mWrappingEnabled;
266
267
        /**
268
         * Typdef.
269
         */
270
        typedef std::list<SelectionListener*> SelectionListenerList;
271
272
        /**
273
         * The selection listeners of the list box.
274
         */
275
        SelectionListenerList mSelectionListeners;
276
277
        /**
278
         * Typedef.
279
         */
280
        typedef SelectionListenerList::iterator SelectionListenerIterator;
281
282
        Color mHighlightColor;
283
        Color mForegroundSelectedColor;
284
        Color mForegroundSelectedColor2;
285
        int mOldSelected;
286
        int mPadding;
287
        int mPressedIndex;
288
        unsigned int mRowHeight;
289
        int mItemPadding;
290
        Skin *mSkin;
291
        static float mAlpha;
292
        bool mDistributeMousePressed;
293
        bool mCenterText;
294
};
295
296
#endif  // GUI_WIDGETS_LISTBOX_H