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