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