]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
Add Container class
[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 "panel.h"
13 #include "resources.h"
14 #include "widget.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GLtk {
20
21 Widget::Widget(const Resources &r):
22         res(r),
23         style(0),
24         state(NORMAL),
25         visible(true),
26         parent(0)
27 { }
28
29 Widget::~Widget()
30 {
31         if(parent)
32                 parent->remove(*this);
33 }
34
35 void Widget::set_position(int x, int y)
36 {
37         geom.x=x;
38         geom.y=y;
39         on_geometry_change();
40 }
41
42 void Widget::set_size(unsigned w, unsigned h)
43 {
44         geom.w=w;
45         geom.h=h;
46         on_geometry_change();
47 }
48
49 void Widget::set_geometry(const Geometry &g)
50 {
51         geom=g;
52         on_geometry_change();
53 }
54
55 void Widget::set_style(const string &s)
56 {
57         style_name=s;
58         update_style();
59 }
60
61 void Widget::set_visible(bool v)
62 {
63         if(v==visible)
64                 return;
65
66         visible=v;
67
68         signal_visibility_changed.emit(visible);
69 }
70
71 void Widget::set_focus()
72 {
73         if(!parent)
74                 throw InvalidState("No parent");
75         if(!visible)
76                 throw InvalidState("Can't set focus on invisible widget");
77
78         signal_request_focus.emit();
79 }
80
81 void Widget::render() const
82 {
83         if(!style)
84                 throw InvalidState(format("Attempt to render a widget without a style (class=\"%s\")", get_class()));
85
86         GL::push_matrix();
87         GL::translate(geom.x, geom.y, 0);
88         for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
89         {
90                 if(i->get_name().empty())
91                         render_graphic(*i);
92                 else
93                         render_special(*i);
94         }
95         GL::pop_matrix();
96 }
97
98 void Widget::render_graphic(const Part &part) const
99 {
100         GL::push_matrix();
101         part.render(geom, state);
102         GL::pop_matrix();
103 }
104
105 void Widget::render_text(const Part &part, const string &text) const
106 {
107         const GL::Font *const font=style->get_font();
108         const float font_size=font->get_default_size();
109
110         Geometry rgeom;
111         rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
112         rgeom.h=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
113         rgeom.y=static_cast<int>(-font->get_descent()*font_size);
114         part.get_alignment().apply(rgeom, geom, part.get_margin());
115
116         GL::push_matrix();
117         GL::translate(rgeom.x, rgeom.y, 0);
118         GL::scale_uniform(font_size);
119
120         const GL::Color &color=style->get_font_color();
121         GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
122         imm.color(color.r, color.g, color.b);
123         font->draw_string(text, imm);
124
125         GL::pop_matrix();
126 }
127
128 void Widget::pointer_enter()
129 {
130         state|=HOVER;
131 }
132
133 void Widget::pointer_leave()
134 {
135         state&=~HOVER;
136 }
137
138 void Widget::focus_in()
139 {
140         state|=FOCUS;
141 }
142
143 void Widget::focus_out()
144 {
145         state&=~FOCUS;
146 }
147
148 void Widget::update_style()
149 {
150         string sname=get_class();
151         if(!style_name.empty())
152         {
153                 sname+='-';
154                 sname+=style_name;
155         }
156         style=res.get<Style>(sname);
157         on_style_change();
158 }
159
160 void Widget::set_parent(Container *p)
161 {
162         if(parent && p)
163                 throw InvalidState("Widget is already in a Container");
164         parent=p;
165
166         on_reparent();
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