]> Dogcows Code - chaz/openbox/blob - render/render.c
dont use obsolete xmlChildrenNode
[chaz/openbox] / render / render.c
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3
4 #include "render.h"
5 #include "gradient.h"
6 #include "font.h"
7 #include "mask.h"
8 #include "color.h"
9 #include "image.h"
10 #include "theme.h"
11
12 #include <glib.h>
13
14 #ifdef HAVE_STDLIB_H
15 # include <stdlib.h>
16 #endif
17
18 static void pixel_data_to_pixmap(RrAppearance *l,
19 gint x, gint y, gint w, gint h);
20
21 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
22 {
23 int i, transferred = 0, sw;
24 RrPixel32 *source, *dest;
25 Pixmap oldp;
26 RrRect tarea; /* area in which to draw textures */
27 gboolean resized;
28
29 if (w <= 0 || h <= 0) return;
30
31 resized = (a->w != w || a->h != h);
32
33 oldp = a->pixmap; /* save to free after changing the visible pixmap */
34 a->pixmap = XCreatePixmap(RrDisplay(a->inst),
35 RrRootWindow(a->inst),
36 w, h, RrDepth(a->inst));
37
38 g_assert(a->pixmap != None);
39 a->w = w;
40 a->h = h;
41
42 if (a->xftdraw != NULL)
43 XftDrawDestroy(a->xftdraw);
44 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
45 RrVisual(a->inst), RrColormap(a->inst));
46 g_assert(a->xftdraw != NULL);
47
48 g_free(a->surface.pixel_data);
49 a->surface.pixel_data = g_new(RrPixel32, w * h);
50
51 if (a->surface.grad == RR_SURFACE_PARENTREL) {
52 g_assert (a->surface.parent);
53 g_assert (a->surface.parent->w);
54
55 sw = a->surface.parent->w;
56 source = (a->surface.parent->surface.pixel_data +
57 a->surface.parentx + sw * a->surface.parenty);
58 dest = a->surface.pixel_data;
59 for (i = 0; i < h; i++, source += sw, dest += w) {
60 memcpy(dest, source, w * sizeof(RrPixel32));
61 }
62 } else
63 RrRender(a, w, h);
64
65 {
66 gint l, t, r, b;
67 RrMargins(a, &l, &t, &r, &b);
68 RECT_SET(tarea, l, t, w - l - r, h - t - b);
69 }
70
71 for (i = 0; i < a->textures; i++) {
72 switch (a->texture[i].type) {
73 case RR_TEXTURE_NONE:
74 break;
75 case RR_TEXTURE_TEXT:
76 if (!transferred) {
77 transferred = 1;
78 if (a->surface.grad != RR_SURFACE_SOLID)
79 pixel_data_to_pixmap(a, 0, 0, w, h);
80 }
81 if (a->xftdraw == NULL) {
82 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
83 RrVisual(a->inst),
84 RrColormap(a->inst));
85 }
86 RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
87 break;
88 case RR_TEXTURE_LINE_ART:
89 if (!transferred) {
90 transferred = 1;
91 if (a->surface.grad != RR_SURFACE_SOLID)
92 pixel_data_to_pixmap(a, 0, 0, w, h);
93 }
94 XDrawLine(RrDisplay(a->inst), a->pixmap,
95 RrColorGC(a->texture[i].data.lineart.color),
96 a->texture[i].data.lineart.x1,
97 a->texture[i].data.lineart.y1,
98 a->texture[i].data.lineart.x2,
99 a->texture[i].data.lineart.y2);
100 break;
101 case RR_TEXTURE_MASK:
102 if (!transferred) {
103 transferred = 1;
104 if (a->surface.grad != RR_SURFACE_SOLID)
105 pixel_data_to_pixmap(a, 0, 0, w, h);
106 }
107 RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
108 break;
109 case RR_TEXTURE_RGBA:
110 g_assert(!transferred);
111 RrImageDraw(a->surface.pixel_data,
112 &a->texture[i].data.rgba,
113 a->w, a->h,
114 &tarea);
115 break;
116 }
117 }
118
119 if (!transferred) {
120 transferred = 1;
121 if (a->surface.grad != RR_SURFACE_SOLID)
122 pixel_data_to_pixmap(a, 0, 0, w, h);
123 }
124
125 XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
126 XClearWindow(RrDisplay(a->inst), win);
127 if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
128 }
129
130 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
131 {
132 RrAppearance *out;
133
134 out = g_new0(RrAppearance, 1);
135 out->inst = inst;
136 out->textures = numtex;
137 if (numtex) out->texture = g_new0(RrTexture, numtex);
138
139 return out;
140 }
141
142 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
143 {
144 RrSurface *spo, *spc;
145 RrAppearance *copy = g_new(RrAppearance, 1);
146 gint i;
147
148 copy->inst = orig->inst;
149
150 spo = &(orig->surface);
151 spc = &(copy->surface);
152 spc->grad = spo->grad;
153 spc->relief = spo->relief;
154 spc->bevel = spo->bevel;
155 if (spo->primary != NULL)
156 spc->primary = RrColorNew(copy->inst,
157 spo->primary->r,
158 spo->primary->g,
159 spo->primary->b);
160 else spc->primary = NULL;
161
162 if (spo->secondary != NULL)
163 spc->secondary = RrColorNew(copy->inst,
164 spo->secondary->r,
165 spo->secondary->g,
166 spo->secondary->b);
167 else spc->secondary = NULL;
168
169 if (spo->border_color != NULL)
170 spc->border_color = RrColorNew(copy->inst,
171 spo->border_color->r,
172 spo->border_color->g,
173 spo->border_color->b);
174 else spc->border_color = NULL;
175
176 if (spo->interlace_color != NULL)
177 spc->interlace_color = RrColorNew(copy->inst,
178 spo->interlace_color->r,
179 spo->interlace_color->g,
180 spo->interlace_color->b);
181 else spc->interlace_color = NULL;
182
183 if (spo->bevel_dark != NULL)
184 spc->bevel_dark = RrColorNew(copy->inst,
185 spo->bevel_dark->r,
186 spo->bevel_dark->g,
187 spo->bevel_dark->b);
188 else spc->bevel_dark = NULL;
189
190 if (spo->bevel_light != NULL)
191 spc->bevel_light = RrColorNew(copy->inst,
192 spo->bevel_light->r,
193 spo->bevel_light->g,
194 spo->bevel_light->b);
195 else spc->bevel_light = NULL;
196
197 spc->interlaced = spo->interlaced;
198 spc->border = spo->border;
199 spc->parent = NULL;
200 spc->parentx = spc->parenty = 0;
201 spc->pixel_data = NULL;
202
203 copy->textures = orig->textures;
204 copy->texture = g_memdup(orig->texture,
205 orig->textures * sizeof(RrTexture));
206 for (i = 0; i < copy->textures; ++i)
207 if (copy->texture[i].type == RR_TEXTURE_RGBA) {
208 copy->texture[i].data.rgba.cache = NULL;
209 }
210 copy->pixmap = None;
211 copy->xftdraw = NULL;
212 copy->w = copy->h = 0;
213 return copy;
214 }
215
216 void RrAppearanceFree(RrAppearance *a)
217 {
218 gint i;
219
220 if (a) {
221 RrSurface *p;
222 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
223 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
224 for (i = 0; i < a->textures; ++i)
225 if (a->texture[i].type == RR_TEXTURE_RGBA) {
226 g_free(a->texture[i].data.rgba.cache);
227 a->texture[i].data.rgba.cache = NULL;
228 }
229 if (a->textures)
230 g_free(a->texture);
231 p = &a->surface;
232 RrColorFree(p->primary);
233 RrColorFree(p->secondary);
234 RrColorFree(p->border_color);
235 RrColorFree(p->interlace_color);
236 RrColorFree(p->bevel_dark);
237 RrColorFree(p->bevel_light);
238 g_free(p->pixel_data);
239
240 g_free(a);
241 }
242 }
243
244
245 static void pixel_data_to_pixmap(RrAppearance *l,
246 gint x, gint y, gint w, gint h)
247 {
248 RrPixel32 *in, *scratch;
249 Pixmap out;
250 XImage *im = NULL;
251 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
252 ZPixmap, 0, NULL, w, h, 32, 0);
253 g_assert(im != NULL);
254
255 in = l->surface.pixel_data;
256 out = l->pixmap;
257
258 /* this malloc is a complete waste of time on normal 32bpp
259 as reduce_depth just sets im->data = data and returns
260 */
261 scratch = g_new(RrPixel32, im->width * im->height);
262 im->data = (char*) scratch;
263 RrReduceDepth(l->inst, in, im);
264 XPutImage(RrDisplay(l->inst), out,
265 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
266 im, 0, 0, x, y, w, h);
267 im->data = NULL;
268 XDestroyImage(im);
269 g_free(scratch);
270 }
271
272 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
273 {
274 *l = *t = *r = *b = 0;
275
276 if (a->surface.grad != RR_SURFACE_PARENTREL) {
277 if (a->surface.relief != RR_RELIEF_FLAT) {
278 switch (a->surface.bevel) {
279 case RR_BEVEL_1:
280 *l = *t = *r = *b = 1;
281 break;
282 case RR_BEVEL_2:
283 *l = *t = *r = *b = 2;
284 break;
285 }
286 } else if (a->surface.border) {
287 *l = *t = *r = *b = 1;
288 }
289 }
290 }
291
292 void RrMinsize(RrAppearance *a, gint *w, gint *h)
293 {
294 gint i;
295 gint m;
296 gint l, t, r, b;
297 *w = *h = 0;
298
299 for (i = 0; i < a->textures; ++i) {
300 switch (a->texture[i].type) {
301 case RR_TEXTURE_NONE:
302 break;
303 case RR_TEXTURE_MASK:
304 *w = MAX(*w, a->texture[i].data.mask.mask->width);
305 *h = MAX(*h, a->texture[i].data.mask.mask->height);
306 break;
307 case RR_TEXTURE_TEXT:
308 m = RrFontMeasureString(a->texture[i].data.text.font,
309 a->texture[i].data.text.string);
310 *w = MAX(*w, m);
311 m = RrFontHeight(a->texture[i].data.text.font);
312 *h += MAX(*h, m);
313 break;
314 case RR_TEXTURE_RGBA:
315 *w += MAX(*w, a->texture[i].data.rgba.width);
316 *h += MAX(*h, a->texture[i].data.rgba.height);
317 break;
318 case RR_TEXTURE_LINE_ART:
319 *w += MAX(*w, MAX(a->texture[i].data.lineart.x1,
320 a->texture[i].data.lineart.x2));
321 *h += MAX(*h, MAX(a->texture[i].data.lineart.y1,
322 a->texture[i].data.lineart.y2));
323 break;
324 }
325 }
326
327 RrMargins(a, &l, &t, &r, &b);
328
329 *w += l + r;
330 *h += t + b;
331
332 if (*w < 1) *w = 1;
333 if (*h < 1) *h = 1;
334 }
335
336 gboolean RrPixmapToRGBA(const RrInstance *inst,
337 Pixmap pmap, Pixmap mask,
338 gint *w, gint *h, RrPixel32 **data)
339 {
340 Window xr;
341 gint xx, xy;
342 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
343 XImage *xi, *xm = NULL;
344
345 if (!XGetGeometry(RrDisplay(inst),
346 pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
347 return FALSE;
348 if (mask) {
349 if (!XGetGeometry(RrDisplay(inst), mask,
350 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
351 return FALSE;
352 if (pw != mw || ph != mh || xd != 1)
353 return FALSE;
354 }
355
356 xi = XGetImage(RrDisplay(inst), pmap,
357 0, 0, pw, ph, 0xffffffff, ZPixmap);
358 if (!xi)
359 return FALSE;
360
361 if (mask) {
362 xm = XGetImage(RrDisplay(inst), mask,
363 0, 0, mw, mh, 0xffffffff, ZPixmap);
364 if (!xm)
365 return FALSE;
366 }
367
368 *data = g_new(RrPixel32, pw * ph);
369 RrIncreaseDepth(inst, *data, xi);
370
371 if (mask) {
372 /* apply transparency from the mask */
373 di = 0;
374 for (i = 0, y = 0; y < ph; ++y) {
375 for (x = 0; x < pw; ++x, ++i) {
376 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
377 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
378 }
379 di += xm->bytes_per_line;
380 }
381 }
382
383 *w = pw;
384 *h = ph;
385
386 return TRUE;
387 }
This page took 0.04896 seconds and 4 git commands to generate.