]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
1c73d7640cc610c49ccb33f6e7f4b0fe315a83d0
[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::set_parent(Container *p)
96 {
97         if(parent && p)
98                 throw hierarchy_error("widget already parented");
99         else if(p==parent)
100                 return;
101
102         try
103         {
104                 parent = p;
105
106                 on_reparent();
107                 update_style();
108         }
109         catch(...)
110         {
111                 // The container has not yet added the widget as its child
112                 parent = 0;
113                 throw;
114         }
115 }
116
117 void Widget::set_style(const string &s)
118 {
119         style_name = s;
120         update_style();
121 }
122
123 void Widget::update_style()
124 {
125         Widget *top;
126         for(top=this; top->parent; top=top->parent) ;
127         Root *root = dynamic_cast<Root *>(top);
128         if(!root)
129                 style = 0;
130         else
131         {
132                 string sname = get_class();
133                 if(!style_name.empty())
134                 {
135                         sname += '-';
136                         sname += style_name;
137                 }
138
139                 style = &root->get_resources().get<Style>(sname);
140         }
141
142         on_style_change();
143         signal_autosize_changed.emit();
144         mark_rebuild();
145 }
146
147 void Widget::set_tooltip(const string &t)
148 {
149         tooltip = t;
150 }
151
152 void Widget::set_visible(bool v)
153 {
154         if(v==visible)
155                 return;
156
157         visible = v;
158
159         signal_visibility_changed.emit(visible);
160 }
161
162 void Widget::set_focusable(bool f)
163 {
164         input_type = (f ? INPUT_TEXT : INPUT_NONE);
165 }
166
167 void Widget::set_focus()
168 {
169         if(!parent)
170                 throw hierarchy_error("no parent");
171         if(!visible)
172                 throw logic_error("!visible");
173
174         signal_request_focus.emit();
175 }
176
177 void Widget::set_enabled(bool e)
178 {
179         set_state(DISABLED, (e ? NORMAL : DISABLED));
180 }
181
182 void Widget::set_state(State mask, State bits)
183 {
184         State old_state = state;
185         state = (state&~mask)|bits;
186         if(style && style->compare_states(old_state, state))
187                 mark_rebuild();
188 }
189
190 void Widget::set_animation_interval(const Time::TimeDelta &iv)
191 {
192         if(iv<Time::zero)
193                 throw invalid_argument("Widget::set_animation_interval");
194
195         anim_interval = iv;
196         signal_request_animation.emit(anim_interval);
197 }
198
199 void Widget::stop_animation()
200 {
201         set_animation_interval(Time::zero);
202 }
203
204 void Widget::mark_rebuild()
205 {
206         if(rebuild_needed)
207                 return;
208
209         rebuild_needed = true;
210         signal_rebuild_needed.emit();
211 }
212
213 void Widget::rebuild_hierarchy()
214 {
215         if(rebuild_needed)
216         {
217                 rebuild_needed = false;
218                 rebuild();
219         }
220 }
221
222 void Widget::rebuild()
223 {
224         if(!style)
225                 return;
226
227         PartCache::Rebuild rebuild_cache(part_cache);
228         const Style::PartSeq &parts = style->get_parts();
229         for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
230         {
231                 if(i->get_name().empty())
232                         i->build(geom, state, part_cache);
233                 else
234                         rebuild_special(*i);
235         }
236 }
237
238 void Widget::rebuild_special(const Part &part)
239 {
240         part_cache.insert_special(part);
241 }
242
243 void Widget::render(GL::Renderer &renderer) const
244 {
245         if(!style)
246                 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
247
248         GL::Renderer::Push _push(renderer);
249         renderer.transform(GL::Matrix::translation(geom.x, geom.y, 0));
250         const PartCache::PartList &parts = part_cache.get_parts();
251         for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
252         {
253                 if(i->mesh && i->texture)
254                 {
255                         renderer.set_texture(i->texture);
256                         i->mesh->draw(renderer);
257                 }
258                 else if(i->part)
259                         render_special(*i->part, renderer);
260         }
261 }
262
263 void Widget::pointer_enter()
264 {
265         set_state(HOVER);
266 }
267
268 void Widget::pointer_leave()
269 {
270         clear_state(HOVER);
271 }
272
273 void Widget::touch_press(int x, int y, unsigned finger)
274 {
275         if(finger==0)
276                 button_press(x, y, 1);
277 }
278
279 void Widget::touch_release(int x, int y, unsigned finger)
280 {
281         if(finger==0)
282                 button_release(x, y, 1);
283 }
284
285 void Widget::touch_motion(int x, int y, unsigned finger)
286 {
287         if(finger==0)
288                 pointer_motion(x, y);
289 }
290
291 void Widget::focus_in()
292 {
293         set_state(FOCUS);
294 }
295
296 void Widget::focus_out()
297 {
298         clear_state(FOCUS);
299 }
300
301
302 Widget::Loader::Loader(Widget &w):
303         DataFile::ObjectLoader<Widget>(w)
304 {
305         add("position", &Loader::position);
306         add("size",     &Loader::size);
307         add("style",    &Loader::style);
308         add("visible",  &Widget::visible);
309 }
310
311 void Widget::Loader::position(int x, int y)
312 {
313         obj.set_position(x, y);
314 }
315
316 void Widget::Loader::size(unsigned w, unsigned h)
317 {
318         obj.set_size(w, h);
319 }
320
321 void Widget::Loader::style(const string &s)
322 {
323         obj.set_style(s);
324 }
325
326 } // namespace GLtk
327 } // namespace Msp