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-2018 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"
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 
90 class ListBox notfinal : public Widget,
91  public MouseListener,
92  public KeyListener
93 {
94  public:
98  ListBox(const Widget2 *const widget,
99  ListModel *const listModel,
100  const std::string &skin);
101 
103 
104  ~ListBox() override;
105 
106  void postInit() override;
107 
111  void draw(Graphics *const graphics) override A_NONNULL(2);
112 
113  void safeDraw(Graphics *const graphics) override A_NONNULL(2);
114 
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;
127 
128  void mouseWheelMovedDown(MouseEvent& event) override final;
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 
141  { 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  { mCenterText = b; }
151 
153  { return mPressedIndex; }
154 
155  virtual unsigned int getRowHeight() const A_WARN_UNUSED
156  { return mRowHeight; }
157 
158  void setRowHeight(unsigned int n) noexcept2
159  { mRowHeight = n; }
160 
168  { return mSelected; }
169 
177  void setSelected(const int selected);
178 
185  void setListModel(ListModel *listModel);
186 
194  { return mListModel; }
195 
209  { return mWrappingEnabled; }
210 
222  void setWrappingEnabled(const bool wrappingEnabled) noexcept2
223  { mWrappingEnabled = wrappingEnabled; }
224 
235  void addSelectionListener(SelectionListener *const selectionListener);
236 
243  selectionListener);
244 
250 
251  protected:
256 
261 
266 
270  typedef std::list<SelectionListener*> SelectionListenerList;
271 
275  SelectionListenerList mSelectionListeners;
276 
280  typedef SelectionListenerList::iterator SelectionListenerIterator;
281 
286  int mPadding;
288  unsigned int mRowHeight;
291  static float mAlpha;
294 };
295 
296 #endif // GUI_WIDGETS_LISTBOX_H
#define A_DELETE_COPY(func)
Definition: localconsts.h:52
int mSelected
Definition: listbox.h:255
Skin * mSkin
Definition: listbox.h:290
void setSelected(const int selected)
Definition: listbox.cpp:398
int mPadding
Definition: listbox.h:286
Color mForegroundSelectedColor2
Definition: listbox.h:284
Definition: skin.h:35
void setListModel(ListModel *listModel)
Definition: listbox.cpp:427
SelectionListenerList mSelectionListeners
Definition: listbox.h:275
void addSelectionListener(SelectionListener *const selectionListener)
Definition: listbox.cpp:434
#define A_WARN_UNUSED
Definition: localconsts.h:152
unsigned int mRowHeight
Definition: listbox.h:288
Color mHighlightColor
Definition: listbox.h:282
void refocus()
Definition: listbox.cpp:364
void mouseDragged(MouseEvent &event)
Definition: listbox.cpp:354
ListModel * getListModel() const
Definition: listbox.h:193
void setDistributeMousePressed(const bool b)
Definition: listbox.h:140
void safeDraw(Graphics *const graphics)
Definition: listbox.cpp:288
#define A_NONNULL(...)
Definition: localconsts.h:159
virtual void adjustSize()
Definition: listbox.cpp:373
ListModel * mListModel
Definition: listbox.h:260
void removeSelectionListener(SelectionListener *const selectionListener)
Definition: listbox.cpp:439
Color mForegroundSelectedColor
Definition: listbox.h:283
virtual int getSelectionByMouse(const int y) const
Definition: listbox.cpp:391
void setWrappingEnabled(const bool wrappingEnabled)
Definition: listbox.h:222
bool isWrappingEnabled() const
Definition: listbox.h:208
void mouseReleased1(const MouseEvent &event)
Definition: listbox.cpp:345
void mousePressed(MouseEvent &event)
Definition: listbox.cpp:303
std::list< SelectionListener * > SelectionListenerList
Definition: listbox.h:270
void mouseWheelMovedUp(MouseEvent &event)
Definition: listbox.cpp:295
void updateAlpha()
Definition: listbox.cpp:153
#define notfinal
Definition: localconsts.h:252
int getSelected() const
Definition: listbox.h:167
void mouseWheelMovedDown(MouseEvent &event)
Definition: listbox.cpp:299
SelectionListenerList::iterator SelectionListenerIterator
Definition: listbox.h:280
void mouseReleased(MouseEvent &event)
Definition: listbox.cpp:310
static float mAlpha
Definition: listbox.h:291
ListBox(const Widget2 *const widget, ListModel *const listModel, const std::string &skin)
Definition: listbox.cpp:88
int mPressedIndex
Definition: listbox.h:287
void logic()
Definition: listbox.cpp:384
void setCenter(const bool b)
Definition: listbox.h:149
int mItemPadding
Definition: listbox.h:289
bool mCenterText
Definition: listbox.h:293
void postInit()
Definition: listbox.cpp:139
Definition: widget.h:97
void distributeValueChangedEvent()
Definition: listbox.cpp:445
void draw(Graphics *const graphics)
Definition: listbox.cpp:162
bool mWrappingEnabled
Definition: listbox.h:265
Definition: color.h:74
int mOldSelected
Definition: listbox.h:285
virtual unsigned int getRowHeight() const
Definition: listbox.h:155
int getPressedIndex() const
Definition: listbox.h:152
~ListBox()
Definition: listbox.cpp:144
void keyPressed(KeyEvent &event)
Definition: listbox.cpp:245
bool mDistributeMousePressed
Definition: listbox.h:292
#define noexcept2
Definition: localconsts.h:49
void setRowHeight(unsigned int n)
Definition: listbox.h:158