]> Dogcows Code - chaz/tint2/blob - src/tooltip/tooltip.c
*fix* merged tooltip to the new timer syntax
[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 .task = 0,
38 .window = 0,
39 .show_timeout = { 0, 0 },
40 .hide_timeout = { 0, 0 },
41 .enabled = False,
42 .mapped = False,
43 .paddingx = 0,
44 .paddingy = 0,
45 .font_color = { .color={1, 1, 1}, .alpha=1 },
46 .background_color = { .color={0.5, 0.4, 0.5}, .alpha=1 },
47 .border = { .color={0, 0, 0}, .alpha=1, .width=1, .rounded=0 },
48 .font_desc = 0,
49 .show_timer_id = 0,
50 .hide_timer_id = 0
51 };
52
53 void init_tooltip()
54 {
55 if (!g_tooltip.font_desc)
56 g_tooltip.font_desc = pango_font_description_from_string("sans 10");
57
58 if (g_tooltip.show_timer_id == 0)
59 g_tooltip.show_timer_id = install_timer(0, 0, 0, 0, tooltip_show);
60 if (g_tooltip.hide_timer_id == 0)
61 g_tooltip.hide_timer_id = install_timer(0, 0, 0, 0, tooltip_hide);
62
63 XSetWindowAttributes attr;
64 attr.override_redirect = True;
65 attr.event_mask = ExposureMask;
66 if (g_tooltip.window) XDestroyWindow(server.dsp, g_tooltip.window);
67 g_tooltip.window = XCreateWindow(server.dsp, server.root_win, 0, 0, 100, 20, 0, server.depth, InputOutput, CopyFromParent, CWOverrideRedirect|CWEventMask, &attr);
68 }
69
70
71 void cleanup_tooltip()
72 {
73 stop_timeouts();
74 tooltip_hide();
75 g_tooltip.enabled = False;
76 if (g_tooltip.task) {
77 g_tooltip.task = 0;
78 }
79 if (g_tooltip.window) {
80 XDestroyWindow(server.dsp, g_tooltip.window);
81 g_tooltip.window = 0;
82 }
83 if (g_tooltip.font_desc) {
84 pango_font_description_free(g_tooltip.font_desc);
85 g_tooltip.font_desc = 0;
86 }
87 }
88
89
90 void tooltip_trigger_show(Task* task, int x_root, int y_root)
91 {
92 x = x_root;
93 y = y_root;
94
95 if (g_tooltip.mapped && g_tooltip.task != task) {
96 g_tooltip.task = task;
97 tooltip_update();
98 stop_timeouts();
99 }
100 else if (!g_tooltip.mapped) {
101 g_tooltip.task = task;
102 start_show_timeout();
103 }
104 }
105
106
107 void tooltip_show()
108 {
109 if (!g_tooltip.mapped) {
110 g_tooltip.mapped = True;
111 XMapWindow(server.dsp, g_tooltip.window);
112 //tooltip_update();
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.task->title, -1);
127 PangoRectangle r1, r2;
128 pango_layout_get_pixel_extents(layout, &r1, &r2);
129 width = 2*g_tooltip.border.width + 2*g_tooltip.paddingx + r2.width;
130 height = 2*g_tooltip.border.width + 2*g_tooltip.paddingy + r2.height;
131
132 Panel* panel = g_tooltip.task->area.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.task->area.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>=0 && y>=0)
158 return; // no adjustment needed
159
160 if (panel_horizontal) {
161 min_x=0;
162 max_width=screen_width;
163 max_height=screen_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=screen_width-panel->area.width;
171 min_y=0;
172 max_height=screen_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.task) {
197 tooltip_hide();
198 return;
199 }
200
201 //printf("tooltip_update\n");
202 tooltip_update_geometry();
203 tooltip_adjust_geometry();
204 XMoveResizeWindow(server.dsp, g_tooltip.window, x, y, width, height);
205
206 // Stuff for drawing the tooltip
207 cairo_surface_t *cs;
208 cairo_t *c;
209 PangoLayout* layout;
210 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
211 c = cairo_create(cs);
212 Color bc = g_tooltip.background_color;
213 cairo_rectangle(c, 0, 0, width, height);
214 cairo_set_source_rgb(c, bc.color[0], bc.color[1], bc.color[2]);
215 cairo_fill(c);
216 Border b = g_tooltip.border;
217 cairo_set_source_rgba(c, b.color[0], b.color[1], b.color[2], b.alpha);
218 cairo_set_line_width(c, b.width);
219 cairo_rectangle(c, b.width/2.0, b.width/2.0, width-b.width, height-b.width);
220 cairo_stroke(c);
221
222 config_color fc = g_tooltip.font_color;
223 cairo_set_source_rgba(c, fc.color[0], fc.color[1], fc.color[2], fc.alpha);
224 layout = pango_cairo_create_layout(c);
225 pango_layout_set_font_description(layout, g_tooltip.font_desc);
226 pango_layout_set_text(layout, g_tooltip.task->title, -1);
227 PangoRectangle r1, r2;
228 pango_layout_get_pixel_extents(layout, &r1, &r2);
229 pango_layout_set_width(layout, width*PANGO_SCALE);
230 pango_layout_set_height(layout, height*PANGO_SCALE);
231 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
232 // 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.)
233 cairo_move_to(c, -r1.x/2+g_tooltip.border.width+g_tooltip.paddingx, -r1.y/2+g_tooltip.border.width+g_tooltip.paddingy);
234 pango_cairo_show_layout (c, layout);
235
236 g_object_unref (layout);
237 cairo_destroy (c);
238 cairo_surface_destroy (cs);
239 }
240
241
242 void tooltip_trigger_hide(Tooltip* tooltip)
243 {
244 if (g_tooltip.mapped) {
245 g_tooltip.task = 0;
246 start_hide_timeout();
247 }
248 else {
249 // tooltip not visible yet, but maybe a timeout is still pending
250 stop_timeouts();
251 }
252 }
253
254
255 void tooltip_hide()
256 {
257 if (g_tooltip.mapped) {
258 g_tooltip.mapped = False;
259 XUnmapWindow(server.dsp, g_tooltip.window);
260 }
261 }
262
263
264 void start_show_timeout()
265 {
266 reset_timer(g_tooltip.hide_timer_id, 0, 0, 0, 0);
267 struct timespec t = g_tooltip.show_timeout;
268 if (t.tv_sec == 0 && t.tv_nsec == 0)
269 tooltip_show();
270 else
271 reset_timer(g_tooltip.show_timer_id, t.tv_sec, t.tv_nsec, 0, 0);
272 }
273
274
275 void start_hide_timeout()
276 {
277 reset_timer(g_tooltip.show_timer_id, 0, 0, 0, 0);
278 struct timespec t = g_tooltip.hide_timeout;
279 if (t.tv_sec == 0 && t.tv_nsec == 0)
280 tooltip_hide();
281 else
282 reset_timer(g_tooltip.hide_timer_id, t.tv_sec, t.tv_nsec, 0, 0);
283 }
284
285 void stop_timeouts()
286 {
287 reset_timer(g_tooltip.show_timer_id, 0, 0, 0, 0);
288 reset_timer(g_tooltip.hide_timer_id, 0, 0, 0, 0);
289 }
This page took 0.046118 seconds and 4 git commands to generate.