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