Split RrPaint to RrPaint and RrPaintPixmap, so you can paint things other than window...
[mikachu/openbox.git] / render / render.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    render.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003        Ben Jansens
6    Copyright (c) 2003        Derek Foreman
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    See the COPYING file for a copy of the GNU General Public License.
19 */
20
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23
24 #include "render.h"
25 #include "gradient.h"
26 #include "font.h"
27 #include "mask.h"
28 #include "color.h"
29 #include "image.h"
30 #include "theme.h"
31
32 #include <glib.h>
33
34 #ifdef HAVE_STDLIB_H
35 #  include <stdlib.h>
36 #endif
37
38 static void pixel_data_to_pixmap(RrAppearance *l,
39                                  gint x, gint y, gint w, gint h);
40
41 Pixmap RrPaintPixmap(RrAppearance *a, gint w, gint h)
42 {
43     gint i, transferred = 0, sw, sh, partial_w, partial_h;
44     RrPixel32 *source, *dest;
45     Pixmap oldp;
46     RrRect tarea; /* area in which to draw textures */
47     gboolean resized;
48
49     if (w <= 0 || h <= 0) return None;
50
51     if (a->surface.parentx < 0 || a->surface.parenty < 0) {
52         /* ob_debug("Invalid parent co-ordinates\n"); */
53         return None;
54     }
55     resized = (a->w != w || a->h != h);
56
57     oldp = a->pixmap; /* save to free after changing the visible pixmap */
58     a->pixmap = XCreatePixmap(RrDisplay(a->inst),
59                               RrRootWindow(a->inst),
60                               w, h, RrDepth(a->inst));
61
62     g_assert(a->pixmap != None);
63     a->w = w;
64     a->h = h;
65
66     if (a->xftdraw != NULL)
67         XftDrawDestroy(a->xftdraw);
68     a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
69                                RrVisual(a->inst), RrColormap(a->inst));
70     g_assert(a->xftdraw != NULL);
71
72     g_free(a->surface.pixel_data);
73     a->surface.pixel_data = g_new(RrPixel32, w * h);
74
75     if (a->surface.grad == RR_SURFACE_PARENTREL) {
76         g_assert (a->surface.parent);
77         g_assert (a->surface.parent->w);
78
79         sw = a->surface.parent->w;
80         sh = a->surface.parent->h;
81
82         if (a->surface.parentx >= sw || a->surface.parenty >= sh) {
83             return oldp;
84         }
85
86         source = (a->surface.parent->surface.pixel_data +
87                   a->surface.parentx + sw * a->surface.parenty);
88         dest = a->surface.pixel_data;
89
90         if (a->surface.parentx + w > sw) {
91             partial_w = sw - a->surface.parentx;
92         } else partial_w = w;
93
94         if (a->surface.parenty + h > sh) {
95             partial_h = sh - a->surface.parenty;
96         } else partial_h = h;
97
98         for (i = 0; i < partial_h; i++, source += sw, dest += w) {
99             memcpy(dest, source, partial_w * sizeof(RrPixel32));
100         }
101     } else
102         RrRender(a, w, h);
103
104     {
105         gint l, t, r, b;
106         RrMargins(a, &l, &t, &r, &b);
107         RECT_SET(tarea, l, t, w - l - r, h - t - b); 
108     }       
109
110     for (i = 0; i < a->textures; i++) {
111         switch (a->texture[i].type) {
112         case RR_TEXTURE_NONE:
113             break;
114         case RR_TEXTURE_TEXT:
115             if (!transferred) {
116                 transferred = 1;
117                 if ((a->surface.grad != RR_SURFACE_SOLID)
118                     || (a->surface.interlaced))
119                     pixel_data_to_pixmap(a, 0, 0, w, h);
120             }
121             if (a->xftdraw == NULL) {
122                 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap, 
123                                            RrVisual(a->inst),
124                                            RrColormap(a->inst));
125             }
126             RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
127             break;
128         case RR_TEXTURE_LINE_ART:
129             if (!transferred) {
130                 transferred = 1;
131                 if ((a->surface.grad != RR_SURFACE_SOLID)
132                     || (a->surface.interlaced))
133                     pixel_data_to_pixmap(a, 0, 0, w, h);
134             }
135             XDrawLine(RrDisplay(a->inst), a->pixmap,
136                       RrColorGC(a->texture[i].data.lineart.color),
137                       a->texture[i].data.lineart.x1,
138                       a->texture[i].data.lineart.y1,
139                       a->texture[i].data.lineart.x2,
140                       a->texture[i].data.lineart.y2);
141             break;
142         case RR_TEXTURE_MASK:
143             if (!transferred) {
144                 transferred = 1;
145                 if ((a->surface.grad != RR_SURFACE_SOLID)
146                     || (a->surface.interlaced))
147                     pixel_data_to_pixmap(a, 0, 0, w, h);
148             }
149             RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
150             break;
151         case RR_TEXTURE_RGBA:
152             g_assert(!transferred);
153             RrImageDraw(a->surface.pixel_data,
154                         &a->texture[i].data.rgba,
155                         a->w, a->h,
156                         &tarea);
157         break;
158         }
159     }
160
161     if (!transferred) {
162         transferred = 1;
163         if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced))
164             pixel_data_to_pixmap(a, 0, 0, w, h);
165     }
166
167     return oldp;
168 }
169
170 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
171 {
172     Pixmap oldp;
173
174     oldp = RrPaintPixmap(a, w, h);
175     XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
176     XClearWindow(RrDisplay(a->inst), win);
177     /* free this after changing the visible pixmap */
178     if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
179 }
180
181 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
182 {
183   RrAppearance *out;
184
185   out = g_new0(RrAppearance, 1);
186   out->inst = inst;
187   out->textures = numtex;
188   if (numtex) out->texture = g_new0(RrTexture, numtex);
189
190   return out;
191 }
192
193 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
194 {
195     RrSurface *spo, *spc;
196     RrAppearance *copy = g_new(RrAppearance, 1);
197     gint i;
198
199     copy->inst = orig->inst;
200
201     spo = &(orig->surface);
202     spc = &(copy->surface);
203     spc->grad = spo->grad;
204     spc->relief = spo->relief;
205     spc->bevel = spo->bevel;
206     if (spo->primary != NULL)
207         spc->primary = RrColorNew(copy->inst,
208                                   spo->primary->r,
209                                   spo->primary->g, 
210                                   spo->primary->b);
211     else spc->primary = NULL;
212
213     if (spo->secondary != NULL)
214         spc->secondary = RrColorNew(copy->inst,
215                                     spo->secondary->r,
216                                     spo->secondary->g,
217                                     spo->secondary->b);
218     else spc->secondary = NULL;
219
220     if (spo->border_color != NULL)
221         spc->border_color = RrColorNew(copy->inst,
222                                        spo->border_color->r,
223                                        spo->border_color->g,
224                                        spo->border_color->b);
225     else spc->border_color = NULL;
226
227     if (spo->interlace_color != NULL)
228         spc->interlace_color = RrColorNew(copy->inst,
229                                        spo->interlace_color->r,
230                                        spo->interlace_color->g,
231                                        spo->interlace_color->b);
232     else spc->interlace_color = NULL;
233
234     if (spo->bevel_dark != NULL)
235         spc->bevel_dark = RrColorNew(copy->inst,
236                                      spo->bevel_dark->r,
237                                      spo->bevel_dark->g,
238                                      spo->bevel_dark->b);
239     else spc->bevel_dark = NULL;
240
241     if (spo->bevel_light != NULL)
242         spc->bevel_light = RrColorNew(copy->inst,
243                                       spo->bevel_light->r,
244                                       spo->bevel_light->g,
245                                       spo->bevel_light->b);
246     else spc->bevel_light = NULL;
247
248     spc->interlaced = spo->interlaced;
249     spc->border = spo->border;
250     spc->parent = NULL;
251     spc->parentx = spc->parenty = 0;
252     spc->pixel_data = NULL;
253
254     copy->textures = orig->textures;
255     copy->texture = g_memdup(orig->texture,
256                              orig->textures * sizeof(RrTexture));
257     for (i = 0; i < copy->textures; ++i)
258         if (copy->texture[i].type == RR_TEXTURE_RGBA) {
259             copy->texture[i].data.rgba.cache = NULL;
260         }
261     copy->pixmap = None;
262     copy->xftdraw = NULL;
263     copy->w = copy->h = 0;
264     return copy;
265 }
266
267 void RrAppearanceFree(RrAppearance *a)
268 {
269     gint i;
270
271     if (a) {
272         RrSurface *p;
273         if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
274         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
275         for (i = 0; i < a->textures; ++i)
276             if (a->texture[i].type == RR_TEXTURE_RGBA) {
277                 g_free(a->texture[i].data.rgba.cache);
278                 a->texture[i].data.rgba.cache = NULL;
279             }
280         if (a->textures)
281             g_free(a->texture);
282         p = &a->surface;
283         RrColorFree(p->primary);
284         RrColorFree(p->secondary);
285         RrColorFree(p->border_color);
286         RrColorFree(p->interlace_color);
287         RrColorFree(p->bevel_dark);
288         RrColorFree(p->bevel_light);
289         g_free(p->pixel_data);
290         p->pixel_data = NULL;
291         g_free(a);
292     }
293 }
294
295
296 static void pixel_data_to_pixmap(RrAppearance *l,
297                                  gint x, gint y, gint w, gint h)
298 {
299     RrPixel32 *in, *scratch;
300     Pixmap out;
301     XImage *im = NULL;
302     im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
303                       ZPixmap, 0, NULL, w, h, 32, 0);
304     g_assert(im != NULL);
305
306     in = l->surface.pixel_data;
307     out = l->pixmap;
308
309 /* this malloc is a complete waste of time on normal 32bpp
310    as reduce_depth just sets im->data = data and returns
311 */
312     scratch = g_new(RrPixel32, im->width * im->height);
313     im->data = (gchar*) scratch;
314     RrReduceDepth(l->inst, in, im);
315     XPutImage(RrDisplay(l->inst), out,
316               DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
317               im, 0, 0, x, y, w, h);
318     im->data = NULL;
319     XDestroyImage(im);
320     g_free(scratch);
321 }
322
323 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
324 {
325     *l = *t = *r = *b = 0;
326
327     if (a->surface.grad != RR_SURFACE_PARENTREL) {
328         if (a->surface.relief != RR_RELIEF_FLAT) {
329             switch (a->surface.bevel) {
330             case RR_BEVEL_1:
331                 *l = *t = *r = *b = 1;
332                 break;
333             case RR_BEVEL_2:
334                 *l = *t = *r = *b = 2;
335                 break;
336             }
337         } else if (a->surface.border) {
338             *l = *t = *r = *b = 1;
339         }
340     }
341 }
342
343 void RrMinsize(RrAppearance *a, gint *w, gint *h)
344 {
345     gint i;
346     RrSize *m;
347     gint l, t, r, b;
348     *w = *h = 0;
349
350     for (i = 0; i < a->textures; ++i) {
351         switch (a->texture[i].type) {
352         case RR_TEXTURE_NONE:
353             break;
354         case RR_TEXTURE_MASK:
355             *w = MAX(*w, a->texture[i].data.mask.mask->width);
356             *h = MAX(*h, a->texture[i].data.mask.mask->height);
357             break;
358         case RR_TEXTURE_TEXT:
359             m = RrFontMeasureString(a->texture[i].data.text.font,
360                                     a->texture[i].data.text.string, 
361                                     a->texture[i].data.text.shadow_offset_x,
362                                     a->texture[i].data.text.shadow_offset_y);
363             *w = MAX(*w, m->width + 4);
364             m->height = RrFontHeight(a->texture[i].data.text.font,
365                                      a->texture[i].data.text.shadow_offset_y);
366             *h += MAX(*h, m->height);
367             g_free(m);
368             break;
369         case RR_TEXTURE_RGBA:
370             *w += MAX(*w, a->texture[i].data.rgba.width);
371             *h += MAX(*h, a->texture[i].data.rgba.height);
372             break;
373         case RR_TEXTURE_LINE_ART:
374             *w += MAX(*w, MAX(a->texture[i].data.lineart.x1,
375                               a->texture[i].data.lineart.x2));
376             *h += MAX(*h, MAX(a->texture[i].data.lineart.y1,
377                               a->texture[i].data.lineart.y2));
378             break;
379         }
380     }
381
382     RrMargins(a, &l, &t, &r, &b);
383
384     *w += l + r;
385     *h += t + b;
386
387     if (*w < 1) *w = 1;
388     if (*h < 1) *h = 1;
389 }
390
391 static void reverse_bits(gchar *c, gint n)
392 {
393     gint i;
394     for (i = 0; i < n; i++, c++)
395         *c = (((*c * 0x0802UL & 0x22110UL) |
396                (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
397 }
398
399 gboolean RrPixmapToRGBA(const RrInstance *inst,
400                         Pixmap pmap, Pixmap mask,
401                         gint *w, gint *h, RrPixel32 **data)
402 {
403     Window xr;
404     gint xx, xy;
405     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
406     XImage *xi, *xm = NULL;
407
408     if (!XGetGeometry(RrDisplay(inst), pmap,
409                       &xr, &xx, &xy, &pw, &ph, &xb, &xd))
410         return FALSE;
411
412     if (mask) {
413         if (!XGetGeometry(RrDisplay(inst), mask,
414                           &xr, &xx, &xy, &mw, &mh, &xb, &xd))
415             return FALSE;
416         if (pw != mw || ph != mh || xd != 1)
417             return FALSE;
418     }
419
420     xi = XGetImage(RrDisplay(inst), pmap,
421                    0, 0, pw, ph, 0xffffffff, ZPixmap);
422     if (!xi)
423         return FALSE;
424
425     if (mask) {
426         xm = XGetImage(RrDisplay(inst), mask,
427                        0, 0, mw, mh, 0xffffffff, ZPixmap);
428         if (!xm) {
429             XDestroyImage(xi);
430             return FALSE;
431         }
432         if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
433             reverse_bits(xm->data, xm->bytes_per_line * xm->height);
434     }
435
436     if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
437         reverse_bits(xi->data, xi->bytes_per_line * xi->height);
438
439     *data = g_new(RrPixel32, pw * ph);
440     RrIncreaseDepth(inst, *data, xi);
441
442     if (mask) {
443         /* apply transparency from the mask */
444         di = 0;
445         for (i = 0, y = 0; y < ph; ++y) {
446             for (x = 0; x < pw; ++x, ++i) {
447                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
448                     (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
449             }
450             di += xm->bytes_per_line;
451         }
452     }
453
454     *w = pw;
455     *h = ph;
456
457     XDestroyImage(xi);
458     if (mask)
459         XDestroyImage(xm);
460
461     return TRUE;
462 }