]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
Add protected functions for manipulating widget state
[libs/gltk.git] / source / widget.cpp
1 #include <msp/gl/immediate.h>
2 #include <msp/gl/matrix.h>
3 #include <msp/gl/transform.h>
4 #include <msp/strings/format.h>
5 #include "container.h"
6 #include "resources.h"
7 #include "root.h"
8 #include "widget.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GLtk {
14
15 Widget::Widget():
16         style(0),
17         state(NORMAL),
18         visible(true),
19         focusable(true),
20         parent(0)
21 { }
22
23 Widget::~Widget()
24 {
25         if(parent)
26         {
27                 Container *p = parent;
28                 parent = 0;
29                 p->remove(*this);
30         }
31 }
32
33 void Widget::set_position(int x, int y)
34 {
35         geom.x = x;
36         geom.y = y;
37         on_geometry_change();
38 }
39
40 void Widget::set_size(unsigned w, unsigned h)
41 {
42         geom.w = w;
43         geom.h = h;
44         on_geometry_change();
45 }
46
47 void Widget::autosize()
48 {
49         geom.w = 0;
50         geom.h = 0;
51         const Style::PartSeq &parts = style->get_parts();
52         for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
53                 if(i->get_name().empty())
54                 {
55                         geom.w = max(geom.w, i->get_geometry().w);
56                         geom.h = max(geom.h, i->get_geometry().h);
57                 }
58 }
59
60 void Widget::set_geometry(const Geometry &g)
61 {
62         geom = g;
63         on_geometry_change();
64 }
65
66 void Widget::set_parent(Container *p)
67 {
68         if(parent && p)
69                 throw hierarchy_error("widget already parented");
70         else if(p==parent)
71                 return;
72         parent = p;
73
74         on_reparent();
75         update_style();
76 }
77
78 void Widget::set_style(const string &s)
79 {
80         style_name = s;
81         update_style();
82 }
83
84 void Widget::update_style()
85 {
86         Widget *top;
87         for(top=this; top->parent; top=top->parent) ;
88         Root *root = dynamic_cast<Root *>(top);
89         if(!root)
90                 style = 0;
91         else
92         {
93                 string sname = get_class();
94                 if(!style_name.empty())
95                 {
96                         sname += '-';
97                         sname += style_name;
98                 }
99
100                 style = &root->get_resources().get<Style>(sname);
101         }
102
103         on_style_change();
104         signal_autosize_changed.emit();
105 }
106
107 void Widget::set_tooltip(const string &t)
108 {
109         tooltip = t;
110 }
111
112 void Widget::set_visible(bool v)
113 {
114         if(v==visible)
115                 return;
116
117         visible = v;
118
119         signal_visibility_changed.emit(visible);
120 }
121
122 void Widget::set_focusable(bool f)
123 {
124         focusable = f;
125 }
126
127 void Widget::set_focus()
128 {
129         if(!parent)
130                 throw hierarchy_error("no parent");
131         if(!visible)
132                 throw logic_error("!visible");
133
134         signal_request_focus.emit();
135 }
136
137 void Widget::set_state(State mask, State bits)
138 {
139         state = (state&~mask)|bits;
140 }
141
142 void Widget::render() const
143 {
144         if(!style)
145                 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
146
147         GL::push_matrix();
148         GL::translate(geom.x, geom.y, 0);
149         const Style::PartSeq &parts = style->get_parts();
150         for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
151         {
152                 if(i->get_name().empty())
153                 {
154                         GL::PushMatrix push_;
155                         i->render(geom, state);
156                 }
157                 else
158                         render_special(*i);
159         }
160         GL::pop_matrix();
161 }
162
163 void Widget::pointer_enter()
164 {
165         set_state(HOVER);
166 }
167
168 void Widget::pointer_leave()
169 {
170         clear_state(HOVER);
171 }
172
173 void Widget::focus_in()
174 {
175         set_state(FOCUS);
176 }
177
178 void Widget::focus_out()
179 {
180         clear_state(FOCUS);
181 }
182
183
184 Widget::Loader::Loader(Widget &w):
185         DataFile::ObjectLoader<Widget>(w)
186 {
187         add("position", &Loader::position);
188         add("size",     &Loader::size);
189         add("style",    &Loader::style);
190         add("visible",  &Widget::visible);
191 }
192
193 void Widget::Loader::position(int x, int y)
194 {
195         obj.set_position(x, y);
196 }
197
198 void Widget::Loader::size(unsigned w, unsigned h)
199 {
200         obj.set_size(w, h);
201 }
202
203 void Widget::Loader::style(const string &s)
204 {
205         obj.set_style(s);
206 }
207
208 } // namespace GLtk
209 } // namespace Msp