]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
Add Toggle widget
[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::update_style()
119 {
120         string sname=get_class();
121         if(!style_name.empty())
122         {
123                 sname+='-';
124                 sname+=style_name;
125         }
126         style=res.get<Style>(sname);
127         on_style_change();
128 }
129
130 void Widget::set_parent(Panel *p)
131 {
132         if(parent && p)
133                 throw InvalidState("Widget is already in a Panel");
134         parent=p;
135
136         on_reparent();
137 }
138
139 void Widget::set_parent(Widget &w, Panel *p)
140 {
141         w.set_parent(p);
142 }
143
144
145 Widget::Loader::Loader(Widget &w):
146         wdg(w)
147 {
148         add("position", &Loader::position);
149         add("size",     &Loader::size);
150         add("style",    &Loader::style);
151         add("visible",  &Widget::visible);
152 }
153
154 void Widget::Loader::position(int x, int y)
155 {
156         wdg.set_position(x, y);
157 }
158
159 void Widget::Loader::size(unsigned w, unsigned h)
160 {
161         wdg.set_size(w, h);
162 }
163
164 void Widget::Loader::style(const string &s)
165 {
166         wdg.set_style(s);
167 }
168
169 } // namespace GLtk
170 } // namespace Msp