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