]> Dogcows Code - chaz/tint2/blob - src/tooltip/tooltip.c
fixed : update tooltip when title change (downloading file)
[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, 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 // give the tooltip some reasonable default values
38 Tooltip g_tooltip = {
39 .tooltip_text = 0,
40 .area = 0,
41 .panel = 0,
42 .window = 0,
43 .show_timeout_msec = 0,
44 .hide_timeout_msec = 0,
45 .enabled = False,
46 .mapped = False,
47 .paddingx = 0,
48 .paddingy = 0,
49 .font_color = { .color={1, 1, 1}, .alpha=1 },
50 .bg = 0,
51 .font_desc = 0,
52 .timeout = 0
53 };
54
55 void init_tooltip()
56 {
57 if (!g_tooltip.font_desc)
58 g_tooltip.font_desc = pango_font_description_from_string("sans 10");
59
60 XSetWindowAttributes attr;
61 attr.override_redirect = True;
62 attr.event_mask = StructureNotifyMask;
63 attr.colormap = server.colormap;
64 attr.background_pixel = 0;
65 attr.border_pixel = 0;
66 unsigned long mask = CWEventMask|CWColormap|CWBorderPixel|CWBackPixel|CWOverrideRedirect;
67 if (g_tooltip.window) XDestroyWindow(server.dsp, g_tooltip.window);
68 g_tooltip.window = XCreateWindow(server.dsp, server.root_win, 0, 0, 100, 20, 0, server.depth, InputOutput, server.visual, mask, &attr);
69 }
70
71
72 void cleanup_tooltip()
73 {
74 stop_tooltip_timeout();
75 tooltip_hide(0);
76 g_tooltip.enabled = False;
77 tooltip_copy_text(0);
78 if (g_tooltip.window) {
79 XDestroyWindow(server.dsp, g_tooltip.window);
80 g_tooltip.window = 0;
81 }
82 if (g_tooltip.font_desc) {
83 pango_font_description_free(g_tooltip.font_desc);
84 g_tooltip.font_desc = 0;
85 }
86 }
87
88
89 void tooltip_trigger_show(Area* area, Panel* p, int x_root, int y_root)
90 {
91 x = x_root;
92 y = y_root;
93 g_tooltip.panel = p;
94 if (g_tooltip.mapped && g_tooltip.area != area) {
95 tooltip_copy_text(area);
96 tooltip_update();
97 stop_tooltip_timeout();
98 }
99 else if (!g_tooltip.mapped) {
100 start_show_timeout();
101 }
102 }
103
104
105 void tooltip_show(void* arg)
106 {
107 int mx, my;
108 Window w;
109 XTranslateCoordinates( server.dsp, server.root_win, g_tooltip.panel->main_win, x, y, &mx, &my, &w);
110 Area* area = click_area(g_tooltip.panel, mx, my);
111 stop_tooltip_timeout();
112 if (!g_tooltip.mapped && area->_get_tooltip_text) {
113 tooltip_copy_text(area);
114 g_tooltip.mapped = True;
115 XMapWindow(server.dsp, g_tooltip.window);
116 tooltip_update();
117 XFlush(server.dsp);
118 }
119 }
120
121
122 void tooltip_update_geometry()
123 {
124 cairo_surface_t *cs;
125 cairo_t *c;
126 PangoLayout* layout;
127 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
128 c = cairo_create(cs);
129 layout = pango_cairo_create_layout(c);
130 pango_layout_set_font_description(layout, g_tooltip.font_desc);
131 pango_layout_set_text(layout, g_tooltip.tooltip_text, -1);
132 PangoRectangle r1, r2;
133 pango_layout_get_pixel_extents(layout, &r1, &r2);
134 width = 2*g_tooltip.bg->border.width + 2*g_tooltip.paddingx + r2.width;
135 height = 2*g_tooltip.bg->border.width + 2*g_tooltip.paddingy + r2.height;
136
137 Panel* panel = g_tooltip.panel;
138 if (panel_horizontal && panel_position & BOTTOM)
139 y = panel->posy-height;
140 else if (panel_horizontal && panel_position & TOP)
141 y = panel->posy + panel->area.height;
142 else if (panel_position & LEFT)
143 x = panel->posx + panel->area.width;
144 else
145 x = panel->posx - width;
146
147 g_object_unref(layout);
148 cairo_destroy(c);
149 cairo_surface_destroy(cs);
150 }
151
152
153 void tooltip_adjust_geometry()
154 {
155 // adjust coordinates and size to not go offscreen
156 // it seems quite impossible that the height needs to be adjusted, but we do it anyway.
157
158 int min_x, min_y, max_width, max_height;
159 Panel* panel = g_tooltip.panel;
160 int screen_width = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width;
161 int screen_height = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height;
162 if ( x+width <= screen_width && y+height <= screen_height && x>=0 && y>=0)
163 return; // no adjustment needed
164
165 if (panel_horizontal) {
166 min_x=0;
167 max_width=screen_width;
168 max_height=screen_height-panel->area.height;
169 if (panel_position & BOTTOM)
170 min_y=0;
171 else
172 min_y=panel->area.height;
173 }
174 else {
175 max_width=screen_width-panel->area.width;
176 min_y=0;
177 max_height=screen_height;
178 if (panel_position & LEFT)
179 min_x=panel->area.width;
180 else
181 min_x=0;
182 }
183
184 if (x+width > server.monitor[panel->monitor].x + server.monitor[panel->monitor].width)
185 x = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width - width;
186 if ( y+height > server.monitor[panel->monitor].y + server.monitor[panel->monitor].height)
187 y = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height - height;
188
189 if (x<min_x)
190 x=min_x;
191 if (width>max_width)
192 width = max_width;
193 if (y<min_y)
194 y=min_y;
195 if (height>max_height)
196 height=max_height;
197 }
198
199 void tooltip_update()
200 {
201 if (!g_tooltip.tooltip_text) {
202 tooltip_hide(0);
203 return;
204 }
205
206 tooltip_update_geometry();
207 tooltip_adjust_geometry();
208 XMoveResizeWindow(server.dsp, g_tooltip.window, x, y, width, height);
209
210 // Stuff for drawing the tooltip
211 cairo_surface_t *cs;
212 cairo_t *c;
213 PangoLayout* layout;
214 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
215 c = cairo_create(cs);
216 Color bc = g_tooltip.bg->back;
217 Border b = g_tooltip.bg->border;
218 if (real_transparency) {
219 clear_pixmap(g_tooltip.window, 0, 0, width, height);
220 draw_rect(c, b.width, b.width, width-2*b.width, height-2*b.width, b.rounded-b.width/1.571);
221 cairo_set_source_rgba(c, bc.color[0], bc.color[1], bc.color[2], bc.alpha);
222 }
223 else {
224 cairo_rectangle(c, 0., 0, width, height);
225 cairo_set_source_rgb(c, bc.color[0], bc.color[1], bc.color[2]);
226 }
227 cairo_fill(c);
228 cairo_set_line_width(c, b.width);
229 if (real_transparency) draw_rect(c, b.width/2.0, b.width/2.0, width - b.width, height - b.width, b.rounded);
230 else cairo_rectangle(c, b.width/2.0, b.width/2.0, width-b.width, height-b.width);
231 cairo_set_source_rgba(c, b.color[0], b.color[1], b.color[2], b.alpha);
232 cairo_stroke(c);
233
234 Color fc = g_tooltip.font_color;
235 cairo_set_source_rgba(c, fc.color[0], fc.color[1], fc.color[2], fc.alpha);
236 layout = pango_cairo_create_layout(c);
237 pango_layout_set_font_description(layout, g_tooltip.font_desc);
238 pango_layout_set_text(layout, g_tooltip.tooltip_text, -1);
239 PangoRectangle r1, r2;
240 pango_layout_get_pixel_extents(layout, &r1, &r2);
241 pango_layout_set_width(layout, width*PANGO_SCALE);
242 pango_layout_set_height(layout, height*PANGO_SCALE);
243 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
244 // 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.)
245 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);
246 pango_cairo_show_layout (c, layout);
247
248 g_object_unref (layout);
249 cairo_destroy (c);
250 cairo_surface_destroy (cs);
251 }
252
253
254 void tooltip_trigger_hide(Tooltip* tooltip)
255 {
256 if (g_tooltip.mapped) {
257 tooltip_copy_text(0);
258 start_hide_timeout();
259 }
260 else {
261 // tooltip not visible yet, but maybe a timeout is still pending
262 stop_tooltip_timeout();
263 }
264 }
265
266
267 void tooltip_hide(void* arg)
268 {
269 stop_tooltip_timeout();
270 if (g_tooltip.mapped) {
271 g_tooltip.mapped = False;
272 XUnmapWindow(server.dsp, g_tooltip.window);
273 XFlush(server.dsp);
274 }
275 }
276
277
278 void start_show_timeout()
279 {
280 if (g_tooltip.timeout)
281 change_timeout(g_tooltip.timeout, g_tooltip.show_timeout_msec, 0, tooltip_show, 0);
282 else
283 g_tooltip.timeout = add_timeout(g_tooltip.show_timeout_msec, 0, tooltip_show, 0);
284 }
285
286
287 void start_hide_timeout()
288 {
289 if (g_tooltip.timeout)
290 change_timeout(g_tooltip.timeout, g_tooltip.hide_timeout_msec, 0, tooltip_hide, 0);
291 else
292 g_tooltip.timeout = add_timeout(g_tooltip.hide_timeout_msec, 0, tooltip_hide, 0);
293 }
294
295
296 void stop_tooltip_timeout()
297 {
298 if (g_tooltip.timeout) {
299 stop_timeout(g_tooltip.timeout);
300 g_tooltip.timeout = 0;
301 }
302 }
303
304
305 void tooltip_copy_text(Area* area)
306 {
307 free(g_tooltip.tooltip_text);
308 if (area && area->_get_tooltip_text)
309 g_tooltip.tooltip_text = strdup(area->_get_tooltip_text(area));
310 else
311 g_tooltip.tooltip_text = 0;
312 g_tooltip.area = area;
313 }
This page took 0.049575 seconds and 5 git commands to generate.