]> git.tdb.fi Git - libs/gltk.git/blob - source/panel.cpp
Enable loading of widgets from datafiles (not implemented for all widgets yet)
[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                 for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
33                         if((*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
34                         {
35                                 (*i)->button_press(x-geom.x, y-geom.y, btn);
36                                 pointer_grab=btn;
37                                 set_input_focus(*i);
38                         }
39         }
40 }
41
42 void Panel::button_release(int x, int y, unsigned btn)
43 {
44         if(pointer_grab>0)
45         {
46                 pointer_focus->button_release(x-geom.x, y-geom.y, btn);
47
48                 if(btn==pointer_grab)
49                 {
50                         pointer_grab=0;
51
52                         for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
53                                 if((*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
54                                 {
55                                         set_pointer_focus(*i);
56                                         break;
57                                 }
58                 }
59         }
60         else if(geom.is_inside(x, y))
61         {
62                 for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
63                         if((*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
64                                 (*i)->button_release(x-geom.x, y-geom.y, btn);
65         }
66 }
67
68 void Panel::pointer_motion(int x, int y)
69 {
70         if(pointer_grab>0)
71                 pointer_focus->pointer_motion(x-geom.x, y-geom.y);
72         else if(geom.is_inside(x, y))
73         {
74                 bool found=false;
75                 for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
76                         if((*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
77                         {
78                                 set_pointer_focus(*i);
79                                 (*i)->pointer_motion(x-geom.x, y-geom.y);
80                                 found=true;
81                         }
82
83                 if(!found)
84                         set_pointer_focus(0);
85         }
86 }
87
88 void Panel::key_press(unsigned key, unsigned mod, wchar_t ch)
89 {
90         if(input_focus)
91                 input_focus->key_press(key, mod, ch);
92 }
93
94 void Panel::key_release(unsigned key, unsigned mod)
95 {
96         if(input_focus)
97                 input_focus->key_release(key, mod);
98 }
99
100 void Panel::focus_out()
101 {
102         set_input_focus(0);
103 }
104
105 void Panel::add(Widget &wdg)
106 {
107         children.push_back(&wdg);
108 }
109
110 void Panel::render_part(const Part &part) const
111 {
112         if(part.get_name()=="children")
113         {
114                 for(ChildSeq::const_iterator i=children.begin(); i!=children.end(); ++i)
115                         (*i)->render();
116         }
117         else
118                 Widget::render_part(part);
119 }
120
121 void Panel::set_pointer_focus(Widget *wdg)
122 {
123         if(wdg!=pointer_focus && pointer_grab==0)
124         {
125                 if(pointer_focus)
126                         pointer_focus->pointer_leave();
127
128                 pointer_focus=wdg;
129
130                 if(pointer_focus)
131                         pointer_focus->pointer_enter();
132         }
133 }
134
135 void Panel::set_input_focus(Widget *wdg)
136 {
137         if(wdg!=input_focus)
138         {
139                 if(input_focus)
140                         input_focus->focus_out();
141
142                 input_focus=wdg;
143
144                 if(input_focus)
145                         input_focus->focus_in();
146         }
147 }
148
149
150 Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
151         Widget::Loader(p),
152         panel(p),
153         wdg_map(m)
154 {
155         add("label", &Loader::child<Label>);
156 }
157
158 template<typename T>
159 void Panel::Loader::child(const string &n)
160 {
161         RefPtr<T> chl=new T(panel.res);
162         load_sub(*chl);
163         panel.add(*chl.get());
164         wdg_map[n]=chl.release();
165 }
166
167 } // namespace GLtk
168 } // namespace Msp