libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
layout.h
Go to the documentation of this file.
1
8#pragma once
9
12#include <promeki/core/rect.h>
13#include <promeki/core/list.h>
14
16
17class TuiWidget;
18
26class TuiLayout : public ObjectBase {
27 PROMEKI_OBJECT(TuiLayout, ObjectBase)
28 public:
29 TuiLayout(ObjectBase *parent = nullptr);
30 virtual ~TuiLayout();
31
33 virtual void addWidget(TuiWidget *widget);
34
36 void removeWidget(TuiWidget *widget);
37
39 virtual void addLayout(TuiLayout *layout);
40
42 void setMargins(int top, int right, int bottom, int left);
43
46
48 void setSpacing(int spacing) { _spacing = spacing; }
49
51 int spacing() const { return _spacing; }
52
54 void margins(int &top, int &right, int &bottom, int &left) const {
55 top = _marginTop;
56 right = _marginRight;
57 bottom = _marginBottom;
58 left = _marginLeft;
59 }
60
65 virtual void calculateLayout(const Rect2Di32 &available) = 0;
66
70 virtual Size2Di32 sizeHint() const;
71
73 const List<TuiWidget *> &widgets() const { return _widgets; }
74
76 const List<TuiLayout *> &layouts() const { return _layouts; }
77
78 protected:
83
84 List<TuiWidget *> _widgets;
85 List<TuiLayout *> _layouts;
86
87 private:
88 int _spacing = 0;
89 int _marginTop = 0;
90 int _marginRight = 0;
91 int _marginBottom = 0;
92 int _marginLeft = 0;
93};
94
99 LeftToRight,
100 RightToLeft,
101 TopToBottom,
102 BottomToTop
103};
104
112class TuiBoxLayout : public TuiLayout {
113 PROMEKI_OBJECT(TuiBoxLayout, TuiLayout)
114 public:
121
123 TuiBoxDirection direction() const { return _direction; }
124
126 void addWidget(TuiWidget *widget) override;
127
129 void addLayout(TuiLayout *layout) override;
130
132 void addStretch(int factor = 1);
133
135 void setStretch(int index, int factor);
136
138 void calculateLayout(const Rect2Di32 &available) override;
139
141 Size2Di32 sizeHint() const override;
142
143 private:
145 struct Item {
146 enum Type { WidgetItem, LayoutItem, StretchItem };
147 Type type;
148 TuiWidget *widget = nullptr;
149 TuiLayout *layout = nullptr;
150 int stretchFactor = 0;
151 };
152
153 TuiBoxDirection _direction;
154 List<Item> _items;
155};
156
161 PROMEKI_OBJECT(TuiHBoxLayout, TuiBoxLayout)
162 public:
164 : TuiBoxLayout(LeftToRight, parent) {}
165};
166
171 PROMEKI_OBJECT(TuiVBoxLayout, TuiBoxLayout)
172 public:
174 : TuiBoxLayout(TopToBottom, parent) {}
175};
176
180class TuiGridLayout : public TuiLayout {
181 PROMEKI_OBJECT(TuiGridLayout, TuiLayout)
182 public:
183 TuiGridLayout(ObjectBase *parent = nullptr);
184
188 void addWidget(TuiWidget *widget, int row, int col,
189 int rowSpan = 1, int colSpan = 1);
190
192 void setRowStretch(int row, int factor);
193
195 void setColumnStretch(int col, int factor);
196
198 void setRowMinimumHeight(int row, int height);
199
201 void setColumnMinimumWidth(int col, int width);
202
204 void calculateLayout(const Rect2Di32 &available) override;
205
206 private:
207 struct GridItem {
208 TuiWidget *widget;
209 int row;
210 int col;
211 int rowSpan;
212 int colSpan;
213 };
214 List<GridItem> _items;
215 Map<int, int> _rowStretch;
216 Map<int, int> _colStretch;
217 Map<int, int> _rowMinHeight;
218 Map<int, int> _colMinWidth;
219 int _rowCount = 0;
220 int _colCount = 0;
221};
222
Dynamic array container wrapping std::vector.
Definition list.h:40
Base object for promeki.
Definition objectbase.h:129
ObjectBase * parent() const
Returns the parent object, if one. nullptr if none.
Definition objectbase.h:258
Layout that arranges widgets in a horizontal or vertical line.
Definition layout.h:112
TuiBoxLayout(TuiBoxDirection direction, ObjectBase *parent=nullptr)
Constructs a TuiBoxLayout.
void addStretch(int factor=1)
Adds a stretch item with the given factor.
TuiBoxDirection direction() const
Returns the direction.
Definition layout.h:123
Size2Di32 sizeHint() const override
Returns the preferred size based on children and direction.
void calculateLayout(const Rect2Di32 &available) override
Calculates and applies the layout.
void addWidget(TuiWidget *widget) override
Adds a widget to the ordered item list.
void addLayout(TuiLayout *layout) override
Adds a sub-layout to the ordered item list.
void setStretch(int index, int factor)
Sets the stretch factor for the item at the given index.
Grid layout with rows and columns.
Definition layout.h:180
void setRowStretch(int row, int factor)
Sets the stretch factor for a row.
void addWidget(TuiWidget *widget, int row, int col, int rowSpan=1, int colSpan=1)
Adds a widget at the given row/column with optional span.
void setRowMinimumHeight(int row, int height)
Sets the minimum height for a row.
void setColumnStretch(int col, int factor)
Sets the stretch factor for a column.
void setColumnMinimumWidth(int col, int width)
Sets the minimum width for a column.
void calculateLayout(const Rect2Di32 &available) override
Calculates and applies the layout.
Horizontal box layout (left to right).
Definition layout.h:160
Abstract base class for TUI layout managers.
Definition layout.h:26
int spacing() const
Returns the spacing.
Definition layout.h:51
void removeWidget(TuiWidget *widget)
Removes a widget from this layout.
Rect2Di32 contentRect(const Rect2Di32 &available) const
Returns the content rect after subtracting margins.
void margins(int &top, int &right, int &bottom, int &left) const
Returns the margins.
Definition layout.h:54
virtual void calculateLayout(const Rect2Di32 &available)=0
Calculates and applies the layout to the given rectangle.
virtual void addWidget(TuiWidget *widget)
Adds a widget to this layout.
virtual Size2Di32 sizeHint() const
Returns the preferred size for this layout based on its children.
void setMargins(int margin)
Sets uniform margins.
Definition layout.h:45
virtual void addLayout(TuiLayout *layout)
Adds a nested layout.
const List< TuiWidget * > & widgets() const
Returns the list of managed widgets.
Definition layout.h:73
void setSpacing(int spacing)
Sets the spacing between items.
Definition layout.h:48
void setMargins(int top, int right, int bottom, int left)
Sets the margins (top, right, bottom, left).
const List< TuiLayout * > & layouts() const
Returns the list of sub-layouts.
Definition layout.h:76
Vertical box layout (top to bottom).
Definition layout.h:170
Base class for all TUI widgets.
Definition widget.h:88
TuiBoxDirection
Box layout direction.
Definition layout.h:98
#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
TextStream & right(TextStream &s)
Sets field alignment to Right.
Definition textstream.h:451
TextStream & left(TextStream &s)
Sets field alignment to Left.
Definition textstream.h:445