1 /**************************************************************************
3 * Copyright (C) 2009 Andreas.Fink (Andreas.Fink85@gmail.com)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 **************************************************************************/
23 #include <cairo-xlib.h>
30 static int x
, y
, width
, height
;
32 // the next functions are helper functions for tooltip handling
33 void start_show_timeout();
34 void start_hide_timeout();
36 void tooltip_copy_text(Area
* area
);
38 // give the tooltip some reasonable default values
44 .show_timeout
= { 0, 0 },
45 .hide_timeout
= { 0, 0 },
50 .font_color
= { .color
={1, 1, 1}, .alpha
=1 },
51 .background_color
= { .color
={0.5, 0.4, 0.5}, .alpha
=1 },
52 .border
= { .color
={0, 0, 0}, .alpha
=1, .width
=1, .rounded
=0 },
60 if (!g_tooltip
.font_desc
)
61 g_tooltip
.font_desc
= pango_font_description_from_string("sans 10");
63 if (g_tooltip
.show_timer_id
== 0)
64 g_tooltip
.show_timer_id
= install_timer(0, 0, 0, 0, tooltip_show
);
65 if (g_tooltip
.hide_timer_id
== 0)
66 g_tooltip
.hide_timer_id
= install_timer(0, 0, 0, 0, tooltip_hide
);
68 XSetWindowAttributes attr
;
69 attr
.override_redirect
= True
;
70 attr
.event_mask
= StructureNotifyMask
;
71 if (g_tooltip
.window
) XDestroyWindow(server
.dsp
, g_tooltip
.window
);
72 g_tooltip
.window
= XCreateWindow(server
.dsp
, server
.root_win
, 0, 0, 100, 20, 0, server
.depth
, InputOutput
, CopyFromParent
, CWOverrideRedirect
|CWEventMask
, &attr
);
76 void cleanup_tooltip()
80 g_tooltip
.enabled
= False
;
82 if (g_tooltip
.window
) {
83 XDestroyWindow(server
.dsp
, g_tooltip
.window
);
86 if (g_tooltip
.font_desc
) {
87 pango_font_description_free(g_tooltip
.font_desc
);
88 g_tooltip
.font_desc
= 0;
93 void tooltip_trigger_show(Area
* area
, Panel
* p
, int x_root
, int y_root
)
98 if (g_tooltip
.mapped
&& g_tooltip
.area
!= area
) {
99 tooltip_copy_text(area
);
103 else if (!g_tooltip
.mapped
) {
104 start_show_timeout();
105 //printf("!g_tooltip.mapped %d %d\n", x, y);
112 Area
* area
= click_area(g_tooltip
.panel
, x
, y
);
114 if (!g_tooltip
.mapped
&& area
->_get_tooltip_text
) {
115 tooltip_copy_text(area
);
116 g_tooltip
.mapped
= True
;
117 XMapWindow(server
.dsp
, g_tooltip
.window
);
123 void tooltip_update_geometry()
128 cs
= cairo_xlib_surface_create(server
.dsp
, g_tooltip
.window
, server
.visual
, width
, height
);
129 c
= cairo_create(cs
);
130 layout
= pango_cairo_create_layout(c
);
131 pango_layout_set_font_description(layout
, g_tooltip
.font_desc
);
132 pango_layout_set_text(layout
, g_tooltip
.tooltip_text
, -1);
133 PangoRectangle r1
, r2
;
134 pango_layout_get_pixel_extents(layout
, &r1
, &r2
);
135 width
= 2*g_tooltip
.border
.width
+ 2*g_tooltip
.paddingx
+ r2
.width
;
136 height
= 2*g_tooltip
.border
.width
+ 2*g_tooltip
.paddingy
+ r2
.height
;
138 Panel
* panel
= g_tooltip
.panel
;
139 if (panel_horizontal
&& panel_position
& BOTTOM
)
140 y
= panel
->posy
-height
;
141 else if (panel_horizontal
&& panel_position
& TOP
)
142 y
= panel
->posy
+ panel
->area
.height
;
143 else if (panel_position
& LEFT
)
144 x
= panel
->posx
+ panel
->area
.width
;
146 x
= panel
->posx
- width
;
148 g_object_unref(layout
);
150 cairo_surface_destroy(cs
);
154 void tooltip_adjust_geometry()
156 // adjust coordinates and size to not go offscreen
157 // it seems quite impossible that the height needs to be adjusted, but we do it anyway.
159 int min_x
, min_y
, max_width
, max_height
;
160 Panel
* panel
= g_tooltip
.panel
;
161 int screen_width
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
;
162 int screen_height
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
;
163 if ( x
+width
<= screen_width
&& y
+height
<= screen_height
&& x
>=0 && y
>=0)
164 return; // no adjustment needed
166 if (panel_horizontal
) {
168 max_width
=screen_width
;
169 max_height
=screen_height
-panel
->area
.height
;
170 if (panel_position
& BOTTOM
)
173 min_y
=panel
->area
.height
;
176 max_width
=screen_width
-panel
->area
.width
;
178 max_height
=screen_height
;
179 if (panel_position
& LEFT
)
180 min_x
=panel
->area
.width
;
185 if (x
+width
> server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
)
186 x
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
- width
;
187 if ( y
+height
> server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
)
188 y
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
- height
;
196 if (height
>max_height
)
200 void tooltip_update()
202 if (!g_tooltip
.tooltip_text
) {
207 // printf("tooltip_update\n");
208 tooltip_update_geometry();
209 tooltip_adjust_geometry();
210 XMoveResizeWindow(server
.dsp
, g_tooltip
.window
, x
, y
, width
, height
);
212 // Stuff for drawing the tooltip
216 cs
= cairo_xlib_surface_create(server
.dsp
, g_tooltip
.window
, server
.visual
, width
, height
);
217 c
= cairo_create(cs
);
218 Color bc
= g_tooltip
.background_color
;
219 cairo_rectangle(c
, 0, 0, width
, height
);
220 cairo_set_source_rgb(c
, bc
.color
[0], bc
.color
[1], bc
.color
[2]);
222 Border b
= g_tooltip
.border
;
223 cairo_set_source_rgba(c
, b
.color
[0], b
.color
[1], b
.color
[2], b
.alpha
);
224 cairo_set_line_width(c
, b
.width
);
225 cairo_rectangle(c
, b
.width
/2.0, b
.width
/2.0, width
-b
.width
, height
-b
.width
);
228 config_color fc
= g_tooltip
.font_color
;
229 cairo_set_source_rgba(c
, fc
.color
[0], fc
.color
[1], fc
.color
[2], fc
.alpha
);
230 layout
= pango_cairo_create_layout(c
);
231 pango_layout_set_font_description(layout
, g_tooltip
.font_desc
);
232 pango_layout_set_text(layout
, g_tooltip
.tooltip_text
, -1);
233 PangoRectangle r1
, r2
;
234 pango_layout_get_pixel_extents(layout
, &r1
, &r2
);
235 pango_layout_set_width(layout
, width
*PANGO_SCALE
);
236 pango_layout_set_height(layout
, height
*PANGO_SCALE
);
237 pango_layout_set_ellipsize(layout
, PANGO_ELLIPSIZE_END
);
238 // I do not know why this is the right way, but with the below cairo_move_to it seems to be centered (horiz. and vert.)
239 cairo_move_to(c
, -r1
.x
/2+g_tooltip
.border
.width
+g_tooltip
.paddingx
, -r1
.y
/2+g_tooltip
.border
.width
+g_tooltip
.paddingy
);
240 pango_cairo_show_layout (c
, layout
);
242 g_object_unref (layout
);
244 cairo_surface_destroy (cs
);
248 void tooltip_trigger_hide(Tooltip
* tooltip
)
250 if (g_tooltip
.mapped
) {
251 tooltip_copy_text(0);
252 start_hide_timeout();
255 // tooltip not visible yet, but maybe a timeout is still pending
264 if (g_tooltip
.mapped
) {
265 g_tooltip
.mapped
= False
;
266 XUnmapWindow(server
.dsp
, g_tooltip
.window
);
272 void start_show_timeout()
274 reset_timer(g_tooltip
.hide_timer_id
, 0, 0, 0, 0);
275 struct timespec t
= g_tooltip
.show_timeout
;
276 if (t
.tv_sec
== 0 && t
.tv_nsec
== 0) {
280 reset_timer(g_tooltip
.show_timer_id
, t
.tv_sec
, t
.tv_nsec
, 0, 0);
285 void start_hide_timeout()
287 reset_timer(g_tooltip
.show_timer_id
, 0, 0, 0, 0);
288 struct timespec t
= g_tooltip
.hide_timeout
;
289 if (t
.tv_sec
== 0 && t
.tv_nsec
== 0)
292 reset_timer(g_tooltip
.hide_timer_id
, t
.tv_sec
, t
.tv_nsec
, 0, 0);
297 reset_timer(g_tooltip
.show_timer_id
, 0, 0, 0, 0);
298 reset_timer(g_tooltip
.hide_timer_id
, 0, 0, 0, 0);
302 void tooltip_copy_text(Area
* area
)
304 free(g_tooltip
.tooltip_text
);
305 if (area
&& area
->_get_tooltip_text
)
306 g_tooltip
.tooltip_text
= strdup(area
->_get_tooltip_text(area
));
308 g_tooltip
.tooltip_text
= 0;
309 g_tooltip
.area
= area
;