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