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