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