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