]> Dogcows Code - chaz/tint2/blob - src/panel.c
*add* tooltips
[chaz/tint2] / src / panel.c
1 /**************************************************************************
2 *
3 * Copyright (C) 2008 Pål Staurland (staura@gmail.com)
4 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 **************************************************************************/
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
24 #include <cairo.h>
25 #include <cairo-xlib.h>
26 #include <pango/pangocairo.h>
27
28 #include "server.h"
29 #include "window.h"
30 #include "task.h"
31 #include "panel.h"
32
33
34 int signal_pending;
35 // --------------------------------------------------
36 // mouse events
37 int mouse_middle;
38 int mouse_right;
39 int mouse_scroll_up;
40 int mouse_scroll_down;
41 int mouse_tilt_left;
42 int mouse_tilt_right;
43
44 int panel_mode;
45 int wm_menu;
46 int panel_dock=0; // default not in the dock
47 int panel_position;
48 int panel_horizontal;
49 int panel_refresh;
50
51 Task *task_active;
52 Task *task_drag;
53 Task *task_urgent;
54 int tick_urgent;
55 int max_tick_urgent;
56
57 Panel *panel1 = 0;
58 int nb_panel;
59
60
61
62 void init_panel()
63 {
64 int i;
65 Panel *p;
66
67 for (i=0 ; i < nb_panel ; i++) {
68 p = &panel1[i];
69
70 p->area.parent = p;
71 p->area.panel = p;
72 p->area.on_screen = 1;
73 p->area.resize = 1;
74 p->area._resize = resize_panel;
75 p->g_taskbar.parent = p;
76 p->g_taskbar.panel = p;
77 p->g_task.area.panel = p;
78
79 // add childs
80 if (p->clock.area.on_screen)
81 p->area.list = g_slist_append(p->area.list, &p->clock);
82 #ifdef ENABLE_BATTERY
83 if (p->battery.area.on_screen)
84 p->area.list = g_slist_append(p->area.list, &p->battery);
85 #endif
86 // systray only on first panel
87 if (systray.area.on_screen && i == 0)
88 p->area.list = g_slist_append(p->area.list, &systray);
89
90 // full width mode
91 if (!p->initial_width) {
92 p->initial_width = 100;
93 p->pourcentx = 1;
94 }
95
96 // detect panel size
97 if (panel_horizontal) {
98 if (p->pourcentx)
99 p->area.width = (float)server.monitor[p->monitor].width * p->initial_width / 100;
100 else
101 p->area.width = p->initial_width;
102 if (p->pourcenty)
103 p->area.height = (float)server.monitor[p->monitor].height * p->initial_height / 100;
104 else
105 p->area.height = p->initial_height;
106 if (p->area.pix.border.rounded > p->area.height/2)
107 p->area.pix.border.rounded = p->area.height/2;
108 }
109 else {
110 if (p->pourcentx)
111 p->area.height = (float)server.monitor[p->monitor].height * p->initial_width / 100;
112 else
113 p->area.height = p->initial_width;
114 if (p->pourcenty)
115 p->area.width = (float)server.monitor[p->monitor].width * p->initial_height / 100;
116 else
117 p->area.width = p->initial_height;
118 if (p->area.pix.border.rounded > p->area.width/2)
119 p->area.pix.border.rounded = p->area.width/2;
120 }
121
122 /* panel position determined here */
123 if (panel_position & LEFT) {
124 p->posx = server.monitor[p->monitor].x + p->marginx;
125 }
126 else {
127 if (panel_position & RIGHT) {
128 p->posx = server.monitor[p->monitor].x + server.monitor[p->monitor].width - p->area.width - p->marginx;
129 }
130 else {
131 if (panel_horizontal)
132 p->posx = server.monitor[p->monitor].x + ((server.monitor[p->monitor].width - p->area.width) / 2);
133 else
134 p->posx = server.monitor[p->monitor].x + p->marginx;
135 }
136 }
137 if (panel_position & TOP) {
138 p->posy = server.monitor[p->monitor].y + p->marginy;
139 }
140 else {
141 if (panel_position & BOTTOM) {
142 p->posy = server.monitor[p->monitor].y + server.monitor[p->monitor].height - p->area.height - p->marginy;
143 }
144 else {
145 p->posy = server.monitor[p->monitor].y + ((server.monitor[p->monitor].height - p->area.height) / 2);
146 }
147 }
148 // printf("panel : posx %d, posy %d, width %d, height %d\n", p->posx, p->posy, p->area.width, p->area.height);
149
150 // Catch some events
151 long event_mask = ExposureMask|ButtonPressMask|ButtonReleaseMask;
152 if (g_tooltip.enabled)
153 event_mask |= PointerMotionMask|LeaveWindowMask;
154 XSetWindowAttributes att = { ParentRelative, 0L, 0, 0L, 0, 0, Always, 0L, 0L, False, event_mask, NoEventMask, False, 0, 0 };
155 if (p->main_win) XDestroyWindow(server.dsp, p->main_win);
156 p->main_win = XCreateWindow(server.dsp, server.root_win, p->posx, p->posy, p->area.width, p->area.height, 0, server.depth, InputOutput, CopyFromParent, CWEventMask, &att);
157
158 set_panel_properties(p);
159 set_panel_background(p);
160
161 XMapWindow (server.dsp, p->main_win);
162 }
163 panel_refresh = 1;
164 }
165
166
167 void cleanup_panel()
168 {
169 if (!panel1) return;
170
171 task_active = 0;
172 task_drag = 0;
173 task_urgent = 0;
174 cleanup_systray();
175 cleanup_taskbar();
176
177 // font allocated once
178 if (panel1[0].g_task.font_desc) {
179 pango_font_description_free(panel1[0].g_task.font_desc);
180 panel1[0].g_task.font_desc = 0;
181 }
182
183 int i;
184 Panel *p;
185 for (i=0 ; i < nb_panel ; i++) {
186 p = &panel1[i];
187
188 free_area(&p->area);
189 free_area(&p->g_task.area);
190 free_area(&p->g_taskbar);
191
192 if (p->temp_pmap) {
193 XFreePixmap(server.dsp, p->temp_pmap);
194 p->temp_pmap = 0;
195 }
196 if (p->main_win) {
197 XDestroyWindow(server.dsp, p->main_win);
198 p->main_win = 0;
199 }
200 }
201
202 if (panel1) free(panel1);
203 panel1 = 0;
204
205 if (g_tooltip.window) {
206 XDestroyWindow(server.dsp, g_tooltip.window);
207 g_tooltip.window = 0;
208 }
209 if (g_tooltip.font_desc) {
210 pango_font_description_free(g_tooltip.font_desc);
211 g_tooltip.font_desc = 0;
212 }
213 }
214
215
216 void resize_panel(void *obj)
217 {
218 Panel *panel = (Panel*)obj;
219
220 if (panel_horizontal) {
221 int taskbar_width, modulo_width = 0;
222
223 taskbar_width = panel->area.width - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
224 if (panel->clock.area.on_screen && panel->clock.area.width)
225 taskbar_width -= (panel->clock.area.width + panel->area.paddingx);
226 #ifdef ENABLE_BATTERY
227 if (panel->battery.area.on_screen && panel->battery.area.width)
228 taskbar_width -= (panel->battery.area.width + panel->area.paddingx);
229 #endif
230 // TODO : systray only on first panel. search better implementation !
231 if (systray.area.on_screen && systray.area.width && panel == &panel1[0])
232 taskbar_width -= (systray.area.width + panel->area.paddingx);
233
234 if (panel_mode == MULTI_DESKTOP) {
235 int width = taskbar_width - ((panel->nb_desktop-1) * panel->area.paddingx);
236 taskbar_width = width / panel->nb_desktop;
237 modulo_width = width % panel->nb_desktop;
238 }
239
240 // change posx and width for all taskbar
241 int i, posx;
242 posx = panel->area.pix.border.width + panel->area.paddingxlr;
243 for (i=0 ; i < panel->nb_desktop ; i++) {
244 panel->taskbar[i].area.posx = posx;
245 panel->taskbar[i].area.width = taskbar_width;
246 panel->taskbar[i].area.resize = 1;
247 if (modulo_width) {
248 panel->taskbar[i].area.width++;
249 modulo_width--;
250 }
251 //printf("taskbar %d : posx %d, width, %d, posy %d\n", i, posx, panel->taskbar[i].area.width, posx + panel->taskbar[i].area.width);
252 if (panel_mode == MULTI_DESKTOP)
253 posx += panel->taskbar[i].area.width + panel->area.paddingx;
254 }
255 }
256 else {
257 int taskbar_height, modulo_height = 0;
258 int i, posy;
259
260 taskbar_height = panel->area.height - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
261 if (panel->clock.area.on_screen && panel->clock.area.height)
262 taskbar_height -= (panel->clock.area.height + panel->area.paddingx);
263 #ifdef ENABLE_BATTERY
264 if (panel->battery.area.on_screen && panel->battery.area.height)
265 taskbar_height -= (panel->battery.area.height + panel->area.paddingx);
266 #endif
267 // TODO : systray only on first panel. search better implementation !
268 if (systray.area.on_screen && systray.area.height && panel == &panel1[0])
269 taskbar_height -= (systray.area.height + panel->area.paddingx);
270
271 posy = panel->area.height - panel->area.pix.border.width - panel->area.paddingxlr - taskbar_height;
272 if (panel_mode == MULTI_DESKTOP) {
273 int height = taskbar_height - ((panel->nb_desktop-1) * panel->area.paddingx);
274 taskbar_height = height / panel->nb_desktop;
275 modulo_height = height % panel->nb_desktop;
276 }
277
278 // change posy and height for all taskbar
279 for (i=0 ; i < panel->nb_desktop ; i++) {
280 panel->taskbar[i].area.posy = posy;
281 panel->taskbar[i].area.height = taskbar_height;
282 panel->taskbar[i].area.resize = 1;
283 if (modulo_height) {
284 panel->taskbar[i].area.height++;
285 modulo_height--;
286 }
287 if (panel_mode == MULTI_DESKTOP)
288 posy += panel->taskbar[i].area.height + panel->area.paddingx;
289 }
290 }
291 }
292
293
294 void visible_object()
295 {
296 Panel *panel;
297 int i, j;
298
299 for (i=0 ; i < nb_panel ; i++) {
300 panel = &panel1[i];
301
302 Taskbar *taskbar;
303 for (j=0 ; j < panel->nb_desktop ; j++) {
304 taskbar = &panel->taskbar[j];
305 if (panel_mode != MULTI_DESKTOP && taskbar->desktop != server.desktop) {
306 // SINGLE_DESKTOP and not current desktop
307 taskbar->area.on_screen = 0;
308 }
309 else {
310 taskbar->area.on_screen = 1;
311 }
312 }
313 }
314 panel_refresh = 1;
315 }
316
317
318 void set_panel_properties(Panel *p)
319 {
320 XStoreName (server.dsp, p->main_win, "tint2");
321
322 // TODO: check if the name is really needed for a panel/taskbar ?
323 gsize len;
324 gchar *name = g_locale_to_utf8("tint2", -1, NULL, &len, NULL);
325 if (name != NULL) {
326 XChangeProperty(server.dsp, p->main_win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 8, PropModeReplace, (unsigned char *) name, (int) len);
327 g_free(name);
328 }
329
330 // Dock
331 long val = server.atom._NET_WM_WINDOW_TYPE_DOCK;
332 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_WINDOW_TYPE, XA_ATOM, 32, PropModeReplace, (unsigned char *) &val, 1);
333
334 // Reserved space
335 long struts [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
336 if (panel_horizontal) {
337 if (panel_position & TOP) {
338 struts[2] = p->area.height + p->marginy;
339 struts[8] = p->posx;
340 // p->area.width - 1 allowed full screen on monitor 2
341 struts[9] = p->posx + p->area.width - 1;
342 }
343 else {
344 struts[3] = p->area.height + p->marginy;
345 struts[10] = p->posx;
346 // p->area.width - 1 allowed full screen on monitor 2
347 struts[11] = p->posx + p->area.width - 1;
348 }
349 }
350 else {
351 if (panel_position & LEFT) {
352 struts[0] = p->area.width + p->marginx;
353 struts[4] = p->posy;
354 // p->area.width - 1 allowed full screen on monitor 2
355 struts[5] = p->posy + p->area.height - 1;
356 }
357 else {
358 struts[1] = p->area.width + p->marginx;
359 struts[6] = p->posy;
360 // p->area.width - 1 allowed full screen on monitor 2
361 struts[7] = p->posy + p->area.height - 1;
362 }
363 }
364 // Old specification : fluxbox need _NET_WM_STRUT.
365 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 4);
366 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT_PARTIAL, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 12);
367
368 // Sticky and below other window
369 val = 0xFFFFFFFF;
370 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_DESKTOP, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &val, 1);
371 Atom state[4];
372 state[0] = server.atom._NET_WM_STATE_SKIP_PAGER;
373 state[1] = server.atom._NET_WM_STATE_SKIP_TASKBAR;
374 state[2] = server.atom._NET_WM_STATE_STICKY;
375 state[3] = server.atom._NET_WM_STATE_BELOW;
376 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STATE, XA_ATOM, 32, PropModeReplace, (unsigned char *) state, 4);
377
378 // Fixed position
379 XSizeHints size_hints;
380 size_hints.flags = PPosition;
381 XChangeProperty (server.dsp, p->main_win, XA_WM_NORMAL_HINTS, XA_WM_SIZE_HINTS, 32, PropModeReplace, (unsigned char *) &size_hints, sizeof (XSizeHints) / 4);
382
383 // Unfocusable
384 XWMHints wmhints;
385 if (panel_dock) {
386 // TODO: Xdnd feature cannot be used in withdrawn state at the moment (at least GTK apps fail, qt seems to work)
387 wmhints.icon_window = wmhints.window_group = p->main_win;
388 wmhints.flags = StateHint | IconWindowHint;
389 wmhints.initial_state = WithdrawnState;
390 }
391 else {
392 wmhints.flags = InputHint;
393 wmhints.input = False;
394 }
395 XSetWMHints(server.dsp, p->main_win, &wmhints);
396
397 // Undecorated
398 long prop[5] = { 2, 0, 0, 0, 0 };
399 XChangeProperty(server.dsp, p->main_win, server.atom._MOTIF_WM_HINTS, server.atom._MOTIF_WM_HINTS, 32, PropModeReplace, (unsigned char *) prop, 5);
400
401 // XdndAware - Register for Xdnd events
402 int version=5;
403 XChangeProperty(server.dsp, p->main_win, server.atom.XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char*)&version, 1);
404 }
405
406
407 void set_panel_background(Panel *p)
408 {
409 get_root_pixmap();
410
411 if (p->area.pix.pmap) XFreePixmap (server.dsp, p->area.pix.pmap);
412 p->area.pix.pmap = XCreatePixmap (server.dsp, server.root_win, p->area.width, p->area.height, server.depth);
413
414 // copy background (server.root_pmap) in panel.area.pix.pmap
415 Window dummy;
416 int x, y;
417 XTranslateCoordinates(server.dsp, p->main_win, server.root_win, 0, 0, &x, &y, &dummy);
418 XSetTSOrigin(server.dsp, server.gc, -x, -y) ;
419 XFillRectangle(server.dsp, p->area.pix.pmap, server.gc, 0, 0, p->area.width, p->area.height);
420
421 // draw background panel
422 cairo_surface_t *cs;
423 cairo_t *c;
424 cs = cairo_xlib_surface_create (server.dsp, p->area.pix.pmap, server.visual, p->area.width, p->area.height);
425 c = cairo_create (cs);
426
427 draw_background(&p->area, c, 0);
428
429 cairo_destroy (c);
430 cairo_surface_destroy (cs);
431
432 // redraw panel's object
433 GSList *l0;
434 Area *a;
435 for (l0 = p->area.list; l0 ; l0 = l0->next) {
436 a = l0->data;
437 set_redraw(a);
438 }
439 }
440
441
442 Panel *get_panel(Window win)
443 {
444 int i;
445 for (i=0 ; i < nb_panel ; i++) {
446 if (panel1[i].main_win == win) {
447 return &panel1[i];
448 }
449 }
450 return 0;
451 }
452
This page took 0.067484 seconds and 5 git commands to generate.