TableLayout.h
1/* Copyright (C) 2017 Marc Boris Duerner
2
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
7
8 As a special exception, you may use this file as part of a free
9 software library without restriction. Specifically, if other files
10 instantiate templates or use macros or inline functions from this
11 file, or you compile this file and link it with other files to
12 produce an executable, this file does not by itself cause the
13 resulting executable to be covered by the GNU General Public
14 License. This exception does not however invalidate any other
15 reasons why the executable file might be covered by the GNU Library
16 General Public License.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 MA 02110-1301 USA
27*/
28
29#ifndef PT_FORMS_TABLELAYOUT_H
30#define PT_FORMS_TABLELAYOUT_H
31
32#include <Pt/Forms/Api.h>
33#include <Pt/Forms/Layout.h>
34#include <vector>
35
36namespace Pt {
37
38namespace Forms {
39
40class PT_FORMS_API TableLayout : public Layout
41{
42 typedef Layout Base;
43
44 public:
45 enum SizeMode
46 {
47 Fill,
48 Preferred,
49 Fixed
50 };
51
52 public:
53 TableLayout();
54
55 virtual ~TableLayout();
56
57 void addItem(Control& control, std::size_t row, std::size_t column);
58
59 void removeItem(Control& control);
60
61 void setColumn(std::size_t col, SizeMode mode, double size = 0);
62
63 void setRow(std::size_t row, SizeMode mode, double size = 0);
64
65 protected:
66 virtual void onRemoveControl(Control& control);
67
68 virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
69
70 virtual void onLayout(const Gfx::RectF& rect);
71
72 private:
73 class SizeInfo
74 {
75 public:
76 SizeInfo()
77 : _mode(Preferred)
78 , _size(0)
79 { }
80
81 SizeInfo(SizeMode m, double size)
82 : _mode(m)
83 , _size(size)
84 { }
85
86 SizeMode mode() const
87 { return _mode; }
88
89 double size() const
90 { return _size; }
91
92 void setSize(double s)
93 { _size = s; }
94
95 private:
96 SizeMode _mode;
97 double _size;
98 };
99
100 std::vector<SizeInfo> _columnSizes;
101 std::vector<SizeInfo> _rowSizes;
102
103 typedef std::vector<Control*> Row;
104 std::vector<Row> _rows;
105};
106
107
141class PT_FORMS_API TableLayout2 : public Layout
142{
143 typedef Layout Base;
144
145 public:
149 {
153
157
161 };
162
163 public:
167
170 virtual ~TableLayout2();
171
176 void addItem(Control& control, std::size_t row, std::size_t column);
177
180 void removeItem(Control& control);
181
184 void setColumn(std::size_t col, SizeMode mode, double size = 0);
185
188 void setRow(std::size_t row, SizeMode mode, double size = 0);
189
190 protected:
193 virtual void onRemoveControl(Control& control);
194
197 virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
198
201 virtual void onLayout(const Gfx::RectF& rect);
202
203 private:
204 class SizeInfo
205 {
206 public:
207 SizeInfo()
208 : _mode(Preferred)
209 , _size(0)
210 { }
211
212 SizeInfo(SizeMode m, double size)
213 : _mode(m)
214 , _size(size)
215 { }
216
217 SizeMode mode() const
218 { return _mode; }
219
220 double size() const
221 { return _size; }
222
223 private:
224 SizeMode _mode;
225 double _size;
226 };
227
228 typedef std::vector<Control*> Row;
229
230 Control* cell(std::size_t row, std::size_t column) const;
231
232 SizeMode columnMode(std::size_t column) const;
233
234 SizeMode rowMode(std::size_t row) const;
235
236 void measureItem(Control& item,
237 SizeMode colMode,
238 SizeMode rowMode,
239 double width,
240 double height);
241
242 void computeTracks(std::vector<double>& tracks,
243 const std::vector<SizeInfo>& infos,
244 std::size_t count,
245 double content,
246 bool horizontal) const;
247
248 private:
249 std::vector<SizeInfo> _columnSizes;
250 std::vector<SizeInfo> _rowSizes;
251 std::vector<Row> _rows;
252};
253
254} // namespace
255
256} // namespace
257
258#endif
Reusable view used as application content.
Definition Control.h:107
Control that arranges child controls.
Definition Layout.h:46
Constraint used when measuring a control.
Definition SizePolicy.h:48
TableLayout2()
Creates an empty table layout.
void addItem(Control &control, std::size_t row, std::size_t column)
Attaches control at row and column.
void removeItem(Control &control)
Detaches control without destroying it.
virtual ~TableLayout2()
Destroys the layout. Attached controls are not destroyed.
virtual void onLayout(const Gfx::RectF &rect)
Assigns each visible cell its track rectangle.
virtual void onRemoveControl(Control &control)
Clears the cell that held control.
virtual Gfx::SizeF onMeasure(const SizePolicy &policy)
Measures tracks, then Fill cells at the resolved sizes.
SizeMode
Sizing rule for a row or column track.
Definition TableLayout.h:149
@ Fill
The track shares leftover space.
Definition TableLayout.h:152
@ Preferred
The track uses the largest preferred child size.
Definition TableLayout.h:156
@ Fixed
The track uses a fixed size.
Definition TableLayout.h:160
void setColumn(std::size_t col, SizeMode mode, double size=0)
Sets column col to mode and optional fixed size.
void setRow(std::size_t row, SizeMode mode, double size=0)
Sets row row to mode and optional fixed size.
const Gfx::SizeF & size() const
Returns the size in local logical coordinates.
Graphical user interfaces.
Definition ActivateEvent.h:40
Core module.
Definition Allocator.h:33