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