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