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 Widget *wdg=pointer_focus;
100 if(btn==pointer_grab)
101 set_pointer_focus(get_child_at(x, y), 0);
103 const Geometry &cgeom=wdg->get_geometry();
104 wdg->button_release(x-cgeom.x, y-cgeom.y, btn);
106 else if(geom.is_inside_relative(x, y))
108 if(Widget *wdg=get_child_at(x, y))
110 const Geometry &cgeom=wdg->get_geometry();
111 wdg->button_release(x-cgeom.x, y-cgeom.y, btn);
116 void Panel::pointer_motion(int x, int y)
120 const Geometry &cgeom=pointer_focus->get_geometry();
121 pointer_focus->pointer_motion(x-cgeom.x, y-cgeom.y);
123 else if(geom.is_inside_relative(x, y))
125 Widget *wdg=get_child_at(x, y);
126 set_pointer_focus(wdg, 0);
129 const Geometry &cgeom=wdg->get_geometry();
130 wdg->pointer_motion(x-cgeom.x, y-cgeom.y);
135 void Panel::pointer_leave()
137 set_pointer_focus(0, 0);
140 void Panel::key_press(unsigned key, unsigned mod, wchar_t ch)
143 input_focus->key_press(key, mod, ch);
146 void Panel::key_release(unsigned key, unsigned mod)
149 input_focus->key_release(key, mod);
152 void Panel::focus_out()
157 void Panel::child_hidden(Widget &wdg)
159 if(&wdg==pointer_focus)
160 set_pointer_focus(0, 0);
161 if(&wdg==input_focus)
165 void Panel::grab_pointer(Widget &wdg)
167 if(pointer_grab==0 || pointer_focus==&wdg)
169 set_pointer_focus(&wdg, 255);
171 parent->grab_pointer(*this);
174 throw InvalidState("Pointer is already grabbed");
177 void Panel::ungrab_pointer(Widget &wdg)
179 if(pointer_focus==&wdg)
181 set_pointer_focus(0, 0);
183 parent->ungrab_pointer(*this);
185 else if(pointer_grab>0)
186 throw Exception("Someone is trying to steal the pointer!");
189 void Panel::grab_focus(Widget &wdg)
191 list<Widget *>::iterator i=find(children.begin(), children.end(), &wdg);
192 if(i==children.end())
193 throw InvalidState("That Widget is not in this Panel");
195 set_input_focus(&wdg);
197 parent->grab_focus(*this);
200 void Panel::render_special(const Part &part) const
202 if(part.get_name()=="children")
204 for(list<Widget *>::const_iterator i=children.begin(); i!=children.end(); ++i)
205 if((*i)->is_visible())
210 void Panel::set_pointer_focus(Widget *wdg, int grab)
213 throw InvalidParameterValue("Can't grab on null widget");
215 if(wdg!=pointer_focus)
218 pointer_focus->pointer_leave();
223 pointer_focus->pointer_enter();
229 void Panel::set_input_focus(Widget *wdg)
234 input_focus->focus_out();
239 input_focus->focus_in();
243 Widget *Panel::get_child_at(int x, int y)
245 for(list<Widget *>::reverse_iterator i=children.rbegin(); i!=children.rend(); ++i)
246 if((*i)->is_visible() && (*i)->get_geometry().is_inside(x, y))
253 Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
258 add("button", &Loader::child<Button>);
259 add("dropdown", &Loader::child<Dropdown>);
260 add("entry", &Loader::child<Entry>);
261 add("hslider", &Loader::child<HSlider>);
262 add("indicator", &Loader::child<Indicator>);
263 add("label", &Loader::child<Label>);
264 add("list", &Loader::child<List>);
265 add("panel", &Loader::panel);
266 add("table", &Loader::child<Table>);
267 add("toggle", &Loader::child<Toggle>);
268 add("vslider", &Loader::child<VSlider>);
272 void Panel::Loader::child(const string &n)
274 RefPtr<T> chl=new T(pnl.res);
277 wdg_map[n]=chl.release();
280 void Panel::Loader::panel(const string &n)
282 RefPtr<Panel> p=new Panel(pnl.res);
283 load_sub(*p, wdg_map);
285 wdg_map[n]=p.release();