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