]> Dogcows Code - chaz/tint2/blob - src/tooltip/tooltip.c
05da0d03588a7aa9c2647431607641bf0d835af1
[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 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 = 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;
83 if (!panel_horizontal)
84 y -= height/2;
85 g_tooltip.panel = p;
86 if (g_tooltip.mapped && g_tooltip.area != area) {
87 tooltip_copy_text(area);
88 tooltip_update();
89 stop_tooltip_timeout();
90 }
91 else if (!g_tooltip.mapped) {
92 start_show_timeout();
93 }
94 }
95
96
97 void tooltip_show(void* arg)
98 {
99 int mx, my;
100 Window w;
101 XTranslateCoordinates( server.dsp, server.root_win, g_tooltip.panel->main_win, x, y, &mx, &my, &w);
102 Area* area;
103 if (!panel_horizontal)
104 my += height/2; /* we adjusted y in tooltip_trigger_show, revert or we won't find the correct area anymore */
105 area = click_area(g_tooltip.panel, mx, my);
106 stop_tooltip_timeout();
107 if (!g_tooltip.mapped && area->_get_tooltip_text) {
108 tooltip_copy_text(area);
109 g_tooltip.mapped = True;
110 XMapWindow(server.dsp, g_tooltip.window);
111 tooltip_update();
112 XFlush(server.dsp);
113 }
114 }
115
116
117 void tooltip_update_geometry()
118 {
119 cairo_surface_t *cs;
120 cairo_t *c;
121 PangoLayout* layout;
122 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
123 c = cairo_create(cs);
124 layout = pango_cairo_create_layout(c);
125 pango_layout_set_font_description(layout, g_tooltip.font_desc);
126 pango_layout_set_text(layout, g_tooltip.tooltip_text, -1);
127 PangoRectangle r1, r2;
128 pango_layout_get_pixel_extents(layout, &r1, &r2);
129 width = 2*g_tooltip.bg->border.width + 2*g_tooltip.paddingx + r2.width;
130 height = 2*g_tooltip.bg->border.width + 2*g_tooltip.paddingy + r2.height;
131
132 Panel* panel = g_tooltip.panel;
133 if (panel_horizontal && panel_position & BOTTOM)
134 y = panel->posy-height;
135 else if (panel_horizontal && panel_position & TOP)
136 y = panel->posy + panel->area.height;
137 else if (panel_position & LEFT)
138 x = panel->posx + panel->area.width;
139 else
140 x = panel->posx - width;
141
142 g_object_unref(layout);
143 cairo_destroy(c);
144 cairo_surface_destroy(cs);
145 }
146
147
148 void tooltip_adjust_geometry()
149 {
150 // adjust coordinates and size to not go offscreen
151 // it seems quite impossible that the height needs to be adjusted, but we do it anyway.
152
153 int min_x, min_y, max_width, max_height;
154 Panel* panel = g_tooltip.panel;
155 int screen_width = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width;
156 int screen_height = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height;
157 if ( x+width <= screen_width && y+height <= screen_height && x>=server.monitor[panel->monitor].x && y>=server.monitor[panel->monitor].y )
158 return; // no adjustment needed
159
160 if (panel_horizontal) {
161 min_x=0;
162 max_width=server.monitor[panel->monitor].width;
163 max_height=server.monitor[panel->monitor].height-panel->area.height;
164 if (panel_position & BOTTOM)
165 min_y=0;
166 else
167 min_y=panel->area.height;
168 }
169 else {
170 max_width=server.monitor[panel->monitor].width-panel->area.width;
171 min_y=0;
172 max_height=server.monitor[panel->monitor].height;
173 if (panel_position & LEFT)
174 min_x=panel->area.width;
175 else
176 min_x=0;
177 }
178
179 if (x+width > server.monitor[panel->monitor].x + server.monitor[panel->monitor].width)
180 x = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width - width;
181 if ( y+height > server.monitor[panel->monitor].y + server.monitor[panel->monitor].height)
182 y = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height - height;
183
184 if (x<min_x)
185 x=min_x;
186 if (width>max_width)
187 width = max_width;
188 if (y<min_y)
189 y=min_y;
190 if (height>max_height)
191 height=max_height;
192 }
193
194 void tooltip_update()
195 {
196 if (!g_tooltip.tooltip_text) {
197 tooltip_hide(0);
198 return;
199 }
200
201 tooltip_update_geometry();
202 tooltip_adjust_geometry();
203 XMoveResizeWindow(server.dsp, g_tooltip.window, x, y, width, height);
204
205 // Stuff for drawing the tooltip
206 cairo_surface_t *cs;
207 cairo_t *c;
208 PangoLayout* layout;
209 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
210 c = cairo_create(cs);
211 Color bc = g_tooltip.bg->back;
212 Border b = g_tooltip.bg->border;
213 if (server.real_transparency) {
214 clear_pixmap(g_tooltip.window, 0, 0, width, height);
215 draw_rect(c, b.width, b.width, width-2*b.width, height-2*b.width, b.rounded-b.width/1.571);
216 cairo_set_source_rgba(c, bc.color[0], bc.color[1], bc.color[2], bc.alpha);
217 }
218 else {
219 cairo_rectangle(c, 0., 0, width, height);
220 cairo_set_source_rgb(c, bc.color[0], bc.color[1], bc.color[2]);
221 }
222 cairo_fill(c);
223 cairo_set_line_width(c, b.width);
224 if (server.real_transparency)
225 draw_rect(c, b.width/2.0, b.width/2.0, width - b.width, height - b.width, b.rounded);
226 else
227 cairo_rectangle(c, b.width/2.0, b.width/2.0, width-b.width, height-b.width);
228 cairo_set_source_rgba(c, b.color[0], b.color[1], b.color[2], b.alpha);
229 cairo_stroke(c);
230
231 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.tooltip_text, -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.bg->border.width+g_tooltip.paddingx, -r1.y/2+g_tooltip.bg->border.width+g_tooltip.paddingy);
243 pango_cairo_show_layout (c, layout);
244
245 g_object_unref (layout);
246 cairo_destroy (c);
247 cairo_surface_destroy (cs);
248 }
249
250
251 void tooltip_trigger_hide(Tooltip* tooltip)
252 {
253 if (g_tooltip.mapped) {
254 tooltip_copy_text(0);
255 start_hide_timeout();
256 }
257 else {
258 // tooltip not visible yet, but maybe a timeout is still pending
259 stop_tooltip_timeout();
260 }
261 }
262
263
264 void tooltip_hide(void* arg)
265 {
266 stop_tooltip_timeout();
267 if (g_tooltip.mapped) {
268 g_tooltip.mapped = False;
269 XUnmapWindow(server.dsp, g_tooltip.window);
270 XFlush(server.dsp);
271 }
272 }
273
274
275 void start_show_timeout()
276 {
277 if (g_tooltip.timeout)
278 change_timeout(g_tooltip.timeout, g_tooltip.show_timeout_msec, 0, tooltip_show, 0);
279 else
280 g_tooltip.timeout = add_timeout(g_tooltip.show_timeout_msec, 0, tooltip_show, 0);
281 }
282
283
284 void start_hide_timeout()
285 {
286 if (g_tooltip.timeout)
287 change_timeout(g_tooltip.timeout, g_tooltip.hide_timeout_msec, 0, tooltip_hide, 0);
288 else
289 g_tooltip.timeout = add_timeout(g_tooltip.hide_timeout_msec, 0, tooltip_hide, 0);
290 }
291
292
293 void stop_tooltip_timeout()
294 {
295 if (g_tooltip.timeout) {
296 stop_timeout(g_tooltip.timeout);
297 g_tooltip.timeout = 0;
298 }
299 }
300
301
302 void tooltip_copy_text(Area* area)
303 {
304 free(g_tooltip.tooltip_text);
305 if (area && area->_get_tooltip_text)
306 g_tooltip.tooltip_text = strdup(area->_get_tooltip_text(area));
307 else
308 g_tooltip.tooltip_text = 0;
309 g_tooltip.area = area;
310 }
This page took 0.041576 seconds and 3 git commands to generate.