]> Dogcows Code - chaz/tint2/blob - src/tooltip/tooltip.c
cleanup
[chaz/tint2] / src / tooltip / tooltip.c
1 /**************************************************************************
2 *
3 * Copyright (C) 2009 Andreas.Fink85 ()
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 tooltip_sighandler(int sig)
63 {
64 if (g_tooltip.current_state == TOOLTIP_ABOUT_TO_SHOW)
65 tooltip_show();
66 else if (g_tooltip.current_state == TOOLTIP_ABOUT_TO_HIDE)
67 tooltip_hide();
68 }
69
70
71 void tooltip_trigger_show(Task* task, int x_root, int y_root)
72 {
73 x = x_root;
74 y = y_root;
75
76 if (g_tooltip.mapped && g_tooltip.task != task) {
77 g_tooltip.task = task;
78 tooltip_update();
79 alarm(0);
80 }
81 else if (!g_tooltip.mapped) {
82 g_tooltip.current_state = TOOLTIP_ABOUT_TO_SHOW;
83 g_tooltip.task = task;
84 struct timeval t = g_tooltip.show_timeout.it_value;
85 if (t.tv_sec == 0 && t.tv_usec == 0)
86 tooltip_show();
87 else
88 setitimer(ITIMER_REAL, &g_tooltip.show_timeout, 0);
89 }
90 }
91
92
93 void tooltip_show()
94 {
95 if (!g_tooltip.mapped) {
96 g_tooltip.mapped = True;
97 XMapWindow(server.dsp, g_tooltip.window);
98 tooltip_update();
99 alarm(0);
100 }
101 }
102
103
104 void tooltip_update_geometry()
105 {
106 cairo_surface_t *cs;
107 cairo_t *c;
108 PangoLayout* layout;
109 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
110 c = cairo_create(cs);
111 layout = pango_cairo_create_layout(c);
112 pango_layout_set_font_description(layout, g_tooltip.font_desc);
113 pango_layout_set_text(layout, g_tooltip.task->title, -1);
114 PangoRectangle r1, r2;
115 pango_layout_get_pixel_extents(layout, &r1, &r2);
116 width = 2*g_tooltip.border.width + 2*g_tooltip.paddingx + r2.width;
117 height = 2*g_tooltip.border.width + 2*g_tooltip.paddingy + r2.height;
118
119 Panel* panel = g_tooltip.task->area.panel;
120 if (panel_horizontal && panel_position & BOTTOM)
121 y = panel->posy-height;
122 else if (panel_horizontal && panel_position & TOP)
123 y = panel->posy + panel->area.height;
124 else if (panel_position & LEFT)
125 x = panel->posx + panel->area.width;
126 else
127 x = panel->posx - width;
128 g_object_unref(layout);
129 cairo_destroy(c);
130 cairo_surface_destroy(cs);
131 }
132
133
134 void tooltip_adjust_geometry()
135 {
136 // adjust coordinates and size to not go offscreen
137 // it seems quite impossible that the height needs to be adjusted, but we do it anyway.
138
139 int min_x, min_y, max_width, max_height;
140 Panel* panel = g_tooltip.task->area.panel;
141 int screen_width = server.monitor[panel->monitor].width;
142 int screen_height = server.monitor[panel->monitor].height;
143 if ( x+width <= screen_width && y+height <= screen_height && x>=0 && y>=0)
144 return; // no adjustment needed
145
146 if (panel_horizontal) {
147 min_x=0;
148 max_width=screen_width;
149 max_height=screen_height-panel->area.height;
150 if (panel_position & BOTTOM)
151 min_y=0;
152 else
153 min_y=panel->area.height;
154 }
155 else {
156 max_width=screen_width-panel->area.width;
157 min_y=0;
158 max_height=screen_height;
159 if (panel_position & LEFT)
160 min_x=panel->area.width;
161 else
162 min_x=0;
163 }
164
165 if (x+width > server.monitor[panel->monitor].width)
166 x = server.monitor[panel->monitor].width-width;
167 if ( y+height>server.monitor[panel->monitor].height)
168 y = server.monitor[panel->monitor].height-height;
169
170 if (x<min_x)
171 x=min_x;
172 if (width>max_width)
173 width = max_width;
174 if (y<min_y)
175 y=min_y;
176 if (height>max_height)
177 height=max_height;
178 }
179
180 void tooltip_update()
181 {
182 if (!g_tooltip.task) {
183 tooltip_hide();
184 return;
185 }
186
187 tooltip_update_geometry();
188 tooltip_adjust_geometry();
189 XMoveResizeWindow(server.dsp, g_tooltip.window, x, y, width, height);
190
191 // Stuff for drawing the tooltip
192 cairo_surface_t *cs;
193 cairo_t *c;
194 PangoLayout* layout;
195 cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
196 c = cairo_create(cs);
197 Color bc = g_tooltip.background_color;
198 cairo_rectangle(c, 0, 0, width, height);
199 cairo_set_source_rgb(c, bc.color[0], bc.color[1], bc.color[2]);
200 cairo_fill(c);
201 Border b = g_tooltip.border;
202 cairo_set_source_rgba(c, b.color[0], b.color[1], b.color[2], b.alpha);
203 cairo_set_line_width(c, b.width);
204 cairo_rectangle(c, b.width/2.0, b.width/2.0, width-b.width, height-b.width);
205 cairo_stroke(c);
206
207 config_color fc = g_tooltip.font_color;
208 cairo_set_source_rgba(c, fc.color[0], fc.color[1], fc.color[2], fc.alpha);
209 layout = pango_cairo_create_layout(c);
210 pango_layout_set_font_description(layout, g_tooltip.font_desc);
211 pango_layout_set_text(layout, g_tooltip.task->title, -1);
212 PangoRectangle r1, r2;
213 pango_layout_get_pixel_extents(layout, &r1, &r2);
214 pango_layout_set_width(layout, width*PANGO_SCALE);
215 pango_layout_set_height(layout, height*PANGO_SCALE);
216 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
217 // 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.)
218 cairo_move_to(c, -r1.x/2+g_tooltip.border.width+g_tooltip.paddingx, -r1.y/2+g_tooltip.border.width+g_tooltip.paddingy);
219 pango_cairo_show_layout (c, layout);
220
221 g_object_unref (layout);
222 cairo_destroy (c);
223 cairo_surface_destroy (cs);
224 }
225
226
227 void tooltip_trigger_hide(Tooltip* tooltip)
228 {
229 if (g_tooltip.mapped) {
230 g_tooltip.current_state = TOOLTIP_ABOUT_TO_HIDE;
231 struct timeval t = g_tooltip.hide_timeout.it_value;
232 if (t.tv_sec == 0 && t.tv_usec == 0)
233 tooltip_hide();
234 else
235 setitimer(ITIMER_REAL, &g_tooltip.hide_timeout, 0);
236 }
237 else {
238 // tooltip not visible yet, but maybe an alarm is still pending
239 alarm(0);
240 }
241 }
242
243
244 void tooltip_hide()
245 {
246 if (g_tooltip.mapped) {
247 g_tooltip.mapped = False;
248 XUnmapWindow(server.dsp, g_tooltip.window);
249 }
250 g_tooltip.task = 0;
251 }
This page took 0.0489 seconds and 4 git commands to generate.