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):
25 set_geometry(Geometry(0, 0, window.get_width(), window.get_height()));
29 window.signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
30 window.signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
31 window.signal_pointer_motion.connect(sigc::mem_fun(this, &Root::pointer_motion_event));
32 window.signal_key_press.connect(sigc::mem_fun(this, &Root::key_press_event));
33 window.signal_key_release.connect(sigc::mem_fun(this, &Root::key_release_event));
38 if(tooltip_timeout && Time::now()>tooltip_timeout)
41 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
43 tip = wdg->get_tooltip();
48 tip = signal_tooltip.emit(pointer_x, pointer_y);
49 tooltip_target = this;
56 lbl_tooltip = new Label(res);
58 lbl_tooltip->set_style("tooltip");
61 lbl_tooltip->set_text(tip);
62 lbl_tooltip->autosize();
63 const Geometry &tip_geom = lbl_tooltip->get_geometry();
64 unsigned x = pointer_x+10;
65 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
66 if(x+tip_geom.w>geom.w)
68 if(pointer_x>static_cast<int>(tip_geom.w+2))
69 x = pointer_x-2-tip_geom.w;
71 x = geom.w-tip_geom.w;
73 lbl_tooltip->set_position(x, y);
75 lbl_tooltip->set_visible(true);
78 tooltip_timeout = Time::TimeStamp();
82 void Root::button_press_event(int x, int y, unsigned btn, unsigned mod)
86 Widget *old_focus = pointer_focus;
88 translate_coords(x, y);
89 button_press(x, y, btn);
91 if(!pointer_focus && !old_focus)
92 signal_button_press.emit(x, geom.h-1-y, btn, mod);
96 void Root::button_release_event(int x, int y, unsigned btn, unsigned mod)
100 Widget *old_focus = pointer_focus;
102 translate_coords(x, y);
103 button_release(x, y, btn);
105 if(!pointer_focus && !old_focus)
106 signal_button_release.emit(x, geom.h-1-y, btn, mod);
110 void Root::pointer_motion_event(int x, int y)
114 translate_coords(x, y);
115 pointer_motion(x, y);
121 tooltip_timeout = Time::now()+700*Time::msec;
123 else if(get_descendant_at(x, y)!=tooltip_target)
126 lbl_tooltip->set_visible(false);
131 signal_pointer_motion.emit(x, geom.h-1-y);
135 void Root::key_press_event(unsigned key, unsigned mod, wchar_t ch)
139 Widget *old_focus = input_focus;
141 key_press(Input::key_from_sys(key), mod, ch);
143 if(!input_focus && !old_focus)
144 signal_key_press.emit(key, mod, ch);
148 void Root::key_release_event(unsigned key, unsigned mod)
152 Widget *old_focus = input_focus;
154 key_release(Input::key_from_sys(key), mod);
156 if(!input_focus && !old_focus)
157 signal_key_release.emit(key, mod);
161 void Root::translate_coords(int &x, int &y)
163 x = x*geom.w/window.get_width();
164 y = geom.h-1-y*geom.h/window.get_height();