FifeGUI 0.3.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// Standard library includes
9#include <list>
10
11// Platform config include
12#include "fifechan/platform.hpp"
13
14// Project headers (subdirs before local)
15#include "fifechan/listeners/keylistener.hpp"
16#include "fifechan/listeners/mouselistener.hpp"
17#include "fifechan/listmodel.hpp"
18#include "fifechan/widget.hpp"
19
20namespace fcn
21{
23
37 class FIFEGUI_API ListBox : public Widget, public MouseListener, public KeyListener
38 {
39 public:
40 ListBox();
41
47 explicit ListBox(ListModel* listModel);
48
49 ~ListBox() override = default;
50
51 ListBox(ListBox const &) = delete;
52 ListBox& operator=(ListBox const &) = delete;
53 ListBox(ListBox&&) = delete;
54 ListBox& operator=(ListBox&&) = delete;
55
62 int getSelected() const;
63
72 void setSelected(int selected);
73
80 void setListModel(ListModel* listModel);
81
88 ListModel* getListModel() const;
89
102 bool isWrappingEnabled() const;
103
115 void setWrappingEnabled(bool wrappingEnabled);
116
127 void addSelectionListener(SelectionListener* selectionListener);
128
134 void removeSelectionListener(SelectionListener* selectionListener);
135
144 virtual unsigned int getRowHeight() const;
145
146 // Inherited from Widget
147
149
150 void resizeToContent(bool recursion = true) override;
151 void adjustSize() override;
152 void draw(Graphics* graphics) override;
153
154 void logic() override;
155
156 // Inherited from KeyListener
157
158 void keyPressed(KeyEvent& keyEvent) override;
159
160 // Inherited from MouseListener
161
162 void mousePressed(MouseEvent& mouseEvent) override;
163
164 void mouseWheelMovedUp(MouseEvent& mouseEvent) override;
165
166 void mouseWheelMovedDown(MouseEvent& mouseEvent) override;
167
168 void mouseDragged(MouseEvent& mouseEvent) override;
169
170 protected:
178
182 int mSelected{-1};
183
188
192 bool mWrappingEnabled{false};
193
197 using SelectionListenerList = std::list<SelectionListener*>;
198
203
205 using SelectionListenerIterator = SelectionListenerList::iterator;
206
213 void adjustSizeImpl();
214 };
215} // namespace fcn
216
217#endif // INCLUDE_FIFECHAN_WIDGETS_LISTBOX_HPP_
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
Represents a key event.
Definition keyevent.hpp:26
KeyListener(KeyListener const &)=default
Copy constructor.
void setListModel(ListModel *listModel)
Sets the list model to use.
Definition listbox.cpp:214
void setWrappingEnabled(bool wrappingEnabled)
Sets the list box to wrap or not when selecting items with a keyboard.
Definition listbox.cpp:256
void adjustSize() override
Resizes the widget's size to fit the content exactly.
Definition listbox.cpp:246
int getSelected() const
Gets the selected item as an index in the list model.
Definition listbox.cpp:109
void mouseWheelMovedDown(MouseEvent &mouseEvent) override
Called when the mouse wheel has moved down on the widget area.
Definition listbox.cpp:200
void mouseDragged(MouseEvent &mouseEvent) override
Called when the mouse has moved and the mouse has previously been pressed on the widget.
Definition listbox.cpp:209
bool isWrappingEnabled() const
Checks whether the list box wraps when selecting items with a keyboard.
Definition listbox.cpp:251
void mouseWheelMovedUp(MouseEvent &mouseEvent) override
Called when the mouse wheel has moved up on the widget area.
Definition listbox.cpp:189
SelectionListenerList::iterator SelectionListenerIterator
Iterator for SelectionListenerList.
Definition listbox.hpp:205
int mSelected
The selected item as an index in the list model.
Definition listbox.hpp:182
void logic() override
Called for all widgets in the GUI each time Gui::logic is called.
Definition listbox.cpp:104
void addSelectionListener(SelectionListener *selectionListener)
Adds a selection listener to the list box.
Definition listbox.cpp:261
void adjustSizeImpl()
Concrete implementation of adjustSize.
Definition listbox.cpp:231
void removeSelectionListener(SelectionListener *selectionListener)
Removes a selection listener from the list box.
Definition listbox.cpp:267
bool mWrappingEnabled
True if wrapping is enabled, false otherwise.
Definition listbox.hpp:192
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed down on the widget area.
Definition listbox.cpp:181
void resizeToContent(bool recursion=true) override
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition listbox.cpp:226
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
Definition listbox.cpp:142
ListModel * mListModel
The list model to use.
Definition listbox.hpp:187
ListModel * getListModel() const
Gets the list model used.
Definition listbox.cpp:221
void draw(Graphics *graphics) override
Draws the widget.
Definition listbox.cpp:41
SelectionListenerList mSelectionListeners
The selection listeners of the list box.
Definition listbox.hpp:202
void distributeValueChangedEvent()
Notifies all registered selection listeners of a value change.
Definition listbox.cpp:272
void setSelected(int selected)
Sets the selected item.
Definition listbox.cpp:114
std::list< SelectionListener * > SelectionListenerList
Typdef.
Definition listbox.hpp:197
virtual unsigned int getRowHeight() const
Gets the height of a row.
Definition listbox.cpp:282
Interface for a data model representing a list (used by ListBox/DropDown).
Definition listmodel.hpp:26
Represents a mouse event.
MouseListener(MouseListener const &)=default
Copy constructor.
Interface for listening to selection change events.
Widget()
Constructor.
Definition widget.cpp:52
virtual void resizeToContent(bool recursion=true)
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1580
Used replacement tokens by configure_file():