]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
a9c233b5d8c370a0ece166c00bc81a8fb1b1f0ff
[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         focusable(true),
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         if(!style)
44                 return;
45
46         Geometry ageom;
47         ageom.x = geom.x;
48         ageom.y = geom.y;
49         const Style::PartSeq &parts = style->get_parts();
50         for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
51         {
52                 if(i->get_name().empty())
53                 {
54                         const Geometry &pgeom = i->get_geometry();
55                         const Sides &pmargin = i->get_margin();
56                         ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right);
57                         ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom);
58                 }
59                 else
60                         autosize_special(*i, ageom);
61         }
62
63         set_geometry(ageom);
64 }
65
66 void Widget::set_geometry(const Geometry &g)
67 {
68         bool size_changed = (g.w!=geom.w || g.h!=geom.h);
69         geom = g;
70         if(size_changed)
71         {
72                 // TODO maybe rename this to on_size_change
73                 on_geometry_change();
74                 rebuild();
75         }
76 }
77
78 void Widget::map_coords_to_ancestor(int &x, int &y, const Widget &ancestor) const
79 {
80         for(const Widget *w=this; w; w=w->get_parent())
81         {
82                 if(w==&ancestor)
83                         return;
84
85                 const Geometry &wgeom = w->get_geometry();
86                 x += wgeom.x;
87                 y += wgeom.y;
88         }
89
90         throw hierarchy_error("not an ancestor");
91 }
92
93 void Widget::set_parent(Container *p)
94 {
95         if(parent && p)
96                 throw hierarchy_error("widget already parented");
97         else if(p==parent)
98                 return;
99
100         try
101         {
102                 parent = p;
103
104                 on_reparent();
105                 update_style();
106         }
107         catch(...)
108         {
109                 // The container has not yet added the widget as its child
110                 parent = 0;
111                 throw;
112         }
113 }
114
115 void Widget::set_style(const string &s)
116 {
117         style_name = s;
118         update_style();
119 }
120
121 void Widget::update_style()
122 {
123         Widget *top;
124         for(top=this; top->parent; top=top->parent) ;
125         Root *root = dynamic_cast<Root *>(top);
126         if(!root)
127                 style = 0;
128         else
129         {
130                 string sname = get_class();
131                 if(!style_name.empty())
132                 {
133                         sname += '-';
134                         sname += style_name;
135                 }
136
137                 style = &root->get_resources().get<Style>(sname);
138         }
139
140         on_style_change();
141         signal_autosize_changed.emit();
142         rebuild();
143 }
144
145 void Widget::set_tooltip(const string &t)
146 {
147         tooltip = t;
148 }
149
150 void Widget::set_visible(bool v)
151 {
152         if(v==visible)
153                 return;
154
155         visible = v;
156
157         signal_visibility_changed.emit(visible);
158 }
159
160 void Widget::set_focusable(bool f)
161 {
162         focusable = f;
163 }
164
165 void Widget::set_focus()
166 {
167         if(!parent)
168                 throw hierarchy_error("no parent");
169         if(!visible)
170                 throw logic_error("!visible");
171
172         signal_request_focus.emit();
173 }
174
175 void Widget::set_state(State mask, State bits)
176 {
177         state = (state&~mask)|bits;
178         rebuild();
179 }
180
181 void Widget::rebuild()
182 {
183         if(!style)
184                 return;
185
186         part_cache.clear();
187         const Style::PartSeq &parts = style->get_parts();
188         for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
189         {
190                 if(i->get_name().empty())
191                         i->build(geom, state, part_cache);
192                 else
193                         rebuild_special(*i);
194         }
195 }
196
197 void Widget::rebuild_special(const Part &part)
198 {
199         part_cache.insert_special(part);
200 }
201
202 void Widget::render(GL::Renderer &renderer) const
203 {
204         if(!style)
205                 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
206
207         GL::MatrixStack::Push _pushm(renderer.matrix_stack());
208         renderer.matrix_stack() *= GL::Matrix::translation(geom.x, geom.y, 0);
209         const PartCache::PartList &parts = part_cache.get_parts();
210         for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
211         {
212                 if(i->mesh && i->texture)
213                 {
214                         renderer.set_texture(i->texture);
215                         i->mesh->draw(renderer);
216                 }
217                 else if(i->part)
218                         render_special(*i->part, renderer);
219         }
220 }
221
222 void Widget::pointer_enter()
223 {
224         set_state(HOVER);
225 }
226
227 void Widget::pointer_leave()
228 {
229         clear_state(HOVER);
230 }
231
232 void Widget::focus_in()
233 {
234         set_state(FOCUS);
235 }
236
237 void Widget::focus_out()
238 {
239         clear_state(FOCUS);
240 }
241
242
243 Widget::Loader::Loader(Widget &w):
244         DataFile::ObjectLoader<Widget>(w)
245 {
246         add("position", &Loader::position);
247         add("size",     &Loader::size);
248         add("style",    &Loader::style);
249         add("visible",  &Widget::visible);
250 }
251
252 void Widget::Loader::position(int x, int y)
253 {
254         obj.set_position(x, y);
255 }
256
257 void Widget::Loader::size(unsigned w, unsigned h)
258 {
259         obj.set_size(w, h);
260 }
261
262 void Widget::Loader::style(const string &s)
263 {
264         obj.set_style(s);
265 }
266
267 } // namespace GLtk
268 } // namespace Msp