]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
6ac4d2aa7d7679bc39512614ff32705f835891b6
[libs/gltk.git] / source / widget.cpp
1 #include <msp/gl/matrix.h>
2 #include <msp/strings/format.h>
3 #include "container.h"
4 #include "resources.h"
5 #include "root.h"
6 #include "widget.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GLtk {
12
13 Widget::Widget():
14         style(0),
15         state(NORMAL),
16         visible(true),
17         input_type(INPUT_NONE),
18         parent(0),
19         rebuild_needed(false)
20 { }
21
22 Widget::~Widget()
23 {
24         if(parent)
25         {
26                 Container *p = parent;
27                 parent = 0;
28                 p->remove(*this);
29         }
30 }
31
32 void Widget::set_position(int x, int y)
33 {
34         set_geometry(Geometry(x, y, geom.w, geom.h));
35 }
36
37 void Widget::set_size(unsigned w, unsigned h)
38 {
39         set_geometry(Geometry(geom.x, geom.y, w, h));
40 }
41
42 void Widget::autosize()
43 {
44         Geometry ageom;
45         autosize(ageom);
46         set_geometry(ageom);
47 }
48
49 void Widget::autosize(Geometry &ageom) const
50 {
51         if(!style)
52                 return;
53
54         ageom = Geometry(geom.x, geom.y, 0, 0);
55         for(const Part &p: style->get_parts())
56         {
57                 if(p.get_name().empty())
58                 {
59                         const Geometry &pgeom = p.get_geometry();
60                         const Sides &pmargin = p.get_margin();
61                         ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right);
62                         ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom);
63                 }
64                 else
65                         autosize_special(p, ageom);
66         }
67 }
68
69 void Widget::set_geometry(const Geometry &g)
70 {
71         bool size_changed = (g.w!=geom.w || g.h!=geom.h);
72         geom = g;
73         if(size_changed)
74         {
75                 on_size_change();
76                 mark_rebuild();
77         }
78 }
79
80 void Widget::map_coords_to_ancestor(int &x, int &y, const Widget &ancestor) const
81 {
82         for(const Widget *w=this; w; w=w->get_parent())
83         {
84                 if(w==&ancestor)
85                         return;
86
87                 const Geometry &wgeom = w->get_geometry();
88                 x += wgeom.x;
89                 y += wgeom.y;
90         }
91
92         throw hierarchy_error("not an ancestor");
93 }
94
95 void Widget::map_coords_to_root(int &x, int &y) const
96 {
97         for(const Widget *w=this; w; w=w->get_parent())
98         {
99                 const Geometry &wgeom = w->get_geometry();
100                 x += wgeom.x;
101                 y += wgeom.y;
102         }
103 }
104
105 void Widget::set_parent(Container *p)
106 {
107         if(parent && p)
108                 throw hierarchy_error("widget already parented");
109         else if(p==parent)
110                 return;
111
112         try
113         {
114                 parent = p;
115
116                 on_reparent();
117                 update_style();
118         }
119         catch(...)
120         {
121                 // The container has not yet added the widget as its child
122                 parent = 0;
123                 throw;
124         }
125 }
126
127 void Widget::set_style(const string &s)
128 {
129         style_name = s;
130         update_style();
131 }
132
133 void Widget::update_style()
134 {
135         Widget *top;
136         for(top=this; top->parent; top=top->parent) ;
137         Root *root = dynamic_cast<Root *>(top);
138         if(!root)
139                 style = 0;
140         else
141         {
142                 string sname = get_class();
143                 if(!style_name.empty())
144                 {
145                         sname += '-';
146                         sname += style_name;
147                 }
148
149                 style = &root->get_resources().get<Style>(sname);
150         }
151
152         on_style_change();
153         signal_autosize_changed.emit();
154         mark_rebuild();
155 }
156
157 void Widget::set_tooltip(const string &t)
158 {
159         tooltip = t;
160 }
161
162 void Widget::set_visible(bool v)
163 {
164         if(v==visible)
165                 return;
166
167         visible = v;
168
169         signal_visibility_changed.emit(visible);
170 }
171
172 void Widget::set_focus()
173 {
174         if(!parent)
175                 throw hierarchy_error("no parent");
176         if(!visible)
177                 throw logic_error("!visible");
178
179         signal_request_focus.emit();
180 }
181
182 void Widget::set_enabled(bool e)
183 {
184         set_state(DISABLED, (e ? NORMAL : DISABLED));
185 }
186
187 void Widget::set_state(State mask, State bits)
188 {
189         State old_state = state;
190         state = (state&~mask)|bits;
191         if(style && style->compare_states(old_state, state))
192                 mark_rebuild();
193 }
194
195 void Widget::set_animation_interval(const Time::TimeDelta &iv)
196 {
197         if(iv<Time::zero)
198                 throw invalid_argument("Widget::set_animation_interval");
199
200         anim_interval = iv;
201         signal_request_animation.emit(anim_interval);
202 }
203
204 void Widget::stop_animation()
205 {
206         set_animation_interval(Time::zero);
207 }
208
209 void Widget::mark_rebuild()
210 {
211         if(rebuild_needed)
212                 return;
213
214         rebuild_needed = true;
215         signal_rebuild_needed.emit();
216 }
217
218 void Widget::rebuild_hierarchy()
219 {
220         if(rebuild_needed)
221         {
222                 rebuild_needed = false;
223                 rebuild();
224         }
225 }
226
227 void Widget::rebuild()
228 {
229         if(!style)
230                 return;
231
232         PartCache::Rebuild rebuild_cache(part_cache);
233         for(const Part &p: style->get_parts())
234         {
235                 if(p.get_name().empty())
236                         p.build(geom, state, part_cache);
237                 else
238                         rebuild_special(p);
239         }
240 }
241
242 void Widget::rebuild_special(const Part &part)
243 {
244         part_cache.insert_special(part);
245 }
246
247 void Widget::render(GL::Renderer &renderer) const
248 {
249         if(!style)
250                 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
251
252         static const GL::Tag texture_tag("ui_tex");
253
254         GL::Renderer::Push _push(renderer);
255         int x = 0;
256         int y = 0;
257         map_coords_to_root(x, y);
258         renderer.set_matrix(GL::Matrix::translation(x, y, 0));
259         for(const CachedPart &p: part_cache.get_parts())
260         {
261                 if(p.mesh && p.texture)
262                 {
263                         renderer.set_texture(texture_tag, p.texture, &style->get_sampler());
264                         p.mesh->draw(renderer);
265                 }
266                 else if(p.part)
267                         render_special(*p.part, renderer);
268         }
269 }
270
271 void Widget::pointer_enter()
272 {
273         set_state(HOVER);
274 }
275
276 void Widget::pointer_leave()
277 {
278         clear_state(HOVER);
279 }
280
281 void Widget::touch_press(int x, int y, unsigned finger)
282 {
283         if(finger==0)
284                 button_press(x, y, 1);
285 }
286
287 void Widget::touch_release(int x, int y, unsigned finger)
288 {
289         if(finger==0)
290                 button_release(x, y, 1);
291 }
292
293 void Widget::touch_motion(int x, int y, unsigned finger)
294 {
295         if(finger==0)
296                 pointer_motion(x, y);
297 }
298
299 void Widget::focus_in()
300 {
301         set_state(FOCUS);
302 }
303
304 void Widget::focus_out()
305 {
306         clear_state(FOCUS);
307 }
308
309
310 Widget::Loader::Loader(Widget &w):
311         DataFile::ObjectLoader<Widget>(w)
312 {
313         add("position", &Loader::position);
314         add("size",     &Loader::size);
315         add("style",    &Loader::style);
316         add("visible",  &Widget::visible);
317 }
318
319 void Widget::Loader::position(int x, int y)
320 {
321         obj.set_position(x, y);
322 }
323
324 void Widget::Loader::size(unsigned w, unsigned h)
325 {
326         obj.set_size(w, h);
327 }
328
329 void Widget::Loader::style(const string &s)
330 {
331         obj.set_style(s);
332 }
333
334 } // namespace GLtk
335 } // namespace Msp