]> git.tdb.fi Git - libs/gltk.git/blob - source/root.cpp
Style update: add spaces around assignments
[libs/gltk.git] / source / root.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/input/keys.h>
9 #include <msp/time/units.h>
10 #include <msp/time/utils.h>
11 #include "label.h"
12 #include "style.h"
13 #include "root.h"
14
15 namespace Msp {
16 namespace GLtk {
17
18 Root::Root(const Resources &r, Graphics::Window &w):
19         Widget(r),
20         Panel(r),
21         window(w),
22         lbl_tooltip(0),
23         tooltip_target(0)
24 {
25         set_geometry(Geometry(0, 0, window.get_width(), window.get_height()));
26
27         update_style();
28
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));
34 }
35
36 void Root::tick()
37 {
38         if(tooltip_timeout && Time::now()>tooltip_timeout)
39         {
40                 std::string tip;
41                 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
42                 {
43                         tip = wdg->get_tooltip();
44                         tooltip_target = wdg;
45                 }
46                 else
47                 {
48                         tip = signal_tooltip.emit(pointer_x, pointer_y);
49                         tooltip_target = this;
50                 }
51
52                 if(!tip.empty())
53                 {
54                         if(!lbl_tooltip)
55                         {
56                                 lbl_tooltip = new Label(res);
57                                 add(*lbl_tooltip);
58                                 lbl_tooltip->set_style("tooltip");
59                         }
60
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)
67                         {
68                                 if(pointer_x>static_cast<int>(tip_geom.w+2))
69                                         x = pointer_x-2-tip_geom.w;
70                                 else
71                                         x = geom.w-tip_geom.w;
72                         }
73                         lbl_tooltip->set_position(x, y);
74                         raise(*lbl_tooltip);
75                         lbl_tooltip->set_visible(true);
76                 }
77
78                 tooltip_timeout = Time::TimeStamp();
79         }
80 }
81
82 void Root::button_press_event(int x, int y, unsigned btn, unsigned mod)
83 {
84         if(visible)
85         {
86                 Widget *old_focus = pointer_focus;
87
88                 translate_coords(x, y);
89                 button_press(x, y, btn);
90
91                 if(!pointer_focus && !old_focus)
92                         signal_button_press.emit(x, geom.h-1-y, btn, mod);
93         }
94 }
95
96 void Root::button_release_event(int x, int y, unsigned btn, unsigned mod)
97 {
98         if(visible)
99         {
100                 Widget *old_focus = pointer_focus;
101
102                 translate_coords(x, y);
103                 button_release(x, y, btn);
104
105                 if(!pointer_focus && !old_focus)
106                         signal_button_release.emit(x, geom.h-1-y, btn, mod);
107         }
108 }
109
110 void Root::pointer_motion_event(int x, int y)
111 {
112         if(visible)
113         {
114                 translate_coords(x, y);
115                 pointer_motion(x, y);
116
117                 if(!tooltip_target)
118                 {
119                         pointer_x = x;
120                         pointer_y = y;
121                         tooltip_timeout = Time::now()+700*Time::msec;
122                 }
123                 else if(get_descendant_at(x, y)!=tooltip_target)
124                 {
125                         if(lbl_tooltip)
126                                 lbl_tooltip->set_visible(false);
127                         tooltip_target = 0;
128                 }
129
130                 if(!pointer_focus)
131                         signal_pointer_motion.emit(x, geom.h-1-y);
132         }
133 }
134
135 void Root::key_press_event(unsigned key, unsigned mod, wchar_t ch)
136 {
137         if(visible)
138         {
139                 Widget *old_focus = input_focus;
140
141                 key_press(Input::key_from_sys(key), mod, ch);
142
143                 if(!input_focus && !old_focus)
144                         signal_key_press.emit(key, mod, ch);
145         }
146 }
147
148 void Root::key_release_event(unsigned key, unsigned mod)
149 {
150         if(visible)
151         {
152                 Widget *old_focus = input_focus;
153
154                 key_release(Input::key_from_sys(key), mod);
155
156                 if(!input_focus && !old_focus)
157                         signal_key_release.emit(key, mod);
158         }
159 }
160
161 void Root::translate_coords(int &x, int &y)
162 {
163         x = x*geom.w/window.get_width();
164         y = geom.h-1-y*geom.h/window.get_height();
165 }
166
167 } // namespace GLtk
168 } // namespace Msp