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