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