]> Dogcows Code - chaz/tint2/blob - src/util/area.c
*fix* image rendering in real_transparency mode fixed (imlib_render_image does not...
[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 // QUESTION: Why do we need Pixmaps for drawing? Can't we draw directly in the Window???
34 // Parent could pass a cairo_surface_t to the children, and children use it, for drawing...
35
36
37 // 1) resize child
38 // 2) resize parent
39 // 3) redraw parent
40 // 4) redraw child
41 void refresh (Area *a)
42 {
43 // don't draw and resize hide objects
44 if (!a->on_screen) return;
45
46 size(a);
47
48 // don't draw transparent objects (without foreground and without background)
49 if (a->redraw) {
50 a->redraw = 0;
51 // force redraw of child
52 GSList *l;
53 for (l = a->list ; l ; l = l->next)
54 set_redraw(l->data);
55
56 //printf("draw area posx %d, width %d\n", a->posx, a->width);
57 draw(a, 0);
58 if (a->use_active)
59 draw(a, 1);
60 }
61
62 // draw current Area
63 Pixmap pmap = (a->is_active == 0) ? (a->pix.pmap) : (a->pix_active.pmap);
64 if (pmap == 0) printf("empty area posx %d, width %d\n", a->posx, a->width);
65 XCopyArea (server.dsp, pmap, ((Panel *)a->panel)->temp_pmap, server.gc, 0, 0, a->width, a->height, a->posx, a->posy);
66
67 // and then refresh child object
68 GSList *l;
69 for (l = a->list; l ; l = l->next)
70 refresh(l->data);
71 }
72
73
74 void size (Area *a)
75 {
76 GSList *l;
77
78 if (a->resize) {
79 a->resize = 0;
80 // force the resize of childs
81 for (l = a->list; l ; l = l->next) {
82 Area *area = (Area*)l->data;
83 area->resize = 1;
84 size(area);
85 }
86
87 // resize can generate a redraw
88 if (a->_resize)
89 a->_resize(a);
90 }
91 }
92
93
94 void set_redraw (Area *a)
95 {
96 a->redraw = 1;
97
98 GSList *l;
99 for (l = a->list ; l ; l = l->next)
100 set_redraw(l->data);
101 }
102
103
104 void draw (Area *a, int active)
105 {
106 Pixmap *pmap = (active == 0) ? (&a->pix.pmap) : (&a->pix_active.pmap);
107
108 if (*pmap) XFreePixmap (server.dsp, *pmap);
109 *pmap = XCreatePixmap (server.dsp, server.root_win, a->width, a->height, server.depth);
110
111 // add layer of root pixmap (or clear pixmap if real_transparency==true)
112 if (real_transparency)
113 clear_pixmap(*pmap, 0 ,0, a->width, a->height);
114 XCopyArea (server.dsp, ((Panel *)a->panel)->temp_pmap, *pmap, server.gc, a->posx, a->posy, a->width, a->height, 0, 0);
115
116 cairo_surface_t *cs;
117 cairo_t *c;
118
119 cs = cairo_xlib_surface_create (server.dsp, *pmap, server.visual, a->width, a->height);
120 c = cairo_create (cs);
121
122 draw_background (a, c, active);
123
124 if (a->_draw_foreground)
125 a->_draw_foreground(a, c, active);
126
127 cairo_destroy (c);
128 cairo_surface_destroy (cs);
129 }
130
131
132 void draw_background (Area *a, cairo_t *c, int active)
133 {
134 Pmap *pix = (active == 0) ? (&a->pix) : (&a->pix_active);
135
136 if (pix->back.alpha > 0.0) {
137 //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);
138 draw_rect(c, pix->border.width, pix->border.width, a->width-(2.0 * pix->border.width), a->height-(2.0*pix->border.width), pix->border.rounded - pix->border.width/1.571);
139 cairo_set_source_rgba(c, pix->back.color[0], pix->back.color[1], pix->back.color[2], pix->back.alpha);
140
141 cairo_fill(c);
142 }
143
144 if (pix->border.width > 0 && pix->border.alpha > 0.0) {
145 cairo_set_line_width (c, pix->border.width);
146
147 // draw border inside (x, y, width, height)
148 draw_rect(c, pix->border.width/2.0, pix->border.width/2.0, a->width - pix->border.width, a->height - pix->border.width, pix->border.rounded);
149 /*
150 // convert : radian = degre * M_PI/180
151 // définir le dégradé dans un carré de (0,0) (100,100)
152 // ensuite ce dégradé est extrapolé selon le ratio width/height
153 // dans repère (0, 0) (100, 100)
154 double X0, Y0, X1, Y1, degre;
155 // x = X * (a->width / 100), y = Y * (a->height / 100)
156 double x0, y0, x1, y1;
157 X0 = 0;
158 Y0 = 100;
159 X1 = 100;
160 Y1 = 0;
161 degre = 45;
162 // et ensuite faire la changement d'unité du repère
163 // car ce qui doit resté inchangée est les traits et pas la direction
164
165 // il faut d'abord appliquer une rotation de 90° (et -180° si l'angle est supérieur à 180°)
166 // ceci peut être appliqué une fois pour toute au départ
167 // ensuite calculer l'angle dans le nouveau repère
168 // puis faire une rotation de 90°
169 x0 = X0 * ((double)a->width / 100);
170 x1 = X1 * ((double)a->width / 100);
171 y0 = Y0 * ((double)a->height / 100);
172 y1 = Y1 * ((double)a->height / 100);
173
174 x0 = X0 * ((double)a->height / 100);
175 x1 = X1 * ((double)a->height / 100);
176 y0 = Y0 * ((double)a->width / 100);
177 y1 = Y1 * ((double)a->width / 100);
178
179 cairo_pattern_t *linpat;
180 linpat = cairo_pattern_create_linear (x0, y0, x1, y1);
181 cairo_pattern_add_color_stop_rgba (linpat, 0, a->border.color[0], a->border.color[1], a->border.color[2], a->border.alpha);
182 cairo_pattern_add_color_stop_rgba (linpat, 1, a->border.color[0], a->border.color[1], a->border.color[2], 0);
183 cairo_set_source (c, linpat);
184 */
185 cairo_set_source_rgba (c, pix->border.color[0], pix->border.color[1], pix->border.color[2], pix->border.alpha);
186
187 cairo_stroke (c);
188 //cairo_pattern_destroy (linpat);
189 }
190 }
191
192
193 void remove_area (Area *a)
194 {
195 Area *parent = (Area*)a->parent;
196
197 parent->list = g_slist_remove(parent->list, a);
198 set_redraw (parent);
199
200 }
201
202
203 void add_area (Area *a)
204 {
205 Area *parent = (Area*)a->parent;
206
207 parent->list = g_slist_remove(parent->list, a);
208 set_redraw (parent);
209
210 }
211
212
213 void free_area (Area *a)
214 {
215 GSList *l0;
216 for (l0 = a->list; l0 ; l0 = l0->next)
217 free_area (l0->data);
218
219 if (a->list) {
220 g_slist_free(a->list);
221 a->list = 0;
222 }
223 if (a->pix.pmap) {
224 XFreePixmap (server.dsp, a->pix.pmap);
225 a->pix.pmap = 0;
226 }
227 if (a->pix_active.pmap) {
228 XFreePixmap (server.dsp, a->pix_active.pmap);
229 a->pix_active.pmap = 0;
230 }
231 }
232
233
234 void draw_rect(cairo_t *c, double x, double y, double w, double h, double r)
235 {
236 if (r > 0.0) {
237 double c1 = 0.55228475 * r;
238
239 cairo_move_to(c, x+r, y);
240 cairo_rel_line_to(c, w-2*r, 0);
241 cairo_rel_curve_to(c, c1, 0.0, r, c1, r, r);
242 cairo_rel_line_to(c, 0, h-2*r);
243 cairo_rel_curve_to(c, 0.0, c1, c1-r, r, -r, r);
244 cairo_rel_line_to (c, -w +2*r, 0);
245 cairo_rel_curve_to (c, -c1, 0, -r, -c1, -r, -r);
246 cairo_rel_line_to (c, 0, -h + 2 * r);
247 cairo_rel_curve_to (c, 0, -c1, r - c1, -r, r, -r);
248 }
249 else
250 cairo_rectangle(c, x, y, w, h);
251 }
252
253
254 void clear_pixmap(Pixmap p, int x, int y, int w, int h)
255 {
256 Picture pict = XRenderCreatePicture(server.dsp, p, XRenderFindVisualFormat(server.dsp, server.visual), 0, 0);
257 XRenderColor col = { .red=0, .green=0, .blue=0, .alpha=0 };
258 XRenderFillRectangle(server.dsp, PictOpSrc, pict, &col, x, y, w, h);
259 }
This page took 0.05368 seconds and 5 git commands to generate.