]> git.tdb.fi Git - libs/gltk.git/blob - source/root.cpp
Change Style so it doesn't need a special loading function
[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         GL::Renderer renderer(0);
85         Widget::render(renderer);
86 }
87
88 void Root::button_press_event(unsigned btn)
89 {
90         if(visible)
91         {
92                 int x, y;
93                 get_pointer(x, y);
94                 button_press(x, y, btn);
95         }
96 }
97
98 void Root::button_release_event(unsigned btn)
99 {
100         if(visible)
101         {
102                 int x, y;
103                 get_pointer(x, y);
104                 button_release(x, y, btn);
105         }
106 }
107
108 void Root::axis_motion_event(unsigned, float, float)
109 {
110         if(visible)
111         {
112                 int x, y;
113                 get_pointer(x, y);
114                 pointer_motion(x, y);
115
116                 if(!tooltip_target)
117                 {
118                         pointer_x = x;
119                         pointer_y = y;
120                         tooltip_timeout = Time::now()+700*Time::msec;
121                 }
122                 else if(get_descendant_at(x, y)!=tooltip_target)
123                 {
124                         if(lbl_tooltip)
125                                 lbl_tooltip->set_visible(false);
126                         tooltip_target = 0;
127                 }
128         }
129 }
130
131 void Root::key_press_event(unsigned key)
132 {
133         // XXX Modifiers
134         if(visible)
135                 key_press(key, 0);
136 }
137
138 void Root::key_release_event(unsigned key)
139 {
140         if(visible)
141                 key_release(key, 0);
142 }
143
144 void Root::character_event(StringCodec::unichar ch)
145 {
146         if(visible)
147                 character(ch);
148 }
149
150 void Root::get_pointer(int &x, int &y)
151 {
152         x = (mouse.get_axis_value(0)*0.5+0.5)*geom.w;
153         y = (mouse.get_axis_value(1)*0.5+0.5)*geom.h;
154 }
155
156 } // namespace GLtk
157 } // namespace Msp