3 This file is part of libmspgltk
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/core/refptr.h>
13 #include "indicator.h"
27 Panel::Panel(const Resources &r):
38 while(!children.empty())
39 delete children.front();
42 void Panel::add(Widget &wdg)
44 set_parent(wdg, this);
45 children.push_back(&wdg);
48 void Panel::remove(Widget &wdg)
50 ChildSeq::iterator i=find(children.begin(), children.end(), &wdg);
53 if(&wdg==pointer_focus)
54 set_pointer_focus(0, 0);
63 void Panel::raise(Widget &wdg)
65 ChildSeq::iterator i=find(children.begin(), children.end(), &wdg);
69 children.push_back(&wdg);
73 void Panel::button_press(int x, int y, unsigned btn)
77 const Geometry &cgeom=pointer_focus->get_geometry();
78 pointer_focus->button_press(x-cgeom.x, y-cgeom.y, btn);
80 else if(geom.is_inside_relative(x, y))
82 if(Widget *wdg=get_child_at(x, y))
84 set_pointer_focus(wdg, btn);
87 const Geometry &cgeom=wdg->get_geometry();
88 wdg->button_press(x-cgeom.x, y-cgeom.y, btn);
93 void Panel::button_release(int x, int y, unsigned btn)
97 const Geometry &cgeom=pointer_focus->get_geometry();
98 pointer_focus->button_release(x-cgeom.x, y-cgeom.y, btn);
100 if(btn==pointer_grab)
101 set_pointer_focus(get_child_at(x, y), 0);
103 else if(geom.is_inside_relative(x, y))
105 if(Widget *wdg=get_child_at(x, y))
107 const Geometry &cgeom=wdg->get_geometry();
108 wdg->button_release(x-cgeom.x, y-cgeom.y, btn);
113 void Panel::pointer_motion(int x, int y)
117 const Geometry &cgeom=pointer_focus->get_geometry();
118 pointer_focus->pointer_motion(x-cgeom.x, y-cgeom.y);
120 else if(geom.is_inside_relative(x, y))
122 Widget *wdg=get_child_at(x, y);
123 set_pointer_focus(wdg, 0);
126 const Geometry &cgeom=wdg->get_geometry();
127 wdg->pointer_motion(x-cgeom.x, y-cgeom.y);
132 void Panel::pointer_leave()
134 set_pointer_focus(0, 0);
137 void Panel::key_press(unsigned key, unsigned mod, wchar_t ch)
140 input_focus->key_press(key, mod, ch);
143 void Panel::key_release(unsigned key, unsigned mod)
146 input_focus->key_release(key, mod);
149 void Panel::focus_out()
154 void Panel::child_hidden(Widget &wdg)
156 if(&wdg==pointer_focus)
157 set_pointer_focus(0, 0);
160 void Panel::grab_pointer(Widget &wdg)
162 if(pointer_grab==0 || pointer_focus==&wdg)
163 set_pointer_focus(&wdg, 255);
165 throw InvalidState("Pointer is already grabbed");
168 void Panel::ungrab_pointer(Widget &wdg)
170 if(pointer_focus==&wdg)
171 set_pointer_focus(0, 0);
172 else if(pointer_grab>0)
173 throw Exception("Someone is trying to steal the pointer!");
176 void Panel::render_special(const Part &part) const
178 if(part.get_name()=="children")
180 for(ChildSeq::const_iterator i=children.begin(); i!=children.end(); ++i)
181 if((*i)->is_visible())
186 void Panel::set_pointer_focus(Widget *wdg, int grab)
189 throw InvalidParameterValue("Can't grab on null widget");
191 if(wdg!=pointer_focus)
194 pointer_focus->pointer_leave();
199 pointer_focus->pointer_enter();
205 void Panel::set_input_focus(Widget *wdg)
210 input_focus->focus_out();
215 input_focus->focus_in();
219 Widget *Panel::get_child_at(int x, int y)
221 for(ChildSeq::reverse_iterator i=children.rbegin(); i!=children.rend(); ++i)
222 if((*i)->is_visible() && (*i)->get_geometry().is_inside(x, y))
229 Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
234 add("button", &Loader::child<Button>);
235 add("dropdown", &Loader::child<Dropdown>);
236 add("entry", &Loader::child<Entry>);
237 add("hslider", &Loader::child<HSlider>);
238 add("indicator", &Loader::child<Indicator>);
239 add("label", &Loader::child<Label>);
240 add("list", &Loader::child<List>);
241 add("panel", &Loader::panel);
242 add("table", &Loader::child<Table>);
243 add("toggle", &Loader::child<Toggle>);
244 add("vslider", &Loader::child<VSlider>);
248 void Panel::Loader::child(const string &n)
250 RefPtr<T> chl=new T(pnl.res);
253 wdg_map[n]=chl.release();
256 void Panel::Loader::panel(const string &n)
258 RefPtr<Panel> p=new Panel(pnl.res);
259 load_sub(*p, wdg_map);
261 wdg_map[n]=p.release();