]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
Add Text class with multiline support
[libs/gltk.git] / source / widget.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/gl/immediate.h>
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/transform.h>
11 #include <msp/strings/formatter.h>
12 #include "panel.h"
13 #include "resources.h"
14 #include "widget.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GLtk {
20
21 Widget::Widget(const Resources &r):
22         res(r),
23         style(0),
24         state(NORMAL),
25         visible(true),
26         parent(0)
27 { }
28
29 Widget::~Widget()
30 {
31         if(parent)
32                 parent->remove(*this);
33 }
34
35 void Widget::set_position(int x, int y)
36 {
37         geom.x=x;
38         geom.y=y;
39         on_geometry_change();
40 }
41
42 void Widget::set_size(unsigned w, unsigned h)
43 {
44         geom.w=w;
45         geom.h=h;
46         on_geometry_change();
47 }
48
49 void Widget::set_geometry(const Geometry &g)
50 {
51         geom=g;
52         on_geometry_change();
53 }
54
55 void Widget::set_style(const string &s)
56 {
57         style_name=s;
58         update_style();
59 }
60
61 void Widget::set_tooltip(const string &t)
62 {
63         tooltip = t;
64 }
65
66 void Widget::set_visible(bool v)
67 {
68         if(v==visible)
69                 return;
70
71         visible=v;
72
73         signal_visibility_changed.emit(visible);
74 }
75
76 void Widget::set_focus()
77 {
78         if(!parent)
79                 throw InvalidState("No parent");
80         if(!visible)
81                 throw InvalidState("Can't set focus on invisible widget");
82
83         signal_request_focus.emit();
84 }
85
86 void Widget::render() const
87 {
88         if(!style)
89                 throw InvalidState(format("Attempt to render a widget without a style (class=\"%s\")", get_class()));
90
91         GL::push_matrix();
92         GL::translate(geom.x, geom.y, 0);
93         for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
94         {
95                 if(i->get_name().empty())
96                 {
97                         GL::PushMatrix push_;
98                         i->render(geom, state);
99                 }
100                         //render_graphic(*i);
101                 else
102                         render_special(*i);
103         }
104         GL::pop_matrix();
105 }
106
107 /*void Widget::render_graphic(const Part &part) const
108 {
109         GL::push_matrix();
110         part.render(geom, state);
111         GL::pop_matrix();
112 }
113
114 void Widget::render_text(const Part &part, const string &text) const
115 {
116         const GL::Font *const font=style->get_font();
117         const float font_size=font->get_default_size();
118
119         Geometry rgeom;
120         rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
121         rgeom.h=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
122         rgeom.y=static_cast<int>(-font->get_descent()*font_size);
123         part.get_alignment().apply(rgeom, geom, part.get_margin());
124
125         GL::push_matrix();
126         GL::translate(rgeom.x, rgeom.y, 0);
127         GL::scale_uniform(font_size);
128
129         const GL::Color &color=style->get_font_color();
130         GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
131         imm.color(color.r, color.g, color.b);
132         font->draw_string(text, imm);
133
134         GL::pop_matrix();
135 }*/
136
137 void Widget::pointer_enter()
138 {
139         state|=HOVER;
140 }
141
142 void Widget::pointer_leave()
143 {
144         state&=~HOVER;
145 }
146
147 void Widget::focus_in()
148 {
149         state|=FOCUS;
150 }
151
152 void Widget::focus_out()
153 {
154         state&=~FOCUS;
155 }
156
157 void Widget::update_style()
158 {
159         string sname=get_class();
160         if(!style_name.empty())
161         {
162                 sname+='-';
163                 sname+=style_name;
164         }
165         style=res.get<Style>(sname);
166         on_style_change();
167 }
168
169 void Widget::set_parent(Container *p)
170 {
171         if(parent && p)
172                 throw InvalidState("Widget is already in a Container");
173         parent=p;
174
175         on_reparent();
176 }
177
178 void Widget::set_parent(Widget &w, Container *p)
179 {
180         w.set_parent(p);
181 }
182
183
184 Widget::Loader::Loader(Widget &w):
185         wdg(w)
186 {
187         add("position", &Loader::position);
188         add("size",     &Loader::size);
189         add("style",    &Loader::style);
190         add("visible",  &Widget::visible);
191 }
192
193 void Widget::Loader::position(int x, int y)
194 {
195         wdg.set_position(x, y);
196 }
197
198 void Widget::Loader::size(unsigned w, unsigned h)
199 {
200         wdg.set_size(w, h);
201 }
202
203 void Widget::Loader::style(const string &s)
204 {
205         wdg.set_style(s);
206 }
207
208 } // namespace GLtk
209 } // namespace Msp