]> Dogcows Code - chaz/tint2/blob - src/util/area.c
rendering engine : _on_change_layout() called when pos/size changed
[chaz/tint2] / src / util / area.c
1 /**************************************************************************
2 *
3 * Tint2 : area
4 *
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
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 * - 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
67 *
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).
72 *
73 ************************************************************/
74
75 void init_rendering(void *obj, int pos)
76 {
77 Area *a = (Area*)obj;
78
79 // initialize fixed position/size
80 GSList *l;
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);
87 }
88 else {
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);
92 }
93 }
94 }
95
96
97 void rendering(void *obj)
98 {
99 Panel *panel = (Panel*)obj;
100
101 size_by_content(&panel->area);
102 size_by_layout(&panel->area, 0, 0);
103
104 refresh(&panel->area);
105 }
106
107
108 void size_by_content (Area *a)
109 {
110 // don't resize hiden objects
111 if (!a->on_screen) return;
112
113 // children node are resized before its parent
114 GSList *l;
115 for (l = a->list; l ; l = l->next)
116 size_by_content(l->data);
117
118 // calculate area's size
119 a->on_changed = 0;
120 if (a->resize && a->size_mode == SIZE_BY_CONTENT) {
121 a->resize = 0;
122
123 if (a->_resize) {
124 if (a->_resize(a)) {
125 // 'size' changed => 'resize = 1' on the parent and redraw object
126 ((Area*)a->parent)->resize = 1;
127 a->on_changed = 1;
128 a->redraw = 1;
129 }
130 }
131 }
132 }
133
134
135 void size_by_layout (Area *a, int pos, int level)
136 {
137 // don't resize hiden objects
138 if (!a->on_screen) return;
139
140 // parent node is resized before its children
141 // calculate area's size
142 GSList *l;
143 if (a->resize && a->size_mode == SIZE_BY_LAYOUT) {
144 a->resize = 0;
145
146 if (a->_resize) {
147 if (a->_resize(a)) {
148 // if 'size' changed then 'resize = 1' on childs with SIZE_BY_LAYOUT
149 a->on_changed = 1;
150 for (l = a->list; l ; l = l->next) {
151 if (((Area*)l->data)->size_mode == SIZE_BY_LAYOUT)
152 ((Area*)l->data)->resize = 1;
153 }
154 }
155 }
156 }
157
158 // update position of childs
159 pos += a->paddingxlr + a->bg->border.width;
160 int i=0;
161 for (l = a->list; l ; l = l->next) {
162 Area *child = ((Area*)l->data);
163 if (!child->on_screen) continue;
164 i++;
165
166 if (panel_horizontal) {
167 if (pos != child->posx) {
168 // pos changed => redraw
169 child->posx = pos;
170 child->on_changed = 1;
171 child->redraw = 1;
172 }
173 }
174 else {
175 if (pos != child->posy) {
176 // pos changed => redraw
177 child->posy = pos;
178 child->on_changed = 1;
179 child->redraw = 1;
180 }
181 }
182 //printf("level %d, object %d, pos %d\n", level, i, pos);
183
184 size_by_layout(child, pos, level+1);
185
186 if (panel_horizontal)
187 pos += child->width + a->paddingx;
188 else
189 pos += child->height + a->paddingx;
190 }
191
192 if (a->on_changed && a->_on_change_layout)
193 a->_on_change_layout (a);
194 }
195
196
197 void refresh (Area *a)
198 {
199 // don't draw and resize hide objects
200 if (!a->on_screen) return;
201
202 // don't draw transparent objects (without foreground and without background)
203 if (a->redraw) {
204 a->redraw = 0;
205 // force redraw of child
206 GSList *l;
207 for (l = a->list ; l ; l = l->next)
208 ((Area*)l->data)->redraw = 1;
209 // set_redraw(l->data);
210
211 //printf("draw area posx %d, width %d\n", a->posx, a->width);
212 draw(a);
213 }
214
215 // draw current Area
216 if (a->pix == 0) printf("empty area posx %d, width %d\n", a->posx, a->width);
217 XCopyArea (server.dsp, a->pix, ((Panel *)a->panel)->temp_pmap, server.gc, 0, 0, a->width, a->height, a->posx, a->posy);
218
219 // and then refresh child object
220 GSList *l;
221 for (l = a->list; l ; l = l->next)
222 refresh(l->data);
223 }
224
225
226 int resize_by_layout(void *obj)
227 {
228 Area *child, *a = (Area*)obj;
229 int size, nb_by_content=0, nb_by_layout=0;
230
231 if (panel_horizontal) {
232 // detect free size for SIZE_BY_LAYOUT's Area
233 size = a->width - (2 * (a->paddingxlr + a->bg->border.width));
234 GSList *l;
235 for (l = a->list ; l ; l = l->next) {
236 child = (Area*)l->data;
237 if (child->on_screen && child->size_mode == SIZE_BY_CONTENT) {
238 size -= child->width;
239 nb_by_content++;
240 }
241 if (child->on_screen && child->size_mode == SIZE_BY_LAYOUT)
242 nb_by_layout++;
243 }
244 if (nb_by_content+nb_by_layout)
245 size -= ((nb_by_content+nb_by_layout-1) * a->paddingx);
246 //printf("resize_panel : size_panel %d, size_layout %d\n", panel->area.width, size);
247
248 int width=0, modulo=0;
249 if (nb_by_layout) {
250 width = size / nb_by_layout;
251 modulo = size % nb_by_layout;
252 }
253
254 // resize SIZE_BY_LAYOUT objects
255 for (l = a->list ; l ; l = l->next) {
256 child = (Area*)l->data;
257 if (child->on_screen && child->size_mode == SIZE_BY_LAYOUT) {
258 child->width = width;
259 child->resize = 1;
260 if (modulo) {
261 child->width++;
262 modulo--;
263 }
264 }
265 }
266 }
267 else {
268 // detect free size for SIZE_BY_LAYOUT's Area
269 size = a->height - (2 * (a->paddingxlr + a->bg->border.width));
270 GSList *l;
271 for (l = a->list ; l ; l = l->next) {
272 child = (Area*)l->data;
273 if (child->on_screen && child->size_mode == SIZE_BY_CONTENT) {
274 size -= child->height;
275 nb_by_content++;
276 }
277 if (child->on_screen && child->size_mode == SIZE_BY_LAYOUT)
278 nb_by_layout++;
279 }
280 if (nb_by_content+nb_by_layout)
281 size -= ((nb_by_content+nb_by_layout-1) * a->paddingx);
282
283 int height=0, modulo=0;
284 if (nb_by_layout) {
285 height = size / nb_by_layout;
286 modulo = size % nb_by_layout;
287 }
288
289 // resize SIZE_BY_LAYOUT objects
290 for (l = a->list ; l ; l = l->next) {
291 child = (Area*)l->data;
292 if (child->on_screen && child->size_mode == SIZE_BY_LAYOUT) {
293 child->height = height;
294 child->resize = 1;
295 if (modulo) {
296 child->height++;
297 modulo--;
298 }
299 }
300 }
301 }
302 return 0;
303 }
304
305
306 void set_redraw (Area *a)
307 {
308 a->redraw = 1;
309
310 GSList *l;
311 for (l = a->list ; l ; l = l->next)
312 set_redraw(l->data);
313 }
314
315 void hide(Area *a)
316 {
317 Area *parent = (Area*)a->parent;
318
319 a->on_screen = 0;
320 parent->resize = 1;
321 if (panel_horizontal)
322 a->width = 0;
323 else
324 a->height = 0;
325 }
326
327 void show(Area *a)
328 {
329 Area *parent = (Area*)a->parent;
330
331 a->on_screen = 1;
332 parent->resize = 1;
333 a->resize = 1;
334 }
335
336 void draw (Area *a)
337 {
338 if (a->pix) XFreePixmap (server.dsp, a->pix);
339 a->pix = XCreatePixmap (server.dsp, server.root_win, a->width, a->height, server.depth);
340
341 // add layer of root pixmap (or clear pixmap if real_transparency==true)
342 if (server.real_transparency)
343 clear_pixmap(a->pix, 0 ,0, a->width, a->height);
344 XCopyArea (server.dsp, ((Panel *)a->panel)->temp_pmap, a->pix, server.gc, a->posx, a->posy, a->width, a->height, 0, 0);
345
346 cairo_surface_t *cs;
347 cairo_t *c;
348
349 cs = cairo_xlib_surface_create (server.dsp, a->pix, server.visual, a->width, a->height);
350 c = cairo_create (cs);
351
352 draw_background (a, c);
353
354 if (a->_draw_foreground)
355 a->_draw_foreground(a, c);
356
357 cairo_destroy (c);
358 cairo_surface_destroy (cs);
359 }
360
361
362 void draw_background (Area *a, cairo_t *c)
363 {
364 if (a->bg->back.alpha > 0.0) {
365 //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);
366 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);
367 cairo_set_source_rgba(c, a->bg->back.color[0], a->bg->back.color[1], a->bg->back.color[2], a->bg->back.alpha);
368 cairo_fill(c);
369 }
370
371 if (a->bg->border.width > 0 && a->bg->border.alpha > 0.0) {
372 cairo_set_line_width (c, a->bg->border.width);
373
374 // draw border inside (x, y, width, height)
375 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);
376 /*
377 // convert : radian = degre * M_PI/180
378 // definir le degrade dans un carre de (0,0) (100,100)
379 // ensuite ce degrade est extrapoler selon le ratio width/height
380 // dans repere (0, 0) (100, 100)
381 double X0, Y0, X1, Y1, degre;
382 // x = X * (a->width / 100), y = Y * (a->height / 100)
383 double x0, y0, x1, y1;
384 X0 = 0;
385 Y0 = 100;
386 X1 = 100;
387 Y1 = 0;
388 degre = 45;
389 // et ensuite faire la changement d'unite du repere
390 // car ce qui doit reste inchangee est les traits et pas la direction
391
392 // il faut d'abord appliquer une rotation de 90 (et -180 si l'angle est superieur a 180)
393 // ceci peut etre applique une fois pour toute au depart
394 // ensuite calculer l'angle dans le nouveau repare
395 // puis faire une rotation de 90
396 x0 = X0 * ((double)a->width / 100);
397 x1 = X1 * ((double)a->width / 100);
398 y0 = Y0 * ((double)a->height / 100);
399 y1 = Y1 * ((double)a->height / 100);
400
401 x0 = X0 * ((double)a->height / 100);
402 x1 = X1 * ((double)a->height / 100);
403 y0 = Y0 * ((double)a->width / 100);
404 y1 = Y1 * ((double)a->width / 100);
405
406 cairo_pattern_t *linpat;
407 linpat = cairo_pattern_create_linear (x0, y0, x1, y1);
408 cairo_pattern_add_color_stop_rgba (linpat, 0, a->border.color[0], a->border.color[1], a->border.color[2], a->border.alpha);
409 cairo_pattern_add_color_stop_rgba (linpat, 1, a->border.color[0], a->border.color[1], a->border.color[2], 0);
410 cairo_set_source (c, linpat);
411 */
412 cairo_set_source_rgba (c, a->bg->border.color[0], a->bg->border.color[1], a->bg->border.color[2], a->bg->border.alpha);
413
414 cairo_stroke (c);
415 //cairo_pattern_destroy (linpat);
416 }
417 }
418
419
420 void remove_area (Area *a)
421 {
422 Area *parent = (Area*)a->parent;
423
424 parent->list = g_slist_remove(parent->list, a);
425 set_redraw (parent);
426
427 }
428
429
430 void add_area (Area *a)
431 {
432 Area *parent = (Area*)a->parent;
433
434 parent->list = g_slist_remove(parent->list, a);
435 set_redraw (parent);
436
437 }
438
439
440 void free_area (Area *a)
441 {
442 GSList *l0;
443 for (l0 = a->list; l0 ; l0 = l0->next)
444 free_area (l0->data);
445
446 if (a->list) {
447 g_slist_free(a->list);
448 a->list = 0;
449 }
450 if (a->pix) {
451 XFreePixmap (server.dsp, a->pix);
452 a->pix = 0;
453 }
454 }
455
456
457 void draw_rect(cairo_t *c, double x, double y, double w, double h, double r)
458 {
459 if (r > 0.0) {
460 double c1 = 0.55228475 * r;
461
462 cairo_move_to(c, x+r, y);
463 cairo_rel_line_to(c, w-2*r, 0);
464 cairo_rel_curve_to(c, c1, 0.0, r, c1, r, r);
465 cairo_rel_line_to(c, 0, h-2*r);
466 cairo_rel_curve_to(c, 0.0, c1, c1-r, r, -r, r);
467 cairo_rel_line_to (c, -w +2*r, 0);
468 cairo_rel_curve_to (c, -c1, 0, -r, -c1, -r, -r);
469 cairo_rel_line_to (c, 0, -h + 2 * r);
470 cairo_rel_curve_to (c, 0, -c1, r - c1, -r, r, -r);
471 }
472 else
473 cairo_rectangle(c, x, y, w, h);
474 }
475
476
477 void clear_pixmap(Pixmap p, int x, int y, int w, int h)
478 {
479 Picture pict = XRenderCreatePicture(server.dsp, p, XRenderFindVisualFormat(server.dsp, server.visual), 0, 0);
480 XRenderColor col = { .red=0, .green=0, .blue=0, .alpha=0 };
481 XRenderFillRectangle(server.dsp, PictOpSrc, pict, &col, x, y, w, h);
482 XRenderFreePicture(server.dsp, pict);
483 }
This page took 0.063374 seconds and 4 git commands to generate.