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 
36 namespace Pt {
37 
38 namespace Forms {
39 
40 class 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 
112 class PT_FORMS_API TableLayout2 : public Layout
113 {
114  typedef Layout Base;
115 
116  public:
117  enum SizeMode
118  {
119  Fill,
120  Preferred,
121  Fixed
122  };
123 
124  public:
125  TableLayout2();
126 
127  virtual ~TableLayout2();
128 
129  void addItem(Control& control, std::size_t row, std::size_t column);
130 
131  void removeItem(Control& control);
132 
133  void setColumn(std::size_t col, SizeMode mode, double size = 0);
134 
135  void setRow(std::size_t row, SizeMode mode, double size = 0);
136 
137  protected:
138  virtual void onRemoveControl(Control& control);
139 
140  virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
141 
142  virtual void onLayout(const Gfx::RectF& rect);
143 
144  private:
145  class SizeInfo
146  {
147  public:
148  SizeInfo()
149  : _mode(Preferred)
150  , _size(0)
151  { }
152 
153  SizeInfo(SizeMode m, double size)
154  : _mode(m)
155  , _size(size)
156  { }
157 
158  SizeMode mode() const
159  { return _mode; }
160 
161  double size() const
162  { return _size; }
163 
164  private:
165  SizeMode _mode;
166  double _size;
167  };
168 
169  typedef std::vector<Control*> Row;
170 
171  Control* cell(std::size_t row, std::size_t column) const;
172 
173  SizeMode columnMode(std::size_t column) const;
174 
175  SizeMode rowMode(std::size_t row) const;
176 
177  void measureItem(Control& item,
178  SizeMode colMode,
179  SizeMode rowMode,
180  double width,
181  double height);
182 
183  void computeTracks(std::vector<double>& tracks,
184  const std::vector<SizeInfo>& infos,
185  std::size_t count,
186  double content,
187  bool horizontal) const;
188 
189  private:
190  std::vector<SizeInfo> _columnSizes;
191  std::vector<SizeInfo> _rowSizes;
192  std::vector<Row> _rows;
193 };
194 
195 } // namespace
196 
197 } // namespace
198 
199 #endif
Core module.
Definition: Allocator.h:33
Size with floating-point width and height.
Definition: Size.h:47
A view that can be attached as application content.
Definition: Control.h:84
A widget that connects controls to a paint surface.
Definition: View.h:66
Table layout with two-phase measure for Fill and nested layouts.
Definition: TableLayout.h:113
Rect with floating-point coordinates.
Definition: Rect.h:47
A control that arranges child controls.
Definition: Layout.h:54