]> git.tdb.fi Git - libs/gltk.git/blob - source/panel.cpp
Add icon support to Button
[libs/gltk.git] / source / panel.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 <algorithm>
9 #include <msp/core/refptr.h>
10 #include "button.h"
11 #include "dropdown.h"
12 #include "entry.h"
13 #include "hslider.h"
14 #include "indicator.h"
15 #include "label.h"
16 #include "list.h"
17 #include "panel.h"
18 #include "part.h"
19 #include "table.h"
20 #include "toggle.h"
21 #include "vslider.h"
22
23 using namespace std;
24
25 namespace Msp {
26 namespace GLtk {
27
28 Panel::Panel(const Resources &r):
29         Widget(r),
30         pointer_focus(0),
31         pointer_grab(0),
32         input_focus(0)
33 {
34         update_style();
35 }
36
37 Panel::~Panel()
38 {
39         while(!children.empty())
40                 delete children.front();
41 }
42
43 void Panel::add(Widget &wdg)
44 {
45         set_parent(wdg, this);
46         children.push_back(&wdg);
47 }
48
49 void Panel::remove(Widget &wdg)
50 {
51         list<Widget *>::iterator i=find(children.begin(), children.end(), &wdg);
52         if(i==children.end())
53                 throw InvalidState("That Widget is not in this Panel");
54
55         if(&wdg==pointer_focus)
56                 set_pointer_focus(0, 0);
57         if(&wdg==input_focus)
58                 set_input_focus(0);
59
60         set_parent(wdg, 0);
61         children.erase(i);
62 }
63
64 void Panel::raise(Widget &wdg)
65 {
66         list<Widget *>::iterator i=find(children.begin(), children.end(), &wdg);
67         if(i==children.end())
68                 throw InvalidState("That Widget is not in this Panel");
69
70         children.erase(i);
71         children.push_back(&wdg);
72 }
73
74 void Panel::button_press(int x, int y, unsigned btn)
75 {
76         if(pointer_grab>0)
77         {
78                 const Geometry &cgeom=pointer_focus->get_geometry();
79                 pointer_focus->button_press(x-cgeom.x, y-cgeom.y, btn);
80         }
81         else if(geom.is_inside_relative(x, y))
82         {
83                 if(Widget *wdg=get_child_at(x, y))
84                 {
85                         set_pointer_focus(wdg, btn);
86                         set_input_focus(wdg);
87
88                         const Geometry &cgeom=wdg->get_geometry();
89                         wdg->button_press(x-cgeom.x, y-cgeom.y, btn);
90                 }
91         }
92 }
93
94 void Panel::button_release(int x, int y, unsigned btn)
95 {
96         if(pointer_grab>0)
97         {
98                 const Geometry &cgeom=pointer_focus->get_geometry();
99                 pointer_focus->button_release(x-cgeom.x, y-cgeom.y, btn);
100
101                 if(btn==pointer_grab)
102                         set_pointer_focus(get_child_at(x, y), 0);
103         }
104         else if(geom.is_inside_relative(x, y))
105         {
106                 if(Widget *wdg=get_child_at(x, y))
107                 {
108                         const Geometry &cgeom=wdg->get_geometry();
109                         wdg->button_release(x-cgeom.x, y-cgeom.y, btn);
110                 }
111         }
112 }
113
114 void Panel::pointer_motion(int x, int y)
115 {
116         if(pointer_grab>0)
117         {
118                 const Geometry &cgeom=pointer_focus->get_geometry();
119                 pointer_focus->pointer_motion(x-cgeom.x, y-cgeom.y);
120         }
121         else if(geom.is_inside_relative(x, y))
122         {
123                 Widget *wdg=get_child_at(x, y);
124                 set_pointer_focus(wdg, 0);
125                 if(wdg)
126                 {
127                         const Geometry &cgeom=wdg->get_geometry();
128                         wdg->pointer_motion(x-cgeom.x, y-cgeom.y);
129                 }
130         }
131 }
132
133 void Panel::pointer_leave()
134 {
135         set_pointer_focus(0, 0);
136 }
137
138 void Panel::key_press(unsigned key, unsigned mod, wchar_t ch)
139 {
140         if(input_focus)
141                 input_focus->key_press(key, mod, ch);
142 }
143
144 void Panel::key_release(unsigned key, unsigned mod)
145 {
146         if(input_focus)
147                 input_focus->key_release(key, mod);
148 }
149
150 void Panel::focus_out()
151 {
152         set_input_focus(0);
153 }
154
155 void Panel::child_hidden(Widget &wdg)
156 {
157         if(&wdg==pointer_focus)
158                 set_pointer_focus(0, 0);
159         if(&wdg==input_focus)
160                 set_input_focus(0);
161 }
162
163 void Panel::grab_pointer(Widget &wdg)
164 {
165         if(pointer_grab==0 || pointer_focus==&wdg)
166         {
167                 set_pointer_focus(&wdg, 255);
168                 if(parent)
169                         parent->grab_pointer(*this);
170         }
171         else
172                 throw InvalidState("Pointer is already grabbed");
173 }
174
175 void Panel::ungrab_pointer(Widget &wdg)
176 {
177         if(pointer_focus==&wdg)
178         {
179                 set_pointer_focus(0, 0);
180                 if(parent)
181                         parent->ungrab_pointer(*this);
182         }
183         else if(pointer_grab>0)
184                 throw Exception("Someone is trying to steal the pointer!");
185 }
186
187 void Panel::grab_focus(Widget &wdg)
188 {
189         list<Widget *>::iterator i=find(children.begin(), children.end(), &wdg);
190         if(i==children.end())
191                 throw InvalidState("That Widget is not in this Panel");
192         
193         set_input_focus(&wdg);
194         if(parent)
195                 parent->grab_focus(*this);
196 }
197
198 void Panel::render_special(const Part &part) const
199 {
200         if(part.get_name()=="children")
201         {
202                 for(list<Widget *>::const_iterator i=children.begin(); i!=children.end(); ++i)
203                         if((*i)->is_visible())
204                                 (*i)->render();
205         }
206 }
207
208 void Panel::set_pointer_focus(Widget *wdg, int grab)
209 {
210         if(grab>0 && !wdg)
211                 throw InvalidParameterValue("Can't grab on null widget");
212
213         if(wdg!=pointer_focus)
214         {
215                 if(pointer_focus)
216                         pointer_focus->pointer_leave();
217
218                 pointer_focus=wdg;
219
220                 if(pointer_focus)
221                         pointer_focus->pointer_enter();
222         }
223
224         pointer_grab=grab;
225 }
226
227 void Panel::set_input_focus(Widget *wdg)
228 {
229         if(wdg!=input_focus)
230         {
231                 if(input_focus)
232                         input_focus->focus_out();
233
234                 input_focus=wdg;
235
236                 if(input_focus)
237                         input_focus->focus_in();
238         }
239 }
240
241 Widget *Panel::get_child_at(int x, int y)
242 {
243         for(list<Widget *>::reverse_iterator i=children.rbegin(); i!=children.rend(); ++i)
244                 if((*i)->is_visible() && (*i)->get_geometry().is_inside(x, y))
245                         return *i;
246
247         return 0;
248 }
249
250
251 Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
252         Widget::Loader(p),
253         pnl(p),
254         wdg_map(m)
255 {
256         add("button",    &Loader::child<Button>);
257         add("dropdown",  &Loader::child<Dropdown>);
258         add("entry",     &Loader::child<Entry>);
259         add("hslider",   &Loader::child<HSlider>);
260         add("indicator", &Loader::child<Indicator>);
261         add("label",     &Loader::child<Label>);
262         add("list",      &Loader::child<List>);
263         add("panel",     &Loader::panel);
264         add("table",     &Loader::child<Table>);
265         add("toggle",    &Loader::child<Toggle>);
266         add("vslider",   &Loader::child<VSlider>);
267 }
268
269 template<typename T>
270 void Panel::Loader::child(const string &n)
271 {
272         RefPtr<T> chl=new T(pnl.res);
273         load_sub(*chl);
274         pnl.add(*chl.get());
275         wdg_map[n]=chl.release();
276 }
277
278 void Panel::Loader::panel(const string &n)
279 {
280         RefPtr<Panel> p=new Panel(pnl.res);
281         load_sub(*p, wdg_map);
282         pnl.add(*p.get());
283         wdg_map[n]=p.release();
284 }
285
286 } // namespace GLtk
287 } // namespace Msp