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
, int x_root
, int y_root
)
83 if (g_tooltip
.mapped
&& g_tooltip
.area
!= area
) {
84 tooltip_copy_text(area
);
86 stop_tooltip_timeout();
88 else if (!g_tooltip
.mapped
) {
94 void tooltip_show(void* arg
)
98 XTranslateCoordinates( server
.dsp
, server
.root_win
, g_tooltip
.panel
->main_win
, x
, y
, &mx
, &my
, &w
);
99 Area
* area
= click_area(g_tooltip
.panel
, mx
, my
);
100 stop_tooltip_timeout();
101 if (!g_tooltip
.mapped
&& area
->_get_tooltip_text
) {
102 tooltip_copy_text(area
);
103 g_tooltip
.mapped
= True
;
104 XMapWindow(server
.dsp
, g_tooltip
.window
);
111 void tooltip_update_geometry()
116 cs
= cairo_xlib_surface_create(server
.dsp
, g_tooltip
.window
, server
.visual
, width
, height
);
117 c
= cairo_create(cs
);
118 layout
= pango_cairo_create_layout(c
);
119 pango_layout_set_font_description(layout
, g_tooltip
.font_desc
);
120 pango_layout_set_text(layout
, g_tooltip
.tooltip_text
, -1);
121 PangoRectangle r1
, r2
;
122 pango_layout_get_pixel_extents(layout
, &r1
, &r2
);
123 width
= 2*g_tooltip
.bg
->border
.width
+ 2*g_tooltip
.paddingx
+ r2
.width
;
124 height
= 2*g_tooltip
.bg
->border
.width
+ 2*g_tooltip
.paddingy
+ r2
.height
;
126 Panel
* panel
= g_tooltip
.panel
;
127 if (panel_horizontal
&& panel_position
& BOTTOM
)
128 y
= panel
->posy
-height
;
129 else if (panel_horizontal
&& panel_position
& TOP
)
130 y
= panel
->posy
+ panel
->area
.height
;
131 else if (panel_position
& LEFT
)
132 x
= panel
->posx
+ panel
->area
.width
;
134 x
= panel
->posx
- width
;
136 g_object_unref(layout
);
138 cairo_surface_destroy(cs
);
142 void tooltip_adjust_geometry()
144 // adjust coordinates and size to not go offscreen
145 // it seems quite impossible that the height needs to be adjusted, but we do it anyway.
147 int min_x
, min_y
, max_width
, max_height
;
148 Panel
* panel
= g_tooltip
.panel
;
149 int screen_width
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
;
150 int screen_height
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
;
151 if ( x
+width
<= screen_width
&& y
+height
<= screen_height
&& x
>=0 && y
>=0)
152 return; // no adjustment needed
154 if (panel_horizontal
) {
156 max_width
=screen_width
;
157 max_height
=screen_height
-panel
->area
.height
;
158 if (panel_position
& BOTTOM
)
161 min_y
=panel
->area
.height
;
164 max_width
=screen_width
-panel
->area
.width
;
166 max_height
=screen_height
;
167 if (panel_position
& LEFT
)
168 min_x
=panel
->area
.width
;
173 if (x
+width
> server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
)
174 x
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
- width
;
175 if ( y
+height
> server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
)
176 y
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
- height
;
184 if (height
>max_height
)
188 void tooltip_update()
190 if (!g_tooltip
.tooltip_text
) {
195 tooltip_update_geometry();
196 tooltip_adjust_geometry();
197 XMoveResizeWindow(server
.dsp
, g_tooltip
.window
, x
, y
, width
, height
);
199 // Stuff for drawing the tooltip
203 cs
= cairo_xlib_surface_create(server
.dsp
, g_tooltip
.window
, server
.visual
, width
, height
);
204 c
= cairo_create(cs
);
205 Color bc
= g_tooltip
.bg
->back
;
206 Border b
= g_tooltip
.bg
->border
;
207 if (server
.real_transparency
) {
208 clear_pixmap(g_tooltip
.window
, 0, 0, width
, height
);
209 draw_rect(c
, b
.width
, b
.width
, width
-2*b
.width
, height
-2*b
.width
, b
.rounded
-b
.width
/1.571);
210 cairo_set_source_rgba(c
, bc
.color
[0], bc
.color
[1], bc
.color
[2], bc
.alpha
);
213 cairo_rectangle(c
, 0., 0, width
, height
);
214 cairo_set_source_rgb(c
, bc
.color
[0], bc
.color
[1], bc
.color
[2]);
217 cairo_set_line_width(c
, b
.width
);
218 if (server
.real_transparency
)
219 draw_rect(c
, b
.width
/2.0, b
.width
/2.0, width
- b
.width
, height
- b
.width
, b
.rounded
);
221 cairo_rectangle(c
, b
.width
/2.0, b
.width
/2.0, width
-b
.width
, height
-b
.width
);
222 cairo_set_source_rgba(c
, b
.color
[0], b
.color
[1], b
.color
[2], b
.alpha
);
225 Color fc
= g_tooltip
.font_color
;
226 cairo_set_source_rgba(c
, fc
.color
[0], fc
.color
[1], fc
.color
[2], fc
.alpha
);
227 layout
= pango_cairo_create_layout(c
);
228 pango_layout_set_font_description(layout
, g_tooltip
.font_desc
);
229 pango_layout_set_text(layout
, g_tooltip
.tooltip_text
, -1);
230 PangoRectangle r1
, r2
;
231 pango_layout_get_pixel_extents(layout
, &r1
, &r2
);
232 pango_layout_set_width(layout
, width
*PANGO_SCALE
);
233 pango_layout_set_height(layout
, height
*PANGO_SCALE
);
234 pango_layout_set_ellipsize(layout
, PANGO_ELLIPSIZE_END
);
235 // 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.)
236 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
);
237 pango_cairo_show_layout (c
, layout
);
239 g_object_unref (layout
);
241 cairo_surface_destroy (cs
);
245 void tooltip_trigger_hide(Tooltip
* tooltip
)
247 if (g_tooltip
.mapped
) {
248 tooltip_copy_text(0);
249 start_hide_timeout();
252 // tooltip not visible yet, but maybe a timeout is still pending
253 stop_tooltip_timeout();
258 void tooltip_hide(void* arg
)
260 stop_tooltip_timeout();
261 if (g_tooltip
.mapped
) {
262 g_tooltip
.mapped
= False
;
263 XUnmapWindow(server
.dsp
, g_tooltip
.window
);
269 void start_show_timeout()
271 if (g_tooltip
.timeout
)
272 change_timeout(g_tooltip
.timeout
, g_tooltip
.show_timeout_msec
, 0, tooltip_show
, 0);
274 g_tooltip
.timeout
= add_timeout(g_tooltip
.show_timeout_msec
, 0, tooltip_show
, 0);
278 void start_hide_timeout()
280 if (g_tooltip
.timeout
)
281 change_timeout(g_tooltip
.timeout
, g_tooltip
.hide_timeout_msec
, 0, tooltip_hide
, 0);
283 g_tooltip
.timeout
= add_timeout(g_tooltip
.hide_timeout_msec
, 0, tooltip_hide
, 0);
287 void stop_tooltip_timeout()
289 if (g_tooltip
.timeout
) {
290 stop_timeout(g_tooltip
.timeout
);
291 g_tooltip
.timeout
= 0;
296 void tooltip_copy_text(Area
* area
)
298 free(g_tooltip
.tooltip_text
);
299 if (area
&& area
->_get_tooltip_text
)
300 g_tooltip
.tooltip_text
= strdup(area
->_get_tooltip_text(area
));
302 g_tooltip
.tooltip_text
= 0;
303 g_tooltip
.area
= area
;