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();
36 void tooltip_copy_text(Area
* area
);
38 // give the tooltip some reasonable default values
44 .show_timeout_msec
= 0,
45 .hide_timeout_msec
= 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 },
59 if (!g_tooltip
.font_desc
)
60 g_tooltip
.font_desc
= pango_font_description_from_string("sans 10");
62 XSetWindowAttributes attr
;
63 attr
.override_redirect
= True
;
64 attr
.event_mask
= StructureNotifyMask
;
65 if (g_tooltip
.window
) XDestroyWindow(server
.dsp
, g_tooltip
.window
);
66 g_tooltip
.window
= XCreateWindow(server
.dsp
, server
.root_win
, 0, 0, 100, 20, 0, server
.depth
, InputOutput
, CopyFromParent
, CWOverrideRedirect
|CWEventMask
, &attr
);
70 void cleanup_tooltip()
72 stop_tooltip_timeout();
74 g_tooltip
.enabled
= False
;
76 if (g_tooltip
.window
) {
77 XDestroyWindow(server
.dsp
, g_tooltip
.window
);
80 if (g_tooltip
.font_desc
) {
81 pango_font_description_free(g_tooltip
.font_desc
);
82 g_tooltip
.font_desc
= 0;
87 void tooltip_trigger_show(Area
* area
, Panel
* p
, int x_root
, int y_root
)
92 if (g_tooltip
.mapped
&& g_tooltip
.area
!= area
) {
93 tooltip_copy_text(area
);
95 stop_tooltip_timeout();
97 else if (!g_tooltip
.mapped
) {
107 XTranslateCoordinates( server
.dsp
, server
.root_win
, g_tooltip
.panel
->main_win
, x
, y
, &mx
, &my
, &w
);
108 Area
* area
= click_area(g_tooltip
.panel
, mx
, my
);
109 stop_tooltip_timeout();
110 if (!g_tooltip
.mapped
&& area
->_get_tooltip_text
) {
111 tooltip_copy_text(area
);
112 g_tooltip
.mapped
= True
;
113 XMapWindow(server
.dsp
, g_tooltip
.window
);
119 void tooltip_update_geometry()
124 cs
= cairo_xlib_surface_create(server
.dsp
, g_tooltip
.window
, server
.visual
, width
, height
);
125 c
= cairo_create(cs
);
126 layout
= pango_cairo_create_layout(c
);
127 pango_layout_set_font_description(layout
, g_tooltip
.font_desc
);
128 pango_layout_set_text(layout
, g_tooltip
.tooltip_text
, -1);
129 PangoRectangle r1
, r2
;
130 pango_layout_get_pixel_extents(layout
, &r1
, &r2
);
131 width
= 2*g_tooltip
.border
.width
+ 2*g_tooltip
.paddingx
+ r2
.width
;
132 height
= 2*g_tooltip
.border
.width
+ 2*g_tooltip
.paddingy
+ r2
.height
;
134 Panel
* panel
= g_tooltip
.panel
;
135 if (panel_horizontal
&& panel_position
& BOTTOM
)
136 y
= panel
->posy
-height
;
137 else if (panel_horizontal
&& panel_position
& TOP
)
138 y
= panel
->posy
+ panel
->area
.height
;
139 else if (panel_position
& LEFT
)
140 x
= panel
->posx
+ panel
->area
.width
;
142 x
= panel
->posx
- width
;
144 g_object_unref(layout
);
146 cairo_surface_destroy(cs
);
150 void tooltip_adjust_geometry()
152 // adjust coordinates and size to not go offscreen
153 // it seems quite impossible that the height needs to be adjusted, but we do it anyway.
155 int min_x
, min_y
, max_width
, max_height
;
156 Panel
* panel
= g_tooltip
.panel
;
157 int screen_width
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
;
158 int screen_height
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
;
159 if ( x
+width
<= screen_width
&& y
+height
<= screen_height
&& x
>=0 && y
>=0)
160 return; // no adjustment needed
162 if (panel_horizontal
) {
164 max_width
=screen_width
;
165 max_height
=screen_height
-panel
->area
.height
;
166 if (panel_position
& BOTTOM
)
169 min_y
=panel
->area
.height
;
172 max_width
=screen_width
-panel
->area
.width
;
174 max_height
=screen_height
;
175 if (panel_position
& LEFT
)
176 min_x
=panel
->area
.width
;
181 if (x
+width
> server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
)
182 x
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
- width
;
183 if ( y
+height
> server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
)
184 y
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
- height
;
192 if (height
>max_height
)
196 void tooltip_update()
198 if (!g_tooltip
.tooltip_text
) {
203 tooltip_update_geometry();
204 tooltip_adjust_geometry();
205 XMoveResizeWindow(server
.dsp
, g_tooltip
.window
, x
, y
, width
, height
);
207 // Stuff for drawing the tooltip
211 cs
= cairo_xlib_surface_create(server
.dsp
, g_tooltip
.window
, server
.visual
, width
, height
);
212 c
= cairo_create(cs
);
213 Color bc
= g_tooltip
.background_color
;
214 cairo_rectangle(c
, 0, 0, width
, height
);
215 cairo_set_source_rgb(c
, bc
.color
[0], bc
.color
[1], bc
.color
[2]);
217 Border b
= g_tooltip
.border
;
218 cairo_set_source_rgba(c
, b
.color
[0], b
.color
[1], b
.color
[2], b
.alpha
);
219 cairo_set_line_width(c
, b
.width
);
220 cairo_rectangle(c
, b
.width
/2.0, b
.width
/2.0, width
-b
.width
, height
-b
.width
);
223 config_color fc
= g_tooltip
.font_color
;
224 cairo_set_source_rgba(c
, fc
.color
[0], fc
.color
[1], fc
.color
[2], fc
.alpha
);
225 layout
= pango_cairo_create_layout(c
);
226 pango_layout_set_font_description(layout
, g_tooltip
.font_desc
);
227 pango_layout_set_text(layout
, g_tooltip
.tooltip_text
, -1);
228 PangoRectangle r1
, r2
;
229 pango_layout_get_pixel_extents(layout
, &r1
, &r2
);
230 pango_layout_set_width(layout
, width
*PANGO_SCALE
);
231 pango_layout_set_height(layout
, height
*PANGO_SCALE
);
232 pango_layout_set_ellipsize(layout
, PANGO_ELLIPSIZE_END
);
233 // 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.)
234 cairo_move_to(c
, -r1
.x
/2+g_tooltip
.border
.width
+g_tooltip
.paddingx
, -r1
.y
/2+g_tooltip
.border
.width
+g_tooltip
.paddingy
);
235 pango_cairo_show_layout (c
, layout
);
237 g_object_unref (layout
);
239 cairo_surface_destroy (cs
);
243 void tooltip_trigger_hide(Tooltip
* tooltip
)
245 if (g_tooltip
.mapped
) {
246 tooltip_copy_text(0);
247 start_hide_timeout();
250 // tooltip not visible yet, but maybe a timeout is still pending
251 stop_tooltip_timeout();
258 stop_tooltip_timeout();
259 if (g_tooltip
.mapped
) {
260 g_tooltip
.mapped
= False
;
261 XUnmapWindow(server
.dsp
, g_tooltip
.window
);
267 void start_show_timeout()
269 if (g_tooltip
.timeout
)
270 change_timeout(g_tooltip
.timeout
, g_tooltip
.show_timeout_msec
, 0, tooltip_show
);
272 g_tooltip
.timeout
= add_timeout(g_tooltip
.show_timeout_msec
, 0, tooltip_show
);
276 void start_hide_timeout()
278 if (g_tooltip
.timeout
)
279 change_timeout(g_tooltip
.timeout
, g_tooltip
.hide_timeout_msec
, 0, tooltip_hide
);
281 g_tooltip
.timeout
= add_timeout(g_tooltip
.hide_timeout_msec
, 0, tooltip_hide
);
285 void stop_tooltip_timeout()
287 if (g_tooltip
.timeout
) {
288 stop_timeout(g_tooltip
.timeout
);
289 g_tooltip
.timeout
= 0;
294 void tooltip_copy_text(Area
* area
)
296 free(g_tooltip
.tooltip_text
);
297 if (area
&& area
->_get_tooltip_text
)
298 g_tooltip
.tooltip_text
= strdup(area
->_get_tooltip_text(area
));
300 g_tooltip
.tooltip_text
= 0;
301 g_tooltip
.area
= area
;