]> git.tdb.fi Git - libs/gltk.git/blob - source/panel.cpp
Add Container class
[libs/gltk.git] / source / panel.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007-2009  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         Container(r),
31         pointer_focus(0),
32         pointer_grabbed(false),
33         input_focus(0)
34 {
35         update_style();
36 }
37
38 void Panel::raise(Widget &wdg)
39 {
40         for(list<Container::Child *>::iterator i=children.begin(); i!=children.end(); ++i)
41                 if((*i)->widget==&wdg)
42                 {
43                         children.splice(children.end(), children, i);
44                         return;
45                 }
46
47         throw InvalidState("That Widget is not in this Panel");
48 }
49
50 void Panel::button_press(int x, int y, unsigned btn)
51 {
52         if(pointer_grabbed)
53         {
54                 const Geometry &cgeom=pointer_focus->get_geometry();
55                 pointer_focus->button_press(x-cgeom.x, y-cgeom.y, btn);
56         }
57         else
58         {
59                 if(Widget *wdg=get_child_at(x, y))
60                 {
61                         set_pointer_focus(wdg);
62                         set_input_focus(wdg);
63                 }
64                 Container::button_press(x, y, btn);
65         }
66 }
67
68 void Panel::button_release(int x, int y, unsigned btn)
69 {
70         if(pointer_grabbed)
71         {
72                 const Geometry &cgeom=pointer_focus->get_geometry();
73                 pointer_focus->button_release(x-cgeom.x, y-cgeom.y, btn);
74         }
75         else
76                 Container::button_release(x, y, btn);
77 }
78
79 void Panel::pointer_motion(int x, int y)
80 {
81         if(pointer_grabbed)
82         {
83                 const Geometry &cgeom=pointer_focus->get_geometry();
84                 pointer_focus->pointer_motion(x-cgeom.x, y-cgeom.y);
85         }
86         else
87         {
88                 set_pointer_focus(get_child_at(x, y));
89                 Container::pointer_motion(x, y);
90         }
91 }
92
93 void Panel::pointer_leave()
94 {
95         Container::pointer_leave();
96         set_pointer_focus(0);
97 }
98
99 void Panel::key_press(unsigned key, unsigned mod, wchar_t ch)
100 {
101         if(input_focus)
102                 input_focus->key_press(key, mod, ch);
103 }
104
105 void Panel::key_release(unsigned key, unsigned mod)
106 {
107         if(input_focus)
108                 input_focus->key_release(key, mod);
109 }
110
111 void Panel::focus_out()
112 {
113         set_input_focus(0);
114 }
115
116 void Panel::render_special(const Part &part) const
117 {
118         if(part.get_name()=="children")
119         {
120                 for(list<Container::Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
121                         if((*i)->widget->is_visible())
122                                 (*i)->widget->render();
123         }
124 }
125
126 Panel::Child *Panel::create_child(Widget *wdg)
127 {
128         return new Child(*this, wdg);
129 }
130
131 void Panel::set_pointer_focus(Widget *wdg)
132 {
133         if(wdg!=pointer_focus)
134         {
135                 if(pointer_focus)
136                         pointer_focus->pointer_leave();
137
138                 pointer_focus=wdg;
139
140                 if(pointer_focus)
141                         pointer_focus->pointer_enter();
142         }
143 }
144
145 void Panel::set_input_focus(Widget *wdg)
146 {
147         if(wdg!=input_focus)
148         {
149                 if(input_focus)
150                         input_focus->focus_out();
151
152                 input_focus=wdg;
153
154                 if(input_focus)
155                 {
156                         raise(*wdg);
157                         input_focus->focus_in();
158                 }
159         }
160 }
161
162
163 Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
164         Widget::Loader(p),
165         pnl(p),
166         wdg_map(m)
167 {
168         add("button",    &Loader::child<Button>);
169         add("dropdown",  &Loader::child<Dropdown>);
170         add("entry",     &Loader::child<Entry>);
171         add("hslider",   &Loader::child<HSlider>);
172         add("indicator", &Loader::child<Indicator>);
173         add("label",     &Loader::child<Label>);
174         add("list",      &Loader::child<List>);
175         add("panel",     &Loader::panel);
176         add("table",     &Loader::child<Table>);
177         add("toggle",    &Loader::child<Toggle>);
178         add("vslider",   &Loader::child<VSlider>);
179 }
180
181 template<typename T>
182 void Panel::Loader::child(const string &n)
183 {
184         RefPtr<T> chl=new T(pnl.res);
185         load_sub(*chl);
186         pnl.add(*chl.get());
187         wdg_map[n]=chl.release();
188 }
189
190 void Panel::Loader::panel(const string &n)
191 {
192         RefPtr<Panel> p=new Panel(pnl.res);
193         load_sub(*p, wdg_map);
194         pnl.add(*p.get());
195         wdg_map[n]=p.release();
196 }
197
198
199 Panel::Child::Child(Panel &p, Widget *w):
200         Container::Child(p, w)
201 {
202         widget->signal_visibility_changed.connect(sigc::mem_fun(this, &Child::visibility_changed));
203         widget->signal_request_focus.connect(sigc::mem_fun(this, &Child::request_focus));
204         widget->signal_grab_pointer.connect(sigc::mem_fun(this, &Child::grab_pointer));
205         widget->signal_ungrab_pointer.connect(sigc::mem_fun(this, &Child::ungrab_pointer));
206 }
207
208 Panel::Child::~Child()
209 {
210         visibility_changed(false);
211 }
212
213 void Panel::Child::visibility_changed(bool v)
214 {
215         if(!v)
216         {
217                 Panel &panel=static_cast<Panel &>(container);
218                 if(widget==panel.pointer_focus)
219                         panel.set_pointer_focus(0);
220                 if(widget==panel.input_focus)
221                         panel.set_input_focus(0);
222         }
223 }
224
225 void Panel::Child::request_focus()
226 {
227         static_cast<Panel &>(container).set_input_focus(widget);
228 }
229
230 void Panel::Child::grab_pointer()
231 {
232         Panel &panel=static_cast<Panel &>(container);
233         if(!panel.pointer_grabbed)
234         {
235                 panel.set_pointer_focus(widget);
236                 panel.pointer_grabbed=true;
237                 panel.signal_grab_pointer.emit();
238         }
239 }
240
241 void Panel::Child::ungrab_pointer()
242 {
243         Panel &panel=static_cast<Panel &>(container);
244         if(panel.pointer_grabbed && panel.pointer_focus==widget)
245         {
246                 // XXX Should set to the widget under pointer
247                 panel.set_pointer_focus(0);
248                 panel.pointer_grabbed=false;
249                 panel.signal_ungrab_pointer.emit();
250         }
251 }
252
253 } // namespace GLtk
254 } // namespace Msp