]> git.tdb.fi Git - libs/gltk.git/blob - source/root.cpp
Adjust event handling to match changes in mspgui
[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 &w):
13         resources(r),
14         window(w),
15         keyboard(window),
16         mouse(window),
17         lbl_tooltip(0),
18         tooltip_target(0)
19 {
20         set_geometry(Geometry(0, 0, window.get_width(), window.get_height()));
21
22         update_style();
23
24         mouse.signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
25         mouse.signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
26         mouse.signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
27         keyboard.signal_button_press.connect(sigc::mem_fun(this, &Root::key_press_event));
28         keyboard.signal_button_release.connect(sigc::mem_fun(this, &Root::key_release_event));
29         keyboard.signal_character.connect(sigc::mem_fun(this, &Root::character_event));
30 }
31
32 void Root::tick()
33 {
34         if(tooltip_timeout && Time::now()>tooltip_timeout)
35         {
36                 std::string tip;
37                 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
38                 {
39                         tip = wdg->get_tooltip();
40                         tooltip_target = wdg;
41                 }
42                 else
43                 {
44                         tip = signal_tooltip.emit(pointer_x, pointer_y);
45                         tooltip_target = this;
46                 }
47
48                 if(!tip.empty())
49                 {
50                         if(!lbl_tooltip)
51                         {
52                                 lbl_tooltip = new Label;
53                                 add(*lbl_tooltip);
54                                 lbl_tooltip->set_style("tooltip");
55                         }
56
57                         lbl_tooltip->set_text(tip);
58                         lbl_tooltip->autosize();
59                         const Geometry &tip_geom = lbl_tooltip->get_geometry();
60                         unsigned x = pointer_x+10;
61                         unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
62                         if(x+tip_geom.w>geom.w)
63                         {
64                                 if(pointer_x>static_cast<int>(tip_geom.w+2))
65                                         x = pointer_x-2-tip_geom.w;
66                                 else
67                                         x = geom.w-tip_geom.w;
68                         }
69                         lbl_tooltip->set_position(x, y);
70                         raise(*lbl_tooltip);
71                         lbl_tooltip->set_visible(true);
72                 }
73
74                 tooltip_timeout = Time::TimeStamp();
75         }
76 }
77
78 void Root::render() const
79 {
80         GL::MatrixStack::projection() = GL::Matrix::ortho_bottomleft(geom.w, geom.h);
81         GL::MatrixStack::modelview() = GL::Matrix();
82         GL::Bind bind_blend(GL::Blend::alpha());
83
84         Widget::render();
85 }
86
87 void Root::button_press_event(unsigned btn)
88 {
89         if(visible)
90         {
91                 int x, y;
92                 get_pointer(x, y);
93                 button_press(x, y, btn);
94         }
95 }
96
97 void Root::button_release_event(unsigned btn)
98 {
99         if(visible)
100         {
101                 int x, y;
102                 get_pointer(x, y);
103                 button_release(x, y, btn);
104         }
105 }
106
107 void Root::axis_motion_event(unsigned, float, float)
108 {
109         if(visible)
110         {
111                 int x, y;
112                 get_pointer(x, y);
113                 pointer_motion(x, y);
114
115                 if(!tooltip_target)
116                 {
117                         pointer_x = x;
118                         pointer_y = y;
119                         tooltip_timeout = Time::now()+700*Time::msec;
120                 }
121                 else if(get_descendant_at(x, y)!=tooltip_target)
122                 {
123                         if(lbl_tooltip)
124                                 lbl_tooltip->set_visible(false);
125                         tooltip_target = 0;
126                 }
127         }
128 }
129
130 void Root::key_press_event(unsigned key)
131 {
132         // XXX Modifiers
133         if(visible)
134                 key_press(key, 0);
135 }
136
137 void Root::key_release_event(unsigned key)
138 {
139         if(visible)
140                 key_release(key, 0);
141 }
142
143 void Root::character_event(StringCodec::unichar ch)
144 {
145         if(visible)
146                 character(ch);
147 }
148
149 void Root::get_pointer(int &x, int &y)
150 {
151         x = (mouse.get_axis_value(0)*0.5+0.5)*geom.w;
152         y = (mouse.get_axis_value(1)*0.5+0.5)*geom.h;
153 }
154
155 } // namespace GLtk
156 } // namespace Msp