]> git.tdb.fi Git - libs/gltk.git/blob - source/panel.cpp
aeca9e71a61e83d6889e351cb678ddd1493f2741
[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                 Widget *wdg=pointer_focus;
99
100                 if(btn==pointer_grab)
101                         set_pointer_focus(get_child_at(x, y), 0);
102
103                 const Geometry &cgeom=wdg->get_geometry();
104                 wdg->button_release(x-cgeom.x, y-cgeom.y, btn);
105         }
106         else if(geom.is_inside_relative(x, y))
107         {
108                 if(Widget *wdg=get_child_at(x, y))
109                 {
110                         const Geometry &cgeom=wdg->get_geometry();
111                         wdg->button_release(x-cgeom.x, y-cgeom.y, btn);
112                 }
113         }
114 }
115
116 void Panel::pointer_motion(int x, int y)
117 {
118         if(pointer_grab>0)
119         {
120                 const Geometry &cgeom=pointer_focus->get_geometry();
121                 pointer_focus->pointer_motion(x-cgeom.x, y-cgeom.y);
122         }
123         else if(geom.is_inside_relative(x, y))
124         {
125                 Widget *wdg=get_child_at(x, y);
126                 set_pointer_focus(wdg, 0);
127                 if(wdg)
128                 {
129                         const Geometry &cgeom=wdg->get_geometry();
130                         wdg->pointer_motion(x-cgeom.x, y-cgeom.y);
131                 }
132         }
133 }
134
135 void Panel::pointer_leave()
136 {
137         set_pointer_focus(0, 0);
138 }
139
140 void Panel::key_press(unsigned key, unsigned mod, wchar_t ch)
141 {
142         if(input_focus)
143                 input_focus->key_press(key, mod, ch);
144 }
145
146 void Panel::key_release(unsigned key, unsigned mod)
147 {
148         if(input_focus)
149                 input_focus->key_release(key, mod);
150 }
151
152 void Panel::focus_out()
153 {
154         set_input_focus(0);
155 }
156
157 void Panel::child_hidden(Widget &wdg)
158 {
159         if(&wdg==pointer_focus)
160                 set_pointer_focus(0, 0);
161         if(&wdg==input_focus)
162                 set_input_focus(0);
163 }
164
165 void Panel::grab_pointer(Widget &wdg)
166 {
167         if(pointer_grab==0 || pointer_focus==&wdg)
168         {
169                 set_pointer_focus(&wdg, 255);
170                 if(parent)
171                         parent->grab_pointer(*this);
172         }
173         else
174                 throw InvalidState("Pointer is already grabbed");
175 }
176
177 void Panel::ungrab_pointer(Widget &wdg)
178 {
179         if(pointer_focus==&wdg)
180         {
181                 set_pointer_focus(0, 0);
182                 if(parent)
183                         parent->ungrab_pointer(*this);
184         }
185         else if(pointer_grab>0)
186                 throw Exception("Someone is trying to steal the pointer!");
187 }
188
189 void Panel::grab_focus(Widget &wdg)
190 {
191         list<Widget *>::iterator i=find(children.begin(), children.end(), &wdg);
192         if(i==children.end())
193                 throw InvalidState("That Widget is not in this Panel");
194         
195         set_input_focus(&wdg);
196         if(parent)
197                 parent->grab_focus(*this);
198 }
199
200 void Panel::render_special(const Part &part) const
201 {
202         if(part.get_name()=="children")
203         {
204                 for(list<Widget *>::const_iterator i=children.begin(); i!=children.end(); ++i)
205                         if((*i)->is_visible())
206                                 (*i)->render();
207         }
208 }
209
210 void Panel::set_pointer_focus(Widget *wdg, int grab)
211 {
212         if(grab>0 && !wdg)
213                 throw InvalidParameterValue("Can't grab on null widget");
214
215         if(wdg!=pointer_focus)
216         {
217                 if(pointer_focus)
218                         pointer_focus->pointer_leave();
219
220                 pointer_focus=wdg;
221
222                 if(pointer_focus)
223                         pointer_focus->pointer_enter();
224         }
225
226         pointer_grab=grab;
227 }
228
229 void Panel::set_input_focus(Widget *wdg)
230 {
231         if(wdg!=input_focus)
232         {
233                 if(input_focus)
234                         input_focus->focus_out();
235
236                 input_focus=wdg;
237
238                 if(input_focus)
239                         input_focus->focus_in();
240         }
241 }
242
243 Widget *Panel::get_child_at(int x, int y)
244 {
245         for(list<Widget *>::reverse_iterator i=children.rbegin(); i!=children.rend(); ++i)
246                 if((*i)->is_visible() && (*i)->get_geometry().is_inside(x, y))
247                         return *i;
248
249         return 0;
250 }
251
252
253 Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
254         Widget::Loader(p),
255         pnl(p),
256         wdg_map(m)
257 {
258         add("button",    &Loader::child<Button>);
259         add("dropdown",  &Loader::child<Dropdown>);
260         add("entry",     &Loader::child<Entry>);
261         add("hslider",   &Loader::child<HSlider>);
262         add("indicator", &Loader::child<Indicator>);
263         add("label",     &Loader::child<Label>);
264         add("list",      &Loader::child<List>);
265         add("panel",     &Loader::panel);
266         add("table",     &Loader::child<Table>);
267         add("toggle",    &Loader::child<Toggle>);
268         add("vslider",   &Loader::child<VSlider>);
269 }
270
271 template<typename T>
272 void Panel::Loader::child(const string &n)
273 {
274         RefPtr<T> chl=new T(pnl.res);
275         load_sub(*chl);
276         pnl.add(*chl.get());
277         wdg_map[n]=chl.release();
278 }
279
280 void Panel::Loader::panel(const string &n)
281 {
282         RefPtr<Panel> p=new Panel(pnl.res);
283         load_sub(*p, wdg_map);
284         pnl.add(*p.get());
285         wdg_map[n]=p.release();
286 }
287
288 } // namespace GLtk
289 } // namespace Msp