]> git.tdb.fi Git - libs/gltk.git/blob - panel.cpp
2ede2a9c77fd8b32a105971b894fb5fba66d3919
[libs/gltk.git] / panel.cpp
1 #include <algorithm>
2 #include <msp/core/refptr.h>
3 #include "button.h"
4 #include "dropdown.h"
5 #include "entry.h"
6 #include "hslider.h"
7 #include "indicator.h"
8 #include "label.h"
9 #include "layout.h"
10 #include "list.h"
11 #include "panel.h"
12 #include "part.h"
13 #include "table.h"
14 #include "toggle.h"
15 #include "vslider.h"
16
17 using namespace std;
18
19 namespace Msp {
20 namespace GLtk {
21
22 Panel::Panel():
23         layout(0),
24         pointer_focus(0),
25         pointer_grabbed(false),
26         input_focus(0)
27 { }
28
29 Panel::~Panel()
30 {
31         delete layout;
32         layout = 0;
33 }
34
35 void Panel::set_layout(Layout *l)
36 {
37         l->set_container(*this);
38         delete layout;
39         layout = l;
40 }
41
42 void Panel::autosize()
43 {
44         if(layout)
45                 layout->autosize();
46 }
47
48 Panel::Child *Panel::create_child(Widget *wdg)
49 {
50         return new Child(*this, wdg);
51 }
52
53 void Panel::raise(Widget &wdg)
54 {
55         for(list<Container::Child *>::iterator i=children.begin(); i!=children.end(); ++i)
56                 if((*i)->widget==&wdg)
57                 {
58                         children.splice(children.end(), children, i);
59                         return;
60                 }
61
62         throw hierarchy_error("widget not in panel");
63 }
64
65 Widget *Panel::get_final_input_focus() const
66 {
67         if(Panel *panel = dynamic_cast<Panel *>(input_focus))
68         {
69                 Widget *focus = panel->get_final_input_focus();
70                 if(focus)
71                         return focus;
72         }
73         return input_focus;
74 }
75
76 void Panel::render_special(const Part &part, GL::Renderer &renderer) const
77 {
78         if(part.get_name()=="children")
79         {
80                 for(list<Container::Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
81                         if((*i)->widget->is_visible())
82                                 (*i)->widget->render(renderer);
83         }
84 }
85
86 void Panel::button_press(int x, int y, unsigned btn)
87 {
88         if(pointer_grabbed)
89         {
90                 const Geometry &cgeom = pointer_focus->get_geometry();
91                 pointer_focus->button_press(x-cgeom.x, y-cgeom.y, btn);
92         }
93         else
94         {
95                 if(Widget *wdg = get_child_at(x, y))
96                 {
97                         set_pointer_focus(wdg);
98                         if(wdg->is_focusable())
99                                 set_input_focus(wdg);
100                 }
101                 Container::button_press(x, y, btn);
102         }
103 }
104
105 void Panel::button_release(int x, int y, unsigned btn)
106 {
107         if(pointer_grabbed)
108         {
109                 const Geometry &cgeom = pointer_focus->get_geometry();
110                 pointer_focus->button_release(x-cgeom.x, y-cgeom.y, btn);
111         }
112         else
113                 Container::button_release(x, y, btn);
114 }
115
116 void Panel::pointer_motion(int x, int y)
117 {
118         if(pointer_grabbed)
119         {
120                 const Geometry &cgeom = pointer_focus->get_geometry();
121                 pointer_focus->pointer_motion(x-cgeom.x, y-cgeom.y);
122         }
123         else
124         {
125                 set_pointer_focus(get_child_at(x, y));
126                 Container::pointer_motion(x, y);
127         }
128 }
129
130 void Panel::pointer_leave()
131 {
132         Container::pointer_leave();
133         set_pointer_focus(0);
134 }
135
136 void Panel::key_press(unsigned key, unsigned mod)
137 {
138         if(input_focus)
139                 input_focus->key_press(key, mod);
140 }
141
142 void Panel::key_release(unsigned key, unsigned mod)
143 {
144         if(input_focus)
145                 input_focus->key_release(key, mod);
146 }
147
148 void Panel::character(wchar_t ch)
149 {
150         if(input_focus)
151                 input_focus->character(ch);
152 }
153
154 void Panel::focus_out()
155 {
156         set_input_focus(0);
157         Widget::focus_out();
158 }
159
160 void Panel::on_geometry_change()
161 {
162         if(layout)
163                 layout->update();
164 }
165
166 void Panel::on_child_added(Widget &wdg)
167 {
168         if(layout)
169         {
170                 layout->add_widget(wdg);
171                 signal_autosize_changed.emit();
172         }
173 }
174
175 void Panel::on_child_removed(Widget &wdg)
176 {
177         if(layout)
178         {
179                 layout->remove_widget(wdg);
180                 signal_autosize_changed.emit();
181         }
182 }
183
184 void Panel::set_pointer_focus(Widget *wdg)
185 {
186         if(wdg!=pointer_focus)
187         {
188                 if(pointer_focus)
189                         pointer_focus->pointer_leave();
190
191                 pointer_focus = wdg;
192
193                 if(pointer_focus)
194                         pointer_focus->pointer_enter();
195         }
196 }
197
198 void Panel::set_input_focus(Widget *wdg)
199 {
200         if(wdg!=input_focus)
201         {
202                 if(input_focus)
203                         input_focus->focus_out();
204
205                 input_focus = wdg;
206
207                 if(input_focus)
208                 {
209                         raise(*wdg);
210                         input_focus->focus_in();
211                 }
212         }
213 }
214
215
216 Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
217         Widget::Loader(p),
218         pnl(p),
219         wdg_map(m)
220 {
221         add("button",    &Loader::child<Button>);
222         add("dropdown",  &Loader::child<Dropdown>);
223         add("entry",     &Loader::child<Entry>);
224         add("hslider",   &Loader::child<HSlider>);
225         add("indicator", &Loader::child<Indicator>);
226         add("label",     &Loader::child<Label>);
227         add("list",      &Loader::child<List>);
228         add("panel",     &Loader::panel);
229         add("table",     &Loader::child<Table>);
230         add("toggle",    &Loader::child<Toggle>);
231         add("vslider",   &Loader::child<VSlider>);
232 }
233
234 template<typename T>
235 void Panel::Loader::child(const string &n)
236 {
237         RefPtr<T> chl = new T();
238         load_sub(*chl);
239         pnl.add(*chl.get());
240         wdg_map[n] = chl.release();
241 }
242
243 void Panel::Loader::panel(const string &n)
244 {
245         RefPtr<Panel> p = new Panel();
246         load_sub(*p, wdg_map);
247         pnl.add(*p.get());
248         wdg_map[n] = p.release();
249 }
250
251
252 Panel::Child::Child(Panel &p, Widget *w):
253         Container::Child(p, w)
254 {
255         widget->signal_visibility_changed.connect(sigc::mem_fun(this, &Child::visibility_changed));
256         widget->signal_request_focus.connect(sigc::mem_fun(this, &Child::request_focus));
257         widget->signal_grab_pointer.connect(sigc::mem_fun(this, &Child::grab_pointer));
258         widget->signal_ungrab_pointer.connect(sigc::mem_fun(this, &Child::ungrab_pointer));
259 }
260
261 Panel::Child::~Child()
262 {
263         visibility_changed(false);
264 }
265
266 void Panel::Child::visibility_changed(bool v)
267 {
268         if(!v)
269         {
270                 Panel &panel = static_cast<Panel &>(container);
271                 if(widget==panel.pointer_focus)
272                         panel.set_pointer_focus(0);
273                 if(widget==panel.input_focus)
274                         panel.set_input_focus(0);
275         }
276 }
277
278 void Panel::Child::autosize_changed()
279 {
280         Panel &panel = static_cast<Panel &>(container);
281         if(panel.layout)
282                 panel.layout->update();
283 }
284
285 void Panel::Child::request_focus()
286 {
287         Panel &panel = static_cast<Panel &>(container);
288         panel.set_input_focus(widget);
289         if(panel.parent && panel.visible)
290                 panel.set_focus();
291 }
292
293 void Panel::Child::grab_pointer()
294 {
295         Panel &panel = static_cast<Panel &>(container);
296         if(!panel.pointer_grabbed)
297         {
298                 panel.set_pointer_focus(widget);
299                 panel.pointer_grabbed = true;
300                 panel.signal_grab_pointer.emit();
301         }
302 }
303
304 void Panel::Child::ungrab_pointer()
305 {
306         Panel &panel = static_cast<Panel &>(container);
307         if(panel.pointer_grabbed && panel.pointer_focus==widget)
308         {
309                 // XXX Should set to the widget under pointer
310                 panel.set_pointer_focus(0);
311                 panel.pointer_grabbed = false;
312                 panel.signal_ungrab_pointer.emit();
313         }
314 }
315
316 } // namespace GLtk
317 } // namespace Msp