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