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