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