]> git.tdb.fi Git - libs/gltk.git/blob - source/root.cpp
Don't let input events that are passed to widgets go to other consumers
[libs/gltk.git] / source / root.cpp
1 #include <msp/gl/blend.h>
2 #include <msp/input/keys.h>
3 #include <msp/time/units.h>
4 #include <msp/time/utils.h>
5 #include "label.h"
6 #include "style.h"
7 #include "root.h"
8
9 namespace Msp {
10 namespace GLtk {
11
12 Root::Root(const Resources &r, Graphics::Window &window):
13         resources(r),
14         keyboard(new Input::Keyboard(window)),
15         mouse(new Input::Mouse(window)),
16         own_input(true)
17 {
18         set_geometry(Geometry(0, 0, window.get_width(), window.get_height()));
19
20         init();
21 }
22
23 Root::Root(const Resources &r, Input::Keyboard *k, Input::Mouse *m):
24         resources(r),
25         keyboard(k),
26         mouse(m),
27         own_input(false)
28 {
29         init();
30 }
31
32 void Root::init()
33 {
34         lbl_tooltip = 0;
35         tooltip_target = 0;
36
37         update_style();
38
39         mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
40         mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
41         mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
42         keyboard->signal_button_press.connect(sigc::mem_fun(this, &Root::key_press_event));
43         keyboard->signal_button_release.connect(sigc::mem_fun(this, &Root::key_release_event));
44         keyboard->signal_character.connect(sigc::mem_fun(this, &Root::character_event));
45 }
46
47 Root::~Root()
48 {
49         if(own_input)
50         {
51                 delete keyboard;
52                 delete mouse;
53         }
54 }
55
56 void Root::tick()
57 {
58         if(tooltip_timeout && Time::now()>tooltip_timeout)
59         {
60                 std::string tip;
61                 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
62                 {
63                         tip = wdg->get_tooltip();
64                         tooltip_target = wdg;
65                 }
66                 else
67                 {
68                         tip = signal_tooltip.emit(pointer_x, pointer_y);
69                         tooltip_target = this;
70                 }
71
72                 if(!tip.empty())
73                 {
74                         if(!lbl_tooltip)
75                         {
76                                 lbl_tooltip = new Label;
77                                 add(*lbl_tooltip);
78                                 lbl_tooltip->set_style("tooltip");
79                         }
80
81                         lbl_tooltip->set_text(tip);
82                         lbl_tooltip->autosize();
83                         const Geometry &tip_geom = lbl_tooltip->get_geometry();
84                         unsigned x = pointer_x+10;
85                         unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
86                         if(x+tip_geom.w>geom.w)
87                         {
88                                 if(pointer_x>static_cast<int>(tip_geom.w+2))
89                                         x = pointer_x-2-tip_geom.w;
90                                 else
91                                         x = geom.w-tip_geom.w;
92                         }
93                         lbl_tooltip->set_position(x, y);
94                         raise(*lbl_tooltip);
95                         lbl_tooltip->set_visible(true);
96                 }
97
98                 tooltip_timeout = Time::TimeStamp();
99         }
100 }
101
102 void Root::render() const
103 {
104         GL::MatrixStack::projection() = GL::Matrix::ortho_bottomleft(geom.w, geom.h);
105         GL::MatrixStack::modelview() = GL::Matrix();
106         GL::Bind bind_blend(GL::Blend::alpha());
107
108         GL::Renderer renderer(0);
109         Widget::render(renderer);
110 }
111
112 bool Root::button_press_event(unsigned btn)
113 {
114         if(visible)
115         {
116                 Widget *old_focus = pointer_focus;
117
118                 int x, y;
119                 get_pointer(x, y);
120                 button_press(x, y, btn);
121
122                 if(pointer_focus || old_focus)
123                         return true;
124         }
125
126         return false;
127 }
128
129 bool Root::button_release_event(unsigned btn)
130 {
131         if(visible)
132         {
133                 Widget *old_focus = pointer_focus;
134
135                 int x, y;
136                 get_pointer(x, y);
137                 button_release(x, y, btn);
138
139                 if(pointer_focus || old_focus)
140                         return true;
141         }
142
143         return false;
144 }
145
146 bool Root::axis_motion_event(unsigned, float, float)
147 {
148         if(visible)
149         {
150                 int x, y;
151                 get_pointer(x, y);
152                 pointer_motion(x, y);
153
154                 if(!tooltip_target)
155                 {
156                         pointer_x = x;
157                         pointer_y = y;
158                         tooltip_timeout = Time::now()+700*Time::msec;
159                 }
160                 else if(get_descendant_at(x, y)!=tooltip_target)
161                 {
162                         if(lbl_tooltip)
163                                 lbl_tooltip->set_visible(false);
164                         tooltip_target = 0;
165                 }
166
167                 if(pointer_focus)
168                         return true;
169         }
170
171         return false;
172 }
173
174 bool Root::key_press_event(unsigned key)
175 {
176         // XXX Modifiers
177         if(visible)
178         {
179                 Widget *old_focus = input_focus;
180
181                 key_press(key, 0);
182
183                 if(input_focus || old_focus)
184                         return true;
185         }
186
187         return false;
188 }
189
190 bool Root::key_release_event(unsigned key)
191 {
192         if(visible)
193         {
194                 Widget *old_focus = input_focus;
195
196                 key_release(key, 0);
197
198                 if(input_focus || old_focus)
199                         return true;
200         }
201
202         return false;
203 }
204
205 bool Root::character_event(StringCodec::unichar ch)
206 {
207         if(visible)
208         {
209                 Widget *old_focus = input_focus;
210
211                 character(ch);
212
213                 if(input_focus || old_focus)
214                         return true;
215         }
216
217         return false;
218 }
219
220 void Root::get_pointer(int &x, int &y)
221 {
222         x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
223         y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
224 }
225
226 } // namespace GLtk
227 } // namespace Msp