FifeGUI 0.2.0
A C++ GUI library designed for games.
dropdown.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_DROPDOWN_HPP_
6#define INCLUDE_FIFECHAN_WIDGETS_DROPDOWN_HPP_
7
8#include <list>
9#include <memory>
10
11#include "fifechan/actionlistener.hpp"
12#include "fifechan/focushandler.hpp"
13#include "fifechan/focuslistener.hpp"
14#include "fifechan/keylistener.hpp"
15#include "fifechan/mouselistener.hpp"
16#include "fifechan/platform.hpp"
17#include "fifechan/selectionlistener.hpp"
18#include "fifechan/widget.hpp"
19
20namespace fcn
21{
22 class ListBox;
23 class ListModel;
24 class ScrollArea;
25
45 class FIFEGUI_API DropDown :
46 public ActionListener,
47 public KeyListener,
48 public MouseListener,
49 public FocusListener,
50 public SelectionListener,
51 public Widget
52 {
53 public:
62 explicit DropDown(ListModel* listModel = nullptr, ScrollArea* scrollArea = nullptr, ListBox* listBox = nullptr);
63
64 ~DropDown() override;
65
66 DropDown(DropDown const &) = delete;
67 DropDown& operator=(DropDown const &) = delete;
68 DropDown(DropDown&&) = delete;
69 DropDown& operator=(DropDown&&) = delete;
70
77 int getSelected() const;
78
86 void setSelected(int selected);
87
94 void setListModel(ListModel* listModel);
95
102 ListModel* getListModel() const;
103
109 void adjustHeight();
110
121 void addSelectionListener(SelectionListener* selectionListener);
122
128 void removeSelectionListener(SelectionListener* selectionListener);
129
130 // Inherited from Widget
131
133
134 void resizeToContent(bool recursion) override;
135 void adjustSize() override;
136 void draw(Graphics* graphics) override;
137
139 void setBaseColor(Color const & color) override;
140
142 void setBackgroundColor(Color const & color) override;
143
145 void setForegroundColor(Color const & color) override;
146
148 void setFont(Font* font) override;
149
151 void setSelectionColor(Color const & color) override;
152
153 // Inherited from Container
154
155 Rectangle getChildrenArea() override;
156
157 // Inherited from FocusListener
158
159 void focusLost(Event const & event) override;
160
161 // Inherited from ActionListener
162
163 void action(ActionEvent const & actionEvent) override;
164
165 // Inherited from DeathListener
166
168 virtual void death(Event const & event);
169
170 // Inherited from KeyListener
171
172 void keyPressed(KeyEvent& keyEvent) override;
173
174 // Inherited from MouseListener
175
176 void mousePressed(MouseEvent& mouseEvent) override;
177
178 void mouseReleased(MouseEvent& mouseEvent) override;
179
180 void mouseWheelMovedUp(MouseEvent& mouseEvent) override;
181
182 void mouseWheelMovedDown(MouseEvent& mouseEvent) override;
183
184 void mouseDragged(MouseEvent& mouseEvent) override;
185
186 // Inherited from SelectionListener
187
188 void valueChanged(SelectionEvent const & event) override;
189
190 protected:
196 virtual void drawButton(Graphics* graphics);
197
201 virtual void dropDown();
202
206 virtual void foldUp();
207
214
218 bool mDroppedDown{false};
219
224 bool mPushed{false};
225
232
236 std::unique_ptr<ScrollArea> mOwnedScrollArea;
237
241 std::unique_ptr<ListBox> mOwnedListBox;
242
247
252
258
265
271 bool mInternalListBox{false};
272
276 bool mIsDragged{false};
277
281 using SelectionListenerList = std::list<SelectionListener*>;
282
287
291 using SelectionListenerIterator = SelectionListenerList::iterator;
292 };
293} // namespace fcn
294
295#endif // INCLUDE_FIFECHAN_WIDGETS_DROPDOWN_HPP_
Represents an action trigger (e.g., button click).
Color.
Definition color.hpp:56
void draw(Graphics *graphics) override
Draws the widget.
Definition dropdown.cpp:67
void resizeToContent(bool recursion) override
Resize this widget to fit its content.
Definition dropdown.cpp:318
virtual void death(Event const &event)
DeathListener callback invoked when a observed widget is destroyed.
Definition dropdown.cpp:367
void setFont(Font *font) override
Set the font used to render items in the dropdown.
Definition dropdown.cpp:433
void action(ActionEvent const &actionEvent) override
Called when an action is received from a widget.
Definition dropdown.cpp:377
DropDown(ListModel *listModel=nullptr, ScrollArea *scrollArea=nullptr, ListBox *listBox=nullptr)
Constructor.
Definition dropdown.cpp:20
bool mInternalListBox
True if an internal list box is used, false if a list box has been passed to the drop down which the ...
Definition dropdown.hpp:271
bool mIsDragged
True if the drop down is dragged.
Definition dropdown.hpp:276
bool mInternalScrollArea
True if an internal scroll area is used, false if a scroll area has been passed to the drop down whic...
Definition dropdown.hpp:264
void focusLost(Event const &event) override
Called when a widget loses focus.
Definition dropdown.cpp:361
void adjustSize() override
Resizes the widget's size to fit the content exactly.
Definition dropdown.cpp:332
ScrollArea * mScrollArea
The scroll area used.
Definition dropdown.hpp:246
virtual void dropDown()
Sets the drop down to be dropped down.
Definition dropdown.cpp:337
void mouseWheelMovedDown(MouseEvent &mouseEvent) override
Called when the mouse wheel has moved down on the widget area.
Definition dropdown.cpp:457
std::unique_ptr< ScrollArea > mOwnedScrollArea
Owned internal scroll area when not supplied externally.
Definition dropdown.hpp:236
void mouseDragged(MouseEvent &mouseEvent) override
Called when the mouse has moved and the mouse has previously been pressed on the widget.
Definition dropdown.cpp:261
Rectangle getChildrenArea() override
Gets the area of the widget occupied by the widget's children.
Definition dropdown.cpp:384
void valueChanged(SelectionEvent const &event) override
Called when the value of a selection has been changed in a Widget.
Definition dropdown.cpp:475
void removeSelectionListener(SelectionListener *selectionListener)
Removes a selection listener from the drop down.
Definition dropdown.cpp:485
virtual void drawButton(Graphics *graphics)
Draws the button of the drop down.
Definition dropdown.cpp:135
SelectionListenerList mSelectionListeners
The selection listener's of the drop down.
Definition dropdown.hpp:286
int getSelected() const
Gets the selected item as an index in the list model.
Definition dropdown.cpp:183
ListBox * mListBox
The list box used.
Definition dropdown.hpp:251
void setListModel(ListModel *listModel)
Sets the list model to use when displaying the list.
Definition dropdown.cpp:268
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
Definition dropdown.cpp:193
void mouseReleased(MouseEvent &mouseEvent) override
Called when a mouse button has been released on the widget area.
Definition dropdown.cpp:239
void setForegroundColor(Color const &color) override
Set the foreground/text color used in the dropdown.
Definition dropdown.cpp:420
void adjustHeight()
Adjusts the height of the drop down to fit the height of the drop down's parent's height.
Definition dropdown.cpp:280
void setBackgroundColor(Color const &color) override
Set the explicit background color for the dropdown.
Definition dropdown.cpp:407
bool mDroppedDown
True if the drop down is dropped down, false otherwise.
Definition dropdown.hpp:218
bool mPushed
True if the drop down has been pushed with the mouse, false otherwise.
Definition dropdown.hpp:224
std::list< SelectionListener * > SelectionListenerList
Typedef.
Definition dropdown.hpp:281
void setSelected(int selected)
Sets the selected item.
Definition dropdown.cpp:188
void setBaseColor(Color const &color) override
Set the base color used for the dropdown background/controls.
Definition dropdown.cpp:394
virtual void foldUp()
Sets the drop down to be folded up.
Definition dropdown.cpp:352
void setSelectionColor(Color const &color) override
Set the color used for the selected item highlight.
Definition dropdown.cpp:466
void addSelectionListener(SelectionListener *selectionListener)
Adds a selection listener to the drop down.
Definition dropdown.cpp:480
void distributeValueChangedEvent()
Distributes a value changed event to all selection listeners of the drop down.
Definition dropdown.cpp:490
ListModel * getListModel() const
Gets the list model used.
Definition dropdown.cpp:275
std::unique_ptr< ListBox > mOwnedListBox
Owned internal list box when not supplied externally.
Definition dropdown.hpp:241
FocusHandler mInternalFocusHandler
The internal focus handler used to keep track of focus for the internal list box.
Definition dropdown.hpp:257
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed on the widget area.
Definition dropdown.cpp:213
SelectionListenerList::iterator SelectionListenerIterator
Typedef.
Definition dropdown.hpp:291
int mFoldedUpHeight
Holds what the height is if the drop down is folded up.
Definition dropdown.hpp:231
void mouseWheelMovedUp(MouseEvent &mouseEvent) override
Called when the mouse wheel has moved up on the widget area.
Definition dropdown.cpp:446
Base class for all GUI event objects.
Definition event.hpp:24
Manages focus navigation and assignment among widgets within a Gui instance.
Abstract interface for font rendering.
Definition font.hpp:24
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
Represents a key event.
Definition keyevent.hpp:22
A scrollable list box allowing item selection.
Definition listbox.hpp:34
Interface for a data model representing a list (used by ListBox/DropDown).
Definition listmodel.hpp:24
Represents a mouse event.
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:20
A scrollable viewport for viewing widgets larger than the visible area.
Represents a change in selection state (e.g., list item selected).
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