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