]>
Dogcows Code - chaz/tint2/blob - src/util/area.c
1 /**************************************************************************
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr) from Omega distribution
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.
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 **************************************************************************/
21 #include <X11/Xutil.h>
22 #include <X11/Xatom.h>
23 #include <X11/extensions/Xrender.h>
27 #include <pango/pangocairo.h>
34 /************************************************************
35 * !!! This design is experimental and not yet fully implemented !!!!!!!!!!!!!
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).
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.
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)
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
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).
72 ************************************************************/
74 void init_rendering(void *obj
, int pos
)
78 // initialize fixed position/size
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
);
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
);
96 void rendering(void *obj
)
98 Panel
*panel
= (Panel
*)obj
;
100 size_by_content(&panel
->area
);
101 size_by_layout(&panel
->area
, 0, 1);
103 refresh(&panel
->area
);
107 void size_by_content (Area
*a
)
109 // don't resize hiden objects
110 if (!a
->on_screen
) return;
112 // children node are resized before its parent
114 for (l
= a
->list
; l
; l
= l
->next
)
115 size_by_content(l
->data
);
117 // calculate area's size
119 if (a
->resize
&& a
->size_mode
== SIZE_BY_CONTENT
) {
124 // 'size' changed => 'resize = 1' on the parent
125 ((Area
*)a
->parent
)->resize
= 1;
133 // calculate total size of all children including
135 int children_size(Area
*a
, int horizontal
)
140 for (l
= a
->list
; l
; l
= l
->next
) {
141 Area
*child
= ((Area
*)l
->data
);
142 if (!child
->on_screen
) continue;
145 size
+= child
->width
+ a
->paddingx
;
147 size
+= child
->height
+ a
->paddingy
;
154 // calculate chilren's align offset depending on the align type
155 int align_offset(Area
*a
, int align
, int horizontal
)
158 int child_size
= children_size(a
, horizontal
);
170 return (size
- child_size
) / 2;
173 return size
- child_size
;
181 void size_by_layout (Area
*a
, int pos
, int level
)
183 // don't resize hiden objects
184 if (!a
->on_screen
) return;
186 // parent node is resized before its children
187 // calculate area's size
189 if (a
->resize
&& a
->size_mode
== SIZE_BY_LAYOUT
) {
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
)
203 // update position of childs
204 pos
+= a
->paddingxlr
+ a
->bg
->border
.width
;
206 for (l
= a
->list
; l
; l
= l
->next
) {
207 Area
*child
= ((Area
*)l
->data
);
208 if (!child
->on_screen
) continue;
211 if (panel_horizontal
) {
212 if (pos
!= child
->posx
) {
213 // pos changed => redraw
215 child
->on_changed
= 1;
219 if (pos
!= child
->posy
) {
220 // pos changed => redraw
222 child
->on_changed
= 1;
226 /*// position of each visible object
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");*/
231 int offset
= align_offset(child
, panel_config
.g_task
.align
, panel_horizontal
);
232 size_by_layout(child
, pos
+ offset
, level
+ 1);
234 if (panel_horizontal
)
235 pos
+= child
->width
+ a
->paddingx
;
237 pos
+= child
->height
+ a
->paddingx
;
243 if (a
->_on_change_layout
)
244 a
->_on_change_layout (a
);
249 void refresh (Area
*a
)
251 // don't draw and resize hide objects
252 if (!a
->on_screen
) return;
254 // don't draw transparent objects (without foreground and without background)
257 // force redraw of child
259 //for (l = a->list ; l ; l = l->next)
260 //((Area*)l->data)->redraw = 1;
262 //printf("draw area posx %d, width %d\n", a->posx, a->width);
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
);
270 // and then refresh child object
272 for (l
= a
->list
; l
; l
= l
->next
)
277 int resize_by_layout(void *obj
, int maximum_size
)
279 Area
*child
, *a
= (Area
*)obj
;
280 int size
, nb_by_content
=0, nb_by_layout
=0;
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
));
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
;
292 if (child
->on_screen
&& child
->size_mode
== SIZE_BY_LAYOUT
)
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
);
299 int width
=0, modulo
=0, old_width
;
301 width
= size
/ nb_by_layout
;
302 modulo
= size
% nb_by_layout
;
303 if (width
> maximum_size
&& maximum_size
!= 0) {
304 width
= maximum_size
;
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
;
319 if (child
->width
!= old_width
)
320 child
->on_changed
= 1;
325 // detect free size for SIZE_BY_LAYOUT's Area
326 size
= a
->height
- (2 * (a
->paddingxlr
+ a
->bg
->border
.width
));
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
;
334 if (child
->on_screen
&& child
->size_mode
== SIZE_BY_LAYOUT
)
337 if (nb_by_content
+nb_by_layout
)
338 size
-= ((nb_by_content
+nb_by_layout
-1) * a
->paddingx
);
340 int height
=0, modulo
=0, old_height
;
342 height
= size
/ nb_by_layout
;
343 modulo
= size
% nb_by_layout
;
344 if (height
> maximum_size
&& maximum_size
!= 0) {
345 height
= maximum_size
;
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
;
360 if (child
->height
!= old_height
)
361 child
->on_changed
= 1;
369 void set_redraw (Area
*a
)
374 for (l
= a
->list
; l
; l
= l
->next
)
380 Area
*parent
= (Area
*)a
->parent
;
384 if (panel_horizontal
)
392 Area
*parent
= (Area
*)a
->parent
;
401 if (a
->pix
) XFreePixmap (server
.dsp
, a
->pix
);
402 a
->pix
= XCreatePixmap (server
.dsp
, server
.root_win
, a
->width
, a
->height
, server
.depth
);
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);
412 cs
= cairo_xlib_surface_create (server
.dsp
, a
->pix
, server
.visual
, a
->width
, a
->height
);
413 c
= cairo_create (cs
);
415 draw_background (a
, c
);
417 if (a
->_draw_foreground
)
418 a
->_draw_foreground(a
, c
);
421 cairo_surface_destroy (cs
);
425 void draw_background (Area
*a
, cairo_t
*c
)
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
);
434 if (a
->bg
->border
.width
> 0 && a
->bg
->border
.alpha
> 0.0) {
435 cairo_set_line_width (c
, a
->bg
->border
.width
);
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
);
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;
452 // et ensuite faire la changement d'unite du repere
453 // car ce qui doit reste inchangee est les traits et pas la direction
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);
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);
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);
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
);
478 //cairo_pattern_destroy (linpat);
483 void remove_area (Area
*a
)
485 Area
*parent
= (Area
*)a
->parent
;
487 parent
->list
= g_slist_remove(parent
->list
, a
);
493 void add_area (Area
*a
)
495 Area
*parent
= (Area
*)a
->parent
;
497 parent
->list
= g_slist_append(parent
->list
, a
);
503 void free_area (Area
*a
)
506 for (l0
= a
->list
; l0
; l0
= l0
->next
)
507 free_area (l0
->data
);
510 g_slist_free(a
->list
);
514 XFreePixmap (server
.dsp
, a
->pix
);
520 void draw_rect(cairo_t
*c
, double x
, double y
, double w
, double h
, double r
)
523 double c1
= 0.55228475 * r
;
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
);
536 cairo_rectangle(c
, x
, y
, w
, h
);
540 void clear_pixmap(Pixmap p
, int x
, int y
, int w
, int h
)
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
);
This page took 0.092396 seconds and 4 git commands to generate.