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