]> Dogcows Code - chaz/tint2/blob - src/util/area.c
d68f4d7865a8e61de6ae7a36da9b03ec64a6a2c1
[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 all 'layering event'.
55 * 1) browse tree SIZE_BY_CONTENT
56 * - resize SIZE_BY_CONTENT node : children are resized before parent
57 * - if 'size' changed then 'resize = 1' on the parent
58 * 2) browse tree SIZE_BY_LAYOUT and POSITION
59 * - resize SIZE_BY_LAYOUT node : parent is resized before children
60 * - if 'size' changed then 'resize = 1' on childs with SIZE_BY_LAYOUT
61 * - calculate position (posx,posy) : parent is calculated before children
62 * - if 'position' changed then 'redraw = 1'
63 * 3) browse tree REDRAW
64 * - redraw needed objects : parent is drawn before children
65 *
66 * CONFIGURE PANEL'S LAYOUT :
67 * 'panel_items' parameter (in config) define the list and the order of nodes in tree's panel.
68 * 'panel_items = SC' define a panel with just Systray and Clock.
69 * So the tree 'Panel.Area' will have 2 childs (Systray and Clock).
70 *
71 ************************************************************/
72
73 void rendering(void *obj)
74 {
75 Panel *panel = (Panel*)obj;
76
77 size_by_content(&panel->area);
78 size_by_layout(&panel->area, 0, 0);
79
80 refresh(&panel->area);
81 }
82
83
84 void size_by_content (Area *a)
85 {
86 // don't resize hiden objects
87 if (!a->on_screen) return;
88
89 // children node are resized before its parent
90 GSList *l;
91 for (l = a->list; l ; l = l->next)
92 size_by_content(l->data);
93
94 // calculate area's size
95 if (a->resize && a->size_mode == SIZE_BY_CONTENT) {
96 a->resize = 0;
97
98 if (a->_resize) {
99 if (a->_resize(a)) {
100 // 'size' changed => 'resize = 1' on the parent and redraw object
101 ((Area*)a->parent)->resize = 1;
102 a->redraw = 1;
103 }
104 }
105 }
106 }
107
108
109 void size_by_layout (Area *a, int pos, int level)
110 {
111 // don't resize hiden objects
112 if (!a->on_screen) return;
113
114 // parent node is resized before its children
115 // calculate area's size
116 GSList *l;
117 if (a->resize && a->size_mode == SIZE_BY_LAYOUT) {
118 a->resize = 0;
119
120 if (a->_resize) {
121 if (a->_resize(a)) {
122 // if 'size' changed then 'resize = 1' on childs with SIZE_BY_LAYOUT
123 for (l = a->list; l ; l = l->next) {
124 if (((Area*)l->data)->size_mode == SIZE_BY_LAYOUT)
125 ((Area*)l->data)->resize = 1;
126 }
127 }
128 }
129 }
130
131 // update position of childs
132 pos += a->paddingxlr + a->bg->border.width;
133 int i=0;
134 for (l = a->list; l ; l = l->next) {
135 Area *child = ((Area*)l->data);
136 i++;
137
138 if (panel_horizontal) {
139 if (pos != child->posx) {
140 // pos changed => redraw
141 child->posx = pos;
142 child->redraw = 1;
143 }
144 }
145 else {
146 if (pos != child->posy) {
147 // pos changed => redraw
148 child->posy = pos;
149 child->redraw = 1;
150 }
151 }
152 //printf("level %d, object %d, pos %d\n", level, i, pos);
153
154 size_by_layout(child, pos, level+1);
155
156 if (panel_horizontal)
157 pos += child->width + a->paddingx;
158 else
159 pos += child->height + a->paddingx;
160 }
161 }
162
163
164 void refresh (Area *a)
165 {
166 // don't draw and resize hide objects
167 if (!a->on_screen) return;
168
169 // don't draw transparent objects (without foreground and without background)
170 if (a->redraw) {
171 a->redraw = 0;
172 // force redraw of child
173 GSList *l;
174 for (l = a->list ; l ; l = l->next)
175 ((Area*)l->data)->redraw = 1;
176 // set_redraw(l->data);
177
178 //printf("draw area posx %d, width %d\n", a->posx, a->width);
179 draw(a);
180 }
181
182 // draw current Area
183 if (a->pix == 0) printf("empty area posx %d, width %d\n", a->posx, a->width);
184 XCopyArea (server.dsp, a->pix, ((Panel *)a->panel)->temp_pmap, server.gc, 0, 0, a->width, a->height, a->posx, a->posy);
185
186 // and then refresh child object
187 GSList *l;
188 for (l = a->list; l ; l = l->next)
189 refresh(l->data);
190 }
191
192
193 void set_redraw (Area *a)
194 {
195 a->redraw = 1;
196
197 GSList *l;
198 for (l = a->list ; l ; l = l->next)
199 set_redraw(l->data);
200 }
201
202
203 void draw (Area *a)
204 {
205 if (a->pix) XFreePixmap (server.dsp, a->pix);
206 a->pix = XCreatePixmap (server.dsp, server.root_win, a->width, a->height, server.depth);
207
208 // add layer of root pixmap (or clear pixmap if real_transparency==true)
209 if (server.real_transparency)
210 clear_pixmap(a->pix, 0 ,0, a->width, a->height);
211 XCopyArea (server.dsp, ((Panel *)a->panel)->temp_pmap, a->pix, server.gc, a->posx, a->posy, a->width, a->height, 0, 0);
212
213 cairo_surface_t *cs;
214 cairo_t *c;
215
216 cs = cairo_xlib_surface_create (server.dsp, a->pix, server.visual, a->width, a->height);
217 c = cairo_create (cs);
218
219 draw_background (a, c);
220
221 if (a->_draw_foreground)
222 a->_draw_foreground(a, c);
223
224 cairo_destroy (c);
225 cairo_surface_destroy (cs);
226 }
227
228
229 void draw_background (Area *a, cairo_t *c)
230 {
231 if (a->bg->back.alpha > 0.0) {
232 //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);
233 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);
234 cairo_set_source_rgba(c, a->bg->back.color[0], a->bg->back.color[1], a->bg->back.color[2], a->bg->back.alpha);
235 cairo_fill(c);
236 }
237
238 if (a->bg->border.width > 0 && a->bg->border.alpha > 0.0) {
239 cairo_set_line_width (c, a->bg->border.width);
240
241 // draw border inside (x, y, width, height)
242 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);
243 /*
244 // convert : radian = degre * M_PI/180
245 // definir le degrade dans un carre de (0,0) (100,100)
246 // ensuite ce degrade est extrapoler selon le ratio width/height
247 // dans repere (0, 0) (100, 100)
248 double X0, Y0, X1, Y1, degre;
249 // x = X * (a->width / 100), y = Y * (a->height / 100)
250 double x0, y0, x1, y1;
251 X0 = 0;
252 Y0 = 100;
253 X1 = 100;
254 Y1 = 0;
255 degre = 45;
256 // et ensuite faire la changement d'unite du repere
257 // car ce qui doit reste inchangee est les traits et pas la direction
258
259 // il faut d'abord appliquer une rotation de 90 (et -180 si l'angle est superieur a 180)
260 // ceci peut etre applique une fois pour toute au depart
261 // ensuite calculer l'angle dans le nouveau repare
262 // puis faire une rotation de 90
263 x0 = X0 * ((double)a->width / 100);
264 x1 = X1 * ((double)a->width / 100);
265 y0 = Y0 * ((double)a->height / 100);
266 y1 = Y1 * ((double)a->height / 100);
267
268 x0 = X0 * ((double)a->height / 100);
269 x1 = X1 * ((double)a->height / 100);
270 y0 = Y0 * ((double)a->width / 100);
271 y1 = Y1 * ((double)a->width / 100);
272
273 cairo_pattern_t *linpat;
274 linpat = cairo_pattern_create_linear (x0, y0, x1, y1);
275 cairo_pattern_add_color_stop_rgba (linpat, 0, a->border.color[0], a->border.color[1], a->border.color[2], a->border.alpha);
276 cairo_pattern_add_color_stop_rgba (linpat, 1, a->border.color[0], a->border.color[1], a->border.color[2], 0);
277 cairo_set_source (c, linpat);
278 */
279 cairo_set_source_rgba (c, a->bg->border.color[0], a->bg->border.color[1], a->bg->border.color[2], a->bg->border.alpha);
280
281 cairo_stroke (c);
282 //cairo_pattern_destroy (linpat);
283 }
284 }
285
286
287 void remove_area (Area *a)
288 {
289 Area *parent = (Area*)a->parent;
290
291 parent->list = g_slist_remove(parent->list, a);
292 set_redraw (parent);
293
294 }
295
296
297 void add_area (Area *a)
298 {
299 Area *parent = (Area*)a->parent;
300
301 parent->list = g_slist_remove(parent->list, a);
302 set_redraw (parent);
303
304 }
305
306
307 void free_area (Area *a)
308 {
309 GSList *l0;
310 for (l0 = a->list; l0 ; l0 = l0->next)
311 free_area (l0->data);
312
313 if (a->list) {
314 g_slist_free(a->list);
315 a->list = 0;
316 }
317 if (a->pix) {
318 XFreePixmap (server.dsp, a->pix);
319 a->pix = 0;
320 }
321 }
322
323
324 void draw_rect(cairo_t *c, double x, double y, double w, double h, double r)
325 {
326 if (r > 0.0) {
327 double c1 = 0.55228475 * r;
328
329 cairo_move_to(c, x+r, y);
330 cairo_rel_line_to(c, w-2*r, 0);
331 cairo_rel_curve_to(c, c1, 0.0, r, c1, r, r);
332 cairo_rel_line_to(c, 0, h-2*r);
333 cairo_rel_curve_to(c, 0.0, c1, c1-r, r, -r, r);
334 cairo_rel_line_to (c, -w +2*r, 0);
335 cairo_rel_curve_to (c, -c1, 0, -r, -c1, -r, -r);
336 cairo_rel_line_to (c, 0, -h + 2 * r);
337 cairo_rel_curve_to (c, 0, -c1, r - c1, -r, r, -r);
338 }
339 else
340 cairo_rectangle(c, x, y, w, h);
341 }
342
343
344 void clear_pixmap(Pixmap p, int x, int y, int w, int h)
345 {
346 Picture pict = XRenderCreatePicture(server.dsp, p, XRenderFindVisualFormat(server.dsp, server.visual), 0, 0);
347 XRenderColor col = { .red=0, .green=0, .blue=0, .alpha=0 };
348 XRenderFillRectangle(server.dsp, PictOpSrc, pict, &col, x, y, w, h);
349 XRenderFreePicture(server.dsp, pict);
350 }
This page took 0.049496 seconds and 4 git commands to generate.