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