FifeGUI 0.3.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// Standard library includes
9#include <list>
10#include <memory>
11
12// Platform config include
13#include "fifechan/platform.hpp"
14
15// Project headers (subdirs before local)
16#include "fifechan/focushandler.hpp"
17#include "fifechan/listeners/actionlistener.hpp"
18#include "fifechan/listeners/focuslistener.hpp"
19#include "fifechan/listeners/keylistener.hpp"
20#include "fifechan/listeners/mouselistener.hpp"
21#include "fifechan/listeners/selectionlistener.hpp"
22#include "fifechan/widget.hpp"
23
24namespace fcn
25{
26 class ListBox;
27 class ListModel;
28 class ScrollArea;
29
49 class FIFEGUI_API DropDown :
50 public ActionListener,
51 public KeyListener,
52 public MouseListener,
53 public FocusListener,
54 public SelectionListener,
55 public Widget
56 {
57 public:
66 explicit DropDown(
67 ListModel* listModel = nullptr, ScrollArea* scrollArea = nullptr, ListBox* listBox = nullptr);
68
69 ~DropDown() override;
70
71 DropDown(DropDown const &) = delete;
72 DropDown& operator=(DropDown const &) = delete;
73 DropDown(DropDown&&) = delete;
74 DropDown& operator=(DropDown&&) = delete;
75
82 int getSelected() const;
83
91 void setSelected(int selected);
92
99 void setListModel(ListModel* listModel);
100
107 ListModel* getListModel() const;
108
114 void adjustHeight();
115
126 void addSelectionListener(SelectionListener* selectionListener);
127
133 void removeSelectionListener(SelectionListener* selectionListener);
134
135 // Inherited from Widget
136
138
139 void resizeToContent(bool recursion = true) override;
140 void adjustSize() override;
141 void draw(Graphics* graphics) override;
142
144 void setBaseColor(Color const & color) override;
145
147 void setBackgroundColor(Color const & color) override;
148
150 void setForegroundColor(Color const & color) override;
151
153 void setFont(Font* font) override;
154
156 void setSelectionColor(Color const & color) override;
157
158 // Inherited from Container
159
160 Rectangle getChildrenArea() override;
161
162 // Inherited from FocusListener
163
164 void focusLost(Event const & event) override;
165
166 // Inherited from ActionListener
167
168 void action(ActionEvent const & actionEvent) override;
169
170 // Inherited from DeathListener
171
173 virtual void death(Event const & event);
174
175 // Inherited from KeyListener
176
177 void keyPressed(KeyEvent& keyEvent) override;
178
179 // Inherited from MouseListener
180
181 void mousePressed(MouseEvent& mouseEvent) override;
182
183 void mouseReleased(MouseEvent& mouseEvent) override;
184
185 void mouseWheelMovedUp(MouseEvent& mouseEvent) override;
186
187 void mouseWheelMovedDown(MouseEvent& mouseEvent) override;
188
189 void mouseDragged(MouseEvent& mouseEvent) override;
190
191 // Inherited from SelectionListener
192
193 void valueChanged(SelectionEvent const & event) override;
194
195 protected:
201 virtual void drawButton(Graphics* graphics);
202
206 virtual void dropDown();
207
211 virtual void foldUp();
212
219
223 bool mDroppedDown{false};
224
229 bool mPushed{false};
230
237
241 std::unique_ptr<ScrollArea> mOwnedScrollArea;
242
246 std::unique_ptr<ListBox> mOwnedListBox;
247
252
257
264
270 bool mInternalListBox{false};
271
275 bool mIsDragged{false};
276
278 using SelectionListenerList = std::list<SelectionListener*>;
279
284
286 using SelectionListenerIterator = SelectionListenerList::iterator;
287 };
288} // namespace fcn
289
290#endif // INCLUDE_FIFECHAN_WIDGETS_DROPDOWN_HPP_
Represents an action trigger (e.g., button click).
Color.
Definition color.hpp:58
void draw(Graphics *graphics) override
Draws the widget.
Definition dropdown.cpp:72
virtual void death(Event const &event)
DeathListener callback invoked when a observed widget is destroyed.
Definition dropdown.cpp:381
void setFont(Font *font) override
Set the font used to render items in the dropdown.
Definition dropdown.cpp:450
void action(ActionEvent const &actionEvent) override
Handles an action event emitted by a widget.
Definition dropdown.cpp:392
DropDown(ListModel *listModel=nullptr, ScrollArea *scrollArea=nullptr, ListBox *listBox=nullptr)
Constructor.
Definition dropdown.cpp:24
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:270
bool mIsDragged
True if the drop down is dragged.
Definition dropdown.hpp:275
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:263
void focusLost(Event const &event) override
Called when a widget loses focus.
Definition dropdown.cpp:373
void adjustSize() override
Resizes the widget's size to fit the content exactly.
Definition dropdown.cpp:342
ScrollArea * mScrollArea
The scroll area used.
Definition dropdown.hpp:251
virtual void dropDown()
Sets the drop down to be dropped down.
Definition dropdown.cpp:347
void mouseWheelMovedDown(MouseEvent &mouseEvent) override
Called when the mouse wheel has moved down on the widget area.
Definition dropdown.cpp:474
std::unique_ptr< ScrollArea > mOwnedScrollArea
Owned internal scroll area when not supplied externally.
Definition dropdown.hpp:241
void mouseDragged(MouseEvent &mouseEvent) override
Called when the mouse has moved and the mouse has previously been pressed on the widget.
Definition dropdown.cpp:269
Rectangle getChildrenArea() override
Gets the area of the widget occupied by the widget's children.
Definition dropdown.cpp:401
void valueChanged(SelectionEvent const &event) override
Called when the value of a selection has been changed in a Widget.
Definition dropdown.cpp:492
void removeSelectionListener(SelectionListener *selectionListener)
Removes a selection listener from the drop down.
Definition dropdown.cpp:503
virtual void drawButton(Graphics *graphics)
Draws the button of the drop down.
Definition dropdown.cpp:143
SelectionListenerList mSelectionListeners
The selection listener's of the drop down.
Definition dropdown.hpp:283
int getSelected() const
Gets the selected item as an index in the list model.
Definition dropdown.cpp:191
void resizeToContent(bool recursion=true) override
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition dropdown.cpp:326
ListBox * mListBox
The list box used.
Definition dropdown.hpp:256
void setListModel(ListModel *listModel)
Sets the list model to use when displaying the list.
Definition dropdown.cpp:276
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
Definition dropdown.cpp:201
void mouseReleased(MouseEvent &mouseEvent) override
Called when a mouse button has been released on the widget area.
Definition dropdown.cpp:247
void setForegroundColor(Color const &color) override
Set the foreground/text color used in the dropdown.
Definition dropdown.cpp:437
void adjustHeight()
Adjusts the height of the drop down to fit the height of the drop down's parent's height.
Definition dropdown.cpp:288
void setBackgroundColor(Color const &color) override
Set the explicit background color for the dropdown.
Definition dropdown.cpp:424
bool mDroppedDown
True if the drop down is dropped down, false otherwise.
Definition dropdown.hpp:223
bool mPushed
True if the drop down has been pushed with the mouse, false otherwise.
Definition dropdown.hpp:229
std::list< SelectionListener * > SelectionListenerList
List of SelectionListener.
Definition dropdown.hpp:278
void setSelected(int selected)
Sets the selected item.
Definition dropdown.cpp:196
void setBaseColor(Color const &color) override
Set the base color used for the dropdown background/controls.
Definition dropdown.cpp:411
virtual void foldUp()
Sets the drop down to be folded up.
Definition dropdown.cpp:362
void setSelectionColor(Color const &color) override
Set the color used for the selected item highlight.
Definition dropdown.cpp:483
void addSelectionListener(SelectionListener *selectionListener)
Adds a selection listener to the drop down.
Definition dropdown.cpp:497
void distributeValueChangedEvent()
Distributes a value changed event to all selection listeners of the drop down.
Definition dropdown.cpp:508
ListModel * getListModel() const
Gets the list model used.
Definition dropdown.cpp:283
std::unique_ptr< ListBox > mOwnedListBox
Owned internal list box when not supplied externally.
Definition dropdown.hpp:246
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed down on the widget area.
Definition dropdown.cpp:221
SelectionListenerList::iterator SelectionListenerIterator
Iterator for SelectionListenerList.
Definition dropdown.hpp:286
int mFoldedUpHeight
Holds what the height is if the drop down is folded up.
Definition dropdown.hpp:236
void mouseWheelMovedUp(MouseEvent &mouseEvent) override
Called when the mouse wheel has moved up on the widget area.
Definition dropdown.cpp:463
Base class for all GUI event objects.
Definition event.hpp:25
Abstract interface for font rendering.
Definition font.hpp:26
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.
A scrollable list box allowing item selection.
Definition listbox.hpp:38
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.
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:22
A scrollable viewport for viewing widgets larger than the visible area.
Represents a change in selection state (e.g., list item selected).
SelectionListener(SelectionListener const &)=default
Copy constructor.
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():