FifeGUI 0.2.0
A C++ GUI library designed for games.
listbox.hpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#ifndef INCLUDE_FIFECHAN_WIDGETS_LISTBOX_HPP_
6#define INCLUDE_FIFECHAN_WIDGETS_LISTBOX_HPP_
7
8#include <list>
9
10#include "fifechan/keylistener.hpp"
11#include "fifechan/listmodel.hpp"
12#include "fifechan/mouselistener.hpp"
13#include "fifechan/platform.hpp"
14#include "fifechan/widget.hpp"
15
16namespace fcn
17{
19
33 class FIFEGUI_API ListBox : public Widget, public MouseListener, public KeyListener
34 {
35 public:
36 ListBox();
37
43 explicit ListBox(ListModel* listModel);
44
45 ~ListBox() override = default;
46
47 ListBox(ListBox const &) = delete;
48 ListBox& operator=(ListBox const &) = delete;
49 ListBox(ListBox&&) = delete;
50 ListBox& operator=(ListBox&&) = delete;
51
58 int getSelected() const;
59
67 void setSelected(int selected);
68
75 void setListModel(ListModel* listModel);
76
83 ListModel* getListModel() const;
84
97 bool isWrappingEnabled() const;
98
110 void setWrappingEnabled(bool wrappingEnabled);
111
122 void addSelectionListener(SelectionListener* selectionListener);
123
129 void removeSelectionListener(SelectionListener* selectionListener);
130
137 virtual unsigned int getRowHeight() const;
138
139 // Inherited from Widget
140
142
143 void resizeToContent(bool recursion) override;
144 void adjustSize() override;
145 void draw(Graphics* graphics) override;
146
147 void logic() override;
148
149 // Inherited from KeyListener
150
151 void keyPressed(KeyEvent& keyEvent) override;
152
153 // Inherited from MouseListener
154
155 void mousePressed(MouseEvent& mouseEvent) override;
156
157 void mouseWheelMovedUp(MouseEvent& mouseEvent) override;
158
159 void mouseWheelMovedDown(MouseEvent& mouseEvent) override;
160
161 void mouseDragged(MouseEvent& mouseEvent) override;
162
163 protected:
170
174 int mSelected{-1};
175
180
184 bool mWrappingEnabled{false};
185
189 using SelectionListenerList = std::list<SelectionListener*>;
190
195
199 using SelectionListenerIterator = SelectionListenerList::iterator;
200
206 void adjustSizeImpl();
207 };
208} // namespace fcn
209
210#endif // INCLUDE_FIFECHAN_WIDGETS_LISTBOX_HPP_
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
Represents a key event.
Definition keyevent.hpp:22
void setListModel(ListModel *listModel)
Sets the list model to use.
Definition listbox.cpp:204
void setWrappingEnabled(bool wrappingEnabled)
Sets the list box to wrap or not when selecting items with a keyboard.
Definition listbox.cpp:246
void adjustSize() override
Resizes the widget's size to fit the content exactly.
Definition listbox.cpp:236
void resizeToContent(bool recursion) override
Resize this widget to fit its content.
Definition listbox.cpp:216
int getSelected() const
Gets the selected item as an index in the list model.
Definition listbox.cpp:102
void mouseWheelMovedDown(MouseEvent &mouseEvent) override
Called when the mouse wheel has moved down on the widget area.
Definition listbox.cpp:190
void mouseDragged(MouseEvent &mouseEvent) override
Called when the mouse has moved and the mouse has previously been pressed on the widget.
Definition listbox.cpp:199
bool isWrappingEnabled() const
Checks whether the list box wraps when selecting items with a keyboard.
Definition listbox.cpp:241
void mouseWheelMovedUp(MouseEvent &mouseEvent) override
Called when the mouse wheel has moved up on the widget area.
Definition listbox.cpp:179
SelectionListenerList::iterator SelectionListenerIterator
Typedef.
Definition listbox.hpp:199
int mSelected
The selected item as an index in the list model.
Definition listbox.hpp:174
void logic() override
Called for all widgets in the gui each time Gui::logic is called.
Definition listbox.cpp:97
void addSelectionListener(SelectionListener *selectionListener)
Adds a selection listener to the list box.
Definition listbox.cpp:251
void adjustSizeImpl()
Concrete implementation of adjustSize.
Definition listbox.cpp:221
void removeSelectionListener(SelectionListener *selectionListener)
Removes a selection listener from the list box.
Definition listbox.cpp:256
bool mWrappingEnabled
True if wrapping is enabled, false otherwise.
Definition listbox.hpp:184
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed on the widget area.
Definition listbox.cpp:171
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
Definition listbox.cpp:135
ListModel * mListModel
The list model to use.
Definition listbox.hpp:179
ListModel * getListModel() const
Gets the list model used.
Definition listbox.cpp:211
void draw(Graphics *graphics) override
Draws the widget.
Definition listbox.cpp:37
SelectionListenerList mSelectionListeners
The selection listeners of the list box.
Definition listbox.hpp:194
void distributeValueChangedEvent()
Distributes a value changed event to all selection listeners of the list box.
Definition listbox.cpp:261
void setSelected(int selected)
Sets the selected item.
Definition listbox.cpp:107
std::list< SelectionListener * > SelectionListenerList
Typdef.
Definition listbox.hpp:189
virtual unsigned int getRowHeight() const
Gets the height of a row.
Definition listbox.cpp:271
Interface for a data model representing a list (used by ListBox/DropDown).
Definition listmodel.hpp:24
Represents a mouse event.
Interface for listening to selection change events.
Widget()
Constructor.
Definition widget.cpp:36
void resizeToContent()
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1417