]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
19949bb3b4909b454ecb7749ffa946169e5fb7f3
[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_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         const Style::PartSeq &parts = style->get_parts();
234         for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
235         {
236                 if(i->get_name().empty())
237                         i->build(geom, state, part_cache);
238                 else
239                         rebuild_special(*i);
240         }
241 }
242
243 void Widget::rebuild_special(const Part &part)
244 {
245         part_cache.insert_special(part);
246 }
247
248 void Widget::render(GL::Renderer &renderer) const
249 {
250         if(!style)
251                 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
252
253         static const GL::Tag texture_tag("ui_tex");
254
255         GL::Renderer::Push _push(renderer);
256         int x = 0;
257         int y = 0;
258         map_coords_to_root(x, y);
259         renderer.set_matrix(GL::Matrix::translation(x, y, 0));
260         const PartCache::PartList &parts = part_cache.get_parts();
261         for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
262         {
263                 if(i->mesh && i->texture)
264                 {
265                         renderer.set_texture(texture_tag, i->texture, &style->get_sampler());
266                         i->mesh->draw(renderer);
267                 }
268                 else if(i->part)
269                         render_special(*i->part, renderer);
270         }
271 }
272
273 void Widget::pointer_enter()
274 {
275         set_state(HOVER);
276 }
277
278 void Widget::pointer_leave()
279 {
280         clear_state(HOVER);
281 }
282
283 void Widget::touch_press(int x, int y, unsigned finger)
284 {
285         if(finger==0)
286                 button_press(x, y, 1);
287 }
288
289 void Widget::touch_release(int x, int y, unsigned finger)
290 {
291         if(finger==0)
292                 button_release(x, y, 1);
293 }
294
295 void Widget::touch_motion(int x, int y, unsigned finger)
296 {
297         if(finger==0)
298                 pointer_motion(x, y);
299 }
300
301 void Widget::focus_in()
302 {
303         set_state(FOCUS);
304 }
305
306 void Widget::focus_out()
307 {
308         clear_state(FOCUS);
309 }
310
311
312 Widget::Loader::Loader(Widget &w):
313         DataFile::ObjectLoader<Widget>(w)
314 {
315         add("position", &Loader::position);
316         add("size",     &Loader::size);
317         add("style",    &Loader::style);
318         add("visible",  &Widget::visible);
319 }
320
321 void Widget::Loader::position(int x, int y)
322 {
323         obj.set_position(x, y);
324 }
325
326 void Widget::Loader::size(unsigned w, unsigned h)
327 {
328         obj.set_size(w, h);
329 }
330
331 void Widget::Loader::style(const string &s)
332 {
333         obj.set_style(s);
334 }
335
336 } // namespace GLtk
337 } // namespace Msp