]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
Reorder class members
[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 "resources.h"
11 #include "widget.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GLtk {
17
18 Widget::Widget(const Resources &r):
19         res(r),
20         style(0),
21         state(NORMAL),
22         visible(true)
23 { }
24
25 void Widget::set_position(int x, int y)
26 {
27         geom.x=x;
28         geom.y=y;
29 }
30
31 void Widget::set_size(unsigned w, unsigned h)
32 {
33         geom.w=w;
34         geom.h=h;
35 }
36
37 void Widget::set_geometry(const Geometry &g)
38 {
39         geom=g;
40 }
41
42 void Widget::set_style(const string &s)
43 {
44         style_name=s;
45         update_style();
46 }
47
48 void Widget::render() const
49 {
50         if(!style)
51                 throw InvalidState("Attempt to render a widget without a style");
52
53         GL::push_matrix();
54         GL::translate(geom.x, geom.y, 0);
55         for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
56                 render_part(*i);
57         GL::pop_matrix();
58 }
59
60 void Widget::render_part(const Part &part) const
61 {
62         render_graphic(part);
63 }
64
65 void Widget::render_graphic(const Part &part) const
66 {
67         GL::push_matrix();
68         part.render(geom, state);
69         GL::pop_matrix();
70 }
71
72 void Widget::render_text(const Part &part, const string &text) const
73 {
74         const GL::Font *const font=style->get_font();
75
76         const float font_size=font->get_default_size();
77         unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
78
79         GL::push_matrix();
80
81         part.get_alignment().apply(geom, text_w, static_cast<unsigned>(font->get_ascent()*font_size));
82         GL::scale_uniform(font_size);
83
84         const GL::Color &color=style->get_font_color();
85         glColor3f(color.r, color.g, color.b);
86         font->draw_string(text);
87         glColor3f(1, 1, 1);
88
89         GL::pop_matrix();
90 }
91
92 void Widget::update_style()
93 {
94         string sname=get_class();
95         if(!style_name.empty())
96         {
97                 sname+='-';
98                 sname+=style_name;
99         }
100         style=res.get<Style>(sname);
101 }
102
103
104 Widget::Loader::Loader(Widget &w):
105         wdg(w)
106 {
107         add("position", &Loader::position);
108         add("size",     &Loader::size);
109         add("style",    &Loader::style);
110         add("visible",  &Widget::visible);
111 }
112
113 void Widget::Loader::position(int x, int y)
114 {
115         wdg.set_position(x, y);
116 }
117
118 void Widget::Loader::size(unsigned w, unsigned h)
119 {
120         wdg.set_size(w, h);
121 }
122
123 void Widget::Loader::style(const string &s)
124 {
125         wdg.set_style(s);
126 }
127
128 } // namespace GLtk
129 } // namespace Msp