]> Dogcows Code - chaz/tint2/blob - src/util/area.c
sample-task-align.patch, src-task-align.patch
[chaz/tint2] / src / util / area.c
1 /**************************************************************************
2 *
3 * Tint2 : area
4 *
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr) from Omega distribution
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **************************************************************************/
19
20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22 #include <X11/Xatom.h>
23 #include <X11/extensions/Xrender.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <pango/pangocairo.h>
28
29 #include "area.h"
30 #include "server.h"
31 #include "panel.h"
32
33
34 /************************************************************
35 * !!! This design is experimental and not yet fully implemented !!!!!!!!!!!!!
36 *
37 * DATA ORGANISATION :
38 * Areas in tint2 are similar to widgets in a GUI.
39 * All graphical objects (panel, taskbar, task, systray, clock, ...) 'inherit' an abstract class 'Area'.
40 * This class 'Area' manage the background, border, size, position and padding.
41 * Area is at the begining of each object (&object == &area).
42 *
43 * tint2 define one panel per monitor. And each panel have a tree of Area.
44 * The root of the tree is Panel.Area. And task, clock, systray, taskbar,... are nodes.
45 *
46 * The tree give the localisation of each object :
47 * - tree's root is in the background while tree's leafe are foreground objects
48 * - position of a node/Area depend on the layout : parent's position (posx, posy), size of previous brothers and parent's padding
49 * - size of a node/Area depend on the content (SIZE_BY_CONTENT objects) or on the layout (SIZE_BY_LAYOUT objects)
50 *
51 * DRAWING AND LAYERING ENGINE :
52 * Redrawing an object (like the clock) could come from an 'external event' (date change)
53 * or from a 'layering event' (position change).
54 * The following 'drawing engine' take care of :
55 * - posx/posy of all Area
56 * - 'layering event' propagation between object
57 * 1) browse tree SIZE_BY_CONTENT
58 * - resize SIZE_BY_CONTENT node : children are resized before parent
59 * - if 'size' changed then 'resize = 1' on the parent
60 * 2) browse tree SIZE_BY_LAYOUT and POSITION
61 * - resize SIZE_BY_LAYOUT node : parent is resized before children
62 * - calculate position (posx,posy) : parent is calculated before children
63 * - if 'position' changed then 'redraw = 1'
64 * 3) browse tree REDRAW
65 * - redraw needed objects : parent is drawn before children
66 *
67 * CONFIGURE PANEL'S LAYOUT :
68 * 'panel_items' parameter (in config) define the list and the order of nodes in tree's panel.
69 * 'panel_items = SC' define a panel with just Systray and Clock.
70 * So the tree 'Panel.Area' will have 2 childs (Systray and Clock).
71 *
72 ************************************************************/
73
74 void init_rendering(void *obj, int pos)
75 {
76 Area *a = (Area*)obj;
77
78 // initialize fixed position/size
79 GSList *l;
80 for (l = a->list; l ; l = l->next) {
81 Area *child = ((Area*)l->data);
82 if (panel_horizontal) {
83 child->posy = pos + a->bg->border.width + a->paddingy;
84 child->height = a->height - (2 * (a->bg->border.width + a->paddingy));
85 init_rendering(child, child->posy);
86 }
87 else {
88 child->posx = pos + a->bg->border.width + a->paddingy;
89 child->width = a->width - (2 * (a->bg->border.width + a->paddingy));
90 init_rendering(child, child->posx);
91 }
92 }
93 }
94
95
96 void rendering(void *obj)
97 {
98 Panel *panel = (Panel*)obj;
99
100 size_by_content(&panel->area);
101 size_by_layout(&panel->area, 0, 1);
102
103 refresh(&panel->area);
104 }
105
106
107 void size_by_content (Area *a)
108 {
109 // don't resize hiden objects
110 if (!a->on_screen) return;
111
112 // children node are resized before its parent
113 GSList *l;
114 for (l = a->list; l ; l = l->next)
115 size_by_content(l->data);
116
117 // calculate area's size
118 a->on_changed = 0;
119 if (a->resize && a->size_mode == SIZE_BY_CONTENT) {
120 a->resize = 0;
121
122 if (a->_resize) {
123 if (a->_resize(a)) {
124 // 'size' changed => 'resize = 1' on the parent
125 ((Area*)a->parent)->resize = 1;
126 a->on_changed = 1;
127 }
128 }
129 }
130 }
131
132
133 // calculate total size of all children including
134 // parent's padding
135 int children_size(Area *a, int horizontal)
136 {
137 int size = 0;
138 GSList *l;
139
140 for (l = a->list; l; l = l->next) {
141 Area *child = ((Area*)l->data);
142 if (!child->on_screen) continue;
143
144 if (horizontal)
145 size += child->width + a->paddingx;
146 else
147 size += child->height + a->paddingy;
148 }
149
150 return size;
151 }
152
153
154 // calculate chilren's align offset depending on the align type
155 int align_offset(Area *a, int align, int horizontal)
156 {
157 int size = 0;
158 int child_size = children_size(a, horizontal);
159
160 if (horizontal)
161 size = a->width;
162 else
163 size = a->height;
164
165 switch (align) {
166 case ALIGN_LEFT:
167 return 0;
168
169 case ALIGN_CENTER:
170 return (size - child_size) / 2;
171
172 case ALIGN_RIGHT:
173 return size - child_size;
174
175 default:
176 return 0;
177 }
178 }
179
180
181 void size_by_layout (Area *a, int pos, int level)
182 {
183 // don't resize hiden objects
184 if (!a->on_screen) return;
185
186 // parent node is resized before its children
187 // calculate area's size
188 GSList *l;
189 if (a->resize && a->size_mode == SIZE_BY_LAYOUT) {
190 a->resize = 0;
191
192 if (a->_resize) {
193 a->_resize(a);
194 // resize childs with SIZE_BY_LAYOUT
195 for (l = a->list; l ; l = l->next) {
196 Area *child = ((Area*)l->data);
197 if (child->size_mode == SIZE_BY_LAYOUT && child->list)
198 child->resize = 1;
199 }
200 }
201 }
202
203 // update position of childs
204 pos += a->paddingxlr + a->bg->border.width;
205 int i=0;
206 for (l = a->list; l ; l = l->next) {
207 Area *child = ((Area*)l->data);
208 if (!child->on_screen) continue;
209 i++;
210
211 if (panel_horizontal) {
212 if (pos != child->posx) {
213 // pos changed => redraw
214 child->posx = pos;
215 child->on_changed = 1;
216 }
217 }
218 else {
219 if (pos != child->posy) {
220 // pos changed => redraw
221 child->posy = pos;
222 child->on_changed = 1;
223 }
224 }
225
226 /*// position of each visible object
227 int k;
228 for (k=0 ; k < level ; k++) printf(" ");
229 printf("tree level %d, object %d, pos %d, %s\n", level, i, pos, (child->size_mode == SIZE_BY_LAYOUT) ? "SIZE_BY_LAYOUT" : "SIZE_BY_CONTENT");*/
230
231 int offset = align_offset(child, panel_config.g_task.align, panel_horizontal);
232 size_by_layout(child, pos + offset, level + 1);
233
234 if (panel_horizontal)
235 pos += child->width + a->paddingx;
236 else
237 pos += child->height + a->paddingx;
238 }
239
240 if (a->on_changed) {
241 // pos/size changed
242 a->redraw = 1;
243 if (a->_on_change_layout)
244 a->_on_change_layout (a);
245 }
246 }
247
248
249 void refresh (Area *a)
250 {
251 // don't draw and resize hide objects
252 if (!a->on_screen) return;
253
254 // don't draw transparent objects (without foreground and without background)
255 if (a->redraw) {
256 a->redraw = 0;
257 // force redraw of child
258 //GSList *l;
259 //for (l = a->list ; l ; l = l->next)
260 //((Area*)l->data)->redraw = 1;
261
262 //printf("draw area posx %d, width %d\n", a->posx, a->width);
263 draw(a);
264 }
265
266 // draw current Area
267 if (a->pix == 0) printf("empty area posx %d, width %d\n", a->posx, a->width);
268 XCopyArea (server.dsp, a->pix, ((Panel *)a->panel)->temp_pmap, server.gc, 0, 0, a->width, a->height, a->posx, a->posy);
269
270 // and then refresh child object
271 GSList *l;
272 for (l = a->list; l ; l = l->next)
273 refresh(l->data);
274 }
275
276
277 int resize_by_layout(void *obj, int maximum_size)
278 {
279 Area *child, *a = (Area*)obj;
280 int size, nb_by_content=0, nb_by_layout=0;
281
282 if (panel_horizontal) {
283 // detect free size for SIZE_BY_LAYOUT's Area
284 size = a->width - (2 * (a->paddingxlr + a->bg->border.width));
285 GSList *l;
286 for (l = a->list ; l ; l = l->next) {
287 child = (Area*)l->data;
288 if (child->on_screen && child->size_mode == SIZE_BY_CONTENT) {
289 size -= child->width;
290 nb_by_content++;
291 }
292 if (child->on_screen && child->size_mode == SIZE_BY_LAYOUT)
293 nb_by_layout++;
294 }
295 //printf(" resize_by_layout Deb %d, %d\n", nb_by_content, nb_by_layout);
296 if (nb_by_content+nb_by_layout)
297 size -= ((nb_by_content+nb_by_layout-1) * a->paddingx);
298
299 int width=0, modulo=0, old_width;
300 if (nb_by_layout) {
301 width = size / nb_by_layout;
302 modulo = size % nb_by_layout;
303 if (width > maximum_size && maximum_size != 0) {
304 width = maximum_size;
305 modulo = 0;
306 }
307 }
308
309 // resize SIZE_BY_LAYOUT objects
310 for (l = a->list ; l ; l = l->next) {
311 child = (Area*)l->data;
312 if (child->on_screen && child->size_mode == SIZE_BY_LAYOUT) {
313 old_width = child->width;
314 child->width = width;
315 if (modulo) {
316 child->width++;
317 modulo--;
318 }
319 if (child->width != old_width)
320 child->on_changed = 1;
321 }
322 }
323 }
324 else {
325 // detect free size for SIZE_BY_LAYOUT's Area
326 size = a->height - (2 * (a->paddingxlr + a->bg->border.width));
327 GSList *l;
328 for (l = a->list ; l ; l = l->next) {
329 child = (Area*)l->data;
330 if (child->on_screen && child->size_mode == SIZE_BY_CONTENT) {
331 size -= child->height;
332 nb_by_content++;
333 }
334 if (child->on_screen && child->size_mode == SIZE_BY_LAYOUT)
335 nb_by_layout++;
336 }
337 if (nb_by_content+nb_by_layout)
338 size -= ((nb_by_content+nb_by_layout-1) * a->paddingx);
339
340 int height=0, modulo=0, old_height;
341 if (nb_by_layout) {
342 height = size / nb_by_layout;
343 modulo = size % nb_by_layout;
344 if (height > maximum_size && maximum_size != 0) {
345 height = maximum_size;
346 modulo = 0;
347 }
348 }
349
350 // resize SIZE_BY_LAYOUT objects
351 for (l = a->list ; l ; l = l->next) {
352 child = (Area*)l->data;
353 if (child->on_screen && child->size_mode == SIZE_BY_LAYOUT) {
354 old_height = child->height;
355 child->height = height;
356 if (modulo) {
357 child->height++;
358 modulo--;
359 }
360 if (child->height != old_height)
361 child->on_changed = 1;
362 }
363 }
364 }
365 return 0;
366 }
367
368
369 void set_redraw (Area *a)
370 {
371 a->redraw = 1;
372
373 GSList *l;
374 for (l = a->list ; l ; l = l->next)
375 set_redraw(l->data);
376 }
377
378 void hide(Area *a)
379 {
380 Area *parent = (Area*)a->parent;
381
382 a->on_screen = 0;
383 parent->resize = 1;
384 if (panel_horizontal)
385 a->width = 0;
386 else
387 a->height = 0;
388 }
389
390 void show(Area *a)
391 {
392 Area *parent = (Area*)a->parent;
393
394 a->on_screen = 1;
395 parent->resize = 1;
396 a->resize = 1;
397 }
398
399 void draw (Area *a)
400 {
401 if (a->pix) XFreePixmap (server.dsp, a->pix);
402 a->pix = XCreatePixmap (server.dsp, server.root_win, a->width, a->height, server.depth);
403
404 // add layer of root pixmap (or clear pixmap if real_transparency==true)
405 if (server.real_transparency)
406 clear_pixmap(a->pix, 0 ,0, a->width, a->height);
407 XCopyArea (server.dsp, ((Panel *)a->panel)->temp_pmap, a->pix, server.gc, a->posx, a->posy, a->width, a->height, 0, 0);
408
409 cairo_surface_t *cs;
410 cairo_t *c;
411
412 cs = cairo_xlib_surface_create (server.dsp, a->pix, server.visual, a->width, a->height);
413 c = cairo_create (cs);
414
415 draw_background (a, c);
416
417 if (a->_draw_foreground)
418 a->_draw_foreground(a, c);
419
420 cairo_destroy (c);
421 cairo_surface_destroy (cs);
422 }
423
424
425 void draw_background (Area *a, cairo_t *c)
426 {
427 if (a->bg->back.alpha > 0.0) {
428 //printf(" draw_background (%d %d) RGBA (%lf, %lf, %lf, %lf)\n", a->posx, a->posy, pix->back.color[0], pix->back.color[1], pix->back.color[2], pix->back.alpha);
429 draw_rect(c, a->bg->border.width, a->bg->border.width, a->width-(2.0 * a->bg->border.width), a->height-(2.0*a->bg->border.width), a->bg->border.rounded - a->bg->border.width/1.571);
430 cairo_set_source_rgba(c, a->bg->back.color[0], a->bg->back.color[1], a->bg->back.color[2], a->bg->back.alpha);
431 cairo_fill(c);
432 }
433
434 if (a->bg->border.width > 0 && a->bg->border.alpha > 0.0) {
435 cairo_set_line_width (c, a->bg->border.width);
436
437 // draw border inside (x, y, width, height)
438 draw_rect(c, a->bg->border.width/2.0, a->bg->border.width/2.0, a->width - a->bg->border.width, a->height - a->bg->border.width, a->bg->border.rounded);
439 /*
440 // convert : radian = degre * M_PI/180
441 // definir le degrade dans un carre de (0,0) (100,100)
442 // ensuite ce degrade est extrapoler selon le ratio width/height
443 // dans repere (0, 0) (100, 100)
444 double X0, Y0, X1, Y1, degre;
445 // x = X * (a->width / 100), y = Y * (a->height / 100)
446 double x0, y0, x1, y1;
447 X0 = 0;
448 Y0 = 100;
449 X1 = 100;
450 Y1 = 0;
451 degre = 45;
452 // et ensuite faire la changement d'unite du repere
453 // car ce qui doit reste inchangee est les traits et pas la direction
454
455 // il faut d'abord appliquer une rotation de 90 (et -180 si l'angle est superieur a 180)
456 // ceci peut etre applique une fois pour toute au depart
457 // ensuite calculer l'angle dans le nouveau repare
458 // puis faire une rotation de 90
459 x0 = X0 * ((double)a->width / 100);
460 x1 = X1 * ((double)a->width / 100);
461 y0 = Y0 * ((double)a->height / 100);
462 y1 = Y1 * ((double)a->height / 100);
463
464 x0 = X0 * ((double)a->height / 100);
465 x1 = X1 * ((double)a->height / 100);
466 y0 = Y0 * ((double)a->width / 100);
467 y1 = Y1 * ((double)a->width / 100);
468
469 cairo_pattern_t *linpat;
470 linpat = cairo_pattern_create_linear (x0, y0, x1, y1);
471 cairo_pattern_add_color_stop_rgba (linpat, 0, a->border.color[0], a->border.color[1], a->border.color[2], a->border.alpha);
472 cairo_pattern_add_color_stop_rgba (linpat, 1, a->border.color[0], a->border.color[1], a->border.color[2], 0);
473 cairo_set_source (c, linpat);
474 */
475 cairo_set_source_rgba (c, a->bg->border.color[0], a->bg->border.color[1], a->bg->border.color[2], a->bg->border.alpha);
476
477 cairo_stroke (c);
478 //cairo_pattern_destroy (linpat);
479 }
480 }
481
482
483 void remove_area (Area *a)
484 {
485 Area *parent = (Area*)a->parent;
486
487 parent->list = g_slist_remove(parent->list, a);
488 set_redraw (parent);
489
490 }
491
492
493 void add_area (Area *a)
494 {
495 Area *parent = (Area*)a->parent;
496
497 parent->list = g_slist_append(parent->list, a);
498 set_redraw (parent);
499
500 }
501
502
503 void free_area (Area *a)
504 {
505 GSList *l0;
506 for (l0 = a->list; l0 ; l0 = l0->next)
507 free_area (l0->data);
508
509 if (a->list) {
510 g_slist_free(a->list);
511 a->list = 0;
512 }
513 if (a->pix) {
514 XFreePixmap (server.dsp, a->pix);
515 a->pix = 0;
516 }
517 }
518
519
520 void draw_rect(cairo_t *c, double x, double y, double w, double h, double r)
521 {
522 if (r > 0.0) {
523 double c1 = 0.55228475 * r;
524
525 cairo_move_to(c, x+r, y);
526 cairo_rel_line_to(c, w-2*r, 0);
527 cairo_rel_curve_to(c, c1, 0.0, r, c1, r, r);
528 cairo_rel_line_to(c, 0, h-2*r);
529 cairo_rel_curve_to(c, 0.0, c1, c1-r, r, -r, r);
530 cairo_rel_line_to (c, -w +2*r, 0);
531 cairo_rel_curve_to (c, -c1, 0, -r, -c1, -r, -r);
532 cairo_rel_line_to (c, 0, -h + 2 * r);
533 cairo_rel_curve_to (c, 0, -c1, r - c1, -r, r, -r);
534 }
535 else
536 cairo_rectangle(c, x, y, w, h);
537 }
538
539
540 void clear_pixmap(Pixmap p, int x, int y, int w, int h)
541 {
542 Picture pict = XRenderCreatePicture(server.dsp, p, XRenderFindVisualFormat(server.dsp, server.visual), 0, 0);
543 XRenderColor col = { .red=0, .green=0, .blue=0, .alpha=0 };
544 XRenderFillRectangle(server.dsp, PictOpSrc, pict, &col, x, y, w, h);
545 XRenderFreePicture(server.dsp, pict);
546 }
This page took 0.092396 seconds and 4 git commands to generate.