]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
Make the DISABLED state actually do something
[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_enabled(bool e)
176 {
177         set_state(DISABLED, (e ? NORMAL : DISABLED));
178 }
179
180 void Widget::set_state(State mask, State bits)
181 {
182         state = (state&~mask)|bits;
183         rebuild();
184 }
185
186 void Widget::rebuild()
187 {
188         if(!style)
189                 return;
190
191         part_cache.clear();
192         const Style::PartSeq &parts = style->get_parts();
193         for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
194         {
195                 if(i->get_name().empty())
196                         i->build(geom, state, part_cache);
197                 else
198                         rebuild_special(*i);
199         }
200 }
201
202 void Widget::rebuild_special(const Part &part)
203 {
204         part_cache.insert_special(part);
205 }
206
207 void Widget::render(GL::Renderer &renderer) const
208 {
209         if(!style)
210                 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
211
212         GL::MatrixStack::Push _pushm(renderer.matrix_stack());
213         renderer.matrix_stack() *= GL::Matrix::translation(geom.x, geom.y, 0);
214         const PartCache::PartList &parts = part_cache.get_parts();
215         for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
216         {
217                 if(i->mesh && i->texture)
218                 {
219                         renderer.set_texture(i->texture);
220                         i->mesh->draw(renderer);
221                 }
222                 else if(i->part)
223                         render_special(*i->part, renderer);
224         }
225 }
226
227 void Widget::pointer_enter()
228 {
229         set_state(HOVER);
230 }
231
232 void Widget::pointer_leave()
233 {
234         clear_state(HOVER);
235 }
236
237 void Widget::focus_in()
238 {
239         set_state(FOCUS);
240 }
241
242 void Widget::focus_out()
243 {
244         clear_state(FOCUS);
245 }
246
247
248 Widget::Loader::Loader(Widget &w):
249         DataFile::ObjectLoader<Widget>(w)
250 {
251         add("position", &Loader::position);
252         add("size",     &Loader::size);
253         add("style",    &Loader::style);
254         add("visible",  &Widget::visible);
255 }
256
257 void Widget::Loader::position(int x, int y)
258 {
259         obj.set_position(x, y);
260 }
261
262 void Widget::Loader::size(unsigned w, unsigned h)
263 {
264         obj.set_size(w, h);
265 }
266
267 void Widget::Loader::style(const string &s)
268 {
269         obj.set_style(s);
270 }
271
272 } // namespace GLtk
273 } // namespace Msp