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