]> git.tdb.fi Git - libs/gltk.git/blob - source/panel.cpp
Add Dropdown widget
[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 <msp/core/refptr.h>
9 #include "button.h"
10 #include "dropdown.h"
11 #include "entry.h"
12 #include "hslider.h"
13 #include "indicator.h"
14 #include "label.h"
15 #include "list.h"
16 #include "panel.h"
17 #include "part.h"
18 #include "toggle.h"
19 #include "vslider.h"
20
21 using namespace std;
22
23 namespace Msp {
24 namespace GLtk {
25
26 Panel::Panel(const Resources &r):
27         Widget(r),
28         pointer_focus(0),
29         pointer_grab(0),
30         input_focus(0)
31 {
32         update_style();
33 }
34
35 Panel::~Panel()
36 {
37         while(!children.empty())
38                 delete children.front();
39 }
40
41 void Panel::add(Widget &wdg)
42 {
43         set_parent(wdg, this);
44         children.push_back(&wdg);
45 }
46
47 void Panel::remove(Widget &wdg)
48 {
49         ChildSeq::iterator i=find(children.begin(), children.end(), &wdg);
50         if(i!=children.end())
51         {
52                 set_parent(wdg, 0);
53                 children.erase(i);
54         }
55 }
56
57 void Panel::raise(Widget &wdg)
58 {
59         ChildSeq::iterator i=find(children.begin(), children.end(), &wdg);
60         if(i!=children.end())
61         {
62                 children.erase(i);
63                 children.push_back(&wdg);
64         }
65 }
66
67 void Panel::button_press(int x, int y, unsigned btn)
68 {
69         if(pointer_grab>0)
70         {
71                 const Geometry &cgeom=pointer_focus->get_geometry();
72                 pointer_focus->button_press(x-cgeom.x, y-cgeom.y, btn);
73         }
74         else if(geom.is_inside_relative(x, y))
75         {
76                 if(Widget *wdg=get_child_at(x, y))
77                 {
78                         set_pointer_focus(wdg, btn);
79                         set_input_focus(wdg);
80
81                         const Geometry &cgeom=wdg->get_geometry();
82                         wdg->button_press(x-cgeom.x, y-cgeom.y, btn);
83                 }
84         }
85 }
86
87 void Panel::button_release(int x, int y, unsigned btn)
88 {
89         if(pointer_grab>0)
90         {
91                 const Geometry &cgeom=pointer_focus->get_geometry();
92                 pointer_focus->button_release(x-cgeom.x, y-cgeom.y, btn);
93
94                 if(btn==pointer_grab)
95                         set_pointer_focus(get_child_at(x, y), 0);
96         }
97         else if(geom.is_inside_relative(x, y))
98         {
99                 if(Widget *wdg=get_child_at(x, y))
100                 {
101                         const Geometry &cgeom=wdg->get_geometry();
102                         wdg->button_release(x-cgeom.x, y-cgeom.y, btn);
103                 }
104         }
105 }
106
107 void Panel::pointer_motion(int x, int y)
108 {
109         if(pointer_grab>0)
110         {
111                 const Geometry &cgeom=pointer_focus->get_geometry();
112                 pointer_focus->pointer_motion(x-cgeom.x, y-cgeom.y);
113         }
114         else if(geom.is_inside_relative(x, y))
115         {
116                 Widget *wdg=get_child_at(x, y);
117                 set_pointer_focus(wdg, 0);
118                 if(wdg)
119                 {
120                         const Geometry &cgeom=wdg->get_geometry();
121                         wdg->pointer_motion(x-cgeom.x, y-cgeom.y);
122                 }
123         }
124 }
125
126 void Panel::pointer_leave()
127 {
128         set_pointer_focus(0, 0);
129 }
130
131 void Panel::key_press(unsigned key, unsigned mod, wchar_t ch)
132 {
133         if(input_focus)
134                 input_focus->key_press(key, mod, ch);
135 }
136
137 void Panel::key_release(unsigned key, unsigned mod)
138 {
139         if(input_focus)
140                 input_focus->key_release(key, mod);
141 }
142
143 void Panel::focus_out()
144 {
145         set_input_focus(0);
146 }
147
148 void Panel::child_hidden(Widget &wdg)
149 {
150         if(&wdg==pointer_focus)
151                 set_pointer_focus(0, 0);
152 }
153
154 void Panel::grab_pointer(Widget &wdg)
155 {
156         if(pointer_grab==0 || pointer_focus==&wdg)
157                 set_pointer_focus(&wdg, 255);
158         else
159                 throw InvalidState("Pointer is already grabbed");
160 }
161
162 void Panel::ungrab_pointer(Widget &wdg)
163 {
164         if(pointer_focus==&wdg)
165                 set_pointer_focus(0, 0);
166         else if(pointer_grab>0)
167                 throw Exception("Someone is trying to steal the pointer!");
168 }
169
170 void Panel::render_special(const Part &part) const
171 {
172         if(part.get_name()=="children")
173         {
174                 for(ChildSeq::const_iterator i=children.begin(); i!=children.end(); ++i)
175                         if((*i)->is_visible())
176                                 (*i)->render();
177         }
178 }
179
180 void Panel::set_pointer_focus(Widget *wdg, int grab)
181 {
182         if(grab>0 && !wdg)
183                 throw InvalidParameterValue("Can't grab on null widget");
184
185         if(wdg!=pointer_focus)
186         {
187                 if(pointer_focus)
188                         pointer_focus->pointer_leave();
189
190                 pointer_focus=wdg;
191
192                 if(pointer_focus)
193                         pointer_focus->pointer_enter();
194         }
195
196         pointer_grab=grab;
197 }
198
199 void Panel::set_input_focus(Widget *wdg)
200 {
201         if(wdg!=input_focus)
202         {
203                 if(input_focus)
204                         input_focus->focus_out();
205
206                 input_focus=wdg;
207
208                 if(input_focus)
209                         input_focus->focus_in();
210         }
211 }
212
213 Widget *Panel::get_child_at(int x, int y)
214 {
215         for(ChildSeq::reverse_iterator i=children.rbegin(); i!=children.rend(); ++i)
216                 if((*i)->is_visible() && (*i)->get_geometry().is_inside(x, y))
217                         return *i;
218
219         return 0;
220 }
221
222
223 Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
224         Widget::Loader(p),
225         pnl(p),
226         wdg_map(m)
227 {
228         add("button",    &Loader::child<Button>);
229         add("dropdown",  &Loader::child<Dropdown>);
230         add("entry",     &Loader::child<Entry>);
231         add("hslider",   &Loader::child<HSlider>);
232         add("indicator", &Loader::child<Indicator>);
233         add("label",     &Loader::child<Label>);
234         add("list",      &Loader::child<List>);
235         add("panel",     &Loader::panel);
236         add("toggle",    &Loader::child<Toggle>);
237         add("vslider",   &Loader::child<VSlider>);
238 }
239
240 template<typename T>
241 void Panel::Loader::child(const string &n)
242 {
243         RefPtr<T> chl=new T(pnl.res);
244         load_sub(*chl);
245         pnl.add(*chl.get());
246         wdg_map[n]=chl.release();
247 }
248
249 void Panel::Loader::panel(const string &n)
250 {
251         RefPtr<Panel> p=new Panel(pnl.res);
252         load_sub(*p, wdg_map);
253         pnl.add(*p.get());
254         wdg_map[n]=p.release();
255 }
256
257 } // namespace GLtk
258 } // namespace Msp