]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
Store the Resources reference only in Root widget
[libs/gltk.git] / source / widget.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007-2009  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                 parent->remove(*this);
34 }
35
36 void Widget::set_position(int x, int y)
37 {
38         geom.x = x;
39         geom.y = y;
40         on_geometry_change();
41 }
42
43 void Widget::set_size(unsigned w, unsigned h)
44 {
45         geom.w = w;
46         geom.h = h;
47         on_geometry_change();
48 }
49
50 void Widget::set_geometry(const Geometry &g)
51 {
52         geom = g;
53         on_geometry_change();
54 }
55
56 void Widget::set_style(const string &s)
57 {
58         style_name = s;
59         update_style();
60 }
61
62 void Widget::set_tooltip(const string &t)
63 {
64         tooltip = t;
65 }
66
67 void Widget::set_visible(bool v)
68 {
69         if(v==visible)
70                 return;
71
72         visible = v;
73
74         signal_visibility_changed.emit(visible);
75 }
76
77 void Widget::set_focusable(bool f)
78 {
79         focusable = f;
80 }
81
82 void Widget::set_focus()
83 {
84         if(!parent)
85                 throw InvalidState("No parent");
86         if(!visible)
87                 throw InvalidState("Can't set focus on invisible widget");
88
89         signal_request_focus.emit();
90 }
91
92 void Widget::render() const
93 {
94         if(!style)
95                 throw InvalidState(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
96
97         GL::push_matrix();
98         GL::translate(geom.x, geom.y, 0);
99         for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
100         {
101                 if(i->get_name().empty())
102                 {
103                         GL::PushMatrix push_;
104                         i->render(geom, state);
105                 }
106                 else
107                         render_special(*i);
108         }
109         GL::pop_matrix();
110 }
111
112 void Widget::pointer_enter()
113 {
114         state |= HOVER;
115 }
116
117 void Widget::pointer_leave()
118 {
119         state &= ~HOVER;
120 }
121
122 void Widget::focus_in()
123 {
124         state |= FOCUS;
125 }
126
127 void Widget::focus_out()
128 {
129         state &= ~FOCUS;
130 }
131
132 void Widget::update_style()
133 {
134         Widget *top;
135         for(top=this; top->parent; top=top->parent) ;
136         Root *root = dynamic_cast<Root *>(top);
137         if(!root)
138                 style = 0;
139         else
140         {
141                 string sname = get_class();
142                 if(!style_name.empty())
143                 {
144                         sname += '-';
145                         sname += style_name;
146                 }
147
148                 style = root->get_resources().get<Style>(sname);
149         }
150
151         on_style_change();
152 }
153
154 void Widget::update_style(Widget &w)
155 {
156         w.update_style();
157 }
158
159 void Widget::set_parent(Container *p)
160 {
161         if(parent && p)
162                 throw InvalidState("Widget is already in a Container");
163         parent = p;
164
165         on_reparent();
166         update_style();
167 }
168
169 void Widget::set_parent(Widget &w, Container *p)
170 {
171         w.set_parent(p);
172 }
173
174
175 Widget::Loader::Loader(Widget &w):
176         wdg(w)
177 {
178         add("position", &Loader::position);
179         add("size",     &Loader::size);
180         add("style",    &Loader::style);
181         add("visible",  &Widget::visible);
182 }
183
184 void Widget::Loader::position(int x, int y)
185 {
186         wdg.set_position(x, y);
187 }
188
189 void Widget::Loader::size(unsigned w, unsigned h)
190 {
191         wdg.set_size(w, h);
192 }
193
194 void Widget::Loader::style(const string &s)
195 {
196         wdg.set_style(s);
197 }
198
199 } // namespace GLtk
200 } // namespace Msp