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