]> Dogcows Code - chaz/tint2/blob - src/panel.c
start on issue 158 : persistent panel config
[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 #include "tooltip.h"
33
34
35 int signal_pending;
36 // --------------------------------------------------
37 // mouse events
38 int mouse_middle;
39 int mouse_right;
40 int mouse_scroll_up;
41 int mouse_scroll_down;
42 int mouse_tilt_left;
43 int mouse_tilt_right;
44
45 int panel_mode;
46 int wm_menu;
47 int panel_dock=0; // default not in the dock
48 int panel_position;
49 int panel_horizontal;
50 int panel_refresh;
51
52 Task *task_active;
53 Task *task_drag;
54 Task *task_urgent;
55 int tick_urgent;
56 int max_tick_urgent;
57
58 // panel's initial config
59 Panel panel_config;
60 // panels (one panel per monitor)
61 Panel *panel1 = 0;
62 int nb_panel;
63
64 Imlib_Image default_icon;
65
66
67 void init_panel()
68 {
69 int i;
70 Panel *p;
71
72 // load default icon
73 char *path;
74 const gchar * const *data_dirs;
75 data_dirs = g_get_system_data_dirs ();
76 for (i = 0; data_dirs[i] != NULL; i++) {
77 path = g_build_filename(data_dirs[i], "tint2", "default_icon.png", NULL);
78 if (g_file_test (path, G_FILE_TEST_EXISTS))
79 default_icon = imlib_load_image(path);
80 g_free(path);
81 }
82
83 //if (panel1)
84 // free(panel1);
85 // alloc panels (one monitor or all monitors)
86 if (panel_config.monitor >= 0)
87 nb_panel = 1;
88 else
89 nb_panel = server.nb_monitor;
90 panel1 = malloc(nb_panel * sizeof(Panel));
91
92 for (i=0 ; i < nb_panel ; i++) {
93 p = &panel1[i];
94
95 memcpy(p, &panel_config, sizeof(Panel));
96 p->monitor = i;
97 p->area.parent = p;
98 p->area.panel = p;
99 p->area.on_screen = 1;
100 p->area.resize = 1;
101 p->area._resize = resize_panel;
102 p->g_taskbar.parent = p;
103 p->g_taskbar.panel = p;
104 p->g_task.area.panel = p;
105
106 // add childs
107 if (p->clock.area.on_screen)
108 p->area.list = g_slist_append(p->area.list, &p->clock);
109 #ifdef ENABLE_BATTERY
110 if (p->battery.area.on_screen)
111 p->area.list = g_slist_append(p->area.list, &p->battery);
112 #endif
113 // systray only on first panel
114 if (systray.area.on_screen && i == 0)
115 p->area.list = g_slist_append(p->area.list, &systray);
116
117 // full width mode
118 if (!p->initial_width) {
119 p->initial_width = 100;
120 p->pourcentx = 1;
121 }
122
123 init_panel_size_and_position(p);
124
125 // Catch some events
126 long event_mask = ExposureMask|ButtonPressMask|ButtonReleaseMask;
127 if (g_tooltip.enabled)
128 event_mask |= PointerMotionMask|LeaveWindowMask;
129 XSetWindowAttributes att = { ParentRelative, 0L, 0, 0L, 0, 0, Always, 0L, 0L, False, event_mask, NoEventMask, False, 0, 0 };
130 if (p->main_win) XDestroyWindow(server.dsp, p->main_win);
131 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);
132
133 set_panel_properties(p);
134 set_panel_background(p);
135
136 XMapWindow (server.dsp, p->main_win);
137 }
138 panel_refresh = 1;
139 }
140
141
142 void init_panel_size_and_position(Panel *panel)
143 {
144 // detect panel size
145 if (panel_horizontal) {
146 if (panel->pourcentx)
147 panel->area.width = (float)server.monitor[panel->monitor].width * panel->initial_width / 100;
148 else
149 panel->area.width = panel->initial_width;
150 if (panel->pourcenty)
151 panel->area.height = (float)server.monitor[panel->monitor].height * panel->initial_height / 100;
152 else
153 panel->area.height = panel->initial_height;
154 if (panel->area.pix.border.rounded > panel->area.height/2)
155 panel->area.pix.border.rounded = panel->area.height/2;
156 }
157 else {
158 if (panel->pourcentx)
159 panel->area.height = (float)server.monitor[panel->monitor].height * panel->initial_width / 100;
160 else
161 panel->area.height = panel->initial_width;
162 if (panel->pourcenty)
163 panel->area.width = (float)server.monitor[panel->monitor].width * panel->initial_height / 100;
164 else
165 panel->area.width = panel->initial_height;
166 if (panel->area.pix.border.rounded > panel->area.width/2)
167 panel->area.pix.border.rounded = panel->area.width/2;
168 }
169
170 // panel position determined here
171 if (panel_position & LEFT) {
172 panel->posx = server.monitor[panel->monitor].x + panel->marginx;
173 }
174 else {
175 if (panel_position & RIGHT) {
176 panel->posx = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width - panel->area.width - panel->marginx;
177 }
178 else {
179 if (panel_horizontal)
180 panel->posx = server.monitor[panel->monitor].x + ((server.monitor[panel->monitor].width - panel->area.width) / 2);
181 else
182 panel->posx = server.monitor[panel->monitor].x + panel->marginx;
183 }
184 }
185 if (panel_position & TOP) {
186 panel->posy = server.monitor[panel->monitor].y + panel->marginy;
187 }
188 else {
189 if (panel_position & BOTTOM) {
190 panel->posy = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height - panel->area.height - panel->marginy;
191 }
192 else {
193 panel->posy = server.monitor[panel->monitor].y + ((server.monitor[panel->monitor].height - panel->area.height) / 2);
194 }
195 }
196 // printf("panel : posx %d, posy %d, width %d, height %d\n", panel->posx, panel->posy, panel->area.width, panel->area.height);
197 }
198
199
200 void cleanup_panel()
201 {
202 if (!panel1) return;
203
204 task_active = 0;
205 task_drag = 0;
206 task_urgent = 0;
207 cleanup_systray();
208 cleanup_taskbar();
209
210 if (default_icon) {
211 imlib_context_set_image(default_icon);
212 imlib_free_image();
213 }
214
215 // font allocated once
216 if (panel1[0].g_task.font_desc) {
217 pango_font_description_free(panel1[0].g_task.font_desc);
218 panel1[0].g_task.font_desc = 0;
219 }
220
221 int i;
222 Panel *p;
223 for (i=0 ; i < nb_panel ; i++) {
224 p = &panel1[i];
225
226 free_area(&p->area);
227 free_area(&p->g_task.area);
228 free_area(&p->g_taskbar);
229
230 if (p->temp_pmap) {
231 XFreePixmap(server.dsp, p->temp_pmap);
232 p->temp_pmap = 0;
233 }
234 if (p->main_win) {
235 XDestroyWindow(server.dsp, p->main_win);
236 p->main_win = 0;
237 }
238 }
239
240 if (panel1) free(panel1);
241 panel1 = 0;
242
243 if (g_tooltip.window) {
244 XDestroyWindow(server.dsp, g_tooltip.window);
245 g_tooltip.window = 0;
246 }
247 if (g_tooltip.font_desc) {
248 pango_font_description_free(g_tooltip.font_desc);
249 g_tooltip.font_desc = 0;
250 }
251 }
252
253
254 void resize_panel(void *obj)
255 {
256 Panel *panel = (Panel*)obj;
257
258 if (panel_horizontal) {
259 int taskbar_width, modulo_width = 0;
260
261 taskbar_width = panel->area.width - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
262 if (panel->clock.area.on_screen && panel->clock.area.width)
263 taskbar_width -= (panel->clock.area.width + panel->area.paddingx);
264 #ifdef ENABLE_BATTERY
265 if (panel->battery.area.on_screen && panel->battery.area.width)
266 taskbar_width -= (panel->battery.area.width + panel->area.paddingx);
267 #endif
268 // TODO : systray only on first panel. search better implementation !
269 if (systray.area.on_screen && systray.area.width && panel == &panel1[0])
270 taskbar_width -= (systray.area.width + panel->area.paddingx);
271
272 if (panel_mode == MULTI_DESKTOP) {
273 int width = taskbar_width - ((panel->nb_desktop-1) * panel->area.paddingx);
274 taskbar_width = width / panel->nb_desktop;
275 modulo_width = width % panel->nb_desktop;
276 }
277
278 // change posx and width for all taskbar
279 int i, posx;
280 posx = panel->area.pix.border.width + panel->area.paddingxlr;
281 for (i=0 ; i < panel->nb_desktop ; i++) {
282 panel->taskbar[i].area.posx = posx;
283 panel->taskbar[i].area.width = taskbar_width;
284 panel->taskbar[i].area.resize = 1;
285 if (modulo_width) {
286 panel->taskbar[i].area.width++;
287 modulo_width--;
288 }
289 //printf("taskbar %d : posx %d, width, %d, posy %d\n", i, posx, panel->taskbar[i].area.width, posx + panel->taskbar[i].area.width);
290 if (panel_mode == MULTI_DESKTOP)
291 posx += panel->taskbar[i].area.width + panel->area.paddingx;
292 }
293 }
294 else {
295 int taskbar_height, modulo_height = 0;
296 int i, posy;
297
298 taskbar_height = panel->area.height - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
299 if (panel->clock.area.on_screen && panel->clock.area.height)
300 taskbar_height -= (panel->clock.area.height + panel->area.paddingx);
301 #ifdef ENABLE_BATTERY
302 if (panel->battery.area.on_screen && panel->battery.area.height)
303 taskbar_height -= (panel->battery.area.height + panel->area.paddingx);
304 #endif
305 // TODO : systray only on first panel. search better implementation !
306 if (systray.area.on_screen && systray.area.height && panel == &panel1[0])
307 taskbar_height -= (systray.area.height + panel->area.paddingx);
308
309 posy = panel->area.height - panel->area.pix.border.width - panel->area.paddingxlr - taskbar_height;
310 if (panel_mode == MULTI_DESKTOP) {
311 int height = taskbar_height - ((panel->nb_desktop-1) * panel->area.paddingx);
312 taskbar_height = height / panel->nb_desktop;
313 modulo_height = height % panel->nb_desktop;
314 }
315
316 // change posy and height for all taskbar
317 for (i=0 ; i < panel->nb_desktop ; i++) {
318 panel->taskbar[i].area.posy = posy;
319 panel->taskbar[i].area.height = taskbar_height;
320 panel->taskbar[i].area.resize = 1;
321 if (modulo_height) {
322 panel->taskbar[i].area.height++;
323 modulo_height--;
324 }
325 if (panel_mode == MULTI_DESKTOP)
326 posy += panel->taskbar[i].area.height + panel->area.paddingx;
327 }
328 }
329 }
330
331
332 void visible_object()
333 {
334 Panel *panel;
335 int i, j;
336
337 for (i=0 ; i < nb_panel ; i++) {
338 panel = &panel1[i];
339
340 Taskbar *taskbar;
341 for (j=0 ; j < panel->nb_desktop ; j++) {
342 taskbar = &panel->taskbar[j];
343 if (panel_mode != MULTI_DESKTOP && taskbar->desktop != server.desktop) {
344 // SINGLE_DESKTOP and not current desktop
345 taskbar->area.on_screen = 0;
346 }
347 else {
348 taskbar->area.on_screen = 1;
349 }
350 }
351 }
352 panel_refresh = 1;
353 }
354
355
356 void set_panel_properties(Panel *p)
357 {
358 XStoreName (server.dsp, p->main_win, "tint2");
359
360 gsize len;
361 gchar *name = g_locale_to_utf8("tint2", -1, NULL, &len, NULL);
362 if (name != NULL) {
363 XChangeProperty(server.dsp, p->main_win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 8, PropModeReplace, (unsigned char *) name, (int) len);
364 g_free(name);
365 }
366
367 // Dock
368 long val = server.atom._NET_WM_WINDOW_TYPE_DOCK;
369 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_WINDOW_TYPE, XA_ATOM, 32, PropModeReplace, (unsigned char *) &val, 1);
370
371 // Reserved space
372 long struts [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
373 if (panel_horizontal) {
374 if (panel_position & TOP) {
375 struts[2] = p->area.height + p->marginy;
376 struts[8] = p->posx;
377 // p->area.width - 1 allowed full screen on monitor 2
378 struts[9] = p->posx + p->area.width - 1;
379 }
380 else {
381 struts[3] = p->area.height + p->marginy;
382 struts[10] = p->posx;
383 // p->area.width - 1 allowed full screen on monitor 2
384 struts[11] = p->posx + p->area.width - 1;
385 }
386 }
387 else {
388 if (panel_position & LEFT) {
389 struts[0] = p->area.width + p->marginx;
390 struts[4] = p->posy;
391 // p->area.width - 1 allowed full screen on monitor 2
392 struts[5] = p->posy + p->area.height - 1;
393 }
394 else {
395 struts[1] = p->area.width + p->marginx;
396 struts[6] = p->posy;
397 // p->area.width - 1 allowed full screen on monitor 2
398 struts[7] = p->posy + p->area.height - 1;
399 }
400 }
401 // Old specification : fluxbox need _NET_WM_STRUT.
402 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 4);
403 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT_PARTIAL, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 12);
404
405 // Sticky and below other window
406 val = 0xFFFFFFFF;
407 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_DESKTOP, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &val, 1);
408 Atom state[4];
409 state[0] = server.atom._NET_WM_STATE_SKIP_PAGER;
410 state[1] = server.atom._NET_WM_STATE_SKIP_TASKBAR;
411 state[2] = server.atom._NET_WM_STATE_STICKY;
412 state[3] = server.atom._NET_WM_STATE_BELOW;
413 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STATE, XA_ATOM, 32, PropModeReplace, (unsigned char *) state, 4);
414
415 // Fixed position and non-resizable window
416 XSizeHints size_hints;
417 size_hints.flags = PPosition|PMinSize|PMaxSize;
418 size_hints.min_width = size_hints.max_width = p->area.width;
419 size_hints.min_height = size_hints.max_height = p->area.height;
420 XSetWMNormalHints(server.dsp, p->main_win, &size_hints);
421
422 // Unfocusable
423 XWMHints wmhints;
424 if (panel_dock) {
425 // TODO: Xdnd feature cannot be used in withdrawn state at the moment (at least GTK apps fail, qt seems to work)
426 wmhints.icon_window = wmhints.window_group = p->main_win;
427 wmhints.flags = StateHint | IconWindowHint;
428 wmhints.initial_state = WithdrawnState;
429 }
430 else {
431 wmhints.flags = InputHint;
432 wmhints.input = False;
433 }
434 XSetWMHints(server.dsp, p->main_win, &wmhints);
435
436 // Undecorated
437 long prop[5] = { 2, 0, 0, 0, 0 };
438 XChangeProperty(server.dsp, p->main_win, server.atom._MOTIF_WM_HINTS, server.atom._MOTIF_WM_HINTS, 32, PropModeReplace, (unsigned char *) prop, 5);
439
440 // XdndAware - Register for Xdnd events
441 int version=5;
442 XChangeProperty(server.dsp, p->main_win, server.atom.XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char*)&version, 1);
443 }
444
445
446 void set_panel_background(Panel *p)
447 {
448 get_root_pixmap();
449
450 if (p->area.pix.pmap) XFreePixmap (server.dsp, p->area.pix.pmap);
451 p->area.pix.pmap = XCreatePixmap (server.dsp, server.root_win, p->area.width, p->area.height, server.depth);
452
453 // copy background (server.root_pmap) in panel.area.pix.pmap
454 Window dummy;
455 int x, y;
456 XTranslateCoordinates(server.dsp, p->main_win, server.root_win, 0, 0, &x, &y, &dummy);
457 XSetTSOrigin(server.dsp, server.gc, -x, -y) ;
458 XFillRectangle(server.dsp, p->area.pix.pmap, server.gc, 0, 0, p->area.width, p->area.height);
459
460 // draw background panel
461 cairo_surface_t *cs;
462 cairo_t *c;
463 cs = cairo_xlib_surface_create (server.dsp, p->area.pix.pmap, server.visual, p->area.width, p->area.height);
464 c = cairo_create (cs);
465
466 draw_background(&p->area, c, 0);
467
468 cairo_destroy (c);
469 cairo_surface_destroy (cs);
470
471 // redraw panel's object
472 GSList *l0;
473 Area *a;
474 for (l0 = p->area.list; l0 ; l0 = l0->next) {
475 a = l0->data;
476 set_redraw(a);
477 }
478 }
479
480
481 Panel *get_panel(Window win)
482 {
483 int i;
484 for (i=0 ; i < nb_panel ; i++) {
485 if (panel1[i].main_win == win) {
486 return &panel1[i];
487 }
488 }
489 return 0;
490 }
491
This page took 0.07101 seconds and 5 git commands to generate.