libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
widget.h
Go to the documentation of this file.
1
8#pragma once
9
12#include <promeki/core/rect.h>
13#include <promeki/core/size2d.h>
14#include <promeki/core/color.h>
15
17
18class TuiLayout;
19class TuiPainter;
20class KeyEvent;
21class MouseEvent;
22
28class TuiPaintEvent : public Event {
29 public:
30 static const Type Paint;
31 TuiPaintEvent() : Event(Paint) {}
32};
33
37class TuiResizeEvent : public Event {
38 public:
39 static const Type Resize;
40 TuiResizeEvent(const Size2Di32 &size)
41 : Event(Resize), _size(size) {}
42 const Size2Di32 &size() const { return _size; }
43 private:
44 Size2Di32 _size;
45};
46
56
68
88class TuiWidget : public ObjectBase {
89 PROMEKI_OBJECT(TuiWidget, ObjectBase)
90 public:
96
98 virtual ~TuiWidget();
99
101 const Rect2Di32 &geometry() const { return _geometry; }
102
105
107 int x() const { return _geometry.x(); }
108
110 int y() const { return _geometry.y(); }
111
113 int width() const { return _geometry.width(); }
114
116 int height() const { return _geometry.height(); }
117
119 Size2Di32 size() const {
120 return Size2Di32(_geometry.width(), _geometry.height());
121 }
122
124 const Size2Di32 &minimumSize() const { return _minimumSize; }
125
127 void setMinimumSize(const Size2Di32 &size) { _minimumSize = size; }
128
130 const Size2Di32 &maximumSize() const { return _maximumSize; }
131
133 void setMaximumSize(const Size2Di32 &size) { _maximumSize = size; }
134
136 TuiSizePolicy sizePolicy() const { return _sizePolicy; }
137
139 void setSizePolicy(TuiSizePolicy policy) { _sizePolicy = policy; }
140
142 bool isVisible() const { return _visible; }
143
151
153 void show();
154
156 void hide();
157
159 void setVisible(bool visible);
160
162 bool isEnabled() const { return _enabled; }
163
165 void setEnabled(bool enabled) { _enabled = enabled; }
166
168 TuiFocusPolicy focusPolicy() const { return _focusPolicy; }
169
171 void setFocusPolicy(TuiFocusPolicy policy) { _focusPolicy = policy; }
172
174 bool hasFocus() const { return _focused; }
175
177 void setFocus();
178
180 void update();
181
183 bool isDirty() const { return _dirty; }
184
186 void clearDirty() { _dirty = false; }
187
189 TuiLayout *layout() const { return _layout; }
190
193
198
203
208
213
215 virtual Size2Di32 sizeHint() const;
216
218 virtual Size2Di32 minimumSizeHint() const;
219
220 // Signals
221 PROMEKI_SIGNAL(resized, Size2Di32)
222 PROMEKI_SIGNAL(visibilityChanged, bool)
223
224 protected:
226 virtual void paintEvent(TuiPaintEvent *e);
227
229 virtual void keyEvent(KeyEvent *e);
230
232 virtual void mouseEvent(MouseEvent *e);
233
236
238 virtual void focusInEvent(Event *e);
239
241 virtual void focusOutEvent(Event *e);
242
244 void event(Event *e) override;
245
246 private:
247 friend class TuiApplication;
248
249 Rect2Di32 _geometry;
250 Size2Di32 _minimumSize;
251 Size2Di32 _maximumSize = Size2Di32(9999, 9999);
252 TuiSizePolicy _sizePolicy = SizePreferred;
253 TuiFocusPolicy _focusPolicy = NoFocus;
254 bool _visible = true;
255 bool _enabled = true;
256 bool _focused = false;
257 bool _dirty = true;
258 TuiLayout *_layout = nullptr;
259};
260
Base class for the event system.
Definition event.h:29
Event delivered when a key is pressed or released.
Definition keyevent.h:22
Dynamic array container wrapping std::vector.
Definition list.h:40
Event delivered for mouse input.
Definition mouseevent.h:23
Base object for promeki.
Definition objectbase.h:129
ObjectBase * parent() const
Returns the parent object, if one. nullptr if none.
Definition objectbase.h:258
T height() const
Returns the height.
Definition rect.h:57
T width() const
Returns the width.
Definition rect.h:54
T x() const
Returns the X coordinate of the left edge.
Definition rect.h:48
T y() const
Returns the Y coordinate of the top edge.
Definition rect.h:51
Application class for TUI programs.
Definition application.h:33
Abstract base class for TUI layout managers.
Definition layout.h:26
Event delivered when a TUI widget needs to repaint.
Definition widget.h:28
Painting context for TUI widgets.
Definition painter.h:26
Event delivered when a TUI widget is resized.
Definition widget.h:37
Base class for all TUI widgets.
Definition widget.h:88
virtual void keyEvent(KeyEvent *e)
Called on keyboard input. Override in subclasses.
int height() const
Returns the height.
Definition widget.h:116
TuiLayout * layout() const
Returns the layout, if one is set.
Definition widget.h:189
Point2Di32 mapToParent(const Point2Di32 &p) const
Maps a local point to the parent's coordinate system.
void setEnabled(bool enabled)
Sets enabled state.
Definition widget.h:165
virtual void resizeEvent(TuiResizeEvent *e)
Called when the widget is resized. Override in subclasses.
virtual void focusOutEvent(Event *e)
Called when the widget loses focus.
virtual Size2Di32 minimumSizeHint() const
Returns the minimum size hint. Override in subclasses.
TuiFocusPolicy focusPolicy() const
Returns the focus policy.
Definition widget.h:168
virtual ~TuiWidget()
Destructor.
virtual Size2Di32 sizeHint() const
Returns the preferred size. Override in subclasses.
int x() const
Returns the X coordinate relative to parent.
Definition widget.h:107
void setMaximumSize(const Size2Di32 &size)
Sets the maximum size constraint.
Definition widget.h:133
bool hasFocus() const
Returns true if this widget has focus.
Definition widget.h:174
void show()
Shows the widget.
void setMinimumSize(const Size2Di32 &size)
Sets the minimum size constraint.
Definition widget.h:127
Point2Di32 mapToGlobal(const Point2Di32 &p) const
Maps a local point to global (screen) coordinates.
TuiWidget(ObjectBase *parent=nullptr)
Constructs a TuiWidget.
TuiSizePolicy sizePolicy() const
Returns the size policy.
Definition widget.h:136
bool isEnabled() const
Returns true if the widget is enabled.
Definition widget.h:162
void setLayout(TuiLayout *layout)
Sets the layout for this widget.
void setGeometry(const Rect2Di32 &rect)
Sets the geometry.
virtual void focusInEvent(Event *e)
Called when the widget gains focus.
void setSizePolicy(TuiSizePolicy policy)
Sets the size policy.
Definition widget.h:139
void hide()
Hides the widget.
void setFocusPolicy(TuiFocusPolicy policy)
Sets the focus policy.
Definition widget.h:171
const Rect2Di32 & geometry() const
Returns the geometry relative to parent.
Definition widget.h:101
const Size2Di32 & minimumSize() const
Returns the minimum size constraint.
Definition widget.h:124
virtual void paintEvent(TuiPaintEvent *e)
Called to paint the widget. Override in subclasses.
void setFocus()
Requests focus for this widget.
Point2Di32 mapFromParent(const Point2Di32 &p) const
Maps a parent point to local coordinates.
void event(Event *e) override
Event dispatch override.
Size2Di32 size() const
Returns the size.
Definition widget.h:119
void update()
Marks the widget as needing a repaint.
void clearDirty()
Clears the dirty flag.
Definition widget.h:186
bool isEffectivelyVisible() const
Returns true if the widget and all its ancestors are visible.
const Size2Di32 & maximumSize() const
Returns the maximum size constraint.
Definition widget.h:130
bool isDirty() const
Returns true if the widget needs repainting.
Definition widget.h:183
void setVisible(bool visible)
Sets visibility.
int y() const
Returns the Y coordinate relative to parent.
Definition widget.h:110
virtual void mouseEvent(MouseEvent *e)
Called on mouse input. Override in subclasses.
bool isVisible() const
Returns true if the widget's own visibility flag is set.
Definition widget.h:142
Point2Di32 mapFromGlobal(const Point2Di32 &p) const
Maps a global (screen) point to local coordinates.
int width() const
Returns the width.
Definition widget.h:113
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
Size2DTemplate< int32_t > Size2Di32
2D size with int32_t components.
Definition size2d.h:105
TuiSizePolicy
Size policy for TUI widget layout.
Definition widget.h:60
@ SizePreferred
Prefers sizeHint(), can grow or shrink.
Definition widget.h:64
@ SizeMinimum
Can grow, prefers minimum size.
Definition widget.h:62
@ SizeMaximum
Can shrink, prefers maximum size.
Definition widget.h:63
@ SizeMinimumExpanding
Prefers minimum but takes extra space.
Definition widget.h:66
@ SizeFixed
Fixed size, does not grow or shrink.
Definition widget.h:61
@ SizeExpanding
Takes all available space.
Definition widget.h:65
TuiFocusPolicy
Focus policy for TUI widgets.
Definition widget.h:50
@ TabFocus
Widget can receive focus via Tab.
Definition widget.h:52
@ NoFocus
Widget cannot receive focus.
Definition widget.h:51
@ ClickFocus
Widget can receive focus via mouse click.
Definition widget.h:53
@ StrongFocus
Widget can receive focus via Tab or mouse click.
Definition widget.h:54