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