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