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