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