]> Dogcows Code - chaz/tint2/blob - src/tooltip/tooltip.c
61e21b50b1381a64f842c7c53db1f2c28555efbb
[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 <cairo.h>
21 #include <cairo-xlib.h>
22
23 #include "server.h"
24 #include "tooltip.h"
25 #include "panel.h"
26 #include "timer.h"
27
28 static int x, y, width, height;
29
30 // the next functions are helper functions for tooltip handling
31 void start_show_timeout();
32 void start_hide_timeout();
33 void stop_timeouts();
34
35 // give the tooltip some reasonable default values
36 Tooltip g_tooltip = {
37 .area = 0,
38 .panel = 0,
39 .window = 0,
40 .show_timeout = { 0, 0 },
41 .hide_timeout = { 0, 0 },
42 .enabled = False,
43 .mapped = False,
44 .paddingx = 0,
45 .paddingy = 0,
46 .font_color = { .color={1, 1, 1}, .alpha=1 },
47 .background_color = { .color={0.5, 0.4, 0.5}, .alpha=1 },
48 .border = { .color={0, 0, 0}, .alpha=1, .width=1, .rounded=0 },
49 .font_desc = 0,
50 .show_timer_id = 0,
51 .hide_timer_id = 0
52 };
53
54 void init_tooltip()
55 {
56 if (!g_tooltip.font_desc)
57 g_tooltip.font_desc = pango_font_description_from_string("sans 10");
58
59 if (g_tooltip.show_timer_id == 0)
60 g_tooltip.show_timer_id = install_timer(0, 0, 0, 0, tooltip_show);
61 if (g_tooltip.hide_timer_id == 0)
62 g_tooltip.hide_timer_id = install_timer(0, 0, 0, 0, tooltip_hide);
63
64 XSetWindowAttributes attr;
65 attr.override_redirect = True;
66 attr.event_mask = StructureNotifyMask;
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, CopyFromParent, CWOverrideRedirect|CWEventMask, &attr);
69 }
70
71
72 void cleanup_tooltip()
73 {
74 stop_timeouts();
75 tooltip_hide();
76 g_tooltip.enabled = False;
77 g_tooltip.area = 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 g_tooltip.area = area;
96 tooltip_update();
97 stop_timeouts();
98 }
99 else if (!g_tooltip.mapped) {
100 start_show_timeout();
101 }
102 }
103
104
105 void tooltip_show()
106 {
107 Area* area = click_area(g_tooltip.panel, x, y);
108 stop_timeouts();
109 if (!g_tooltip.mapped && area->_get_tooltip_text) {
110 g_tooltip.area = area;
111 g_tooltip.mapped = True;
112 XMapWindow(server.dsp, g_tooltip.window);
113 XFlush(server.dsp);
114 }
115 }
116
117
118 void tooltip_update_geometry()
119 {
120 cairo_surface_t *cs;
121 cairo_t *c;
122 PangoLayout* layout;
123 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
124 c = cairo_create(cs);
125 layout = pango_cairo_create_layout(c);
126 pango_layout_set_font_description(layout, g_tooltip.font_desc);
127 pango_layout_set_text(layout, g_tooltip.area->_get_tooltip_text(g_tooltip.area), -1);
128 PangoRectangle r1, r2;
129 pango_layout_get_pixel_extents(layout, &r1, &r2);
130 width = 2*g_tooltip.border.width + 2*g_tooltip.paddingx + r2.width;
131 height = 2*g_tooltip.border.width + 2*g_tooltip.paddingy + r2.height;
132
133 Panel* panel = g_tooltip.panel;
134 if (panel_horizontal && panel_position & BOTTOM)
135 y = panel->posy-height;
136 else if (panel_horizontal && panel_position & TOP)
137 y = panel->posy + panel->area.height;
138 else if (panel_position & LEFT)
139 x = panel->posx + panel->area.width;
140 else
141 x = panel->posx - width;
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.area->_get_tooltip_text) {
198 tooltip_hide();
199 return;
200 }
201
202 // printf("tooltip_update\n");
203 tooltip_update_geometry();
204 tooltip_adjust_geometry();
205 XMoveResizeWindow(server.dsp, g_tooltip.window, x, y, width, height);
206
207 // Stuff for drawing the tooltip
208 cairo_surface_t *cs;
209 cairo_t *c;
210 PangoLayout* layout;
211 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
212 c = cairo_create(cs);
213 Color bc = g_tooltip.background_color;
214 cairo_rectangle(c, 0, 0, width, height);
215 cairo_set_source_rgb(c, bc.color[0], bc.color[1], bc.color[2]);
216 cairo_fill(c);
217 Border b = g_tooltip.border;
218 cairo_set_source_rgba(c, b.color[0], b.color[1], b.color[2], b.alpha);
219 cairo_set_line_width(c, b.width);
220 cairo_rectangle(c, b.width/2.0, b.width/2.0, width-b.width, height-b.width);
221 cairo_stroke(c);
222
223 config_color fc = g_tooltip.font_color;
224 cairo_set_source_rgba(c, fc.color[0], fc.color[1], fc.color[2], fc.alpha);
225 layout = pango_cairo_create_layout(c);
226 pango_layout_set_font_description(layout, g_tooltip.font_desc);
227 pango_layout_set_text(layout, g_tooltip.area->_get_tooltip_text(g_tooltip.area), -1);
228 PangoRectangle r1, r2;
229 pango_layout_get_pixel_extents(layout, &r1, &r2);
230 pango_layout_set_width(layout, width*PANGO_SCALE);
231 pango_layout_set_height(layout, height*PANGO_SCALE);
232 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
233 // 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.)
234 cairo_move_to(c, -r1.x/2+g_tooltip.border.width+g_tooltip.paddingx, -r1.y/2+g_tooltip.border.width+g_tooltip.paddingy);
235 pango_cairo_show_layout (c, layout);
236
237 g_object_unref (layout);
238 cairo_destroy (c);
239 cairo_surface_destroy (cs);
240 }
241
242
243 void tooltip_trigger_hide(Tooltip* tooltip)
244 {
245 if (g_tooltip.mapped) {
246 g_tooltip.area = 0;
247 start_hide_timeout();
248 }
249 else {
250 // tooltip not visible yet, but maybe a timeout is still pending
251 stop_timeouts();
252 }
253 }
254
255
256 void tooltip_hide()
257 {
258 stop_timeouts();
259 if (g_tooltip.mapped) {
260 g_tooltip.mapped = False;
261 XUnmapWindow(server.dsp, g_tooltip.window);
262 XFlush(server.dsp);
263 }
264 }
265
266
267 void start_show_timeout()
268 {
269 reset_timer(g_tooltip.hide_timer_id, 0, 0, 0, 0);
270 struct timespec t = g_tooltip.show_timeout;
271 if (t.tv_sec == 0 && t.tv_nsec == 0)
272 tooltip_show();
273 else
274 reset_timer(g_tooltip.show_timer_id, t.tv_sec, t.tv_nsec, 0, 0);
275 }
276
277
278 void start_hide_timeout()
279 {
280 reset_timer(g_tooltip.show_timer_id, 0, 0, 0, 0);
281 struct timespec t = g_tooltip.hide_timeout;
282 if (t.tv_sec == 0 && t.tv_nsec == 0)
283 tooltip_hide();
284 else
285 reset_timer(g_tooltip.hide_timer_id, t.tv_sec, t.tv_nsec, 0, 0);
286 }
287
288 void stop_timeouts()
289 {
290 reset_timer(g_tooltip.show_timer_id, 0, 0, 0, 0);
291 reset_timer(g_tooltip.hide_timer_id, 0, 0, 0, 0);
292 }
This page took 0.044723 seconds and 3 git commands to generate.