]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
Clear focus in Panel if focused child is removed
[libs/gltk.git] / source / widget.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007  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         if(!visible && parent)
69                 parent->child_hidden(*this);
70 }
71
72 void Widget::render() const
73 {
74         if(!style)
75                 throw InvalidState(format("Attempt to render a widget without a style (class=\"%s\")", get_class()));
76
77         GL::push_matrix();
78         GL::translate(geom.x, geom.y, 0);
79         for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
80         {
81                 if(i->get_name().empty())
82                         render_graphic(*i);
83                 else
84                         render_special(*i);
85         }
86         GL::pop_matrix();
87 }
88
89 void Widget::render_graphic(const Part &part) const
90 {
91         GL::push_matrix();
92         part.render(geom, state);
93         GL::pop_matrix();
94 }
95
96 void Widget::render_text(const Part &part, const string &text) const
97 {
98         const GL::Font *const font=style->get_font();
99         const float font_size=font->get_default_size();
100
101         Geometry rgeom;
102         rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
103         rgeom.h=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
104         rgeom.y=static_cast<int>(-font->get_descent()*font_size);
105         part.get_alignment().apply(rgeom, geom, part.get_margin());
106
107         GL::push_matrix();
108         GL::translate(rgeom.x, rgeom.y, 0);
109         GL::scale_uniform(font_size);
110
111         const GL::Color &color=style->get_font_color();
112         GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
113         imm.color(color.r, color.g, color.b);
114         font->draw_string(text, imm);
115
116         GL::pop_matrix();
117 }
118
119 void Widget::pointer_enter()
120 {
121         state|=HOVER;
122 }
123
124 void Widget::pointer_leave()
125 {
126         state&=~HOVER;
127 }
128
129 void Widget::focus_in()
130 {
131         state|=FOCUS;
132 }
133
134 void Widget::focus_out()
135 {
136         state&=~FOCUS;
137 }
138
139 void Widget::update_style()
140 {
141         string sname=get_class();
142         if(!style_name.empty())
143         {
144                 sname+='-';
145                 sname+=style_name;
146         }
147         style=res.get<Style>(sname);
148         on_style_change();
149 }
150
151 void Widget::set_parent(Panel *p)
152 {
153         if(parent && p)
154                 throw InvalidState("Widget is already in a Panel");
155         parent=p;
156
157         on_reparent();
158 }
159
160 void Widget::set_parent(Widget &w, Panel *p)
161 {
162         w.set_parent(p);
163 }
164
165
166 Widget::Loader::Loader(Widget &w):
167         wdg(w)
168 {
169         add("position", &Loader::position);
170         add("size",     &Loader::size);
171         add("style",    &Loader::style);
172         add("visible",  &Widget::visible);
173 }
174
175 void Widget::Loader::position(int x, int y)
176 {
177         wdg.set_position(x, y);
178 }
179
180 void Widget::Loader::size(unsigned w, unsigned h)
181 {
182         wdg.set_size(w, h);
183 }
184
185 void Widget::Loader::style(const string &s)
186 {
187         wdg.set_style(s);
188 }
189
190 } // namespace GLtk
191 } // namespace Msp