3 This file is part of libmspgltk
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/core/refptr.h>
14 #include "indicator.h"
28 Panel::Panel(const Resources &r):
39 while(!children.empty())
40 delete children.front();
43 void Panel::add(Widget &wdg)
45 set_parent(wdg, this);
46 children.push_back(&wdg);
49 void Panel::remove(Widget &wdg)
51 list<Widget *>::iterator i=find(children.begin(), children.end(), &wdg);
53 throw InvalidState("That Widget is not in this Panel");
55 if(&wdg==pointer_focus)
56 set_pointer_focus(0, 0);
64 void Panel::raise(Widget &wdg)
66 list<Widget *>::iterator i=find(children.begin(), children.end(), &wdg);
68 throw InvalidState("That Widget is not in this Panel");
71 children.push_back(&wdg);
74 void Panel::button_press(int x, int y, unsigned btn)
78 const Geometry &cgeom=pointer_focus->get_geometry();
79 pointer_focus->button_press(x-cgeom.x, y-cgeom.y, btn);
81 else if(geom.is_inside_relative(x, y))
83 if(Widget *wdg=get_child_at(x, y))
85 set_pointer_focus(wdg, btn);
88 const Geometry &cgeom=wdg->get_geometry();
89 wdg->button_press(x-cgeom.x, y-cgeom.y, btn);
94 void Panel::button_release(int x, int y, unsigned btn)
98 const Geometry &cgeom=pointer_focus->get_geometry();
99 pointer_focus->button_release(x-cgeom.x, y-cgeom.y, btn);
101 if(btn==pointer_grab)
102 set_pointer_focus(get_child_at(x, y), 0);
104 else if(geom.is_inside_relative(x, y))
106 if(Widget *wdg=get_child_at(x, y))
108 const Geometry &cgeom=wdg->get_geometry();
109 wdg->button_release(x-cgeom.x, y-cgeom.y, btn);
114 void Panel::pointer_motion(int x, int y)
118 const Geometry &cgeom=pointer_focus->get_geometry();
119 pointer_focus->pointer_motion(x-cgeom.x, y-cgeom.y);
121 else if(geom.is_inside_relative(x, y))
123 Widget *wdg=get_child_at(x, y);
124 set_pointer_focus(wdg, 0);
127 const Geometry &cgeom=wdg->get_geometry();
128 wdg->pointer_motion(x-cgeom.x, y-cgeom.y);
133 void Panel::pointer_leave()
135 set_pointer_focus(0, 0);
138 void Panel::key_press(unsigned key, unsigned mod, wchar_t ch)
141 input_focus->key_press(key, mod, ch);
144 void Panel::key_release(unsigned key, unsigned mod)
147 input_focus->key_release(key, mod);
150 void Panel::focus_out()
155 void Panel::child_hidden(Widget &wdg)
157 if(&wdg==pointer_focus)
158 set_pointer_focus(0, 0);
159 if(&wdg==input_focus)
163 void Panel::grab_pointer(Widget &wdg)
165 if(pointer_grab==0 || pointer_focus==&wdg)
166 set_pointer_focus(&wdg, 255);
168 throw InvalidState("Pointer is already grabbed");
171 void Panel::ungrab_pointer(Widget &wdg)
173 if(pointer_focus==&wdg)
174 set_pointer_focus(0, 0);
175 else if(pointer_grab>0)
176 throw Exception("Someone is trying to steal the pointer!");
179 void Panel::grab_focus(Widget &wdg)
181 list<Widget *>::iterator i=find(children.begin(), children.end(), &wdg);
182 if(i==children.end())
183 throw InvalidState("That Widget is not in this Panel");
185 set_input_focus(&wdg);
187 parent->grab_focus(*this);
190 void Panel::render_special(const Part &part) const
192 if(part.get_name()=="children")
194 for(list<Widget *>::const_iterator i=children.begin(); i!=children.end(); ++i)
195 if((*i)->is_visible())
200 void Panel::set_pointer_focus(Widget *wdg, int grab)
203 throw InvalidParameterValue("Can't grab on null widget");
205 if(wdg!=pointer_focus)
208 pointer_focus->pointer_leave();
213 pointer_focus->pointer_enter();
219 void Panel::set_input_focus(Widget *wdg)
224 input_focus->focus_out();
229 input_focus->focus_in();
233 Widget *Panel::get_child_at(int x, int y)
235 for(list<Widget *>::reverse_iterator i=children.rbegin(); i!=children.rend(); ++i)
236 if((*i)->is_visible() && (*i)->get_geometry().is_inside(x, y))
243 Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
248 add("button", &Loader::child<Button>);
249 add("dropdown", &Loader::child<Dropdown>);
250 add("entry", &Loader::child<Entry>);
251 add("hslider", &Loader::child<HSlider>);
252 add("indicator", &Loader::child<Indicator>);
253 add("label", &Loader::child<Label>);
254 add("list", &Loader::child<List>);
255 add("panel", &Loader::panel);
256 add("table", &Loader::child<Table>);
257 add("toggle", &Loader::child<Toggle>);
258 add("vslider", &Loader::child<VSlider>);
262 void Panel::Loader::child(const string &n)
264 RefPtr<T> chl=new T(pnl.res);
267 wdg_map[n]=chl.release();
270 void Panel::Loader::panel(const string &n)
272 RefPtr<Panel> p=new Panel(pnl.res);
273 load_sub(*p, wdg_map);
275 wdg_map[n]=p.release();