]>
Dogcows Code - chaz/tint2/blob - area.c
0320bbb549a32e3bd0206be094f12c8d3ecd153c
1 /**************************************************************************
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
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 * - if 'size' changed then 'resize = 1' on childs with SIZE_BY_LAYOUT
63 * - calculate position (posx,posy) : parent is calculated before children
64 * - if 'position' changed then 'redraw = 1'
65 * 3) browse tree REDRAW
66 * - redraw needed objects : parent is drawn before children
68 * CONFIGURE PANEL'S LAYOUT :
69 * 'panel_items' parameter (in config) define the list and the order of nodes in tree's panel.
70 * 'panel_items = SC' define a panel with just Systray and Clock.
71 * So the tree 'Panel.Area' will have 2 childs (Systray and Clock).
73 ************************************************************/
75 void init_rendering(void *obj
, int pos
)
79 // initialize fixed position/size
81 for (l
= a
->list
; l
; l
= l
->next
) {
82 Area
*child
= ((Area
*)l
->data
);
83 if (panel_horizontal
) {
84 child
->posy
= pos
+ a
->bg
->border
.width
+ a
->paddingy
;
85 child
->height
= a
->height
- (2 * (a
->bg
->border
.width
+ a
->paddingy
));
86 init_rendering(child
, child
->posy
);
89 child
->posx
= pos
+ a
->bg
->border
.width
+ a
->paddingy
;
90 child
->width
= a
->width
- (2 * (a
->bg
->border
.width
+ a
->paddingy
));
91 init_rendering(child
, child
->posx
);
97 void rendering(void *obj
)
99 Panel
*panel
= (Panel
*)obj
;
101 size_by_content(&panel
->area
);
102 size_by_layout(&panel
->area
, 0, 0);
104 refresh(&panel
->area
);
108 void size_by_content (Area
*a
)
110 // don't resize hiden objects
111 if (!a
->on_screen
) return;
113 // children node are resized before its parent
115 for (l
= a
->list
; l
; l
= l
->next
)
116 size_by_content(l
->data
);
118 // calculate area's size
119 if (a
->resize
&& a
->size_mode
== SIZE_BY_CONTENT
) {
124 // 'size' changed => 'resize = 1' on the parent and redraw object
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 // if 'size' changed then 'resize = 1' on childs with SIZE_BY_LAYOUT
147 for (l
= a
->list
; l
; l
= l
->next
) {
148 if (((Area
*)l
->data
)->size_mode
== SIZE_BY_LAYOUT
)
149 ((Area
*)l
->data
)->resize
= 1;
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
171 if (pos
!= child
->posy
) {
172 // pos changed => redraw
177 //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
;
189 void refresh (Area
*a
)
191 // don't draw and resize hide objects
192 if (!a
->on_screen
) return;
194 // don't draw transparent objects (without foreground and without background)
197 // force redraw of child
199 for (l
= a
->list
; l
; l
= l
->next
)
200 ((Area
*)l
->data
)->redraw
= 1;
201 // set_redraw(l->data);
203 //printf("draw area posx %d, width %d\n", a->posx, a->width);
208 if (a
->pix
== 0) printf("empty area posx %d, width %d\n", a
->posx
, a
->width
);
209 XCopyArea (server
.dsp
, a
->pix
, ((Panel
*)a
->panel
)->temp_pmap
, server
.gc
, 0, 0, a
->width
, a
->height
, a
->posx
, a
->posy
);
211 // and then refresh child object
213 for (l
= a
->list
; l
; l
= l
->next
)
218 int resize_by_layout(void *obj
)
220 Taskbar
*taskbar
= (Taskbar
*)obj
;
221 Panel
*panel
= (Panel
*)taskbar
->area
.panel
;
224 int task_count
, border_width
;
226 //printf("resize_taskbar : posx et width des taches\n");
227 taskbar
->area
.redraw
= 1;
229 border_width
= taskbar
->area
.bg
->border
.width
;
231 if (panel_horizontal
) {
232 int pixel_width
, modulo_width
=0;
235 // new task width for 'desktop'
236 task_count
= g_slist_length(taskbar
->area
.list
);
237 if (!task_count
) pixel_width
= panel
->g_task
.maximum_width
;
239 taskbar_width
= taskbar
->area
.width
- (2 * border_width
) - (2 * panel
->g_taskbar
.area
.paddingxlr
);
240 if (task_count
>1) taskbar_width
-= ((task_count
-1) * panel
->g_taskbar
.area
.paddingx
);
242 pixel_width
= taskbar_width
/ task_count
;
243 if (pixel_width
> panel
->g_task
.maximum_width
)
244 pixel_width
= panel
->g_task
.maximum_width
;
246 modulo_width
= taskbar_width
% task_count
;
249 taskbar
->task_width
= pixel_width
;
250 taskbar
->task_modulo
= modulo_width
;
251 taskbar
->text_width
= pixel_width
- panel
->g_task
.text_posx
- panel
->g_task
.area
.bg
->border
.width
- panel
->g_task
.area
.paddingx
;
253 // change pos_x and width for all tasks
254 for (l
= taskbar
->area
.list
; l
; l
= l
->next
) {
256 if (!tsk
->area
.on_screen
) continue;
257 set_task_redraw(tsk
); // always redraw task, because the background could have changed (taskbar_active_id)
258 tsk
->area
.width
= pixel_width
;
259 // TODO : move later (when posx is known)
260 // long value[] = { panel->posx+x, panel->posy, pixel_width, panel->area.height };
261 // XChangeProperty (server.dsp, tsk->win, server.atom._NET_WM_ICON_GEOMETRY, XA_CARDINAL, 32, PropModeReplace, (unsigned char*)value, 4);
270 int pixel_height
, modulo_height
=0;
273 // new task width for 'desktop'
274 task_count
= g_slist_length(taskbar
->area
.list
);
275 if (!task_count
) pixel_height
= panel
->g_task
.maximum_height
;
277 taskbar_height
= taskbar
->area
.height
- (2 * border_width
) - (2 * panel
->g_taskbar
.area
.paddingxlr
);
278 if (task_count
>1) taskbar_height
-= ((task_count
-1) * panel
->g_taskbar
.area
.paddingx
);
280 pixel_height
= taskbar_height
/ task_count
;
281 if (pixel_height
> panel
->g_task
.maximum_height
)
282 pixel_height
= panel
->g_task
.maximum_height
;
284 modulo_height
= taskbar_height
% task_count
;
287 taskbar
->task_width
= pixel_height
;
288 taskbar
->task_modulo
= modulo_height
;
289 taskbar
->text_width
= taskbar
->area
.width
- (2 * panel
->g_taskbar
.area
.paddingy
) - panel
->g_task
.text_posx
- panel
->g_task
.area
.bg
->border
.width
- panel
->g_task
.area
.paddingx
;
291 // change pos_y and height for all tasks
292 for (l
= taskbar
->area
.list
; l
; l
= l
->next
) {
294 if (!tsk
->area
.on_screen
) continue;
295 set_task_redraw(tsk
); // always redraw task, because the background could have changed (taskbar_active_id)
296 tsk
->area
.height
= pixel_height
;
297 // TODO : move later (when posy is known)
298 // long value[] = { panel->posx, panel->posy+y, panel->area.width, pixel_height };
299 // XChangeProperty (server.dsp, tsk->win, server.atom._NET_WM_ICON_GEOMETRY, XA_CARDINAL, 32, PropModeReplace, (unsigned char*)value, 4);
311 void set_redraw (Area
*a
)
316 for (l
= a
->list
; l
; l
= l
->next
)
323 if (a
->pix
) XFreePixmap (server
.dsp
, a
->pix
);
324 a
->pix
= XCreatePixmap (server
.dsp
, server
.root_win
, a
->width
, a
->height
, server
.depth
);
326 // add layer of root pixmap (or clear pixmap if real_transparency==true)
327 if (server
.real_transparency
)
328 clear_pixmap(a
->pix
, 0 ,0, a
->width
, a
->height
);
329 XCopyArea (server
.dsp
, ((Panel
*)a
->panel
)->temp_pmap
, a
->pix
, server
.gc
, a
->posx
, a
->posy
, a
->width
, a
->height
, 0, 0);
334 cs
= cairo_xlib_surface_create (server
.dsp
, a
->pix
, server
.visual
, a
->width
, a
->height
);
335 c
= cairo_create (cs
);
337 draw_background (a
, c
);
339 if (a
->_draw_foreground
)
340 a
->_draw_foreground(a
, c
);
343 cairo_surface_destroy (cs
);
347 void draw_background (Area
*a
, cairo_t
*c
)
349 if (a
->bg
->back
.alpha
> 0.0) {
350 //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);
351 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);
352 cairo_set_source_rgba(c
, a
->bg
->back
.color
[0], a
->bg
->back
.color
[1], a
->bg
->back
.color
[2], a
->bg
->back
.alpha
);
356 if (a
->bg
->border
.width
> 0 && a
->bg
->border
.alpha
> 0.0) {
357 cairo_set_line_width (c
, a
->bg
->border
.width
);
359 // draw border inside (x, y, width, height)
360 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
);
362 // convert : radian = degre * M_PI/180
363 // definir le degrade dans un carre de (0,0) (100,100)
364 // ensuite ce degrade est extrapoler selon le ratio width/height
365 // dans repere (0, 0) (100, 100)
366 double X0, Y0, X1, Y1, degre;
367 // x = X * (a->width / 100), y = Y * (a->height / 100)
368 double x0, y0, x1, y1;
374 // et ensuite faire la changement d'unite du repere
375 // car ce qui doit reste inchangee est les traits et pas la direction
377 // il faut d'abord appliquer une rotation de 90 (et -180 si l'angle est superieur a 180)
378 // ceci peut etre applique une fois pour toute au depart
379 // ensuite calculer l'angle dans le nouveau repare
380 // puis faire une rotation de 90
381 x0 = X0 * ((double)a->width / 100);
382 x1 = X1 * ((double)a->width / 100);
383 y0 = Y0 * ((double)a->height / 100);
384 y1 = Y1 * ((double)a->height / 100);
386 x0 = X0 * ((double)a->height / 100);
387 x1 = X1 * ((double)a->height / 100);
388 y0 = Y0 * ((double)a->width / 100);
389 y1 = Y1 * ((double)a->width / 100);
391 cairo_pattern_t *linpat;
392 linpat = cairo_pattern_create_linear (x0, y0, x1, y1);
393 cairo_pattern_add_color_stop_rgba (linpat, 0, a->border.color[0], a->border.color[1], a->border.color[2], a->border.alpha);
394 cairo_pattern_add_color_stop_rgba (linpat, 1, a->border.color[0], a->border.color[1], a->border.color[2], 0);
395 cairo_set_source (c, linpat);
397 cairo_set_source_rgba (c
, a
->bg
->border
.color
[0], a
->bg
->border
.color
[1], a
->bg
->border
.color
[2], a
->bg
->border
.alpha
);
400 //cairo_pattern_destroy (linpat);
405 void remove_area (Area
*a
)
407 Area
*parent
= (Area
*)a
->parent
;
409 parent
->list
= g_slist_remove(parent
->list
, a
);
415 void add_area (Area
*a
)
417 Area
*parent
= (Area
*)a
->parent
;
419 parent
->list
= g_slist_remove(parent
->list
, a
);
425 void free_area (Area
*a
)
428 for (l0
= a
->list
; l0
; l0
= l0
->next
)
429 free_area (l0
->data
);
432 g_slist_free(a
->list
);
436 XFreePixmap (server
.dsp
, a
->pix
);
442 void draw_rect(cairo_t
*c
, double x
, double y
, double w
, double h
, double r
)
445 double c1
= 0.55228475 * r
;
447 cairo_move_to(c
, x
+r
, y
);
448 cairo_rel_line_to(c
, w
-2*r
, 0);
449 cairo_rel_curve_to(c
, c1
, 0.0, r
, c1
, r
, r
);
450 cairo_rel_line_to(c
, 0, h
-2*r
);
451 cairo_rel_curve_to(c
, 0.0, c1
, c1
-r
, r
, -r
, r
);
452 cairo_rel_line_to (c
, -w
+2*r
, 0);
453 cairo_rel_curve_to (c
, -c1
, 0, -r
, -c1
, -r
, -r
);
454 cairo_rel_line_to (c
, 0, -h
+ 2 * r
);
455 cairo_rel_curve_to (c
, 0, -c1
, r
- c1
, -r
, r
, -r
);
458 cairo_rectangle(c
, x
, y
, w
, h
);
462 void clear_pixmap(Pixmap p
, int x
, int y
, int w
, int h
)
464 Picture pict
= XRenderCreatePicture(server
.dsp
, p
, XRenderFindVisualFormat(server
.dsp
, server
.visual
), 0, 0);
465 XRenderColor col
= { .red
=0, .green
=0, .blue
=0, .alpha
=0 };
466 XRenderFillRectangle(server
.dsp
, PictOpSrc
, pict
, &col
, x
, y
, w
, h
);
467 XRenderFreePicture(server
.dsp
, pict
);
This page took 0.055014 seconds and 3 git commands to generate.