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  // TODO: could be the same as Pt::Forms::SizePolicy::Mode
46  enum SizeMode
47  {
48  Fill, // -> Any
49  Preferred,
50  Fixed
51  };
52 
53  public:
54  TableLayout();
55 
56  virtual ~TableLayout();
57 
58  void addItem(Control& control, std::size_t row, std::size_t column);
59 
60  void removeItem(Control& control);
61 
62  void setColumn(std::size_t col, SizeMode mode, double size = 0);
63 
64  void setRow(std::size_t row, SizeMode mode, double size = 0);
65 
66  protected:
67  virtual void onRemoveControl(Control& control);
68 
69  virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
70 
71  virtual void onLayout(const Gfx::RectF& rect);
72 
73  private:
74  class SizeInfo
75  {
76  public:
77  SizeInfo()
78  : _mode(Preferred)
79  , _size(0)
80  { }
81 
82  SizeInfo(SizeMode m, double size)
83  : _mode(m)
84  , _size(size)
85  { }
86 
87  SizeMode mode() const
88  { return _mode; }
89 
90  double size() const
91  { return _size; }
92 
93  void setSize(double s)
94  { _size = s; }
95 
96  private:
97  SizeMode _mode;
98  double _size;
99  };
100 
101  std::vector<SizeInfo> _columnSizes;
102  std::vector<SizeInfo> _rowSizes;
103 
104  typedef std::vector<Control*> Row;
105  std::vector<Row> _rows;
106 };
107 
108 } // namespace
109 
110 } // namespace
111 
112 #endif