]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.cpp
Remove unused code
[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                 else
101                         render_special(*i);
102         }
103         GL::pop_matrix();
104 }
105
106 void Widget::pointer_enter()
107 {
108         state|=HOVER;
109 }
110
111 void Widget::pointer_leave()
112 {
113         state&=~HOVER;
114 }
115
116 void Widget::focus_in()
117 {
118         state|=FOCUS;
119 }
120
121 void Widget::focus_out()
122 {
123         state&=~FOCUS;
124 }
125
126 void Widget::update_style()
127 {
128         string sname=get_class();
129         if(!style_name.empty())
130         {
131                 sname+='-';
132                 sname+=style_name;
133         }
134         style=res.get<Style>(sname);
135         on_style_change();
136 }
137
138 void Widget::set_parent(Container *p)
139 {
140         if(parent && p)
141                 throw InvalidState("Widget is already in a Container");
142         parent=p;
143
144         on_reparent();
145 }
146
147 void Widget::set_parent(Widget &w, Container *p)
148 {
149         w.set_parent(p);
150 }
151
152
153 Widget::Loader::Loader(Widget &w):
154         wdg(w)
155 {
156         add("position", &Loader::position);
157         add("size",     &Loader::size);
158         add("style",    &Loader::style);
159         add("visible",  &Widget::visible);
160 }
161
162 void Widget::Loader::position(int x, int y)
163 {
164         wdg.set_position(x, y);
165 }
166
167 void Widget::Loader::size(unsigned w, unsigned h)
168 {
169         wdg.set_size(w, h);
170 }
171
172 void Widget::Loader::style(const string &s)
173 {
174         wdg.set_style(s);
175 }
176
177 } // namespace GLtk
178 } // namespace Msp