update copyright step 2
[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 void RrPaint(RrAppearance *a, Window win, 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;
50
51     if (a->surface.parentx < 0 || a->surface.parenty < 0) {
52         /* ob_debug("Invalid parent co-ordinates\n"); */
53         return;
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;
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) || (a->surface.interlaced))
118                     pixel_data_to_pixmap(a, 0, 0, w, h);
119             }
120             if (a->xftdraw == NULL) {
121                 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap, 
122                                            RrVisual(a->inst),
123                                            RrColormap(a->inst));
124             }
125             RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
126             break;
127         case RR_TEXTURE_LINE_ART:
128             if (!transferred) {
129                 transferred = 1;
130                 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced))
131                     pixel_data_to_pixmap(a, 0, 0, w, h);
132             }
133             XDrawLine(RrDisplay(a->inst), a->pixmap,
134                       RrColorGC(a->texture[i].data.lineart.color),
135                       a->texture[i].data.lineart.x1,
136                       a->texture[i].data.lineart.y1,
137                       a->texture[i].data.lineart.x2,
138                       a->texture[i].data.lineart.y2);
139             break;
140         case RR_TEXTURE_MASK:
141             if (!transferred) {
142                 transferred = 1;
143                 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced))
144                     pixel_data_to_pixmap(a, 0, 0, w, h);
145             }
146             RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
147             break;
148         case RR_TEXTURE_RGBA:
149             g_assert(!transferred);
150             RrImageDraw(a->surface.pixel_data,
151                         &a->texture[i].data.rgba,
152                         a->w, a->h,
153                         &tarea);
154         break;
155         }
156     }
157
158     if (!transferred) {
159         transferred = 1;
160         if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced))
161             pixel_data_to_pixmap(a, 0, 0, w, h);
162     }
163
164     XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
165     XClearWindow(RrDisplay(a->inst), win);
166     if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
167 }
168
169 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
170 {
171   RrAppearance *out;
172
173   out = g_new0(RrAppearance, 1);
174   out->inst = inst;
175   out->textures = numtex;
176   if (numtex) out->texture = g_new0(RrTexture, numtex);
177
178   return out;
179 }
180
181 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
182 {
183     RrSurface *spo, *spc;
184     RrAppearance *copy = g_new(RrAppearance, 1);
185     gint i;
186
187     copy->inst = orig->inst;
188
189     spo = &(orig->surface);
190     spc = &(copy->surface);
191     spc->grad = spo->grad;
192     spc->relief = spo->relief;
193     spc->bevel = spo->bevel;
194     if (spo->primary != NULL)
195         spc->primary = RrColorNew(copy->inst,
196                                   spo->primary->r,
197                                   spo->primary->g, 
198                                   spo->primary->b);
199     else spc->primary = NULL;
200
201     if (spo->secondary != NULL)
202         spc->secondary = RrColorNew(copy->inst,
203                                     spo->secondary->r,
204                                     spo->secondary->g,
205                                     spo->secondary->b);
206     else spc->secondary = NULL;
207
208     if (spo->border_color != NULL)
209         spc->border_color = RrColorNew(copy->inst,
210                                        spo->border_color->r,
211                                        spo->border_color->g,
212                                        spo->border_color->b);
213     else spc->border_color = NULL;
214
215     if (spo->interlace_color != NULL)
216         spc->interlace_color = RrColorNew(copy->inst,
217                                        spo->interlace_color->r,
218                                        spo->interlace_color->g,
219                                        spo->interlace_color->b);
220     else spc->interlace_color = NULL;
221
222     if (spo->bevel_dark != NULL)
223         spc->bevel_dark = RrColorNew(copy->inst,
224                                      spo->bevel_dark->r,
225                                      spo->bevel_dark->g,
226                                      spo->bevel_dark->b);
227     else spc->bevel_dark = NULL;
228
229     if (spo->bevel_light != NULL)
230         spc->bevel_light = RrColorNew(copy->inst,
231                                       spo->bevel_light->r,
232                                       spo->bevel_light->g,
233                                       spo->bevel_light->b);
234     else spc->bevel_light = NULL;
235
236     spc->interlaced = spo->interlaced;
237     spc->border = spo->border;
238     spc->parent = NULL;
239     spc->parentx = spc->parenty = 0;
240     spc->pixel_data = NULL;
241
242     copy->textures = orig->textures;
243     copy->texture = g_memdup(orig->texture,
244                              orig->textures * sizeof(RrTexture));
245     for (i = 0; i < copy->textures; ++i)
246         if (copy->texture[i].type == RR_TEXTURE_RGBA) {
247             copy->texture[i].data.rgba.cache = NULL;
248         }
249     copy->pixmap = None;
250     copy->xftdraw = NULL;
251     copy->w = copy->h = 0;
252     return copy;
253 }
254
255 void RrAppearanceFree(RrAppearance *a)
256 {
257     gint i;
258
259     if (a) {
260         RrSurface *p;
261         if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
262         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
263         for (i = 0; i < a->textures; ++i)
264             if (a->texture[i].type == RR_TEXTURE_RGBA) {
265                 g_free(a->texture[i].data.rgba.cache);
266                 a->texture[i].data.rgba.cache = NULL;
267             }
268         if (a->textures)
269             g_free(a->texture);
270         p = &a->surface;
271         RrColorFree(p->primary);
272         RrColorFree(p->secondary);
273         RrColorFree(p->border_color);
274         RrColorFree(p->interlace_color);
275         RrColorFree(p->bevel_dark);
276         RrColorFree(p->bevel_light);
277         g_free(p->pixel_data);
278         p->pixel_data = NULL;
279         g_free(a);
280     }
281 }
282
283
284 static void pixel_data_to_pixmap(RrAppearance *l,
285                                  gint x, gint y, gint w, gint h)
286 {
287     RrPixel32 *in, *scratch;
288     Pixmap out;
289     XImage *im = NULL;
290     im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
291                       ZPixmap, 0, NULL, w, h, 32, 0);
292     g_assert(im != NULL);
293
294     in = l->surface.pixel_data;
295     out = l->pixmap;
296
297 /* this malloc is a complete waste of time on normal 32bpp
298    as reduce_depth just sets im->data = data and returns
299 */
300     scratch = g_new(RrPixel32, im->width * im->height);
301     im->data = (gchar*) scratch;
302     RrReduceDepth(l->inst, in, im);
303     XPutImage(RrDisplay(l->inst), out,
304               DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
305               im, 0, 0, x, y, w, h);
306     im->data = NULL;
307     XDestroyImage(im);
308     g_free(scratch);
309 }
310
311 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
312 {
313     *l = *t = *r = *b = 0;
314
315     if (a->surface.grad != RR_SURFACE_PARENTREL) {
316         if (a->surface.relief != RR_RELIEF_FLAT) {
317             switch (a->surface.bevel) {
318             case RR_BEVEL_1:
319                 *l = *t = *r = *b = 1;
320                 break;
321             case RR_BEVEL_2:
322                 *l = *t = *r = *b = 2;
323                 break;
324             }
325         } else if (a->surface.border) {
326             *l = *t = *r = *b = 1;
327         }
328     }
329 }
330
331 void RrMinsize(RrAppearance *a, gint *w, gint *h)
332 {
333     gint i;
334     RrSize *m;
335     gint l, t, r, b;
336     *w = *h = 0;
337
338     for (i = 0; i < a->textures; ++i) {
339         switch (a->texture[i].type) {
340         case RR_TEXTURE_NONE:
341             break;
342         case RR_TEXTURE_MASK:
343             *w = MAX(*w, a->texture[i].data.mask.mask->width);
344             *h = MAX(*h, a->texture[i].data.mask.mask->height);
345             break;
346         case RR_TEXTURE_TEXT:
347             m = RrFontMeasureString(a->texture[i].data.text.font,
348                                     a->texture[i].data.text.string);
349             *w = MAX(*w, m->width + 4);
350             m->height = RrFontHeight(a->texture[i].data.text.font);
351             *h += MAX(*h, m->height);
352             g_free(m);
353             break;
354         case RR_TEXTURE_RGBA:
355             *w += MAX(*w, a->texture[i].data.rgba.width);
356             *h += MAX(*h, a->texture[i].data.rgba.height);
357             break;
358         case RR_TEXTURE_LINE_ART:
359             *w += MAX(*w, MAX(a->texture[i].data.lineart.x1,
360                               a->texture[i].data.lineart.x2));
361             *h += MAX(*h, MAX(a->texture[i].data.lineart.y1,
362                               a->texture[i].data.lineart.y2));
363             break;
364         }
365     }
366
367     RrMargins(a, &l, &t, &r, &b);
368
369     *w += l + r;
370     *h += t + b;
371
372     if (*w < 1) *w = 1;
373     if (*h < 1) *h = 1;
374 }
375
376 static void reverse_bits(gchar *c, gint n)
377 {
378     gint i;
379     for (i = 0; i < n; i++, c++)
380         *c = (((*c * 0x0802UL & 0x22110UL) |
381                (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
382 }
383
384 gboolean RrPixmapToRGBA(const RrInstance *inst,
385                         Pixmap pmap, Pixmap mask,
386                         gint *w, gint *h, RrPixel32 **data)
387 {
388     Window xr;
389     gint xx, xy;
390     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
391     XImage *xi, *xm = NULL;
392
393     if (!XGetGeometry(RrDisplay(inst), pmap,
394                       &xr, &xx, &xy, &pw, &ph, &xb, &xd))
395         return FALSE;
396
397     if (mask) {
398         if (!XGetGeometry(RrDisplay(inst), mask,
399                           &xr, &xx, &xy, &mw, &mh, &xb, &xd))
400             return FALSE;
401         if (pw != mw || ph != mh || xd != 1)
402             return FALSE;
403     }
404
405     xi = XGetImage(RrDisplay(inst), pmap,
406                    0, 0, pw, ph, 0xffffffff, ZPixmap);
407     if (!xi)
408         return FALSE;
409
410     if (mask) {
411         xm = XGetImage(RrDisplay(inst), mask,
412                        0, 0, mw, mh, 0xffffffff, ZPixmap);
413         if (!xm) {
414             XDestroyImage(xi);
415             return FALSE;
416         }
417         if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
418             reverse_bits(xm->data, xm->bytes_per_line * xm->height);
419     }
420
421     if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
422         reverse_bits(xi->data, xi->bytes_per_line * xi->height);
423
424     *data = g_new(RrPixel32, pw * ph);
425     RrIncreaseDepth(inst, *data, xi);
426
427     if (mask) {
428         /* apply transparency from the mask */
429         di = 0;
430         for (i = 0, y = 0; y < ph; ++y) {
431             for (x = 0; x < pw; ++x, ++i) {
432                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
433                     (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
434             }
435             di += xm->bytes_per_line;
436         }
437     }
438
439     *w = pw;
440     *h = ph;
441
442     XDestroyImage(xi);
443     if (mask)
444         XDestroyImage(xm);
445
446     return TRUE;
447 }