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