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