]> Dogcows Code - chaz/tint2/blob - src/tooltip/tooltip.c
157b65cd13376fe8462810798468a5f3a219e025
[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 tooltip_hide();
65 g_tooltip.enabled = False;
66 if (g_tooltip.task) {
67 alarm(0);
68 g_tooltip.task = 0;
69 }
70 if (g_tooltip.window) {
71 XDestroyWindow(server.dsp, g_tooltip.window);
72 g_tooltip.window = 0;
73 }
74 if (g_tooltip.font_desc) {
75 pango_font_description_free(g_tooltip.font_desc);
76 g_tooltip.font_desc = 0;
77 }
78 }
79
80
81 void tooltip_sighandler(int sig)
82 {
83 if (g_tooltip.current_state == TOOLTIP_ABOUT_TO_SHOW)
84 tooltip_show();
85 else if (g_tooltip.current_state == TOOLTIP_ABOUT_TO_HIDE)
86 tooltip_hide();
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 alarm(0);
99 }
100 else if (!g_tooltip.mapped) {
101 g_tooltip.current_state = TOOLTIP_ABOUT_TO_SHOW;
102 g_tooltip.task = task;
103 struct timeval t = g_tooltip.show_timeout.it_value;
104 if (t.tv_sec == 0 && t.tv_usec == 0) {
105 alarm(0);
106 tooltip_show();
107 }
108 else
109 setitimer(ITIMER_REAL, &g_tooltip.show_timeout, 0);
110 }
111 }
112
113
114 void tooltip_show()
115 {
116 if (!g_tooltip.mapped) {
117 g_tooltip.mapped = True;
118 XMapWindow(server.dsp, g_tooltip.window);
119 tooltip_update();
120 alarm(0);
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.task->title, -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.task->area.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 g_object_unref(layout);
150 cairo_destroy(c);
151 cairo_surface_destroy(cs);
152 }
153
154
155 void tooltip_adjust_geometry()
156 {
157 // adjust coordinates and size to not go offscreen
158 // it seems quite impossible that the height needs to be adjusted, but we do it anyway.
159
160 int min_x, min_y, max_width, max_height;
161 Panel* panel = g_tooltip.task->area.panel;
162 int screen_width = server.monitor[panel->monitor].width;
163 int screen_height = server.monitor[panel->monitor].height;
164 if ( x+width <= screen_width && y+height <= screen_height && x>=0 && y>=0)
165 return; // no adjustment needed
166
167 if (panel_horizontal) {
168 min_x=0;
169 max_width=screen_width;
170 max_height=screen_height-panel->area.height;
171 if (panel_position & BOTTOM)
172 min_y=0;
173 else
174 min_y=panel->area.height;
175 }
176 else {
177 max_width=screen_width-panel->area.width;
178 min_y=0;
179 max_height=screen_height;
180 if (panel_position & LEFT)
181 min_x=panel->area.width;
182 else
183 min_x=0;
184 }
185
186 if (x+width > server.monitor[panel->monitor].width)
187 x = server.monitor[panel->monitor].width-width;
188 if ( y+height>server.monitor[panel->monitor].height)
189 y = server.monitor[panel->monitor].height-height;
190
191 if (x<min_x)
192 x=min_x;
193 if (width>max_width)
194 width = max_width;
195 if (y<min_y)
196 y=min_y;
197 if (height>max_height)
198 height=max_height;
199 }
200
201 void tooltip_update()
202 {
203 if (!g_tooltip.task) {
204 tooltip_hide();
205 return;
206 }
207
208 tooltip_update_geometry();
209 tooltip_adjust_geometry();
210 XMoveResizeWindow(server.dsp, g_tooltip.window, x, y, width, height);
211
212 // Stuff for drawing the tooltip
213 cairo_surface_t *cs;
214 cairo_t *c;
215 PangoLayout* layout;
216 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
217 c = cairo_create(cs);
218 Color bc = g_tooltip.background_color;
219 cairo_rectangle(c, 0, 0, width, height);
220 cairo_set_source_rgb(c, bc.color[0], bc.color[1], bc.color[2]);
221 cairo_fill(c);
222 Border b = g_tooltip.border;
223 cairo_set_source_rgba(c, b.color[0], b.color[1], b.color[2], b.alpha);
224 cairo_set_line_width(c, b.width);
225 cairo_rectangle(c, b.width/2.0, b.width/2.0, width-b.width, height-b.width);
226 cairo_stroke(c);
227
228 config_color fc = g_tooltip.font_color;
229 cairo_set_source_rgba(c, fc.color[0], fc.color[1], fc.color[2], fc.alpha);
230 layout = pango_cairo_create_layout(c);
231 pango_layout_set_font_description(layout, g_tooltip.font_desc);
232 pango_layout_set_text(layout, g_tooltip.task->title, -1);
233 PangoRectangle r1, r2;
234 pango_layout_get_pixel_extents(layout, &r1, &r2);
235 pango_layout_set_width(layout, width*PANGO_SCALE);
236 pango_layout_set_height(layout, height*PANGO_SCALE);
237 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
238 // 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.)
239 cairo_move_to(c, -r1.x/2+g_tooltip.border.width+g_tooltip.paddingx, -r1.y/2+g_tooltip.border.width+g_tooltip.paddingy);
240 pango_cairo_show_layout (c, layout);
241
242 g_object_unref (layout);
243 cairo_destroy (c);
244 cairo_surface_destroy (cs);
245 }
246
247
248 void tooltip_trigger_hide(Tooltip* tooltip)
249 {
250 if (g_tooltip.mapped) {
251 g_tooltip.current_state = TOOLTIP_ABOUT_TO_HIDE;
252 struct timeval t = g_tooltip.hide_timeout.it_value;
253 g_tooltip.task = 0;
254 if (t.tv_sec == 0 && t.tv_usec == 0) {
255 tooltip_hide();
256 alarm(0);
257 }
258 else
259 setitimer(ITIMER_REAL, &g_tooltip.hide_timeout, 0);
260 }
261 else {
262 // tooltip not visible yet, but maybe an alarm is still pending
263 alarm(0);
264 }
265 }
266
267
268 void tooltip_hide()
269 {
270 if (g_tooltip.mapped) {
271 g_tooltip.mapped = False;
272 XUnmapWindow(server.dsp, g_tooltip.window);
273 }
274 }
This page took 0.042662 seconds and 3 git commands to generate.