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();
35 void stop_tooltip_timeout();
40 void default_tooltip()
42 // give the tooltip some reasonable default values
43 memset(&g_tooltip
, 0, sizeof(Tooltip
));
45 g_tooltip
.font_color
.color
[0] = 1;
46 g_tooltip
.font_color
.color
[1] = 1;
47 g_tooltip
.font_color
.color
[2] = 1;
48 g_tooltip
.font_color
.alpha
= 1;
51 void cleanup_tooltip()
53 stop_tooltip_timeout();
56 if (g_tooltip
.window
) XDestroyWindow(server
.dsp
, g_tooltip
.window
);
57 if (g_tooltip
.font_desc
) pango_font_description_free(g_tooltip
.font_desc
);
63 if (!g_tooltip
.font_desc
)
64 g_tooltip
.font_desc
= pango_font_description_from_string("sans 10");
66 XSetWindowAttributes attr
;
67 attr
.override_redirect
= True
;
68 attr
.event_mask
= StructureNotifyMask
;
69 attr
.colormap
= server
.colormap
;
70 attr
.background_pixel
= 0;
71 attr
.border_pixel
= 0;
72 unsigned long mask
= CWEventMask
|CWColormap
|CWBorderPixel
|CWBackPixel
|CWOverrideRedirect
;
73 if (g_tooltip
.window
) XDestroyWindow(server
.dsp
, g_tooltip
.window
);
74 g_tooltip
.window
= XCreateWindow(server
.dsp
, server
.root_win
, 0, 0, 100, 20, 0, server
.depth
, InputOutput
, server
.visual
, mask
, &attr
);
78 void tooltip_trigger_show(Area
* area
, Panel
* p
, XEvent
*e
)
80 // Position the tooltip in the center of the area
81 x
= area
->posx
+ area
->width
/ 2 + e
->xmotion
.x_root
- e
->xmotion
.x
;
82 y
= area
->posy
+ area
->height
/ 2 + e
->xmotion
.y_root
- e
->xmotion
.y
;
83 if (!panel_horizontal
)
86 if (g_tooltip
.mapped
&& g_tooltip
.area
!= area
) {
87 tooltip_copy_text(area
);
89 stop_tooltip_timeout();
91 else if (!g_tooltip
.mapped
) {
97 void tooltip_show(void* arg
)
101 XTranslateCoordinates( server
.dsp
, server
.root_win
, g_tooltip
.panel
->main_win
, x
, y
, &mx
, &my
, &w
);
102 Area
* area
= click_area(g_tooltip
.panel
, mx
, my
);
103 stop_tooltip_timeout();
104 if (!g_tooltip
.mapped
&& area
->_get_tooltip_text
) {
105 tooltip_copy_text(area
);
106 g_tooltip
.mapped
= True
;
107 XMapWindow(server
.dsp
, g_tooltip
.window
);
114 void tooltip_update_geometry()
119 cs
= cairo_xlib_surface_create(server
.dsp
, g_tooltip
.window
, server
.visual
, width
, height
);
120 c
= cairo_create(cs
);
121 layout
= pango_cairo_create_layout(c
);
122 pango_layout_set_font_description(layout
, g_tooltip
.font_desc
);
123 pango_layout_set_text(layout
, g_tooltip
.tooltip_text
, -1);
124 PangoRectangle r1
, r2
;
125 pango_layout_get_pixel_extents(layout
, &r1
, &r2
);
126 width
= 2*g_tooltip
.bg
->border
.width
+ 2*g_tooltip
.paddingx
+ r2
.width
;
127 height
= 2*g_tooltip
.bg
->border
.width
+ 2*g_tooltip
.paddingy
+ r2
.height
;
129 Panel
* panel
= g_tooltip
.panel
;
130 if (panel_horizontal
&& panel_position
& BOTTOM
)
131 y
= panel
->posy
-height
;
132 else if (panel_horizontal
&& panel_position
& TOP
)
133 y
= panel
->posy
+ panel
->area
.height
;
134 else if (panel_position
& LEFT
)
135 x
= panel
->posx
+ panel
->area
.width
;
137 x
= panel
->posx
- width
;
139 g_object_unref(layout
);
141 cairo_surface_destroy(cs
);
145 void tooltip_adjust_geometry()
147 // adjust coordinates and size to not go offscreen
148 // it seems quite impossible that the height needs to be adjusted, but we do it anyway.
150 int min_x
, min_y
, max_width
, max_height
;
151 Panel
* panel
= g_tooltip
.panel
;
152 int screen_width
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
;
153 int screen_height
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
;
154 if ( x
+width
<= screen_width
&& y
+height
<= screen_height
&& x
>=server
.monitor
[panel
->monitor
].x
&& y
>=server
.monitor
[panel
->monitor
].y
)
155 return; // no adjustment needed
157 if (panel_horizontal
) {
159 max_width
=server
.monitor
[panel
->monitor
].width
;
160 max_height
=server
.monitor
[panel
->monitor
].height
-panel
->area
.height
;
161 if (panel_position
& BOTTOM
)
164 min_y
=panel
->area
.height
;
167 max_width
=server
.monitor
[panel
->monitor
].width
-panel
->area
.width
;
169 max_height
=server
.monitor
[panel
->monitor
].height
;
170 if (panel_position
& LEFT
)
171 min_x
=panel
->area
.width
;
176 if (x
+width
> server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
)
177 x
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
- width
;
178 if ( y
+height
> server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
)
179 y
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
- height
;
187 if (height
>max_height
)
191 void tooltip_update()
193 if (!g_tooltip
.tooltip_text
) {
198 tooltip_update_geometry();
199 tooltip_adjust_geometry();
200 XMoveResizeWindow(server
.dsp
, g_tooltip
.window
, x
, y
, width
, height
);
202 // Stuff for drawing the tooltip
206 cs
= cairo_xlib_surface_create(server
.dsp
, g_tooltip
.window
, server
.visual
, width
, height
);
207 c
= cairo_create(cs
);
208 Color bc
= g_tooltip
.bg
->back
;
209 Border b
= g_tooltip
.bg
->border
;
210 if (server
.real_transparency
) {
211 clear_pixmap(g_tooltip
.window
, 0, 0, width
, height
);
212 draw_rect(c
, b
.width
, b
.width
, width
-2*b
.width
, height
-2*b
.width
, b
.rounded
-b
.width
/1.571);
213 cairo_set_source_rgba(c
, bc
.color
[0], bc
.color
[1], bc
.color
[2], bc
.alpha
);
216 cairo_rectangle(c
, 0., 0, width
, height
);
217 cairo_set_source_rgb(c
, bc
.color
[0], bc
.color
[1], bc
.color
[2]);
220 cairo_set_line_width(c
, b
.width
);
221 if (server
.real_transparency
)
222 draw_rect(c
, b
.width
/2.0, b
.width
/2.0, width
- b
.width
, height
- b
.width
, b
.rounded
);
224 cairo_rectangle(c
, b
.width
/2.0, b
.width
/2.0, width
-b
.width
, height
-b
.width
);
225 cairo_set_source_rgba(c
, b
.color
[0], b
.color
[1], b
.color
[2], b
.alpha
);
228 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
.bg
->border
.width
+g_tooltip
.paddingx
, -r1
.y
/2+g_tooltip
.bg
->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
256 stop_tooltip_timeout();
261 void tooltip_hide(void* arg
)
263 stop_tooltip_timeout();
264 if (g_tooltip
.mapped
) {
265 g_tooltip
.mapped
= False
;
266 XUnmapWindow(server
.dsp
, g_tooltip
.window
);
272 void start_show_timeout()
274 if (g_tooltip
.timeout
)
275 change_timeout(g_tooltip
.timeout
, g_tooltip
.show_timeout_msec
, 0, tooltip_show
, 0);
277 g_tooltip
.timeout
= add_timeout(g_tooltip
.show_timeout_msec
, 0, tooltip_show
, 0);
281 void start_hide_timeout()
283 if (g_tooltip
.timeout
)
284 change_timeout(g_tooltip
.timeout
, g_tooltip
.hide_timeout_msec
, 0, tooltip_hide
, 0);
286 g_tooltip
.timeout
= add_timeout(g_tooltip
.hide_timeout_msec
, 0, tooltip_hide
, 0);
290 void stop_tooltip_timeout()
292 if (g_tooltip
.timeout
) {
293 stop_timeout(g_tooltip
.timeout
);
294 g_tooltip
.timeout
= 0;
299 void tooltip_copy_text(Area
* area
)
301 free(g_tooltip
.tooltip_text
);
302 if (area
&& area
->_get_tooltip_text
)
303 g_tooltip
.tooltip_text
= strdup(area
->_get_tooltip_text(area
));
305 g_tooltip
.tooltip_text
= 0;
306 g_tooltip
.area
= area
;