]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
e41310a50c5fd5cba82298dd3149983b16a0f8a1
[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::render() const
57 {
58         if(!style)
59                 throw InvalidState("Attempt to render a widget without a style");
60
61         GL::push_matrix();
62         GL::translate(geom.x, geom.y, 0);
63         for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
64                 render_part(*i);
65         GL::pop_matrix();
66 }
67
68 void Widget::render_part(const Part &part) const
69 {
70         render_graphic(part);
71 }
72
73 void Widget::render_graphic(const Part &part) const
74 {
75         GL::push_matrix();
76         part.render(geom, state);
77         GL::pop_matrix();
78 }
79
80 void Widget::render_text(const Part &part, const string &text) const
81 {
82         const GL::Font *const font=style->get_font();
83
84         const float font_size=font->get_default_size();
85         unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
86
87         GL::push_matrix();
88
89         part.get_alignment().apply(geom, text_w, static_cast<unsigned>(font->get_ascent()*font_size));
90         GL::scale_uniform(font_size);
91
92         const GL::Color &color=style->get_font_color();
93         glColor3f(color.r, color.g, color.b);
94         font->draw_string(text);
95         glColor3f(1, 1, 1);
96
97         GL::pop_matrix();
98 }
99
100 void Widget::update_style()
101 {
102         string sname=get_class();
103         if(!style_name.empty())
104         {
105                 sname+='-';
106                 sname+=style_name;
107         }
108         style=res.get<Style>(sname);
109 }
110
111 void Widget::set_parent(Panel *p)
112 {
113         if(parent && p)
114                 throw InvalidState("Widget is already in a Panel");
115         parent=p;
116 }
117
118 void Widget::set_parent(Widget &w, Panel *p)
119 {
120         w.set_parent(p);
121 }
122
123
124 Widget::Loader::Loader(Widget &w):
125         wdg(w)
126 {
127         add("position", &Loader::position);
128         add("size",     &Loader::size);
129         add("style",    &Loader::style);
130         add("visible",  &Widget::visible);
131 }
132
133 void Widget::Loader::position(int x, int y)
134 {
135         wdg.set_position(x, y);
136 }
137
138 void Widget::Loader::size(unsigned w, unsigned h)
139 {
140         wdg.set_size(w, h);
141 }
142
143 void Widget::Loader::style(const string &s)
144 {
145         wdg.set_style(s);
146 }
147
148 } // namespace GLtk
149 } // namespace Msp