]> git.tdb.fi Git - libs/gltk.git/blob - source/panel.cpp
Add key events
[libs/gltk.git] / source / panel.cpp
1 #include "panel.h"
2 #include "part.h"
3
4 namespace Msp {
5 namespace GLtk {
6
7 Panel::Panel(const Resources &r):
8         Widget(r),
9         pointer_focus(0),
10         pointer_grab(0),
11         input_focus(0)
12 {
13         update_style();
14 }
15
16 Panel::~Panel()
17 {
18         for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
19                 delete *i;
20 }
21
22 void Panel::button_press(int x, int y, unsigned btn)
23 {
24         if(pointer_grab>0)
25                 pointer_focus->button_press(x-geom.x, y-geom.y, btn);
26         else if(geom.is_inside(x, y))
27         {
28                 for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
29                         if((*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
30                         {
31                                 (*i)->button_press(x-geom.x, y-geom.y, btn);
32                                 pointer_grab=btn;
33                                 set_input_focus(*i);
34                         }
35         }
36 }
37
38 void Panel::button_release(int x, int y, unsigned btn)
39 {
40         if(pointer_grab>0)
41         {
42                 pointer_focus->button_release(x-geom.x, y-geom.y, btn);
43
44                 if(btn==pointer_grab)
45                 {
46                         pointer_grab=0;
47
48                         for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
49                                 if((*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
50                                 {
51                                         set_pointer_focus(*i);
52                                         break;
53                                 }
54                 }
55         }
56         else if(geom.is_inside(x, y))
57         {
58                 for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
59                         if((*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
60                                 (*i)->button_release(x-geom.x, y-geom.y, btn);
61         }
62 }
63
64 void Panel::pointer_motion(int x, int y)
65 {
66         if(pointer_grab>0)
67                 pointer_focus->pointer_motion(x-geom.x, y-geom.y);
68         else if(geom.is_inside(x, y))
69         {
70                 bool found=false;
71                 for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i)
72                         if((*i)->get_geometry().is_inside(x-geom.x, y-geom.y))
73                         {
74                                 set_pointer_focus(*i);
75                                 (*i)->pointer_motion(x-geom.x, y-geom.y);
76                                 found=true;
77                         }
78
79                 if(!found)
80                         set_pointer_focus(0);
81         }
82 }
83
84 void Panel::key_press(unsigned key, unsigned mod, wchar_t ch)
85 {
86         if(input_focus)
87                 input_focus->key_press(key, mod, ch);
88 }
89
90 void Panel::key_release(unsigned key, unsigned mod)
91 {
92         if(input_focus)
93                 input_focus->key_release(key, mod);
94 }
95
96 void Panel::focus_out()
97 {
98         set_input_focus(0);
99 }
100
101 void Panel::add(Widget &wdg)
102 {
103         children.push_back(&wdg);
104 }
105
106 void Panel::render_part(const Part &part) const
107 {
108         if(part.get_name()=="children")
109         {
110                 for(ChildSeq::const_iterator i=children.begin(); i!=children.end(); ++i)
111                         (*i)->render();
112         }
113         else
114                 Widget::render_part(part);
115 }
116
117 void Panel::set_pointer_focus(Widget *wdg)
118 {
119         if(wdg!=pointer_focus && pointer_grab==0)
120         {
121                 if(pointer_focus)
122                         pointer_focus->pointer_leave();
123
124                 pointer_focus=wdg;
125
126                 if(pointer_focus)
127                         pointer_focus->pointer_enter();
128         }
129 }
130
131 void Panel::set_input_focus(Widget *wdg)
132 {
133         if(wdg!=input_focus)
134         {
135                 if(input_focus)
136                         input_focus->focus_out();
137
138                 input_focus=wdg;
139
140                 if(input_focus)
141                         input_focus->focus_in();
142         }
143 }
144
145 } // namespace GLtk
146 } // namespace Msp