3 This file is part of libmspgltk
4 Copyright © 2007-2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/input/keys.h>
9 #include <msp/time/units.h>
10 #include <msp/time/utils.h>
18 Root::Root(const Resources &r, Graphics::Window &w):
24 set_geometry(Geometry(0, 0, window.get_width(), window.get_height()));
28 window.signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
29 window.signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
30 window.signal_pointer_motion.connect(sigc::mem_fun(this, &Root::pointer_motion_event));
31 window.signal_key_press.connect(sigc::mem_fun(this, &Root::key_press_event));
32 window.signal_key_release.connect(sigc::mem_fun(this, &Root::key_release_event));
37 if(tooltip_timeout && Time::now()>tooltip_timeout)
40 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
42 tip = wdg->get_tooltip();
47 tip = signal_tooltip.emit(pointer_x, pointer_y);
48 tooltip_target = this;
55 lbl_tooltip = new Label;
57 lbl_tooltip->set_style("tooltip");
60 lbl_tooltip->set_text(tip);
61 lbl_tooltip->autosize();
62 const Geometry &tip_geom = lbl_tooltip->get_geometry();
63 unsigned x = pointer_x+10;
64 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
65 if(x+tip_geom.w>geom.w)
67 if(pointer_x>static_cast<int>(tip_geom.w+2))
68 x = pointer_x-2-tip_geom.w;
70 x = geom.w-tip_geom.w;
72 lbl_tooltip->set_position(x, y);
74 lbl_tooltip->set_visible(true);
77 tooltip_timeout = Time::TimeStamp();
81 void Root::button_press_event(int x, int y, unsigned btn, unsigned mod)
85 Widget *old_focus = pointer_focus;
87 translate_coords(x, y);
88 button_press(x, y, btn);
90 if(!pointer_focus && !old_focus)
91 signal_button_press.emit(x, geom.h-1-y, btn, mod);
95 void Root::button_release_event(int x, int y, unsigned btn, unsigned mod)
99 Widget *old_focus = pointer_focus;
101 translate_coords(x, y);
102 button_release(x, y, btn);
104 if(!pointer_focus && !old_focus)
105 signal_button_release.emit(x, geom.h-1-y, btn, mod);
109 void Root::pointer_motion_event(int x, int y)
113 translate_coords(x, y);
114 pointer_motion(x, y);
120 tooltip_timeout = Time::now()+700*Time::msec;
122 else if(get_descendant_at(x, y)!=tooltip_target)
125 lbl_tooltip->set_visible(false);
130 signal_pointer_motion.emit(x, geom.h-1-y);
134 void Root::key_press_event(unsigned key, unsigned mod, wchar_t ch)
138 Widget *old_focus = input_focus;
140 key_press(Input::key_from_sys(key), mod, ch);
142 if(!input_focus && !old_focus)
143 signal_key_press.emit(key, mod, ch);
147 void Root::key_release_event(unsigned key, unsigned mod)
151 Widget *old_focus = input_focus;
153 key_release(Input::key_from_sys(key), mod);
155 if(!input_focus && !old_focus)
156 signal_key_release.emit(key, mod);
160 void Root::translate_coords(int &x, int &y)
162 x = x*geom.w/window.get_width();
163 y = geom.h-1-y*geom.h/window.get_height();