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