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