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