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
;
84 if (g_tooltip
.mapped
&& g_tooltip
.area
!= area
) {
85 tooltip_copy_text(area
);
87 stop_tooltip_timeout();
89 else if (!g_tooltip
.mapped
) {
95 void tooltip_show(void* arg
)
99 XTranslateCoordinates( server
.dsp
, server
.root_win
, g_tooltip
.panel
->main_win
, x
, y
, &mx
, &my
, &w
);
100 Area
* area
= click_area(g_tooltip
.panel
, mx
, my
);
101 stop_tooltip_timeout();
102 if (!g_tooltip
.mapped
&& area
->_get_tooltip_text
) {
103 tooltip_copy_text(area
);
104 g_tooltip
.mapped
= True
;
105 XMapWindow(server
.dsp
, g_tooltip
.window
);
112 void tooltip_update_geometry()
117 cs
= cairo_xlib_surface_create(server
.dsp
, g_tooltip
.window
, server
.visual
, width
, height
);
118 c
= cairo_create(cs
);
119 layout
= pango_cairo_create_layout(c
);
120 pango_layout_set_font_description(layout
, g_tooltip
.font_desc
);
121 pango_layout_set_text(layout
, g_tooltip
.tooltip_text
, -1);
122 PangoRectangle r1
, r2
;
123 pango_layout_get_pixel_extents(layout
, &r1
, &r2
);
124 width
= 2*g_tooltip
.bg
->border
.width
+ 2*g_tooltip
.paddingx
+ r2
.width
;
125 height
= 2*g_tooltip
.bg
->border
.width
+ 2*g_tooltip
.paddingy
+ r2
.height
;
127 Panel
* panel
= g_tooltip
.panel
;
128 if (panel_horizontal
&& panel_position
& BOTTOM
)
129 y
= panel
->posy
-height
;
130 else if (panel_horizontal
&& panel_position
& TOP
)
131 y
= panel
->posy
+ panel
->area
.height
;
132 else if (panel_position
& LEFT
)
133 x
= panel
->posx
+ panel
->area
.width
;
135 x
= panel
->posx
- width
;
137 g_object_unref(layout
);
139 cairo_surface_destroy(cs
);
143 void tooltip_adjust_geometry()
145 // adjust coordinates and size to not go offscreen
146 // it seems quite impossible that the height needs to be adjusted, but we do it anyway.
148 int min_x
, min_y
, max_width
, max_height
;
149 Panel
* panel
= g_tooltip
.panel
;
150 int screen_width
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
;
151 int screen_height
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
;
152 if ( x
+width
<= screen_width
&& y
+height
<= screen_height
&& x
>=0 && y
>=0)
153 return; // no adjustment needed
155 if (panel_horizontal
) {
157 max_width
=screen_width
;
158 max_height
=screen_height
-panel
->area
.height
;
159 if (panel_position
& BOTTOM
)
162 min_y
=panel
->area
.height
;
165 max_width
=screen_width
-panel
->area
.width
;
167 max_height
=screen_height
;
168 if (panel_position
& LEFT
)
169 min_x
=panel
->area
.width
;
174 if (x
+width
> server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
)
175 x
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
- width
;
176 if ( y
+height
> server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
)
177 y
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
- height
;
185 if (height
>max_height
)
189 void tooltip_update()
191 if (!g_tooltip
.tooltip_text
) {
196 tooltip_update_geometry();
197 tooltip_adjust_geometry();
198 XMoveResizeWindow(server
.dsp
, g_tooltip
.window
, x
, y
, width
, height
);
200 // Stuff for drawing the tooltip
204 cs
= cairo_xlib_surface_create(server
.dsp
, g_tooltip
.window
, server
.visual
, width
, height
);
205 c
= cairo_create(cs
);
206 Color bc
= g_tooltip
.bg
->back
;
207 Border b
= g_tooltip
.bg
->border
;
208 if (server
.real_transparency
) {
209 clear_pixmap(g_tooltip
.window
, 0, 0, width
, height
);
210 draw_rect(c
, b
.width
, b
.width
, width
-2*b
.width
, height
-2*b
.width
, b
.rounded
-b
.width
/1.571);
211 cairo_set_source_rgba(c
, bc
.color
[0], bc
.color
[1], bc
.color
[2], bc
.alpha
);
214 cairo_rectangle(c
, 0., 0, width
, height
);
215 cairo_set_source_rgb(c
, bc
.color
[0], bc
.color
[1], bc
.color
[2]);
218 cairo_set_line_width(c
, b
.width
);
219 if (server
.real_transparency
)
220 draw_rect(c
, b
.width
/2.0, b
.width
/2.0, width
- b
.width
, height
- b
.width
, b
.rounded
);
222 cairo_rectangle(c
, b
.width
/2.0, b
.width
/2.0, width
-b
.width
, height
-b
.width
);
223 cairo_set_source_rgba(c
, b
.color
[0], b
.color
[1], b
.color
[2], b
.alpha
);
226 Color fc
= g_tooltip
.font_color
;
227 cairo_set_source_rgba(c
, fc
.color
[0], fc
.color
[1], fc
.color
[2], fc
.alpha
);
228 layout
= pango_cairo_create_layout(c
);
229 pango_layout_set_font_description(layout
, g_tooltip
.font_desc
);
230 pango_layout_set_text(layout
, g_tooltip
.tooltip_text
, -1);
231 PangoRectangle r1
, r2
;
232 pango_layout_get_pixel_extents(layout
, &r1
, &r2
);
233 pango_layout_set_width(layout
, width
*PANGO_SCALE
);
234 pango_layout_set_height(layout
, height
*PANGO_SCALE
);
235 pango_layout_set_ellipsize(layout
, PANGO_ELLIPSIZE_END
);
236 // 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.)
237 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
);
238 pango_cairo_show_layout (c
, layout
);
240 g_object_unref (layout
);
242 cairo_surface_destroy (cs
);
246 void tooltip_trigger_hide(Tooltip
* tooltip
)
248 if (g_tooltip
.mapped
) {
249 tooltip_copy_text(0);
250 start_hide_timeout();
253 // tooltip not visible yet, but maybe a timeout is still pending
254 stop_tooltip_timeout();
259 void tooltip_hide(void* arg
)
261 stop_tooltip_timeout();
262 if (g_tooltip
.mapped
) {
263 g_tooltip
.mapped
= False
;
264 XUnmapWindow(server
.dsp
, g_tooltip
.window
);
270 void start_show_timeout()
272 if (g_tooltip
.timeout
)
273 change_timeout(g_tooltip
.timeout
, g_tooltip
.show_timeout_msec
, 0, tooltip_show
, 0);
275 g_tooltip
.timeout
= add_timeout(g_tooltip
.show_timeout_msec
, 0, tooltip_show
, 0);
279 void start_hide_timeout()
281 if (g_tooltip
.timeout
)
282 change_timeout(g_tooltip
.timeout
, g_tooltip
.hide_timeout_msec
, 0, tooltip_hide
, 0);
284 g_tooltip
.timeout
= add_timeout(g_tooltip
.hide_timeout_msec
, 0, tooltip_hide
, 0);
288 void stop_tooltip_timeout()
290 if (g_tooltip
.timeout
) {
291 stop_timeout(g_tooltip
.timeout
);
292 g_tooltip
.timeout
= 0;
297 void tooltip_copy_text(Area
* area
)
299 free(g_tooltip
.tooltip_text
);
300 if (area
&& area
->_get_tooltip_text
)
301 g_tooltip
.tooltip_text
= strdup(area
->_get_tooltip_text(area
));
303 g_tooltip
.tooltip_text
= 0;
304 g_tooltip
.area
= area
;