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