FifeGUI 0.2.0
A C++ GUI library designed for games.
widget.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_WIDGET_HPP_
6#define INCLUDE_FIFECHAN_WIDGET_HPP_
7
8#include <limits>
9#include <list>
10#include <string>
11#include <type_traits>
12
13#include "fifechan/color.hpp"
14#include "fifechan/rectangle.hpp"
15#include "fifechan/size.hpp"
16#include "fifechan/widgetlistener.hpp"
17
18namespace fcn
19{
20 class ActionListener;
21 class DeathListener;
22 class DefaultFont;
23 class FocusHandler;
24 class FocusListener;
25 class Font;
26 class Graphics;
27 class KeyInput;
28 class KeyListener;
29 class MouseInput;
30 class MouseListener;
32 class WidgetListener;
33
44 class FIFEGUI_API Widget
45 {
46 public:
50 enum class SelectionMode : uint8_t
51 {
52 None = 0,
53 Border = 1,
54 Background = 2
55 };
56
64 Widget();
65
66 virtual ~Widget();
67
68 Widget(Widget const &) = delete;
69 Widget& operator=(Widget const &) = delete;
70 Widget(Widget&&) = delete;
71 Widget& operator=(Widget&&) = delete;
72
89 virtual void draw(Graphics* graphics) = 0;
90
110 virtual void drawOutline(Graphics* graphics);
111
118 virtual void drawBorder(Graphics* graphics);
119
127 virtual void drawSelectionFrame(Graphics* graphics);
128
142 void setOutlineSize(unsigned int size);
143
157 unsigned int getOutlineSize() const;
158
169 void setBorderSize(unsigned int size);
170
171 /**
172 * Gets the size of the widget's border. The border is considered as part of
173 * the widget.
174 *
175 * A border size of 0 means that the widget has no border. The default border size
176 * is 0.
177 *
178 * @return The size of the widget's border.
179 * @see setBorderSize, drawBorder
180 */
181 unsigned int getBorderSize() const;
182
192 void setMargin(int margin);
193
203 void setMarginTop(int margin);
204
214 int getMarginTop() const;
215
225 void setMarginRight(int margin);
226
236 int getMarginRight() const;
237
247 void setMarginBottom(int margin);
248
258 int getMarginBottom() const;
259
269 void setMarginLeft(int margin);
270
280 int getMarginLeft() const;
281
290 void setPadding(unsigned int padding);
291
300 void setPaddingTop(unsigned int padding);
301
310 unsigned int getPaddingTop() const;
311
320 void setPaddingRight(unsigned int padding);
321
330 unsigned int getPaddingRight() const;
331
340 void setPaddingBottom(unsigned int padding);
341
350 unsigned int getPaddingBottom() const;
351
360 void setPaddingLeft(unsigned int padding);
361
370 unsigned int getPaddingLeft() const;
371
378 virtual void logic() { }
379
386 virtual Widget* getParent() const;
387
394 virtual Widget* getTop() const;
395
403 void setWidth(int width);
404
412 int getWidth() const;
413
421 void setHeight(int height);
422
430 int getHeight() const;
431
440 void setSize(int width, int height);
441
449 void setX(int x);
450
458 int getX() const;
459
467 void setY(int y);
468
476 int getY() const;
477
486 void setPosition(int x, int y);
487
495 void setDimension(Rectangle const & dimension);
496
504 Rectangle const & getDimension() const;
505
511 unsigned int getChildrenCount() const;
512
518 unsigned int getVisibleChildrenCount() const;
519
526 void setMinSize(Size const & size);
527
534 Size const & getMinSize() const;
535
542 void setMaxSize(Size const & size);
543
550 Size const & getMaxSize() const;
551
559 void setFixedSize(Size const & size);
560
567 Size const & getFixedSize() const;
568
575 bool isFixedSize() const;
576
584 void setFocusable(bool focusable);
585
592 bool isFocusable() const;
593
599 virtual bool isFocused() const;
600
609 void setEnabled(bool enabled);
610
618 bool isEnabled() const;
619
626 void setVisible(bool visible);
627
634 bool isVisible() const;
635
641 bool isSetVisible() const;
642
649 virtual void setBaseColor(Color const & color);
650
657 Color const & getBaseColor() const;
658
665 virtual void setForegroundColor(Color const & color);
666
672 Color const & getForegroundColor() const;
673
680 virtual void setBackgroundColor(Color const & color);
681
687 Color const & getBackgroundColor() const;
688
695 virtual void setSelectionColor(Color const & color);
696
703 Color const & getSelectionColor() const;
704
711 virtual void setOutlineColor(Color const & color);
712
719 Color const & getOutlineColor() const;
720
727 virtual void setBorderColor(Color const & color);
728
735 Color const & getBorderColor() const;
736
743 virtual void setSelectionMode(SelectionMode mode);
744
751 SelectionMode getSelectionMode() const;
752
758 virtual void requestFocus();
759
764 virtual void requestMoveToTop();
765
770 virtual void requestMoveToBottom();
771
781 virtual void _draw(Graphics* graphics);
782
791 virtual void _logic();
792
803 virtual void _setFocusHandler(FocusHandler* focusHandler);
804
815 virtual FocusHandler* _getFocusHandler();
816
825 void addActionListener(ActionListener* actionListener);
826
833 void removeActionListener(ActionListener* actionListener);
834
843 void addDeathListener(DeathListener* deathListener);
844
851 void removeDeathListener(DeathListener* deathListener);
852
861 void addMouseListener(MouseListener* mouseListener);
862
869 void removeMouseListener(MouseListener* mouseListener);
870
879 void addKeyListener(KeyListener* keyListener);
880
887 void removeKeyListener(KeyListener* keyListener);
888
897 void addFocusListener(FocusListener* focusListener);
898
905 void removeFocusListener(FocusListener* focusListener);
906
915 void addWidgetListener(WidgetListener* widgetListener);
916
923 void removeWidgetListener(WidgetListener* widgetListener);
924
936 void setActionEventId(std::string const & actionEventId);
937
944 std::string const & getActionEventId() const;
945
952 virtual void getAbsolutePosition(int& x, int& y) const;
953
964 virtual void _setParent(Widget* parent);
965
974 Font* getFont() const;
975
982 static void setGlobalFont(Font* font);
983
991 virtual void setFont(Font* font);
992
999 virtual void fontChanged() { }
1000
1008 static bool widgetExists(Widget const * widget);
1009
1019 bool isTabInEnabled() const;
1020
1030 void setTabInEnabled(bool enabled);
1031
1041 bool isTabOutEnabled() const;
1042
1052 void setTabOutEnabled(bool enabled);
1053
1060 virtual bool isModalFocusable() const;
1061
1068 virtual bool isModalMouseInputFocusable() const;
1069
1077 virtual void requestModalFocus();
1078
1087 virtual void requestModalMouseInputFocus();
1088
1095 virtual void releaseModalFocus();
1096
1103 virtual void releaseModalMouseInputFocus();
1104
1111 virtual bool isModalFocused() const;
1112
1120 virtual bool isModalMouseInputFocused() const;
1121
1135 Widget* getWidgetAt(int x, int y)
1136 {
1137 return getWidgetAt(x, y, nullptr);
1138 }
1139
1149 virtual Widget* getWidgetAt(int x, int y, Widget* exclude);
1150
1160 std::list<Widget*> getWidgetsIn(Rectangle const & area)
1161 {
1162 return getWidgetsIn(area, nullptr);
1163 }
1164
1175 virtual std::list<Widget*> getWidgetsIn(Rectangle const & area, Widget* ignore);
1176
1182 virtual std::list<MouseListener*> const & _getMouseListeners();
1183
1189 virtual std::list<KeyListener*> const & _getKeyListeners();
1190
1196 virtual std::list<FocusListener*> const & _getFocusListeners();
1197
1219 virtual Rectangle getChildrenArea();
1220
1228 virtual FocusHandler* _getInternalFocusHandler();
1229
1238 void setInternalFocusHandler(FocusHandler* internalFocusHandler);
1239
1249 virtual void moveToTop(Widget* widget);
1250
1260 virtual void moveToBottom(Widget* widget);
1261
1267 virtual void focusNext();
1268
1274 virtual void focusPrevious();
1275
1283 virtual void showWidgetPart(Widget* widget, Rectangle area);
1284
1294 void setId(std::string const & id);
1295
1305 std::string const & getId() const;
1306
1316 virtual void showPart(Rectangle rectangle);
1317
1331 static void _setVisibilityEventHandler(VisibilityEventHandler* visibilityEventHandler);
1332
1344 static VisibilityEventHandler* _getVisibilityEventHandler();
1345
1351 static void _setGuiDeathListener(DeathListener* deathListener);
1352
1358 static DeathListener* _getGuiDeathListener();
1359
1366 void setVerticalExpand(bool expand);
1367
1374 bool isVerticalExpand() const;
1375
1382 void setHorizontalExpand(bool expand);
1383
1390 bool isHorizontalExpand() const;
1391
1400 {
1401 adaptLayout(true);
1402 }
1403
1409 virtual void adaptLayout(bool top);
1410
1418 {
1419 resizeToContent(true);
1420 }
1421
1427 virtual void resizeToContent(bool recursion) { }
1428
1432 virtual void adjustSize() { }
1433
1441 {
1442 expandContent(true);
1443 }
1444
1450 virtual void expandContent(bool recursion) { }
1451
1455 virtual bool isLayouted()
1456 {
1457 return false;
1458 }
1459
1466 void getLastPosition(int& x, int& y) const;
1467
1474 void setLastPosition(int x, int y);
1475
1481 bool isLastPositionSet() const;
1482
1483 protected:
1487 void distributeActionEvent();
1488
1492 void distributeResizedEvent();
1493
1497 void distributeMovedEvent();
1498
1502 void distributeHiddenEvent();
1503
1507 void distributeShownEvent();
1508
1515 void distributeAncestorMovedEvent(Widget* ancestor);
1516
1523 void distributeAncestorHiddenEvent(Widget* ancestor);
1524
1531 void distributeAncestorShownEvent(Widget* ancestor);
1532
1542 void add(Widget* widget);
1543
1553 virtual void remove(Widget* widget);
1554
1563 virtual void removeAllChildren();
1564
1574 virtual Widget* findWidgetById(std::string const & id);
1575
1580 void resizeToChildren();
1581
1586 void calculateSize();
1587
1593 std::list<Widget*> const & getChildren() const;
1594
1598 std::list<MouseListener*> mMouseListeners;
1599
1603 std::list<KeyListener*> mKeyListeners;
1604
1608 std::list<ActionListener*> mActionListeners;
1609
1613 std::list<DeathListener*> mDeathListeners;
1614
1618 std::list<FocusListener*> mFocusListeners;
1619
1623 std::list<WidgetListener*> mWidgetListeners;
1624
1629
1634
1639
1644
1649
1654
1659
1665
1670 Widget* mParent{nullptr};
1671
1676
1681
1685 unsigned int mOutlineSize{0};
1686
1690 unsigned int mBorderSize{0};
1691
1695 SelectionMode mSelectionMode{SelectionMode::None};
1696
1701
1706
1711
1716
1720 unsigned int mPaddingTop{0};
1721
1725 unsigned int mPaddingRight{0};
1726
1730 unsigned int mPaddingBottom{0};
1731
1735 unsigned int mPaddingLeft{0};
1736
1740 std::string mActionEventId;
1741
1745 bool mFocusable{false};
1746
1750 bool mVisible{true};
1751
1755 bool mTabIn{true};
1756
1760 bool mTabOut{true};
1761
1765 bool mEnabled{true};
1766
1770 std::string mId;
1771
1776
1783 Size mMaxSize{std::numeric_limits<int>::max(), std::numeric_limits<int>::max()};
1784
1789
1793 bool mFixedSizeUsed{false};
1794
1798 bool mVExpand{false};
1799
1803 bool mHExpand{false};
1804
1809
1814
1819
1823 static std::list<Widget*> mWidgetInstances;
1824
1833
1838
1842 std::list<Widget*> mChildren;
1843
1845 int mLastX{0};
1846
1848 int mLastY{0};
1849 };
1850} // namespace fcn
1851
1852// Bitwise operators for Widget::SelectionMode (enum class)
1853namespace fcn
1854{
1855 constexpr Widget::SelectionMode operator|(Widget::SelectionMode a, Widget::SelectionMode b) noexcept
1856 {
1857 using T = std::underlying_type_t<Widget::SelectionMode>;
1858 return static_cast<Widget::SelectionMode>(static_cast<T>(a) | static_cast<T>(b));
1859 }
1860
1861 constexpr Widget::SelectionMode operator&(Widget::SelectionMode a, Widget::SelectionMode b) noexcept
1862 {
1863 using T = std::underlying_type_t<Widget::SelectionMode>;
1864 return static_cast<Widget::SelectionMode>(static_cast<T>(a) & static_cast<T>(b));
1865 }
1866
1867 constexpr Widget::SelectionMode& operator|=(Widget::SelectionMode& a, Widget::SelectionMode b) noexcept
1868 {
1869 a = a | b;
1870 return a;
1871 }
1872
1873 constexpr Widget::SelectionMode& operator&=(Widget::SelectionMode& a, Widget::SelectionMode b) noexcept
1874 {
1875 a = a & b;
1876 return a;
1877 }
1878} // namespace fcn
1879
1880#endif // INCLUDE_FIFECHAN_WIDGET_HPP_
Interface for listening to action events from widgets.
Color.
Definition color.hpp:56
Interface for listening to widget destruction events.
A basic font implementation capable only of drawing rectangles (placeholder).
Manages focus navigation and assignment among widgets within a Gui instance.
Interface for listening to focus gain/loss events.
Abstract interface for font rendering.
Definition font.hpp:24
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
Internal class representing raw keyboard input data.
Definition keyinput.hpp:28
Interface for listening to keyboard events.
Internal class representing raw mouse input data.
Interface for listening to mouse events.
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:20
Represents dimensions defined by width and height.
Definition size.hpp:18
Handles changes in widget visibility states.
Interface for receiving generic events from widgets.
Abstract base class defining the common behavior, properties, and lifecycle of all GUI elements.
Definition widget.hpp:45
std::list< FocusListener * > mFocusListeners
Holds the focus listeners of the widget.
Definition widget.hpp:1618
bool mHExpand
True if the widget can be horizontal expanded.
Definition widget.hpp:1803
void setMargin(int margin)
Sets all 4 margins to one value.
Definition widget.cpp:386
Widget * mParent
Holds the parent of the widget.
Definition widget.hpp:1670
std::list< DeathListener * > mDeathListeners
Holds the death listeners of the widget.
Definition widget.hpp:1613
virtual void fontChanged()
Called when the font has changed.
Definition widget.hpp:999
static DefaultFont mDefaultFont
Holds the default font used by the widget.
Definition widget.hpp:1813
virtual void resizeToContent(bool recursion)
Resize this widget to fit its content.
Definition widget.hpp:1427
FocusHandler * mFocusHandler
Holds the focus handler used by the widget.
Definition widget.hpp:1658
virtual void adjustSize()
Resizes the widget's size to fit the content exactly.
Definition widget.hpp:1432
Size mMinSize
Holds the min size.
Definition widget.hpp:1775
int mMarginTop
Holds the top margin of the widget.
Definition widget.hpp:1700
virtual void drawOutline(Graphics *graphics)
Called when a widget is given a chance to draw a outline around itself.
Definition widget.cpp:88
virtual bool isLayouted()
Helper function to decide if we need to layout.
Definition widget.hpp:1455
void setBorderSize(unsigned int size)
Sets the size of the widget's border.
Definition widget.cpp:376
int mLastY
Last stored Y coordinate used for layout and event calculations.
Definition widget.hpp:1848
std::list< KeyListener * > mKeyListeners
Holds the key listeners of the widget.
Definition widget.hpp:1603
std::list< ActionListener * > mActionListeners
Holds the action listeners of the widget.
Definition widget.hpp:1608
void setOutlineSize(unsigned int size)
Sets the size of the widget's outline.
Definition widget.cpp:366
SelectionMode
Selection mode.
Definition widget.hpp:51
void setMarginTop(int margin)
Sets the top margin.
Definition widget.cpp:394
unsigned int getOutlineSize() const
Gets the size of the widget's outline.
Definition widget.cpp:371
static std::list< Widget * > mWidgetInstances
Holds a list of all instances of widgets.
Definition widget.hpp:1823
virtual void logic()
Called for all widgets in the gui each time Gui::logic is called.
Definition widget.hpp:378
unsigned int mPaddingTop
Holds the top padding of the widget.
Definition widget.hpp:1720
Rectangle mDimension
Holds the dimension of the widget.
Definition widget.hpp:1675
void setMarginRight(int margin)
Sets the right margin.
Definition widget.cpp:404
bool mTabOut
True if the widget has tab in enabled, false otherwise.
Definition widget.hpp:1760
unsigned int getPaddingLeft() const
Gets the left padding.
Definition widget.cpp:477
Color mForegroundColor
Holds the foreground color of the widget.
Definition widget.hpp:1628
std::string mActionEventId
Holds the action event of the widget.
Definition widget.hpp:1740
int getMarginBottom() const
Gets the bottom margin.
Definition widget.cpp:419
Widget()
Constructor.
Definition widget.cpp:36
void expandContent()
Expands the child widgets to the size of this widget, calls recursively all childs.
Definition widget.hpp:1440
void setPaddingTop(unsigned int padding)
Sets the top padding.
Definition widget.cpp:442
Color mOutlineColor
Holds the outline color of the widget.
Definition widget.hpp:1648
Color mBorderColor
Holds the border color of the widget.
Definition widget.hpp:1653
void setPaddingRight(unsigned int padding)
Sets the right padding.
Definition widget.cpp:452
std::list< WidgetListener * > mWidgetListeners
Holds the widget listeners of the widget.
Definition widget.hpp:1623
Size mFixedSize
Holds the fixed size.
Definition widget.hpp:1788
FocusHandler * mInternalFocusHandler
Holds the focus handler used by the widget.
Definition widget.hpp:1664
Widget * getWidgetAt(int x, int y)
Gets a widget at a certain position in the widget.
Definition widget.hpp:1135
virtual void expandContent(bool recursion)
Expands child widgets to fit this widget's size.
Definition widget.hpp:1450
SelectionMode mSelectionMode
Holds the selection mode.
Definition widget.hpp:1695
void resizeToContent()
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1417
int getMarginLeft() const
Gets the left margin.
Definition widget.cpp:429
std::list< MouseListener * > mMouseListeners
Holds the mouse listeners of the widget.
Definition widget.hpp:1598
void adaptLayout()
Execute the layouting.
Definition widget.hpp:1399
unsigned int mBorderSize
Holds the border size of the widget.
Definition widget.hpp:1690
unsigned int mPaddingRight
Holds the right padding of the widget.
Definition widget.hpp:1725
int getMarginRight() const
Gets the right margin.
Definition widget.cpp:409
void setPaddingLeft(unsigned int padding)
Sets the left padding.
Definition widget.cpp:472
unsigned int mPaddingBottom
Holds the bottom padding of the widget.
Definition widget.hpp:1730
int mLastX
Last stored X coordinate used for layout and event calculations.
Definition widget.hpp:1845
int mMarginRight
Holds the top right of the widget.
Definition widget.hpp:1705
virtual void drawBorder(Graphics *graphics)
Called when a widget have a border.
Definition widget.cpp:111
static DeathListener * mGuiDeathListener
Holds the death listener used by the widgets.
Definition widget.hpp:1837
static Font * mGlobalFont
Holds the global font used by the widget.
Definition widget.hpp:1818
unsigned int getPaddingTop() const
Gets the top padding.
Definition widget.cpp:447
unsigned int getBorderSize() const
Gets the size of the widget's border.
Definition widget.cpp:381
virtual void drawSelectionFrame(Graphics *graphics)
Called when a widget is "active" and the selection mode is Frame or FrameWithBackground.
Definition widget.cpp:135
std::list< Widget * > mChildren
Holds all children of the widget.
Definition widget.hpp:1842
std::list< Widget * > getWidgetsIn(Rectangle const &area)
Gets all widgets inside a certain area of the widget.
Definition widget.hpp:1160
bool mEnabled
True if the widget is enabled, false otherwise.
Definition widget.hpp:1765
unsigned int mOutlineSize
Holds the outline size of the widget.
Definition widget.hpp:1685
Color mBaseColor
Holds the base color of the widget.
Definition widget.hpp:1638
bool mVisible
True if the widget visible, false otherwise.
Definition widget.hpp:1750
Rectangle mOffsetRect
Holds the offset dimension of the widget.
Definition widget.hpp:1680
int getMarginTop() const
Gets the top margin.
Definition widget.cpp:399
unsigned int getPaddingBottom() const
Gets the bottom padding.
Definition widget.cpp:467
Color mSelectionColor
Holds the selection color of the widget.
Definition widget.hpp:1643
bool mVExpand
True if the widget can be vertical expanded.
Definition widget.hpp:1798
unsigned int mPaddingLeft
Holds the left padding of the widget.
Definition widget.hpp:1735
Font * mCurrentFont
Holds the font used by the widget.
Definition widget.hpp:1808
bool mFocusable
True if the widget focusable, false otherwise.
Definition widget.hpp:1745
Color mBackgroundColor
Holds the background color of the widget.
Definition widget.hpp:1633
int mMarginLeft
Holds the left margin of the widget.
Definition widget.hpp:1715
int mMarginBottom
Holds the bottom margin of the widget.
Definition widget.hpp:1710
virtual void draw(Graphics *graphics)=0
Draws the widget.
bool mFixedSizeUsed
True if the widget used a fixed size.
Definition widget.hpp:1793
void setMarginLeft(int margin)
Sets the left margin.
Definition widget.cpp:424
void setPaddingBottom(unsigned int padding)
Sets the bottom padding.
Definition widget.cpp:462
bool mTabIn
True if the widget has tab in enabled, false otherwise.
Definition widget.hpp:1755
std::string mId
Holds the id of the widget.
Definition widget.hpp:1770
void setMarginBottom(int margin)
Sets the bottom margin.
Definition widget.cpp:414
unsigned int getPaddingRight() const
Gets the right padding.
Definition widget.cpp:457
void setPadding(unsigned int padding)
Sets all 4 paddings to one value.
Definition widget.cpp:434
Size mMaxSize
Holds the max size.
Definition widget.hpp:1783
static VisibilityEventHandler * mVisibilityEventHandler
Holds the visibility event handler used by the widgets.
Definition widget.hpp:1832