]> git.tdb.fi Git - libs/gltk.git/blob - source/root.cpp
Add Text class with multiline support
[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                 if(Widget *wdg=get_descendant_at(pointer_x, pointer_y))
41                 {
42                         const std::string &tip=wdg->get_tooltip();
43                         if(!tip.empty())
44                         {
45                                 if(!lbl_tooltip)
46                                 {
47                                         lbl_tooltip=new Label(res);
48                                         add(*lbl_tooltip);
49                                         lbl_tooltip->set_style("tooltip");
50                                 }
51
52                                 lbl_tooltip->set_text(tip);
53                                 lbl_tooltip->autosize();
54                                 const Geometry &tip_geom=lbl_tooltip->get_geometry();
55                                 unsigned x=pointer_x+10;
56                                 unsigned y=pointer_y-10-lbl_tooltip->get_geometry().h;
57                                 if(x+tip_geom.w>geom.w)
58                                 {
59                                         if(pointer_x>static_cast<int>(tip_geom.w+2))
60                                                 x=pointer_x-2-tip_geom.w;
61                                         else
62                                                 x=geom.w-tip_geom.w;
63                                 }
64                                 lbl_tooltip->set_position(x, y);
65                                 raise(*lbl_tooltip);
66                                 lbl_tooltip->set_visible(true);
67                                 tooltip_timeout=Time::TimeStamp();
68                                 tooltip_target=wdg;
69                         }
70                 }
71         }
72 }
73
74 void Root::button_press_event(int x, int y, unsigned btn, unsigned)
75 {
76         if(visible)
77         {
78                 translate_coords(x, y);
79                 button_press(x, y, btn);
80         }
81 }
82
83 void Root::button_release_event(int x, int y, unsigned btn, unsigned)
84 {
85         if(visible)
86         {
87                 translate_coords(x, y);
88                 button_release(x, y, btn);
89         }
90 }
91
92 void Root::pointer_motion_event(int x, int y)
93 {
94         if(visible)
95         {
96                 translate_coords(x, y);
97                 pointer_motion(x, y);
98
99                 if(!tooltip_target)
100                 {
101                         if(pointer_focus)
102                         {
103                                 pointer_x=x;
104                                 pointer_y=y;
105                                 tooltip_timeout=Time::now()+700*Time::msec;
106                         }
107                         else
108                                 tooltip_timeout=Time::TimeStamp();
109                 }
110                 else if(get_descendant_at(x, y)!=tooltip_target)
111                 {
112                         lbl_tooltip->set_visible(false);
113                         tooltip_target=0;
114                 }
115         }
116 }
117
118 void Root::key_press_event(unsigned key, unsigned mod, wchar_t ch)
119 {
120         if(visible)
121                 key_press(Input::key_from_sys(key), mod, ch);
122 }
123
124 void Root::key_release_event(unsigned key, unsigned mod)
125 {
126         if(visible)
127                 key_release(Input::key_from_sys(key), mod);
128 }
129
130 void Root::translate_coords(int &x, int &y)
131 {
132         x=x*geom.w/window.get_width();
133         y=geom.h-1-y*geom.h/window.get_height();
134 }
135
136 } // namespace GLtk
137 } // namespace Msp