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