]> Dogcows Code - chaz/tint2/blob - src/tooltip/tooltip.c
Center the tooltip correctly when the text changes while the tooltip is visible
[chaz/tint2] / src / tooltip / tooltip.c
1 /**************************************************************************
2 *
3 * Copyright (C) 2009 Andreas.Fink (Andreas.Fink85@gmail.com)
4 *
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.
8 *
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 **************************************************************************/
17
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <cairo.h>
23 #include <cairo-xlib.h>
24
25 #include "server.h"
26 #include "tooltip.h"
27 #include "panel.h"
28 #include "timer.h"
29
30 static int x_original, y_original, x, y, width, height;
31
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
37 Tooltip g_tooltip;
38
39
40 void default_tooltip()
41 {
42 // give the tooltip some reasonable default values
43 memset(&g_tooltip, 0, sizeof(Tooltip));
44
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;
49 }
50
51 void cleanup_tooltip()
52 {
53 stop_tooltip_timeout();
54 tooltip_hide(0);
55 tooltip_copy_text(0);
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);
58 }
59
60
61 void init_tooltip()
62 {
63 if (!g_tooltip.font_desc)
64 g_tooltip.font_desc = pango_font_description_from_string("sans 10");
65
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);
75 }
76
77
78 void tooltip_trigger_show(Area* area, Panel* p, XEvent *e)
79 {
80 // Position the tooltip in the center of the area
81 x = x_original = area->posx + area->width / 2 + e->xmotion.x_root - e->xmotion.x;
82 y = y_original = area->posy + area->height / 2 + e->xmotion.y_root - e->xmotion.y;
83 g_tooltip.panel = p;
84 if (g_tooltip.mapped && g_tooltip.area != area) {
85 tooltip_copy_text(area);
86 tooltip_update();
87 stop_tooltip_timeout();
88 }
89 else if (!g_tooltip.mapped) {
90 start_show_timeout();
91 }
92 }
93
94
95 void tooltip_show(void* arg)
96 {
97 int mx, my;
98 Window w;
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);
106 tooltip_update();
107 XFlush(server.dsp);
108 }
109 }
110
111
112 void tooltip_update_geometry()
113 {
114 cairo_surface_t *cs;
115 cairo_t *c;
116 PangoLayout* layout;
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;
126
127 x = x_original;
128 y = y_original;
129
130 Panel* panel = g_tooltip.panel;
131 if (panel_horizontal && panel_position & BOTTOM)
132 y = panel->posy-height;
133 else if (panel_horizontal && panel_position & TOP)
134 y = panel->posy + panel->area.height;
135 else if (panel_position & LEFT)
136 x = panel->posx + panel->area.width;
137 else
138 x = panel->posx - width;
139
140 if (!panel_horizontal)
141 y -= height/2;
142
143 g_object_unref(layout);
144 cairo_destroy(c);
145 cairo_surface_destroy(cs);
146 }
147
148
149 void tooltip_adjust_geometry()
150 {
151 // adjust coordinates and size to not go offscreen
152 // it seems quite impossible that the height needs to be adjusted, but we do it anyway.
153
154 int min_x, min_y, max_width, max_height;
155 Panel* panel = g_tooltip.panel;
156 int screen_width = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width;
157 int screen_height = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height;
158 if ( x+width <= screen_width && y+height <= screen_height && x>=0 && y>=0)
159 return; // no adjustment needed
160
161 if (panel_horizontal) {
162 min_x=0;
163 max_width=screen_width;
164 max_height=screen_height-panel->area.height;
165 if (panel_position & BOTTOM)
166 min_y=0;
167 else
168 min_y=panel->area.height;
169 }
170 else {
171 max_width=screen_width-panel->area.width;
172 min_y=0;
173 max_height=screen_height;
174 if (panel_position & LEFT)
175 min_x=panel->area.width;
176 else
177 min_x=0;
178 }
179
180 if (x+width > server.monitor[panel->monitor].x + server.monitor[panel->monitor].width)
181 x = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width - width;
182 if ( y+height > server.monitor[panel->monitor].y + server.monitor[panel->monitor].height)
183 y = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height - height;
184
185 if (x<min_x)
186 x=min_x;
187 if (width>max_width)
188 width = max_width;
189 if (y<min_y)
190 y=min_y;
191 if (height>max_height)
192 height=max_height;
193 }
194
195 void tooltip_update()
196 {
197 if (!g_tooltip.tooltip_text) {
198 tooltip_hide(0);
199 return;
200 }
201
202 tooltip_update_geometry();
203 tooltip_adjust_geometry();
204 XMoveResizeWindow(server.dsp, g_tooltip.window, x, y, width, height);
205
206 // Stuff for drawing the tooltip
207 cairo_surface_t *cs;
208 cairo_t *c;
209 PangoLayout* layout;
210 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
211 c = cairo_create(cs);
212 Color bc = g_tooltip.bg->back;
213 Border b = g_tooltip.bg->border;
214 if (server.real_transparency) {
215 clear_pixmap(g_tooltip.window, 0, 0, width, height);
216 draw_rect(c, b.width, b.width, width-2*b.width, height-2*b.width, b.rounded-b.width/1.571);
217 cairo_set_source_rgba(c, bc.color[0], bc.color[1], bc.color[2], bc.alpha);
218 }
219 else {
220 cairo_rectangle(c, 0., 0, width, height);
221 cairo_set_source_rgb(c, bc.color[0], bc.color[1], bc.color[2]);
222 }
223 cairo_fill(c);
224 cairo_set_line_width(c, b.width);
225 if (server.real_transparency)
226 draw_rect(c, b.width/2.0, b.width/2.0, width - b.width, height - b.width, b.rounded);
227 else
228 cairo_rectangle(c, b.width/2.0, b.width/2.0, width-b.width, height-b.width);
229 cairo_set_source_rgba(c, b.color[0], b.color[1], b.color[2], b.alpha);
230 cairo_stroke(c);
231
232 Color fc = g_tooltip.font_color;
233 cairo_set_source_rgba(c, fc.color[0], fc.color[1], fc.color[2], fc.alpha);
234 layout = pango_cairo_create_layout(c);
235 pango_layout_set_font_description(layout, g_tooltip.font_desc);
236 pango_layout_set_text(layout, g_tooltip.tooltip_text, -1);
237 PangoRectangle r1, r2;
238 pango_layout_get_pixel_extents(layout, &r1, &r2);
239 pango_layout_set_width(layout, width*PANGO_SCALE);
240 pango_layout_set_height(layout, height*PANGO_SCALE);
241 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
242 // 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.)
243 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);
244 pango_cairo_show_layout (c, layout);
245
246 g_object_unref (layout);
247 cairo_destroy (c);
248 cairo_surface_destroy (cs);
249 }
250
251
252 void tooltip_trigger_hide(Tooltip* tooltip)
253 {
254 if (g_tooltip.mapped) {
255 tooltip_copy_text(0);
256 start_hide_timeout();
257 }
258 else {
259 // tooltip not visible yet, but maybe a timeout is still pending
260 stop_tooltip_timeout();
261 }
262 }
263
264
265 void tooltip_hide(void* arg)
266 {
267 stop_tooltip_timeout();
268 if (g_tooltip.mapped) {
269 g_tooltip.mapped = False;
270 XUnmapWindow(server.dsp, g_tooltip.window);
271 XFlush(server.dsp);
272 }
273 }
274
275
276 void start_show_timeout()
277 {
278 if (g_tooltip.timeout)
279 change_timeout(g_tooltip.timeout, g_tooltip.show_timeout_msec, 0, tooltip_show, 0);
280 else
281 g_tooltip.timeout = add_timeout(g_tooltip.show_timeout_msec, 0, tooltip_show, 0);
282 }
283
284
285 void start_hide_timeout()
286 {
287 if (g_tooltip.timeout)
288 change_timeout(g_tooltip.timeout, g_tooltip.hide_timeout_msec, 0, tooltip_hide, 0);
289 else
290 g_tooltip.timeout = add_timeout(g_tooltip.hide_timeout_msec, 0, tooltip_hide, 0);
291 }
292
293
294 void stop_tooltip_timeout()
295 {
296 if (g_tooltip.timeout) {
297 stop_timeout(g_tooltip.timeout);
298 g_tooltip.timeout = 0;
299 }
300 }
301
302
303 void tooltip_copy_text(Area* area)
304 {
305 free(g_tooltip.tooltip_text);
306 if (area && area->_get_tooltip_text)
307 g_tooltip.tooltip_text = strdup(area->_get_tooltip_text(area));
308 else
309 g_tooltip.tooltip_text = 0;
310 g_tooltip.area = area;
311 }
This page took 0.047165 seconds and 4 git commands to generate.