cgv
Loading...
Searching...
No Matches
layout.cxx
1#include <cgv/gui/layout.h>
2#include <cgv/gui/resizable.h>
3
4namespace cgv {
5 namespace gui {
6
7 // constructor that sets a container
8 layout::layout(cgv::base::group_ptr container)
9 {
10 w = 0;
11 h = 0;
12 min_w = 0;
13 min_h = 0;
14 default_w = 0;
15 default_h = 0;
16 spacings_name = "compact";
17 spacings = get_layout_spacings(spacings_name);
18 set_container(container);
19 }
20
21
22 // empty destructor
23 layout::~layout()
24 {
25 }
26
27
28 // set a container to be layouted
29 void layout::set_container(cgv::base::group_ptr container)
30 {
31 if (!container) {
32 std::cerr<<"Error: A container was NULL (maybe an unsuccessfull dynamic_cast?)!"<<std::endl;
33 return;
34 }
35
36 this->container = container;
37 }
38
39
40 // get the layouted container
41 cgv::base::group_ptr layout::get_container()
42 {
43 return container;
44 }
45
46
47 // get a child from the container. make sure that the child can be resized
48 cgv::base::base_ptr layout::get_child(unsigned int i)
49 {
50 if (!container) {
51 std::cerr<<"Layout is not connected to any container"<<std::endl;
52 return 0;
53 }
54
55 return container->get_child(i);
56 }
57
58
59 int layout::get_child_layout_hints(cgv::base::base_ptr child)
60 {
61 // nothing to do if the pointer is unset
62 if (child.empty())
63 return 0;
64
65 // get information via the aligmnent string
66 std::string align("");
67 align = child->get<std::string>("alignment");
68
69 int info = 0;
70
71 for (unsigned int i=0; i<align.length(); i++)
72 switch(align[i]) {
73 case 'x':
74 info |= LH_HEXPAND;
75 break;
76 case 's':
77 info |= LH_HSHRINK;
78 break;
79 case 'f':
80 info |= LH_HFILL;
81 break;
82 case 'X':
83 info |= LH_VEXPAND;
84 break;
85 case 'S':
86 info |= LH_VSHRINK;
87 break;
88 case 'F':
89 info |= LH_VFILL;
90 break;
91 case 'l':
92 info |= LH_LEFT;
93 break;
94 case 'r':
95 info |= LH_RIGHT;
96 break;
97 case 'c':
98 info |= LH_CENTER;
99 break;
100 case 't':
101 info |= LH_TOP;
102 break;
103 case 'b':
104 info |= LH_BOTTOM;
105 break;
106 case 'm':
107 info |= LH_MIDDLE;
108 break;
109 }
110
111 return info;
112 }
113
114
115 // get the size of a child
116 void layout::get_child_size(const cgv::base::base_ptr child, int &width, int &height)
117 {
118 width = height = 0;
119
120 if (!child)
121 return;
122
123 width = child->get<int>("w");
124 height = child->get<int>("h");
125 }
126
127
128 // set the size of a child
129 void layout::set_child_size(const cgv::base::base_ptr child, int width, int height)
130 {
131 if (!child)
132 return;
133
134 child->set<int>("w", width);
135 child->set<int>("h", height);
136 child->set<int>("dolayout", 0);
137 }
138
139
140 // get the default size of a child
141 void layout::get_child_default_size(const cgv::base::base_ptr child, int &width, int &height)
142 {
143 width = height = 0;
144 if (!child)
145 return;
146
147 width = height = 0;
148 // try to get the default width and height
149 width = child->get<int>("dw");
150 height = child->get<int>("dh");
151 }
152
153
154 void layout::get_child_minimum_size(const cgv::base::base_ptr child, int &width, int &height)
155 {
156 width = height = 0;
157 if (!child)
158 return;
159
160 // try to get the mimimum size of a child
161 width = child->get<int>("mw");
162 height = child->get<int>("mh");
163 }
164
165
166
167
168 // get the position of a child
169 void layout::get_child_position(const cgv::base::base_ptr child, int &x, int &y)
170 {
171 x = y = 0;
172
173 if (!child)
174 return;
175
176 x = child->get<int>("x");
177 y = child->get<int>("y");
178 }
179
180
181 // set the position of a child
182 void layout::set_child_position(const cgv::base::base_ptr child, int x, int y)
183 {
184 if (!child)
185 return;
186
187 child->set<int>("x", x);
188 child->set<int>("y", y);
189 }
190
191
192
193
194 void layout::resize(int w, int h)
195 {
196 this->w = w;
197 this->h = h;
198
199 update();
200 }
201
202
204 {
205 return "spacings:string";
206 }
207
208
209 bool layout::set_void(const std::string &property, const std::string &value_type, const void *value_ptr)
210 {
211 if (property == "spacings") {
212 cgv::type::get_variant(spacings_name, value_type, value_ptr);
213 spacings = get_layout_spacings(spacings_name);
214 update();
215 } else
216 if (property == "mw")
217 cgv::type::get_variant(min_w, value_type, value_ptr);
218 else
219 if (property == "mh")
220 cgv::type::get_variant(min_h, value_type, value_ptr);
221 else
222 if (property == "dw")
223 cgv::type::get_variant(default_w, value_type, value_ptr);
224 else
225 if (property == "dh")
226 cgv::type::get_variant(default_h, value_type, value_ptr);
227 else
228 return false;
229
230 return true;
231 }
232
233
234 bool layout::get_void(const std::string &property, const std::string &value_type, void *value_ptr)
235 {
236 if (property == "spacings")
237 cgv::type::set_variant(spacings_name, value_type, value_ptr);
238 else
239 if (property == "mw")
240 cgv::type::set_variant(min_w, value_type, value_ptr);
241 else
242 if (property == "mh")
243 cgv::type::set_variant(min_h, value_type, value_ptr);
244 else
245 if (property == "dw")
246 cgv::type::set_variant(default_w, value_type, value_ptr);
247 else
248 if (property == "dh")
249 cgv::type::set_variant(default_h, value_type, value_ptr);
250 else
251 return false;
252
253 return true;
254 }
255 }
256}
bool empty() const
check if pointer is not yet set
Definition ref_ptr.h:230
virtual std::string get_property_declarations()
return a semicolon separated list of property declarations
Definition layout.cxx:203
virtual bool set_void(const std::string &property, const std::string &value_type, const void *value_ptr)
abstract interface for the setter of a dynamic property.
Definition layout.cxx:209
virtual bool get_void(const std::string &property, const std::string &value_type, void *value_ptr)
abstract interface for the getter of a dynamic property.
Definition layout.cxx:234
virtual void update()
this virtual update allows for example to ask a view to update the viewed value. The default implemen...
Definition layout.h:69
the cgv namespace
Definition print.h:11