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