1 #include <msp/gl/blend.h>
2 #include <msp/input/keys.h>
3 #include <msp/time/units.h>
4 #include <msp/time/utils.h>
12 Root::Root(const Resources &r, Graphics::Window &w):
18 set_geometry(Geometry(0, 0, window.get_width(), window.get_height()));
22 window.signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
23 window.signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
24 window.signal_pointer_motion.connect(sigc::mem_fun(this, &Root::pointer_motion_event));
25 window.signal_key_press.connect(sigc::mem_fun(this, &Root::key_press_event));
26 window.signal_key_release.connect(sigc::mem_fun(this, &Root::key_release_event));
31 if(tooltip_timeout && Time::now()>tooltip_timeout)
34 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
36 tip = wdg->get_tooltip();
41 tip = signal_tooltip.emit(pointer_x, pointer_y);
42 tooltip_target = this;
49 lbl_tooltip = new Label;
51 lbl_tooltip->set_style("tooltip");
54 lbl_tooltip->set_text(tip);
55 lbl_tooltip->autosize();
56 const Geometry &tip_geom = lbl_tooltip->get_geometry();
57 unsigned x = pointer_x+10;
58 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
59 if(x+tip_geom.w>geom.w)
61 if(pointer_x>static_cast<int>(tip_geom.w+2))
62 x = pointer_x-2-tip_geom.w;
64 x = geom.w-tip_geom.w;
66 lbl_tooltip->set_position(x, y);
68 lbl_tooltip->set_visible(true);
71 tooltip_timeout = Time::TimeStamp();
75 void Root::render() const
77 GL::MatrixStack::projection() = GL::Matrix::ortho_bottomleft(geom.w, geom.h);
78 GL::MatrixStack::modelview() = GL::Matrix();
79 GL::Bind bind_blend(GL::Blend::alpha());
84 void Root::button_press_event(int x, int y, unsigned btn, unsigned mod)
88 Widget *old_focus = pointer_focus;
90 translate_coords(x, y);
91 button_press(x, y, btn);
93 if(!pointer_focus && !old_focus)
94 signal_button_press.emit(x, geom.h-1-y, btn, mod);
98 void Root::button_release_event(int x, int y, unsigned btn, unsigned mod)
102 Widget *old_focus = pointer_focus;
104 translate_coords(x, y);
105 button_release(x, y, btn);
107 if(!pointer_focus && !old_focus)
108 signal_button_release.emit(x, geom.h-1-y, btn, mod);
112 void Root::pointer_motion_event(int x, int y)
116 translate_coords(x, y);
117 pointer_motion(x, y);
123 tooltip_timeout = Time::now()+700*Time::msec;
125 else if(get_descendant_at(x, y)!=tooltip_target)
128 lbl_tooltip->set_visible(false);
133 signal_pointer_motion.emit(x, geom.h-1-y);
137 void Root::key_press_event(unsigned key, unsigned mod, wchar_t ch)
141 Widget *old_focus = input_focus;
143 key_press(Input::key_from_sys(key), mod, ch);
145 if(!input_focus && !old_focus)
146 signal_key_press.emit(key, mod, ch);
150 void Root::key_release_event(unsigned key, unsigned mod)
154 Widget *old_focus = input_focus;
156 key_release(Input::key_from_sys(key), mod);
158 if(!input_focus && !old_focus)
159 signal_key_release.emit(key, mod);
163 void Root::translate_coords(int &x, int &y)
165 x = x*geom.w/window.get_width();
166 y = geom.h-1-y*geom.h/window.get_height();