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