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