FifeGUI 0.3.0
A C++ GUI library designed for games.
focushandler.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_FOCUSHANDLER_HPP_
6#define INCLUDE_FIFECHAN_FOCUSHANDLER_HPP_
7
8// Standard library includes
9#include <cstdio>
10#include <vector>
11
12// Project headers (subdirs before local)
13#include "fifechan/events/event.hpp"
14
15namespace fcn
16{
17 class Widget;
18
33 class FIFEGUI_API FocusHandler
34 {
35 public:
36 virtual ~FocusHandler() = default;
37 FocusHandler();
38
39 FocusHandler(FocusHandler const &) = delete;
40 FocusHandler& operator=(FocusHandler const &) = delete;
41 FocusHandler(FocusHandler&&) = delete;
42 FocusHandler& operator=(FocusHandler&&) = delete;
43
55 virtual void requestFocus(Widget* widget);
56
64 virtual bool isFocused(Widget const * widget) const;
65
72 virtual Widget* getMouseCaptureOwner() const;
73
79 virtual bool hasModalFocus() const;
80
88 virtual void pushModal(Widget* focusOwner, Widget* mouseOwner = nullptr);
89
96 virtual void popModal() noexcept;
97
104 virtual void clearModal();
105
112 {
113 public:
122 ModalScope(FocusHandler* handler, Widget* focusOwner, Widget* mouseOwner = nullptr) :
123 mHandler(handler)
124 {
125 if (mHandler != nullptr) {
126 mHandler->pushModal(focusOwner, mouseOwner);
127 }
128 }
129
130 ~ModalScope() noexcept
131 {
132 if (mHandler != nullptr && !mReleased) {
133 mWasPopped = true;
134 // cppcheck-suppress throwInNoexceptFunction
135 mHandler->popModal();
136 }
137
138 if (!mWasPopped && !mReleased) {
139 // ModalScope was destroyed without calling release() or popModal()
140 // This indicates a bug where the modal was not properly released
141 std::fprintf(
142 stderr,
143 "Warning: ModalScope destroyed without calling release() or popModal(). Did you forget "
144 "to call release()?\n");
145 }
146 }
147
148 ModalScope(ModalScope const &) = delete;
149 ModalScope& operator=(ModalScope const &) = delete;
150 ModalScope(ModalScope&&) = delete;
151 ModalScope& operator=(ModalScope&&) = delete;
152
158 void release()
159 {
160 mReleased = true;
161 mWasPopped = true;
162 }
163
164 private:
165 FocusHandler* mHandler{nullptr};
166 bool mReleased{false};
167 bool mWasPopped{false};
168 };
169
175 using InputModalScope [[deprecated("Use InputModalScope instead")]] = ModalScope;
176
184 virtual Widget* getFocusOwner() const;
185
192 virtual void releaseFocus(Widget* widget);
193
200 virtual void setFocusedWidget(Widget* widget);
201
207 virtual Widget* getFocused() const;
208
218 virtual void focusNext();
219
229 virtual void focusPrevious();
230
237 virtual void add(Widget* widget);
238
245 virtual void remove(Widget* widget);
246
253 virtual void focusNone();
254
261 virtual void tabNext();
262
269 virtual void tabPrevious();
270
279 virtual Widget* getDraggedWidget();
280
289 virtual void setDraggedWidget(Widget* draggedWidget);
290
300
309 virtual void setLastWidgetWithMouse(Widget* lastWidgetWithMouse);
310
318
325 virtual void setLastWidgetWithModalFocus(Widget* lastWidgetWithModalFocus);
326
334
341 virtual void setLastWidgetWithModalMouseInputFocus(Widget* lastWidgetWithModalMouseInputFocus);
342
351 virtual Widget* getLastWidgetPressed();
352
361 virtual void setLastWidgetPressed(Widget* lastWidgetPressed);
362
371 virtual void widgetHidden(Widget* widget);
372
373 protected:
379 virtual void distributeFocusLostEvent(Event const & focusEvent);
380
386 virtual void distributeFocusGainedEvent(Event const & focusEvent);
387
389 using WidgetVector = std::vector<Widget*>;
390
392 using WidgetIterator = WidgetVector::iterator;
393
409
413 std::vector<ModalState> mModalStack;
414
419
424
431
436
441
446
451 };
452} // namespace fcn
453
454#endif // INCLUDE_FIFECHAN_FOCUSHANDLER_HPP_
Base class for all GUI event objects.
Definition event.hpp:25
Modal scope management (RAII guard).
ModalScope(FocusHandler *handler, Widget *focusOwner, Widget *mouseOwner=nullptr)
RAII helper that pushes a modal state on construction and pops it on destruction unless release() is ...
void release()
Release ownership so the modal is not popped during destruction.
std::vector< ModalState > mModalStack
Holds the modal stack for nested modal dialogs.
Widget * mFocusedWidget
Holds the focused widget.
virtual bool hasModalFocus() const
Checks if any modal state is active.
virtual Widget * getLastWidgetWithMouse()
Gets the last widget with the mouse.
ModalScope InputModalScope
Alias for ModalScope to clarify its purpose for input handling.
std::vector< Widget * > WidgetVector
Vector of Widget pointers.
WidgetVector mWidgets
Holds the widgets currently being handled by the focus handler.
Widget * mDraggedWidget
Holds the dragged widget.
virtual Widget * getFocusOwner() const
Gets the active input root widget for focus routing.
virtual void focusPrevious()
Focuses the previous widget added to a container.
virtual void widgetHidden(Widget *widget)
Informs the focus handler that a widget was hidden.
virtual void setLastWidgetWithMouse(Widget *lastWidgetWithMouse)
Sets the last widget with the mouse.
virtual void popModal() noexcept
Pops the current modal state from the stack.
virtual void focusNone()
Focuses nothing.
virtual Widget * getDraggedWidget()
Gets the widget being dragged.
Widget * mLastWidgetWithModalMouseInputFocus
Holds the last widget with modal mouse input focus.
virtual void setFocusedWidget(Widget *widget)
Sets the focused widget directly.
Widget * mLastWidgetPressed
Holds the last widget pressed.
virtual void add(Widget *widget)
Adds a widget to by handles by the focus handler.
virtual void setLastWidgetWithModalFocus(Widget *lastWidgetWithModalFocus)
Sets the last widget with modal focus.
virtual void pushModal(Widget *focusOwner, Widget *mouseOwner=nullptr)
Pushes a new modal state onto the stack.
virtual void setLastWidgetPressed(Widget *lastWidgetPressed)
Sets the last widget pressed.
virtual Widget * getLastWidgetWithModalMouseInputFocus()
Gets the last widget with modal mouse input focus.
virtual Widget * getLastWidgetWithModalFocus()
Gets the last widget with modal focus.
virtual void remove(Widget *widget)
Removes a widget from the focus handler.
virtual bool isFocused(Widget const *widget) const
Checks if a widget is focused.
virtual void distributeFocusGainedEvent(Event const &focusEvent)
Distributes a focus gained event.
virtual void tabPrevious()
Focuses the previous widget which allows tabbing in unless current focused Widget disallows tabbing o...
Widget * mLastWidgetWithModalFocus
Holds the last widget with modal focus.
virtual Widget * getMouseCaptureOwner() const
Gets the widget with modal mouse input focus.
virtual void clearModal()
Clears all modal states from the stack.
virtual void tabNext()
Focuses the next widget which allows tabbing in unless the current focused Widget disallows tabbing o...
virtual void distributeFocusLostEvent(Event const &focusEvent)
Distributes a focus lost event.
virtual void focusNext()
Focuses the next widget added to a container.
WidgetVector::iterator WidgetIterator
Iterator for WidgetVector.
virtual void releaseFocus(Widget *widget)
Releases focus for the specified widget if it is currently focused.
virtual void setDraggedWidget(Widget *draggedWidget)
Sets the widget being dragged.
virtual void requestFocus(Widget *widget)
Requests focus for a widget.
virtual Widget * getLastWidgetPressed()
Gets the last widget pressed.
Widget * mLastWidgetWithMouse
Holds the last widget with the mouse.
virtual void setLastWidgetWithModalMouseInputFocus(Widget *lastWidgetWithModalMouseInputFocus)
Sets the last widget with modal mouse input focus.
virtual Widget * getFocused() const
Gets the widget with focus.
Abstract base class defining the common behavior, properties, and lifecycle of all GUI elements.
Definition widget.hpp:56
Used replacement tokens by configure_file():
Represents a single modal state level with focus and mouse owners.
Widget * focusOwner
Widget with modal focus at this level.
Widget * mouseOwner
Widget with modal mouse input at this level.