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 **************************************************************************/
21 #include <cairo-xlib.h>
27 // TODO: Use timer_create instead of setitimer, because SIGALRM is not the right signal for this...
28 // Reason: If we want to implement autohide we have to use another signal...
30 static int x
, y
, width
, height
;
32 // give the tooltip some reasonable default values
36 .show_timeout
= { .it_interval
={0, 0}, .it_value
={0, 0} },
37 .hide_timeout
= { .it_interval
={0, 0}, .it_value
={0, 0} },
39 .current_state
= TOOLTIP_ABOUT_TO_HIDE
,
43 .font_color
= { .color
={1, 1, 1}, .alpha
=1 },
44 .background_color
= { .color
={0.5, 0.4, 0.5}, .alpha
=1 },
45 .border
= { .color
={0, 0, 0}, .alpha
=1, .width
=1, .rounded
=0 },
51 if (!g_tooltip
.font_desc
)
52 g_tooltip
.font_desc
= pango_font_description_from_string("sans 10");
54 XSetWindowAttributes attr
;
55 attr
.override_redirect
= True
;
56 attr
.event_mask
= ExposureMask
;
57 if (g_tooltip
.window
) XDestroyWindow(server
.dsp
, g_tooltip
.window
);
58 g_tooltip
.window
= XCreateWindow(server
.dsp
, server
.root_win
, 0, 0, 100, 20, 0, server
.depth
, InputOutput
, CopyFromParent
, CWOverrideRedirect
|CWEventMask
, &attr
);
62 void cleanup_tooltip()
66 g_tooltip
.enabled
= False
;
67 g_tooltip
.current_state
= TOOLTIP_ABOUT_TO_HIDE
;
71 if (g_tooltip
.window
) {
72 XDestroyWindow(server
.dsp
, g_tooltip
.window
);
75 if (g_tooltip
.font_desc
) {
76 pango_font_description_free(g_tooltip
.font_desc
);
77 g_tooltip
.font_desc
= 0;
82 void tooltip_sighandler(int sig
)
84 if (g_tooltip
.current_state
== TOOLTIP_ABOUT_TO_SHOW
)
86 else if (g_tooltip
.current_state
== TOOLTIP_ABOUT_TO_HIDE
)
91 void tooltip_trigger_show(Task
* task
, int x_root
, int y_root
)
96 if (g_tooltip
.mapped
&& g_tooltip
.task
!= task
) {
97 g_tooltip
.task
= task
;
101 else if (!g_tooltip
.mapped
) {
102 g_tooltip
.current_state
= TOOLTIP_ABOUT_TO_SHOW
;
103 g_tooltip
.task
= task
;
104 struct timeval t
= g_tooltip
.show_timeout
.it_value
;
105 if (t
.tv_sec
== 0 && t
.tv_usec
== 0) {
110 setitimer(ITIMER_REAL
, &g_tooltip
.show_timeout
, 0);
117 if (!g_tooltip
.mapped
) {
118 g_tooltip
.mapped
= True
;
119 XMapWindow(server
.dsp
, g_tooltip
.window
);
126 void tooltip_update_geometry()
131 cs
= cairo_xlib_surface_create(server
.dsp
, g_tooltip
.window
, server
.visual
, width
, height
);
132 c
= cairo_create(cs
);
133 layout
= pango_cairo_create_layout(c
);
134 pango_layout_set_font_description(layout
, g_tooltip
.font_desc
);
135 pango_layout_set_text(layout
, g_tooltip
.task
->title
, -1);
136 PangoRectangle r1
, r2
;
137 pango_layout_get_pixel_extents(layout
, &r1
, &r2
);
138 width
= 2*g_tooltip
.border
.width
+ 2*g_tooltip
.paddingx
+ r2
.width
;
139 height
= 2*g_tooltip
.border
.width
+ 2*g_tooltip
.paddingy
+ r2
.height
;
141 Panel
* panel
= g_tooltip
.task
->area
.panel
;
142 if (panel_horizontal
&& panel_position
& BOTTOM
)
143 y
= panel
->posy
-height
;
144 else if (panel_horizontal
&& panel_position
& TOP
)
145 y
= panel
->posy
+ panel
->area
.height
;
146 else if (panel_position
& LEFT
)
147 x
= panel
->posx
+ panel
->area
.width
;
149 x
= panel
->posx
- width
;
151 g_object_unref(layout
);
153 cairo_surface_destroy(cs
);
157 void tooltip_adjust_geometry()
159 // adjust coordinates and size to not go offscreen
160 // it seems quite impossible that the height needs to be adjusted, but we do it anyway.
162 int min_x
, min_y
, max_width
, max_height
;
163 Panel
* panel
= g_tooltip
.task
->area
.panel
;
164 int screen_width
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
;
165 int screen_height
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
;
166 if ( x
+width
<= screen_width
&& y
+height
<= screen_height
&& x
>=0 && y
>=0)
167 return; // no adjustment needed
169 if (panel_horizontal
) {
171 max_width
=screen_width
;
172 max_height
=screen_height
-panel
->area
.height
;
173 if (panel_position
& BOTTOM
)
176 min_y
=panel
->area
.height
;
179 max_width
=screen_width
-panel
->area
.width
;
181 max_height
=screen_height
;
182 if (panel_position
& LEFT
)
183 min_x
=panel
->area
.width
;
188 if (x
+width
> server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
)
189 x
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
- width
;
190 if ( y
+height
> server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
)
191 y
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
- height
;
199 if (height
>max_height
)
203 void tooltip_update()
205 if (!g_tooltip
.task
) {
210 //printf("tooltip_update\n");
211 tooltip_update_geometry();
212 tooltip_adjust_geometry();
213 XMoveResizeWindow(server
.dsp
, g_tooltip
.window
, x
, y
, width
, height
);
215 // Stuff for drawing the tooltip
219 cs
= cairo_xlib_surface_create(server
.dsp
, g_tooltip
.window
, server
.visual
, width
, height
);
220 c
= cairo_create(cs
);
221 Color bc
= g_tooltip
.background_color
;
222 cairo_rectangle(c
, 0, 0, width
, height
);
223 cairo_set_source_rgb(c
, bc
.color
[0], bc
.color
[1], bc
.color
[2]);
225 Border b
= g_tooltip
.border
;
226 cairo_set_source_rgba(c
, b
.color
[0], b
.color
[1], b
.color
[2], b
.alpha
);
227 cairo_set_line_width(c
, b
.width
);
228 cairo_rectangle(c
, b
.width
/2.0, b
.width
/2.0, width
-b
.width
, height
-b
.width
);
231 config_color fc
= g_tooltip
.font_color
;
232 cairo_set_source_rgba(c
, fc
.color
[0], fc
.color
[1], fc
.color
[2], fc
.alpha
);
233 layout
= pango_cairo_create_layout(c
);
234 pango_layout_set_font_description(layout
, g_tooltip
.font_desc
);
235 pango_layout_set_text(layout
, g_tooltip
.task
->title
, -1);
236 PangoRectangle r1
, r2
;
237 pango_layout_get_pixel_extents(layout
, &r1
, &r2
);
238 pango_layout_set_width(layout
, width
*PANGO_SCALE
);
239 pango_layout_set_height(layout
, height
*PANGO_SCALE
);
240 pango_layout_set_ellipsize(layout
, PANGO_ELLIPSIZE_END
);
241 // 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.)
242 cairo_move_to(c
, -r1
.x
/2+g_tooltip
.border
.width
+g_tooltip
.paddingx
, -r1
.y
/2+g_tooltip
.border
.width
+g_tooltip
.paddingy
);
243 pango_cairo_show_layout (c
, layout
);
245 g_object_unref (layout
);
247 cairo_surface_destroy (cs
);
251 void tooltip_trigger_hide(Tooltip
* tooltip
)
253 if (g_tooltip
.mapped
) {
254 g_tooltip
.current_state
= TOOLTIP_ABOUT_TO_HIDE
;
255 struct timeval t
= g_tooltip
.hide_timeout
.it_value
;
257 if (t
.tv_sec
== 0 && t
.tv_usec
== 0) {
262 setitimer(ITIMER_REAL
, &g_tooltip
.hide_timeout
, 0);
265 // tooltip not visible yet, but maybe an alarm is still pending
273 if (g_tooltip
.mapped
) {
274 g_tooltip
.mapped
= False
;
275 XUnmapWindow(server
.dsp
, g_tooltip
.window
);