]> Dogcows Code - chaz/tint2/blob - src/util/area.c
revert rev 541 : we don t need to duplicate x,y
[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 * AREA :
38 * Areas in tint2 are similar to widgets in a GUI.
39 * Graphical objects (panel, taskbar, task, systray, clock, ...) in tint2 'inherit' an Area class.
40 * Area is an abstract class of objects. It's at the begining of each object (&object == &area).
41 * Area manage the background and border drawing, size and padding.
42 *
43 * DATA ORGANISATION :
44 * tint2 define one panel per monitor. And each panel have a tree of Area (nodes).
45 * The root of the tree is Panel.Area. And task, clock, systray, taskbar,... are nodes.
46 *
47 * 'panel_items' parameter (in config) define the list and the order of nodes in tree's panel.
48 * 'panel_items = SC' define a panel with just Systray and Clock.
49 * So the root Panel.Area will have 2 childs (Systray and Clock).
50 *
51 * The tree allow to browse panel's objects from background to foreground and from left to right.
52 * The position of each node/Area depend on parent's position and brothers on the left.
53 *
54 * DRAWING EVENT :
55 * In the end, redrawing an object (like the clock) could come from an external event (date change)
56 * or from a layering event (size or position change).
57 *
58 * DRAWING LOOP :
59 * 1) browse tree and resize SIZE_BY_CONTENT node
60 * - children node are resized before its parent
61 * - if 'size' changed then 'redraw = 1' and 'resize = 1' on the parent
62 * 2) browse tree and resize SIZE_BY_LAYOUT node
63 * - parent node is resized before its children
64 * - if 'size' changed then 'redraw = 1' and 'resize = 1' on childs with SIZE_BY_LAYOUT
65 * 3) calculate posx of objects
66 * - parent's position is calculated before children's position
67 * - if 'position' changed then 'redraw = 1'
68 * 4) redraw needed objects
69 * - parent node is drawn before its children
70 *
71 * perhaps 2) and 3) can be merged...
72 * répartition entre niveau global et niveau local ??
73 * size_by_content peut-il modifier redraw=1 en cas de changement ? ou est ce géré par chaque composant ?
74 * size_by_layout peut-il modifier redraw ?
75 *
76 ************************************************************/
77
78
79 void refresh (Area *a)
80 {
81 // don't draw and resize hide objects
82 if (!a->on_screen) return;
83
84 // don't draw transparent objects (without foreground and without background)
85 if (a->redraw) {
86 a->redraw = 0;
87 // force redraw of child
88 GSList *l;
89 for (l = a->list ; l ; l = l->next)
90 set_redraw(l->data);
91
92 //printf("draw area posx %d, width %d\n", a->posx, a->width);
93 draw(a);
94 }
95
96 // draw current Area
97 if (a->pix == 0) printf("empty area posx %d, width %d\n", a->posx, a->width);
98 XCopyArea (server.dsp, a->pix, ((Panel *)a->panel)->temp_pmap, server.gc, 0, 0, a->width, a->height, a->posx, a->posy);
99
100 // and then refresh child object
101 GSList *l;
102 for (l = a->list; l ; l = l->next)
103 refresh(l->data);
104 }
105
106
107 void size_by_content (Area *a)
108 {
109 // don't draw and resize hide 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 current area's size
118 if (a->resize && a->size_mode == SIZE_BY_CONTENT) {
119 a->resize = 0;
120
121 // if 'size' changed then 'resize = 1' on the parent
122 if (a->_resize) {
123 a->_resize(a);
124 a->redraw = 1;
125 ((Area*)a->parent)->resize = 1;
126 }
127 }
128 }
129
130
131 void size_by_layout (Area *a)
132 {
133 // don't draw and resize hide objects
134 if (!a->on_screen) return;
135
136 // parent node is resized before its children
137 // calculate current area's size
138 GSList *l;
139 if (a->resize && a->size_mode == SIZE_BY_LAYOUT) {
140 a->resize = 0;
141
142 // if 'size' changed then 'resize = 1' on childs with SIZE_BY_LAYOUT
143 if (a->_resize) {
144 a->_resize(a);
145 for (l = a->list; l ; l = l->next) {
146 if (((Area*)l->data)->size_mode == SIZE_BY_LAYOUT)
147 ((Area*)l->data)->resize = 1;
148 }
149 }
150 }
151
152 for (l = a->list; l ; l = l->next)
153 size_by_layout(l->data);
154 }
155
156
157 void set_redraw (Area *a)
158 {
159 a->redraw = 1;
160
161 GSList *l;
162 for (l = a->list ; l ; l = l->next)
163 set_redraw(l->data);
164 }
165
166
167 void draw (Area *a)
168 {
169 if (a->pix) XFreePixmap (server.dsp, a->pix);
170 a->pix = XCreatePixmap (server.dsp, server.root_win, a->width, a->height, server.depth);
171
172 // add layer of root pixmap (or clear pixmap if real_transparency==true)
173 if (server.real_transparency)
174 clear_pixmap(a->pix, 0 ,0, a->width, a->height);
175 XCopyArea (server.dsp, ((Panel *)a->panel)->temp_pmap, a->pix, server.gc, a->posx, a->posy, a->width, a->height, 0, 0);
176
177 cairo_surface_t *cs;
178 cairo_t *c;
179
180 cs = cairo_xlib_surface_create (server.dsp, a->pix, server.visual, a->width, a->height);
181 c = cairo_create (cs);
182
183 draw_background (a, c);
184
185 if (a->_draw_foreground)
186 a->_draw_foreground(a, c);
187
188 cairo_destroy (c);
189 cairo_surface_destroy (cs);
190 }
191
192
193 void draw_background (Area *a, cairo_t *c)
194 {
195 if (a->bg->back.alpha > 0.0) {
196 //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);
197 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);
198 cairo_set_source_rgba(c, a->bg->back.color[0], a->bg->back.color[1], a->bg->back.color[2], a->bg->back.alpha);
199 cairo_fill(c);
200 }
201
202 if (a->bg->border.width > 0 && a->bg->border.alpha > 0.0) {
203 cairo_set_line_width (c, a->bg->border.width);
204
205 // draw border inside (x, y, width, height)
206 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);
207 /*
208 // convert : radian = degre * M_PI/180
209 // definir le degrade dans un carre de (0,0) (100,100)
210 // ensuite ce degrade est extrapoler selon le ratio width/height
211 // dans repere (0, 0) (100, 100)
212 double X0, Y0, X1, Y1, degre;
213 // x = X * (a->width / 100), y = Y * (a->height / 100)
214 double x0, y0, x1, y1;
215 X0 = 0;
216 Y0 = 100;
217 X1 = 100;
218 Y1 = 0;
219 degre = 45;
220 // et ensuite faire la changement d'unite du repere
221 // car ce qui doit reste inchangee est les traits et pas la direction
222
223 // il faut d'abord appliquer une rotation de 90 (et -180 si l'angle est superieur a 180)
224 // ceci peut etre applique une fois pour toute au depart
225 // ensuite calculer l'angle dans le nouveau repare
226 // puis faire une rotation de 90
227 x0 = X0 * ((double)a->width / 100);
228 x1 = X1 * ((double)a->width / 100);
229 y0 = Y0 * ((double)a->height / 100);
230 y1 = Y1 * ((double)a->height / 100);
231
232 x0 = X0 * ((double)a->height / 100);
233 x1 = X1 * ((double)a->height / 100);
234 y0 = Y0 * ((double)a->width / 100);
235 y1 = Y1 * ((double)a->width / 100);
236
237 cairo_pattern_t *linpat;
238 linpat = cairo_pattern_create_linear (x0, y0, x1, y1);
239 cairo_pattern_add_color_stop_rgba (linpat, 0, a->border.color[0], a->border.color[1], a->border.color[2], a->border.alpha);
240 cairo_pattern_add_color_stop_rgba (linpat, 1, a->border.color[0], a->border.color[1], a->border.color[2], 0);
241 cairo_set_source (c, linpat);
242 */
243 cairo_set_source_rgba (c, a->bg->border.color[0], a->bg->border.color[1], a->bg->border.color[2], a->bg->border.alpha);
244
245 cairo_stroke (c);
246 //cairo_pattern_destroy (linpat);
247 }
248 }
249
250
251 void remove_area (Area *a)
252 {
253 Area *parent = (Area*)a->parent;
254
255 parent->list = g_slist_remove(parent->list, a);
256 set_redraw (parent);
257
258 }
259
260
261 void add_area (Area *a)
262 {
263 Area *parent = (Area*)a->parent;
264
265 parent->list = g_slist_remove(parent->list, a);
266 set_redraw (parent);
267
268 }
269
270
271 void free_area (Area *a)
272 {
273 GSList *l0;
274 for (l0 = a->list; l0 ; l0 = l0->next)
275 free_area (l0->data);
276
277 if (a->list) {
278 g_slist_free(a->list);
279 a->list = 0;
280 }
281 if (a->pix) {
282 XFreePixmap (server.dsp, a->pix);
283 a->pix = 0;
284 }
285 }
286
287
288 void draw_rect(cairo_t *c, double x, double y, double w, double h, double r)
289 {
290 if (r > 0.0) {
291 double c1 = 0.55228475 * r;
292
293 cairo_move_to(c, x+r, y);
294 cairo_rel_line_to(c, w-2*r, 0);
295 cairo_rel_curve_to(c, c1, 0.0, r, c1, r, r);
296 cairo_rel_line_to(c, 0, h-2*r);
297 cairo_rel_curve_to(c, 0.0, c1, c1-r, r, -r, r);
298 cairo_rel_line_to (c, -w +2*r, 0);
299 cairo_rel_curve_to (c, -c1, 0, -r, -c1, -r, -r);
300 cairo_rel_line_to (c, 0, -h + 2 * r);
301 cairo_rel_curve_to (c, 0, -c1, r - c1, -r, r, -r);
302 }
303 else
304 cairo_rectangle(c, x, y, w, h);
305 }
306
307
308 void clear_pixmap(Pixmap p, int x, int y, int w, int h)
309 {
310 Picture pict = XRenderCreatePicture(server.dsp, p, XRenderFindVisualFormat(server.dsp, server.visual), 0, 0);
311 XRenderColor col = { .red=0, .green=0, .blue=0, .alpha=0 };
312 XRenderFillRectangle(server.dsp, PictOpSrc, pict, &col, x, y, w, h);
313 XRenderFreePicture(server.dsp, pict);
314 }
This page took 0.05936 seconds and 5 git commands to generate.