]>
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, 0);
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 void size_by_layout (Area
*a
, int pos
, int level
)
135 // don't resize hiden objects
136 if (!a
->on_screen
) return;
138 // parent node is resized before its children
139 // calculate area's size
141 if (a
->resize
&& a
->size_mode
== SIZE_BY_LAYOUT
) {
146 // resize childs with SIZE_BY_LAYOUT
147 for (l
= a
->list
; l
; l
= l
->next
) {
148 Area
*child
= ((Area
*)l
->data
);
149 if (child
->size_mode
== SIZE_BY_LAYOUT
&& child
->list
)
155 // update position of childs
156 pos
+= a
->paddingxlr
+ a
->bg
->border
.width
;
158 for (l
= a
->list
; l
; l
= l
->next
) {
159 Area
*child
= ((Area
*)l
->data
);
160 if (!child
->on_screen
) continue;
163 if (panel_horizontal
) {
164 if (pos
!= child
->posx
) {
165 // pos changed => redraw
167 child
->on_changed
= 1;
171 if (pos
!= child
->posy
) {
172 // pos changed => redraw
174 child
->on_changed
= 1;
178 //printf("level %d, object %d, pos %d\n", level, i, pos);
179 size_by_layout(child
, pos
, level
+1);
181 if (panel_horizontal
)
182 pos
+= child
->width
+ a
->paddingx
;
184 pos
+= child
->height
+ a
->paddingx
;
190 if (a
->_on_change_layout
)
191 a
->_on_change_layout (a
);
196 void refresh (Area
*a
)
198 // don't draw and resize hide objects
199 if (!a
->on_screen
) return;
201 // don't draw transparent objects (without foreground and without background)
204 // force redraw of child
206 //for (l = a->list ; l ; l = l->next)
207 //((Area*)l->data)->redraw = 1;
209 //printf("draw area posx %d, width %d\n", a->posx, a->width);
214 if (a
->pix
== 0) printf("empty area posx %d, width %d\n", a
->posx
, a
->width
);
215 XCopyArea (server
.dsp
, a
->pix
, ((Panel
*)a
->panel
)->temp_pmap
, server
.gc
, 0, 0, a
->width
, a
->height
, a
->posx
, a
->posy
);
217 // and then refresh child object
219 for (l
= a
->list
; l
; l
= l
->next
)
224 int resize_by_layout(void *obj
, int maximum_size
)
226 Area
*child
, *a
= (Area
*)obj
;
227 int size
, nb_by_content
=0, nb_by_layout
=0;
229 if (panel_horizontal
) {
230 // detect free size for SIZE_BY_LAYOUT's Area
231 size
= a
->width
- (2 * (a
->paddingxlr
+ a
->bg
->border
.width
));
233 for (l
= a
->list
; l
; l
= l
->next
) {
234 child
= (Area
*)l
->data
;
235 if (child
->on_screen
&& child
->size_mode
== SIZE_BY_CONTENT
) {
236 size
-= child
->width
;
239 if (child
->on_screen
&& child
->size_mode
== SIZE_BY_LAYOUT
)
242 if (nb_by_content
+nb_by_layout
)
243 size
-= ((nb_by_content
+nb_by_layout
-1) * a
->paddingx
);
244 //printf("resize_panel : size_panel %d, size_layout %d\n", panel->area.width, size);
246 int width
=0, modulo
=0, old_width
;
248 width
= size
/ nb_by_layout
;
249 modulo
= size
% nb_by_layout
;
250 if (width
> maximum_size
&& maximum_size
!= 0) {
251 width
= maximum_size
;
256 // resize SIZE_BY_LAYOUT objects
257 for (l
= a
->list
; l
; l
= l
->next
) {
258 child
= (Area
*)l
->data
;
259 if (child
->on_screen
&& child
->size_mode
== SIZE_BY_LAYOUT
) {
260 old_width
= child
->width
;
261 child
->width
= width
;
266 if (child
->width
!= old_width
)
267 child
->on_changed
= 1;
272 // detect free size for SIZE_BY_LAYOUT's Area
273 size
= a
->height
- (2 * (a
->paddingxlr
+ a
->bg
->border
.width
));
275 for (l
= a
->list
; l
; l
= l
->next
) {
276 child
= (Area
*)l
->data
;
277 if (child
->on_screen
&& child
->size_mode
== SIZE_BY_CONTENT
) {
278 size
-= child
->height
;
281 if (child
->on_screen
&& child
->size_mode
== SIZE_BY_LAYOUT
)
284 if (nb_by_content
+nb_by_layout
)
285 size
-= ((nb_by_content
+nb_by_layout
-1) * a
->paddingx
);
287 int height
=0, modulo
=0, old_height
;
289 height
= size
/ nb_by_layout
;
290 modulo
= size
% nb_by_layout
;
291 if (height
> maximum_size
&& maximum_size
!= 0) {
292 height
= maximum_size
;
297 // resize SIZE_BY_LAYOUT objects
298 for (l
= a
->list
; l
; l
= l
->next
) {
299 child
= (Area
*)l
->data
;
300 if (child
->on_screen
&& child
->size_mode
== SIZE_BY_LAYOUT
) {
301 old_height
= child
->height
;
302 child
->height
= height
;
307 if (child
->height
!= old_height
)
308 child
->on_changed
= 1;
316 void set_redraw (Area
*a
)
321 for (l
= a
->list
; l
; l
= l
->next
)
327 Area
*parent
= (Area
*)a
->parent
;
331 if (panel_horizontal
)
339 Area
*parent
= (Area
*)a
->parent
;
348 if (a
->pix
) XFreePixmap (server
.dsp
, a
->pix
);
349 a
->pix
= XCreatePixmap (server
.dsp
, server
.root_win
, a
->width
, a
->height
, server
.depth
);
351 // add layer of root pixmap (or clear pixmap if real_transparency==true)
352 if (server
.real_transparency
)
353 clear_pixmap(a
->pix
, 0 ,0, a
->width
, a
->height
);
354 XCopyArea (server
.dsp
, ((Panel
*)a
->panel
)->temp_pmap
, a
->pix
, server
.gc
, a
->posx
, a
->posy
, a
->width
, a
->height
, 0, 0);
359 cs
= cairo_xlib_surface_create (server
.dsp
, a
->pix
, server
.visual
, a
->width
, a
->height
);
360 c
= cairo_create (cs
);
362 draw_background (a
, c
);
364 if (a
->_draw_foreground
)
365 a
->_draw_foreground(a
, c
);
368 cairo_surface_destroy (cs
);
372 void draw_background (Area
*a
, cairo_t
*c
)
374 if (a
->bg
->back
.alpha
> 0.0) {
375 //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);
376 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);
377 cairo_set_source_rgba(c
, a
->bg
->back
.color
[0], a
->bg
->back
.color
[1], a
->bg
->back
.color
[2], a
->bg
->back
.alpha
);
381 if (a
->bg
->border
.width
> 0 && a
->bg
->border
.alpha
> 0.0) {
382 cairo_set_line_width (c
, a
->bg
->border
.width
);
384 // draw border inside (x, y, width, height)
385 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
);
387 // convert : radian = degre * M_PI/180
388 // definir le degrade dans un carre de (0,0) (100,100)
389 // ensuite ce degrade est extrapoler selon le ratio width/height
390 // dans repere (0, 0) (100, 100)
391 double X0, Y0, X1, Y1, degre;
392 // x = X * (a->width / 100), y = Y * (a->height / 100)
393 double x0, y0, x1, y1;
399 // et ensuite faire la changement d'unite du repere
400 // car ce qui doit reste inchangee est les traits et pas la direction
402 // il faut d'abord appliquer une rotation de 90 (et -180 si l'angle est superieur a 180)
403 // ceci peut etre applique une fois pour toute au depart
404 // ensuite calculer l'angle dans le nouveau repare
405 // puis faire une rotation de 90
406 x0 = X0 * ((double)a->width / 100);
407 x1 = X1 * ((double)a->width / 100);
408 y0 = Y0 * ((double)a->height / 100);
409 y1 = Y1 * ((double)a->height / 100);
411 x0 = X0 * ((double)a->height / 100);
412 x1 = X1 * ((double)a->height / 100);
413 y0 = Y0 * ((double)a->width / 100);
414 y1 = Y1 * ((double)a->width / 100);
416 cairo_pattern_t *linpat;
417 linpat = cairo_pattern_create_linear (x0, y0, x1, y1);
418 cairo_pattern_add_color_stop_rgba (linpat, 0, a->border.color[0], a->border.color[1], a->border.color[2], a->border.alpha);
419 cairo_pattern_add_color_stop_rgba (linpat, 1, a->border.color[0], a->border.color[1], a->border.color[2], 0);
420 cairo_set_source (c, linpat);
422 cairo_set_source_rgba (c
, a
->bg
->border
.color
[0], a
->bg
->border
.color
[1], a
->bg
->border
.color
[2], a
->bg
->border
.alpha
);
425 //cairo_pattern_destroy (linpat);
430 void remove_area (Area
*a
)
432 Area
*parent
= (Area
*)a
->parent
;
434 parent
->list
= g_slist_remove(parent
->list
, a
);
440 void add_area (Area
*a
)
442 Area
*parent
= (Area
*)a
->parent
;
444 parent
->list
= g_slist_remove(parent
->list
, a
);
450 void free_area (Area
*a
)
453 for (l0
= a
->list
; l0
; l0
= l0
->next
)
454 free_area (l0
->data
);
457 g_slist_free(a
->list
);
461 XFreePixmap (server
.dsp
, a
->pix
);
467 void draw_rect(cairo_t
*c
, double x
, double y
, double w
, double h
, double r
)
470 double c1
= 0.55228475 * r
;
472 cairo_move_to(c
, x
+r
, y
);
473 cairo_rel_line_to(c
, w
-2*r
, 0);
474 cairo_rel_curve_to(c
, c1
, 0.0, r
, c1
, r
, r
);
475 cairo_rel_line_to(c
, 0, h
-2*r
);
476 cairo_rel_curve_to(c
, 0.0, c1
, c1
-r
, r
, -r
, r
);
477 cairo_rel_line_to (c
, -w
+2*r
, 0);
478 cairo_rel_curve_to (c
, -c1
, 0, -r
, -c1
, -r
, -r
);
479 cairo_rel_line_to (c
, 0, -h
+ 2 * r
);
480 cairo_rel_curve_to (c
, 0, -c1
, r
- c1
, -r
, r
, -r
);
483 cairo_rectangle(c
, x
, y
, w
, h
);
487 void clear_pixmap(Pixmap p
, int x
, int y
, int w
, int h
)
489 Picture pict
= XRenderCreatePicture(server
.dsp
, p
, XRenderFindVisualFormat(server
.dsp
, server
.visual
), 0, 0);
490 XRenderColor col
= { .red
=0, .green
=0, .blue
=0, .alpha
=0 };
491 XRenderFillRectangle(server
.dsp
, PictOpSrc
, pict
, &col
, x
, y
, w
, h
);
492 XRenderFreePicture(server
.dsp
, pict
);
This page took 0.06585 seconds and 5 git commands to generate.