ManaPlus
listbox.h
Go to the documentation of this file.
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"
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 
91 class ListBox notfinal : public Widget,
92  public MouseListener,
93  public KeyListener
94 {
95  public:
99  ListBox(const Widget2 *const widget,
100  ListModel *const listModel,
101  const std::string &skin);
102 
104 
105  ~ListBox() override;
106 
107  void postInit() override;
108 
112  void draw(Graphics *const graphics) override A_NONNULL(2);
113 
114  void safeDraw(Graphics *const graphics) override A_NONNULL(2);
115 
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 
142  { 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  { mCenterText = b; }
152 
154  { return mPressedIndex; }
155 
156  virtual unsigned int getRowHeight() const A_WARN_UNUSED
157  { return mRowHeight; }
158 
159  void setRowHeight(unsigned int n) noexcept2
160  { mRowHeight = n; }
161 
169  { return mSelected; }
170 
178  void setSelected(const int selected);
179 
186  void setListModel(ListModel *listModel);
187 
195  { return mListModel; }
196 
210  { return mWrappingEnabled; }
211 
223  void setWrappingEnabled(const bool wrappingEnabled) noexcept2
224  { mWrappingEnabled = wrappingEnabled; }
225 
236  void addSelectionListener(SelectionListener *const selectionListener);
237 
244  selectionListener);
245 
251 
252  protected:
257 
262 
267 
271  typedef std::list<SelectionListener*> SelectionListenerList;
272 
277 
281  typedef SelectionListenerList::iterator SelectionListenerIterator;
282 
287  int mPadding;
289  unsigned int mRowHeight;
292  static float mAlpha;
295 };
296 
297 #endif // GUI_WIDGETS_LISTBOX_H
Definition: color.h:76
int mSelected
Definition: listbox.h:256
void setDistributeMousePressed(const bool b)
Definition: listbox.h:141
void setListModel(ListModel *listModel)
Definition: listbox.cpp:428
ListBox(const Widget2 *const widget, ListModel *const listModel, const std::string &skin)
Definition: listbox.cpp:89
bool mWrappingEnabled
Definition: listbox.h:266
int getPressedIndex() const
Definition: listbox.h:153
void logic()
Definition: listbox.cpp:385
std::list< SelectionListener * > SelectionListenerList
Definition: listbox.h:271
bool mCenterText
Definition: listbox.h:294
SelectionListenerList mSelectionListeners
Definition: listbox.h:276
void mouseReleased1(const MouseEvent &event)
Definition: listbox.cpp:346
Color mHighlightColor
Definition: listbox.h:283
void mouseWheelMovedDown(MouseEvent &event)
Definition: listbox.cpp:300
void mouseDragged(MouseEvent &event)
Definition: listbox.cpp:355
void mouseWheelMovedUp(MouseEvent &event)
Definition: listbox.cpp:296
void distributeValueChangedEvent()
Definition: listbox.cpp:446
void updateAlpha()
Definition: listbox.cpp:154
bool isWrappingEnabled() const
Definition: listbox.h:209
void mousePressed(MouseEvent &event)
Definition: listbox.cpp:304
unsigned int mRowHeight
Definition: listbox.h:289
virtual unsigned int getRowHeight() const
Definition: listbox.h:156
int mPressedIndex
Definition: listbox.h:288
void draw(Graphics *const graphics)
Definition: listbox.cpp:163
void setRowHeight(unsigned int n)
Definition: listbox.h:159
~ListBox()
Definition: listbox.cpp:145
int mOldSelected
Definition: listbox.h:286
Color mForegroundSelectedColor2
Definition: listbox.h:285
void mouseReleased(MouseEvent &event)
Definition: listbox.cpp:311
void removeSelectionListener(SelectionListener *const selectionListener)
Definition: listbox.cpp:440
virtual void adjustSize()
Definition: listbox.cpp:374
SelectionListenerList::iterator SelectionListenerIterator
Definition: listbox.h:281
void refocus()
Definition: listbox.cpp:365
void safeDraw(Graphics *const graphics)
Definition: listbox.cpp:289
ListModel * mListModel
Definition: listbox.h:261
int mItemPadding
Definition: listbox.h:290
int mPadding
Definition: listbox.h:287
int getSelected() const
Definition: listbox.h:168
static float mAlpha
Definition: listbox.h:292
void keyPressed(KeyEvent &event)
Definition: listbox.cpp:246
Skin * mSkin
Definition: listbox.h:291
void postInit()
Definition: listbox.cpp:140
void setSelected(const int selected)
Definition: listbox.cpp:399
void addSelectionListener(SelectionListener *const selectionListener)
Definition: listbox.cpp:435
virtual int getSelectionByMouse(const int y) const
Definition: listbox.cpp:392
ListModel * getListModel() const
Definition: listbox.h:194
Color mForegroundSelectedColor
Definition: listbox.h:284
void setWrappingEnabled(const bool wrappingEnabled)
Definition: listbox.h:223
void setCenter(const bool b)
Definition: listbox.h:150
bool mDistributeMousePressed
Definition: listbox.h:293
Definition: skin.h:37
Definition: widget.h:99
#define override
Definition: localconsts.h:47
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define notfinal
Definition: localconsts.h:261
#define A_NONNULL(...)
Definition: localconsts.h:168
#define noexcept2
Definition: localconsts.h:50
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53