]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
Adjust things to conform to changes in other libraries
[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 { }
20
21 Widget::~Widget()
22 {
23         if(parent)
24         {
25                 Container *p = parent;
26                 parent = 0;
27                 p->remove(*this);
28         }
29 }
30
31 void Widget::set_position(int x, int y)
32 {
33         set_geometry(Geometry(x, y, geom.w, geom.h));
34 }
35
36 void Widget::set_size(unsigned w, unsigned h)
37 {
38         set_geometry(Geometry(geom.x, geom.y, w, h));
39 }
40
41 void Widget::autosize()
42 {
43         Geometry ageom;
44         autosize(ageom);
45         set_geometry(ageom);
46 }
47
48 void Widget::autosize(Geometry &ageom) const
49 {
50         if(!style)
51                 return;
52
53         ageom = Geometry(geom.x, geom.y, 0, 0);
54         const Style::PartSeq &parts = style->get_parts();
55         for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
56         {
57                 if(i->get_name().empty())
58                 {
59                         const Geometry &pgeom = i->get_geometry();
60                         const Sides &pmargin = i->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(*i, 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_focusable(bool f)
173 {
174         input_type = (f ? INPUT_TEXT : INPUT_NONE);
175 }
176
177 void Widget::set_focus()
178 {
179         if(!parent)
180                 throw hierarchy_error("no parent");
181         if(!visible)
182                 throw logic_error("!visible");
183
184         signal_request_focus.emit();
185 }
186
187 void Widget::set_enabled(bool e)
188 {
189         set_state(DISABLED, (e ? NORMAL : DISABLED));
190 }
191
192 void Widget::set_state(State mask, State bits)
193 {
194         State old_state = state;
195         state = (state&~mask)|bits;
196         if(style && style->compare_states(old_state, state))
197                 mark_rebuild();
198 }
199
200 void Widget::set_animation_interval(const Time::TimeDelta &iv)
201 {
202         if(iv<Time::zero)
203                 throw invalid_argument("Widget::set_animation_interval");
204
205         anim_interval = iv;
206         signal_request_animation.emit(anim_interval);
207 }
208
209 void Widget::stop_animation()
210 {
211         set_animation_interval(Time::zero);
212 }
213
214 void Widget::mark_rebuild()
215 {
216         if(rebuild_needed)
217                 return;
218
219         rebuild_needed = true;
220         signal_rebuild_needed.emit();
221 }
222
223 void Widget::rebuild_hierarchy()
224 {
225         if(rebuild_needed)
226         {
227                 rebuild_needed = false;
228                 rebuild();
229         }
230 }
231
232 void Widget::rebuild()
233 {
234         if(!style)
235                 return;
236
237         PartCache::Rebuild rebuild_cache(part_cache);
238         const Style::PartSeq &parts = style->get_parts();
239         for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
240         {
241                 if(i->get_name().empty())
242                         i->build(geom, state, part_cache);
243                 else
244                         rebuild_special(*i);
245         }
246 }
247
248 void Widget::rebuild_special(const Part &part)
249 {
250         part_cache.insert_special(part);
251 }
252
253 void Widget::render(GL::Renderer &renderer) const
254 {
255         if(!style)
256                 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
257
258         static const GL::Tag texture_tag("ui_tex");
259
260         GL::Renderer::Push _push(renderer);
261         int x = 0;
262         int y = 0;
263         map_coords_to_root(x, y);
264         renderer.set_matrix(GL::Matrix::translation(x, y, 0));
265         const PartCache::PartList &parts = part_cache.get_parts();
266         for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
267         {
268                 if(i->mesh && i->texture)
269                 {
270                         renderer.set_texture(texture_tag, i->texture, &style->get_sampler());
271                         i->mesh->draw(renderer);
272                 }
273                 else if(i->part)
274                         render_special(*i->part, renderer);
275         }
276 }
277
278 void Widget::pointer_enter()
279 {
280         set_state(HOVER);
281 }
282
283 void Widget::pointer_leave()
284 {
285         clear_state(HOVER);
286 }
287
288 void Widget::touch_press(int x, int y, unsigned finger)
289 {
290         if(finger==0)
291                 button_press(x, y, 1);
292 }
293
294 void Widget::touch_release(int x, int y, unsigned finger)
295 {
296         if(finger==0)
297                 button_release(x, y, 1);
298 }
299
300 void Widget::touch_motion(int x, int y, unsigned finger)
301 {
302         if(finger==0)
303                 pointer_motion(x, y);
304 }
305
306 void Widget::focus_in()
307 {
308         set_state(FOCUS);
309 }
310
311 void Widget::focus_out()
312 {
313         clear_state(FOCUS);
314 }
315
316
317 Widget::Loader::Loader(Widget &w):
318         DataFile::ObjectLoader<Widget>(w)
319 {
320         add("position", &Loader::position);
321         add("size",     &Loader::size);
322         add("style",    &Loader::style);
323         add("visible",  &Widget::visible);
324 }
325
326 void Widget::Loader::position(int x, int y)
327 {
328         obj.set_position(x, y);
329 }
330
331 void Widget::Loader::size(unsigned w, unsigned h)
332 {
333         obj.set_size(w, h);
334 }
335
336 void Widget::Loader::style(const string &s)
337 {
338         obj.set_style(s);
339 }
340
341 } // namespace GLtk
342 } // namespace Msp