]> Dogcows Code - chaz/tint2/blob - src/tooltip/tooltip.c
fixed tooltip_adjust_geometry with multi monitor
[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
27 // TODO: Use timer_create instead of setitimer, because SIGALRM is not the right signal for this...
28 // Reason: If we want to implement autohide we have to use another signal...
29
30 static int x, y, width, height;
31
32 // give the tooltip some reasonable default values
33 Tooltip g_tooltip = {
34 .task = 0,
35 .window = 0,
36 .show_timeout = { .it_interval={0, 0}, .it_value={0, 0} },
37 .hide_timeout = { .it_interval={0, 0}, .it_value={0, 0} },
38 .enabled = False,
39 .current_state = TOOLTIP_ABOUT_TO_HIDE,
40 .mapped = False,
41 .paddingx = 0,
42 .paddingy = 0,
43 .font_color = { .color={1, 1, 1}, .alpha=1 },
44 .background_color = { .color={0.5, 0.4, 0.5}, .alpha=1 },
45 .border = { .color={0, 0, 0}, .alpha=1, .width=1, .rounded=0 },
46 .font_desc = 0
47 };
48
49 void init_tooltip()
50 {
51 if (!g_tooltip.font_desc)
52 g_tooltip.font_desc = pango_font_description_from_string("sans 10");
53
54 XSetWindowAttributes attr;
55 attr.override_redirect = True;
56 attr.event_mask = ExposureMask;
57 if (g_tooltip.window) XDestroyWindow(server.dsp, g_tooltip.window);
58 g_tooltip.window = XCreateWindow(server.dsp, server.root_win, 0, 0, 100, 20, 0, server.depth, InputOutput, CopyFromParent, CWOverrideRedirect|CWEventMask, &attr);
59 }
60
61
62 void cleanup_tooltip()
63 {
64 alarm(0);
65 tooltip_hide();
66 g_tooltip.enabled = False;
67 g_tooltip.current_state = TOOLTIP_ABOUT_TO_HIDE;
68 if (g_tooltip.task) {
69 g_tooltip.task = 0;
70 }
71 if (g_tooltip.window) {
72 XDestroyWindow(server.dsp, g_tooltip.window);
73 g_tooltip.window = 0;
74 }
75 if (g_tooltip.font_desc) {
76 pango_font_description_free(g_tooltip.font_desc);
77 g_tooltip.font_desc = 0;
78 }
79 }
80
81
82 void tooltip_sighandler(int sig)
83 {
84 if (g_tooltip.current_state == TOOLTIP_ABOUT_TO_SHOW)
85 tooltip_show();
86 else if (g_tooltip.current_state == TOOLTIP_ABOUT_TO_HIDE)
87 tooltip_hide();
88 }
89
90
91 void tooltip_trigger_show(Task* task, int x_root, int y_root)
92 {
93 x = x_root;
94 y = y_root;
95
96 if (g_tooltip.mapped && g_tooltip.task != task) {
97 g_tooltip.task = task;
98 tooltip_update();
99 alarm(0);
100 }
101 else if (!g_tooltip.mapped) {
102 g_tooltip.current_state = TOOLTIP_ABOUT_TO_SHOW;
103 g_tooltip.task = task;
104 struct timeval t = g_tooltip.show_timeout.it_value;
105 if (t.tv_sec == 0 && t.tv_usec == 0) {
106 alarm(0);
107 tooltip_show();
108 }
109 else
110 setitimer(ITIMER_REAL, &g_tooltip.show_timeout, 0);
111 }
112 }
113
114
115 void tooltip_show()
116 {
117 if (!g_tooltip.mapped) {
118 g_tooltip.mapped = True;
119 XMapWindow(server.dsp, g_tooltip.window);
120 tooltip_update();
121 alarm(0);
122 }
123 }
124
125
126 void tooltip_update_geometry()
127 {
128 cairo_surface_t *cs;
129 cairo_t *c;
130 PangoLayout* layout;
131 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
132 c = cairo_create(cs);
133 layout = pango_cairo_create_layout(c);
134 pango_layout_set_font_description(layout, g_tooltip.font_desc);
135 pango_layout_set_text(layout, g_tooltip.task->title, -1);
136 PangoRectangle r1, r2;
137 pango_layout_get_pixel_extents(layout, &r1, &r2);
138 width = 2*g_tooltip.border.width + 2*g_tooltip.paddingx + r2.width;
139 height = 2*g_tooltip.border.width + 2*g_tooltip.paddingy + r2.height;
140
141 Panel* panel = g_tooltip.task->area.panel;
142 if (panel_horizontal && panel_position & BOTTOM)
143 y = panel->posy-height;
144 else if (panel_horizontal && panel_position & TOP)
145 y = panel->posy + panel->area.height;
146 else if (panel_position & LEFT)
147 x = panel->posx + panel->area.width;
148 else
149 x = panel->posx - width;
150
151 //printf("tooltip_update_geometry x_root %d, y_root %d\n", x, y);
152 //printf(" panel posx %d, posy %d\n", panel->posx, panel->posy);
153 g_object_unref(layout);
154 cairo_destroy(c);
155 cairo_surface_destroy(cs);
156 }
157
158
159 void tooltip_adjust_geometry()
160 {
161 // adjust coordinates and size to not go offscreen
162 // it seems quite impossible that the height needs to be adjusted, but we do it anyway.
163
164 int min_x, min_y, max_width, max_height;
165 Panel* panel = g_tooltip.task->area.panel;
166 int screen_width = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width;
167 int screen_height = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height;
168 if ( x+width <= screen_width && y+height <= screen_height && x>=0 && y>=0)
169 return; // no adjustment needed
170
171 if (panel_horizontal) {
172 min_x=0;
173 max_width=screen_width;
174 max_height=screen_height-panel->area.height;
175 if (panel_position & BOTTOM)
176 min_y=0;
177 else
178 min_y=panel->area.height;
179 }
180 else {
181 max_width=screen_width-panel->area.width;
182 min_y=0;
183 max_height=screen_height;
184 if (panel_position & LEFT)
185 min_x=panel->area.width;
186 else
187 min_x=0;
188 }
189
190 if (x+width > server.monitor[panel->monitor].x + server.monitor[panel->monitor].width)
191 x = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width - width;
192 if ( y+height > server.monitor[panel->monitor].y + server.monitor[panel->monitor].height)
193 y = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height - height;
194
195 if (x<min_x)
196 x=min_x;
197 if (width>max_width)
198 width = max_width;
199 if (y<min_y)
200 y=min_y;
201 if (height>max_height)
202 height=max_height;
203 //printf("tooltip_adjust_geometry x_root %d, y_root %d\n", x, y);
204 }
205
206 void tooltip_update()
207 {
208 if (!g_tooltip.task) {
209 tooltip_hide();
210 return;
211 }
212
213 tooltip_update_geometry();
214 tooltip_adjust_geometry();
215 XMoveResizeWindow(server.dsp, g_tooltip.window, x, y, width, height);
216
217 // Stuff for drawing the tooltip
218 cairo_surface_t *cs;
219 cairo_t *c;
220 PangoLayout* layout;
221 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
222 c = cairo_create(cs);
223 Color bc = g_tooltip.background_color;
224 cairo_rectangle(c, 0, 0, width, height);
225 cairo_set_source_rgb(c, bc.color[0], bc.color[1], bc.color[2]);
226 cairo_fill(c);
227 Border b = g_tooltip.border;
228 cairo_set_source_rgba(c, b.color[0], b.color[1], b.color[2], b.alpha);
229 cairo_set_line_width(c, b.width);
230 cairo_rectangle(c, b.width/2.0, b.width/2.0, width-b.width, height-b.width);
231 cairo_stroke(c);
232
233 config_color fc = g_tooltip.font_color;
234 cairo_set_source_rgba(c, fc.color[0], fc.color[1], fc.color[2], fc.alpha);
235 layout = pango_cairo_create_layout(c);
236 pango_layout_set_font_description(layout, g_tooltip.font_desc);
237 pango_layout_set_text(layout, g_tooltip.task->title, -1);
238 PangoRectangle r1, r2;
239 pango_layout_get_pixel_extents(layout, &r1, &r2);
240 pango_layout_set_width(layout, width*PANGO_SCALE);
241 pango_layout_set_height(layout, height*PANGO_SCALE);
242 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
243 // 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.)
244 cairo_move_to(c, -r1.x/2+g_tooltip.border.width+g_tooltip.paddingx, -r1.y/2+g_tooltip.border.width+g_tooltip.paddingy);
245 pango_cairo_show_layout (c, layout);
246
247 g_object_unref (layout);
248 cairo_destroy (c);
249 cairo_surface_destroy (cs);
250 }
251
252
253 void tooltip_trigger_hide(Tooltip* tooltip)
254 {
255 if (g_tooltip.mapped) {
256 g_tooltip.current_state = TOOLTIP_ABOUT_TO_HIDE;
257 struct timeval t = g_tooltip.hide_timeout.it_value;
258 g_tooltip.task = 0;
259 if (t.tv_sec == 0 && t.tv_usec == 0) {
260 tooltip_hide();
261 alarm(0);
262 }
263 else
264 setitimer(ITIMER_REAL, &g_tooltip.hide_timeout, 0);
265 }
266 else {
267 // tooltip not visible yet, but maybe an alarm is still pending
268 alarm(0);
269 }
270 }
271
272
273 void tooltip_hide()
274 {
275 if (g_tooltip.mapped) {
276 g_tooltip.mapped = False;
277 XUnmapWindow(server.dsp, g_tooltip.window);
278 }
279 }
This page took 0.048193 seconds and 5 git commands to generate.