]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
typedef tweaks
[libs/gltk.git] / source / widget.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007-2011  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 "container.h"
13 #include "resources.h"
14 #include "root.h"
15 #include "widget.h"
16
17 using namespace std;
18
19 namespace Msp {
20 namespace GLtk {
21
22 Widget::Widget():
23         style(0),
24         state(NORMAL),
25         visible(true),
26         focusable(true),
27         parent(0)
28 { }
29
30 Widget::~Widget()
31 {
32         if(parent)
33                 parent->remove(*this);
34 }
35
36 void Widget::set_position(int x, int y)
37 {
38         geom.x = x;
39         geom.y = y;
40         on_geometry_change();
41 }
42
43 void Widget::set_size(unsigned w, unsigned h)
44 {
45         geom.w = w;
46         geom.h = h;
47         on_geometry_change();
48 }
49
50 void Widget::set_geometry(const Geometry &g)
51 {
52         geom = g;
53         on_geometry_change();
54 }
55
56 void Widget::set_parent(Container *p)
57 {
58         if(parent && p)
59                 throw InvalidState("Widget is already in a Container");
60         parent = p;
61
62         on_reparent();
63         update_style();
64 }
65
66 void Widget::set_style(const string &s)
67 {
68         style_name = s;
69         update_style();
70 }
71
72 void Widget::update_style()
73 {
74         Widget *top;
75         for(top=this; top->parent; top=top->parent) ;
76         Root *root = dynamic_cast<Root *>(top);
77         if(!root)
78                 style = 0;
79         else
80         {
81                 string sname = get_class();
82                 if(!style_name.empty())
83                 {
84                         sname += '-';
85                         sname += style_name;
86                 }
87
88                 style = root->get_resources().get<Style>(sname);
89         }
90
91         on_style_change();
92 }
93
94 void Widget::set_tooltip(const string &t)
95 {
96         tooltip = t;
97 }
98
99 void Widget::set_visible(bool v)
100 {
101         if(v==visible)
102                 return;
103
104         visible = v;
105
106         signal_visibility_changed.emit(visible);
107 }
108
109 void Widget::set_focusable(bool f)
110 {
111         focusable = f;
112 }
113
114 void Widget::set_focus()
115 {
116         if(!parent)
117                 throw InvalidState("No parent");
118         if(!visible)
119                 throw InvalidState("Can't set focus on invisible widget");
120
121         signal_request_focus.emit();
122 }
123
124 void Widget::render() const
125 {
126         if(!style)
127                 throw InvalidState(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
128
129         GL::push_matrix();
130         GL::translate(geom.x, geom.y, 0);
131         const Style::PartSeq &parts = style->get_parts();
132         for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
133         {
134                 if(i->get_name().empty())
135                 {
136                         GL::PushMatrix push_;
137                         i->render(geom, state);
138                 }
139                 else
140                         render_special(*i);
141         }
142         GL::pop_matrix();
143 }
144
145 void Widget::pointer_enter()
146 {
147         state |= HOVER;
148 }
149
150 void Widget::pointer_leave()
151 {
152         state &= ~HOVER;
153 }
154
155 void Widget::focus_in()
156 {
157         state |= FOCUS;
158 }
159
160 void Widget::focus_out()
161 {
162         state &= ~FOCUS;
163 }
164
165
166 Widget::Loader::Loader(Widget &w):
167         wdg(w)
168 {
169         add("position", &Loader::position);
170         add("size",     &Loader::size);
171         add("style",    &Loader::style);
172         add("visible",  &Widget::visible);
173 }
174
175 void Widget::Loader::position(int x, int y)
176 {
177         wdg.set_position(x, y);
178 }
179
180 void Widget::Loader::size(unsigned w, unsigned h)
181 {
182         wdg.set_size(w, h);
183 }
184
185 void Widget::Loader::style(const string &s)
186 {
187         wdg.set_style(s);
188 }
189
190 } // namespace GLtk
191 } // namespace Msp