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