]> Dogcows Code - chaz/tint2/blob - src/panel.c
added possibility to put tint2 into the windowmanagers dock
[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 XSetWindowAttributes att = { ParentRelative, 0L, 0, 0L, 0, 0, Always, 0L, 0L, False, ExposureMask|ButtonPressMask|ButtonReleaseMask, NoEventMask, False, 0, 0 };
152 if (p->main_win) XDestroyWindow(server.dsp, p->main_win);
153 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);
154
155 set_panel_properties(p);
156 set_panel_background(p);
157
158 XMapWindow (server.dsp, p->main_win);
159 }
160 panel_refresh = 1;
161 }
162
163
164 void cleanup_panel()
165 {
166 if (!panel1) return;
167
168 task_active = 0;
169 task_drag = 0;
170 task_urgent = 0;
171 cleanup_systray();
172 cleanup_taskbar();
173
174 // font allocated once
175 if (panel1[0].g_task.font_desc) {
176 pango_font_description_free(panel1[0].g_task.font_desc);
177 panel1[0].g_task.font_desc = 0;
178 }
179
180 int i;
181 Panel *p;
182 for (i=0 ; i < nb_panel ; i++) {
183 p = &panel1[i];
184
185 free_area(&p->area);
186 free_area(&p->g_task.area);
187 free_area(&p->g_taskbar);
188
189 if (p->temp_pmap) {
190 XFreePixmap(server.dsp, p->temp_pmap);
191 p->temp_pmap = 0;
192 }
193 if (p->main_win) {
194 XDestroyWindow(server.dsp, p->main_win);
195 p->main_win = 0;
196 }
197 }
198
199 if (panel1) free(panel1);
200 panel1 = 0;
201 }
202
203
204 void resize_panel(void *obj)
205 {
206 Panel *panel = (Panel*)obj;
207
208 if (panel_horizontal) {
209 int taskbar_width, modulo_width = 0;
210
211 taskbar_width = panel->area.width - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
212 if (panel->clock.area.on_screen && panel->clock.area.width)
213 taskbar_width -= (panel->clock.area.width + panel->area.paddingx);
214 #ifdef ENABLE_BATTERY
215 if (panel->battery.area.on_screen && panel->battery.area.width)
216 taskbar_width -= (panel->battery.area.width + panel->area.paddingx);
217 #endif
218 // TODO : systray only on first panel. search better implementation !
219 if (systray.area.on_screen && systray.area.width && panel == &panel1[0])
220 taskbar_width -= (systray.area.width + panel->area.paddingx);
221
222 if (panel_mode == MULTI_DESKTOP) {
223 int width = taskbar_width - ((panel->nb_desktop-1) * panel->area.paddingx);
224 taskbar_width = width / panel->nb_desktop;
225 modulo_width = width % panel->nb_desktop;
226 }
227
228 // change posx and width for all taskbar
229 int i, posx;
230 posx = panel->area.pix.border.width + panel->area.paddingxlr;
231 for (i=0 ; i < panel->nb_desktop ; i++) {
232 panel->taskbar[i].area.posx = posx;
233 panel->taskbar[i].area.width = taskbar_width;
234 panel->taskbar[i].area.resize = 1;
235 if (modulo_width) {
236 panel->taskbar[i].area.width++;
237 modulo_width--;
238 }
239 //printf("taskbar %d : posx %d, width, %d, posy %d\n", i, posx, panel->taskbar[i].area.width, posx + panel->taskbar[i].area.width);
240 if (panel_mode == MULTI_DESKTOP)
241 posx += panel->taskbar[i].area.width + panel->area.paddingx;
242 }
243 }
244 else {
245 int taskbar_height, modulo_height = 0;
246 int i, posy;
247
248 taskbar_height = panel->area.height - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
249 if (panel->clock.area.on_screen && panel->clock.area.height)
250 taskbar_height -= (panel->clock.area.height + panel->area.paddingx);
251 #ifdef ENABLE_BATTERY
252 if (panel->battery.area.on_screen && panel->battery.area.height)
253 taskbar_height -= (panel->battery.area.height + panel->area.paddingx);
254 #endif
255 // TODO : systray only on first panel. search better implementation !
256 if (systray.area.on_screen && systray.area.height && panel == &panel1[0])
257 taskbar_height -= (systray.area.height + panel->area.paddingx);
258
259 posy = panel->area.height - panel->area.pix.border.width - panel->area.paddingxlr - taskbar_height;
260 if (panel_mode == MULTI_DESKTOP) {
261 int height = taskbar_height - ((panel->nb_desktop-1) * panel->area.paddingx);
262 taskbar_height = height / panel->nb_desktop;
263 modulo_height = height % panel->nb_desktop;
264 }
265
266 // change posy and height for all taskbar
267 for (i=0 ; i < panel->nb_desktop ; i++) {
268 panel->taskbar[i].area.posy = posy;
269 panel->taskbar[i].area.height = taskbar_height;
270 panel->taskbar[i].area.resize = 1;
271 if (modulo_height) {
272 panel->taskbar[i].area.height++;
273 modulo_height--;
274 }
275 if (panel_mode == MULTI_DESKTOP)
276 posy += panel->taskbar[i].area.height + panel->area.paddingx;
277 }
278 }
279 }
280
281
282 void visible_object()
283 {
284 Panel *panel;
285 int i, j;
286
287 for (i=0 ; i < nb_panel ; i++) {
288 panel = &panel1[i];
289
290 Taskbar *taskbar;
291 for (j=0 ; j < panel->nb_desktop ; j++) {
292 taskbar = &panel->taskbar[j];
293 if (panel_mode != MULTI_DESKTOP && taskbar->desktop != server.desktop) {
294 // SINGLE_DESKTOP and not current desktop
295 taskbar->area.on_screen = 0;
296 }
297 else {
298 taskbar->area.on_screen = 1;
299 }
300 }
301 }
302 panel_refresh = 1;
303 }
304
305
306 void set_panel_properties(Panel *p)
307 {
308 XStoreName (server.dsp, p->main_win, "tint2");
309
310 // TODO: check if the name is really needed for a panel/taskbar ?
311 gsize len;
312 gchar *name = g_locale_to_utf8("tint2", -1, NULL, &len, NULL);
313 if (name != NULL) {
314 XChangeProperty(server.dsp, p->main_win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 8, PropModeReplace, (unsigned char *) name, (int) len);
315 g_free(name);
316 }
317
318 // Dock
319 long val = server.atom._NET_WM_WINDOW_TYPE_DOCK;
320 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_WINDOW_TYPE, XA_ATOM, 32, PropModeReplace, (unsigned char *) &val, 1);
321
322 // Reserved space
323 long struts [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
324 if (panel_horizontal) {
325 if (panel_position & TOP) {
326 struts[2] = p->area.height + p->marginy;
327 struts[8] = p->posx;
328 // p->area.width - 1 allowed full screen on monitor 2
329 struts[9] = p->posx + p->area.width - 1;
330 }
331 else {
332 struts[3] = p->area.height + p->marginy;
333 struts[10] = p->posx;
334 // p->area.width - 1 allowed full screen on monitor 2
335 struts[11] = p->posx + p->area.width - 1;
336 }
337 }
338 else {
339 if (panel_position & LEFT) {
340 struts[0] = p->area.width + p->marginx;
341 struts[4] = p->posy;
342 // p->area.width - 1 allowed full screen on monitor 2
343 struts[5] = p->posy + p->area.height - 1;
344 }
345 else {
346 struts[1] = p->area.width + p->marginx;
347 struts[6] = p->posy;
348 // p->area.width - 1 allowed full screen on monitor 2
349 struts[7] = p->posy + p->area.height - 1;
350 }
351 }
352 // Old specification : fluxbox need _NET_WM_STRUT.
353 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 4);
354 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT_PARTIAL, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 12);
355
356 // Sticky and below other window
357 val = 0xFFFFFFFF;
358 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_DESKTOP, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &val, 1);
359 Atom state[4];
360 state[0] = server.atom._NET_WM_STATE_SKIP_PAGER;
361 state[1] = server.atom._NET_WM_STATE_SKIP_TASKBAR;
362 state[2] = server.atom._NET_WM_STATE_STICKY;
363 state[3] = server.atom._NET_WM_STATE_BELOW;
364 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STATE, XA_ATOM, 32, PropModeReplace, (unsigned char *) state, 4);
365
366 // Fixed position
367 XSizeHints size_hints;
368 size_hints.flags = PPosition;
369 XChangeProperty (server.dsp, p->main_win, XA_WM_NORMAL_HINTS, XA_WM_SIZE_HINTS, 32, PropModeReplace, (unsigned char *) &size_hints, sizeof (XSizeHints) / 4);
370
371 // Unfocusable
372 XWMHints wmhints;
373 if (panel_dock) {
374 wmhints.icon_window = wmhints.window_group = p->main_win;
375 wmhints.flags = StateHint | IconWindowHint;
376 wmhints.initial_state = WithdrawnState;
377 }
378 else {
379 wmhints.flags = InputHint;
380 wmhints.input = False;
381 }
382 XSetWMHints(server.dsp, p->main_win, &wmhints);
383
384 // Undecorated
385 long prop[5] = { 2, 0, 0, 0, 0 };
386 XChangeProperty(server.dsp, p->main_win, server.atom._MOTIF_WM_HINTS, server.atom._MOTIF_WM_HINTS, 32, PropModeReplace, (unsigned char *) prop, 5);
387
388 // XdndAware - Register for Xdnd events
389 int version=5;
390 XChangeProperty(server.dsp, p->main_win, server.atom.XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char*)&version, 1);
391 }
392
393
394 void set_panel_background(Panel *p)
395 {
396 get_root_pixmap();
397
398 if (p->area.pix.pmap) XFreePixmap (server.dsp, p->area.pix.pmap);
399 p->area.pix.pmap = XCreatePixmap (server.dsp, server.root_win, p->area.width, p->area.height, server.depth);
400
401 // copy background (server.root_pmap) in panel.area.pix.pmap
402 Window dummy;
403 int x, y;
404 XTranslateCoordinates(server.dsp, p->main_win, server.root_win, 0, 0, &x, &y, &dummy);
405 XSetTSOrigin(server.dsp, server.gc, -x, -y) ;
406 XFillRectangle(server.dsp, p->area.pix.pmap, server.gc, 0, 0, p->area.width, p->area.height);
407
408 // draw background panel
409 cairo_surface_t *cs;
410 cairo_t *c;
411 cs = cairo_xlib_surface_create (server.dsp, p->area.pix.pmap, server.visual, p->area.width, p->area.height);
412 c = cairo_create (cs);
413
414 draw_background(&p->area, c, 0);
415
416 cairo_destroy (c);
417 cairo_surface_destroy (cs);
418
419 // redraw panel's object
420 GSList *l0;
421 Area *a;
422 for (l0 = p->area.list; l0 ; l0 = l0->next) {
423 a = l0->data;
424 set_redraw(a);
425 }
426 }
427
428
429 Panel *get_panel(Window win)
430 {
431 int i;
432 for (i=0 ; i < nb_panel ; i++) {
433 if (panel1[i].main_win == win) {
434 return &panel1[i];
435 }
436 }
437 return 0;
438 }
439
This page took 0.062751 seconds and 5 git commands to generate.