allow effects on parentrelative textures, ie bevels and interlaces and borders
[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-2007   Dana 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 "render.h"
22 #include "gradient.h"
23 #include "font.h"
24 #include "mask.h"
25 #include "color.h"
26 #include "image.h"
27 #include "theme.h"
28
29 #include <glib.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include <X11/Xft/Xft.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, force_transfer = 0;
44     Pixmap oldp = None;
45     RrRect tarea; /* area in which to draw textures */
46     gboolean resized;
47
48     if (w <= 0 || h <= 0) return None;
49
50     if (a->surface.parentx < 0 || a->surface.parenty < 0) {
51         /* ob_debug("Invalid parent co-ordinates\n"); */
52         return None;
53     }
54
55     if (a->surface.grad == RR_SURFACE_PARENTREL &&
56         (a->surface.parentx >= a->surface.parent->w ||
57          a->surface.parenty >= a->surface.parent->h))
58     {
59         return None;
60     }
61
62     resized = (a->w != w || a->h != h);
63
64     oldp = a->pixmap; /* save to free after changing the visible pixmap */
65     a->pixmap = XCreatePixmap(RrDisplay(a->inst),
66                               RrRootWindow(a->inst),
67                               w, h, RrDepth(a->inst));
68
69     g_assert(a->pixmap != None);
70     a->w = w;
71     a->h = h;
72
73     if (a->xftdraw != NULL)
74         XftDrawDestroy(a->xftdraw);
75     a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
76                                RrVisual(a->inst), RrColormap(a->inst));
77     g_assert(a->xftdraw != NULL);
78
79     if (resized) {
80         g_free(a->surface.pixel_data);
81         a->surface.pixel_data = g_new(RrPixel32, w * h);
82     }
83
84     RrRender(a, w, h);
85
86     {
87         gint l, t, r, b;
88         RrMargins(a, &l, &t, &r, &b);
89         RECT_SET(tarea, l, t, w - l - r, h - t - b); 
90     }       
91
92     for (i = 0; i < a->textures; i++) {
93         switch (a->texture[i].type) {
94         case RR_TEXTURE_NONE:
95             break;
96         case RR_TEXTURE_TEXT:
97             if (!transferred) {
98                 transferred = 1;
99                 if ((a->surface.grad != RR_SURFACE_SOLID)
100                     || (a->surface.interlaced))
101                     pixel_data_to_pixmap(a, 0, 0, w, h);
102             }
103             if (a->xftdraw == NULL) {
104                 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap, 
105                                            RrVisual(a->inst),
106                                            RrColormap(a->inst));
107             }
108             RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
109             break;
110         case RR_TEXTURE_LINE_ART:
111             if (!transferred) {
112                 transferred = 1;
113                 if ((a->surface.grad != RR_SURFACE_SOLID)
114                     || (a->surface.interlaced))
115                     pixel_data_to_pixmap(a, 0, 0, w, h);
116             }
117             XDrawLine(RrDisplay(a->inst), a->pixmap,
118                       RrColorGC(a->texture[i].data.lineart.color),
119                       a->texture[i].data.lineart.x1,
120                       a->texture[i].data.lineart.y1,
121                       a->texture[i].data.lineart.x2,
122                       a->texture[i].data.lineart.y2);
123             break;
124         case RR_TEXTURE_MASK:
125             if (!transferred) {
126                 transferred = 1;
127                 if ((a->surface.grad != RR_SURFACE_SOLID)
128                     || (a->surface.interlaced))
129                     pixel_data_to_pixmap(a, 0, 0, w, h);
130             }
131             RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
132             break;
133         case RR_TEXTURE_RGBA:
134             g_assert(!transferred);
135             RrImageDraw(a->surface.pixel_data,
136                         &a->texture[i].data.rgba,
137                         a->w, a->h,
138                         &tarea);
139             force_transfer = 1;
140         break;
141         }
142     }
143
144     if (!transferred) {
145         transferred = 1;
146         if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced) ||
147             force_transfer)
148         {
149             pixel_data_to_pixmap(a, 0, 0, w, h);
150         }
151     }
152
153     return oldp;
154 }
155
156 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
157 {
158     Pixmap oldp;
159
160     oldp = RrPaintPixmap(a, w, h);
161     XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
162     XClearWindow(RrDisplay(a->inst), win);
163     /* free this after changing the visible pixmap */
164     if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
165 }
166
167 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
168 {
169   RrAppearance *out;
170
171   out = g_new0(RrAppearance, 1);
172   out->inst = inst;
173   out->textures = numtex;
174   if (numtex) out->texture = g_new0(RrTexture, numtex);
175
176   return out;
177 }
178
179 void RrAppearanceAddTextures(RrAppearance *a, gint numtex)
180 {
181     g_assert(a->textures == 0);
182
183     a->textures = numtex;
184     if (numtex) a->texture = g_new0(RrTexture, numtex);
185 }
186
187 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
188 {
189     RrSurface *spo, *spc;
190     RrAppearance *copy = g_new(RrAppearance, 1);
191     gint i;
192
193     copy->inst = orig->inst;
194
195     spo = &(orig->surface);
196     spc = &(copy->surface);
197     spc->grad = spo->grad;
198     spc->relief = spo->relief;
199     spc->bevel = spo->bevel;
200     if (spo->primary != NULL)
201         spc->primary = RrColorNew(copy->inst,
202                                   spo->primary->r,
203                                   spo->primary->g, 
204                                   spo->primary->b);
205     else spc->primary = NULL;
206
207     if (spo->secondary != NULL)
208         spc->secondary = RrColorNew(copy->inst,
209                                     spo->secondary->r,
210                                     spo->secondary->g,
211                                     spo->secondary->b);
212     else spc->secondary = NULL;
213
214     if (spo->border_color != NULL)
215         spc->border_color = RrColorNew(copy->inst,
216                                        spo->border_color->r,
217                                        spo->border_color->g,
218                                        spo->border_color->b);
219     else spc->border_color = NULL;
220
221     if (spo->interlace_color != NULL)
222         spc->interlace_color = RrColorNew(copy->inst,
223                                        spo->interlace_color->r,
224                                        spo->interlace_color->g,
225                                        spo->interlace_color->b);
226     else spc->interlace_color = NULL;
227
228     if (spo->bevel_dark != NULL)
229         spc->bevel_dark = RrColorNew(copy->inst,
230                                      spo->bevel_dark->r,
231                                      spo->bevel_dark->g,
232                                      spo->bevel_dark->b);
233     else spc->bevel_dark = NULL;
234
235     if (spo->bevel_light != NULL)
236         spc->bevel_light = RrColorNew(copy->inst,
237                                       spo->bevel_light->r,
238                                       spo->bevel_light->g,
239                                       spo->bevel_light->b);
240     else spc->bevel_light = NULL;
241
242     spc->interlaced = spo->interlaced;
243     spc->border = spo->border;
244     spc->parent = NULL;
245     spc->parentx = spc->parenty = 0;
246     spc->pixel_data = NULL;
247
248     copy->textures = orig->textures;
249     copy->texture = g_memdup(orig->texture,
250                              orig->textures * sizeof(RrTexture));
251     for (i = 0; i < copy->textures; ++i)
252         if (copy->texture[i].type == RR_TEXTURE_RGBA) {
253             copy->texture[i].data.rgba.cache = NULL;
254         }
255     copy->pixmap = None;
256     copy->xftdraw = NULL;
257     copy->w = copy->h = 0;
258     return copy;
259 }
260
261 void RrAppearanceFree(RrAppearance *a)
262 {
263     gint i;
264
265     if (a) {
266         RrSurface *p;
267         if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
268         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
269         for (i = 0; i < a->textures; ++i)
270             if (a->texture[i].type == RR_TEXTURE_RGBA) {
271                 g_free(a->texture[i].data.rgba.cache);
272                 a->texture[i].data.rgba.cache = NULL;
273             }
274         if (a->textures)
275             g_free(a->texture);
276         p = &a->surface;
277         RrColorFree(p->primary);
278         RrColorFree(p->secondary);
279         RrColorFree(p->border_color);
280         RrColorFree(p->interlace_color);
281         RrColorFree(p->bevel_dark);
282         RrColorFree(p->bevel_light);
283         g_free(p->pixel_data);
284         p->pixel_data = NULL;
285         g_free(a);
286     }
287 }
288
289
290 static void pixel_data_to_pixmap(RrAppearance *l,
291                                  gint x, gint y, gint w, gint h)
292 {
293     RrPixel32 *in, *scratch;
294     Pixmap out;
295     XImage *im = NULL;
296     im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
297                       ZPixmap, 0, NULL, w, h, 32, 0);
298     g_assert(im != NULL);
299
300     in = l->surface.pixel_data;
301     out = l->pixmap;
302
303 /* this malloc is a complete waste of time on normal 32bpp
304    as reduce_depth just sets im->data = data and returns
305 */
306     scratch = g_new(RrPixel32, im->width * im->height);
307     im->data = (gchar*) scratch;
308     RrReduceDepth(l->inst, in, im);
309     XPutImage(RrDisplay(l->inst), out,
310               DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
311               im, 0, 0, x, y, w, h);
312     im->data = NULL;
313     XDestroyImage(im);
314     g_free(scratch);
315 }
316
317 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
318 {
319     *l = *t = *r = *b = 0;
320
321     if (a->surface.grad != RR_SURFACE_PARENTREL) {
322         if (a->surface.relief != RR_RELIEF_FLAT) {
323             switch (a->surface.bevel) {
324             case RR_BEVEL_1:
325                 *l = *t = *r = *b = 1;
326                 break;
327             case RR_BEVEL_2:
328                 *l = *t = *r = *b = 2;
329                 break;
330             }
331         } else if (a->surface.border) {
332             *l = *t = *r = *b = 1;
333         }
334     }
335 }
336
337 void RrMinSize(RrAppearance *a, gint *w, gint *h)
338 {
339     *w = RrMinWidth(a);
340     *h = RrMinHeight(a);
341 }
342
343 gint RrMinWidth(RrAppearance *a)
344 {
345     gint i;
346     RrSize *m;
347     gint l, t, r, b;
348     gint w = 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             break;
357         case RR_TEXTURE_TEXT:
358             m = RrFontMeasureString(a->texture[i].data.text.font,
359                                     a->texture[i].data.text.string, 
360                                     a->texture[i].data.text.shadow_offset_x,
361                                     a->texture[i].data.text.shadow_offset_y);
362             w = MAX(w, m->width);
363             g_free(m);
364             break;
365         case RR_TEXTURE_RGBA:
366             w += MAX(w, a->texture[i].data.rgba.width);
367             break;
368         case RR_TEXTURE_LINE_ART:
369             w += MAX(w, MAX(a->texture[i].data.lineart.x1,
370                             a->texture[i].data.lineart.x2));
371             break;
372         }
373     }
374
375     RrMargins(a, &l, &t, &r, &b);
376
377     w += l + r;
378
379     if (w < 1) w = 1;
380     return w;
381 }
382
383 gint RrMinHeight(RrAppearance *a)
384 {
385     gint i;
386     gint l, t, r, b;
387     gint h = 0;
388
389     for (i = 0; i < a->textures; ++i) {
390         switch (a->texture[i].type) {
391         case RR_TEXTURE_NONE:
392             break;
393         case RR_TEXTURE_MASK:
394             h = MAX(h, a->texture[i].data.mask.mask->height);
395             break;
396         case RR_TEXTURE_TEXT:
397             h += MAX(h, RrFontHeight(a->texture[i].data.text.font,
398                                      a->texture[i].data.text.shadow_offset_y));
399             break;
400         case RR_TEXTURE_RGBA:
401             h += MAX(h, a->texture[i].data.rgba.height);
402             break;
403         case RR_TEXTURE_LINE_ART:
404             h += MAX(h, MAX(a->texture[i].data.lineart.y1,
405                             a->texture[i].data.lineart.y2));
406             break;
407         }
408     }
409
410     RrMargins(a, &l, &t, &r, &b);
411
412     h += t + b;
413
414     if (h < 1) h = 1;
415     return h;
416 }
417
418 static void reverse_bits(gchar *c, gint n)
419 {
420     gint i;
421     for (i = 0; i < n; i++, c++)
422         *c = (((*c * 0x0802UL & 0x22110UL) |
423                (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
424 }
425
426 gboolean RrPixmapToRGBA(const RrInstance *inst,
427                         Pixmap pmap, Pixmap mask,
428                         gint *w, gint *h, RrPixel32 **data)
429 {
430     Window xr;
431     gint xx, xy;
432     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
433     XImage *xi, *xm = NULL;
434
435     if (!XGetGeometry(RrDisplay(inst), pmap,
436                       &xr, &xx, &xy, &pw, &ph, &xb, &xd))
437         return FALSE;
438
439     if (mask) {
440         if (!XGetGeometry(RrDisplay(inst), mask,
441                           &xr, &xx, &xy, &mw, &mh, &xb, &xd))
442             return FALSE;
443         if (pw != mw || ph != mh || xd != 1)
444             return FALSE;
445     }
446
447     xi = XGetImage(RrDisplay(inst), pmap,
448                    0, 0, pw, ph, 0xffffffff, ZPixmap);
449     if (!xi)
450         return FALSE;
451
452     if (mask) {
453         xm = XGetImage(RrDisplay(inst), mask,
454                        0, 0, mw, mh, 0xffffffff, ZPixmap);
455         if (!xm) {
456             XDestroyImage(xi);
457             return FALSE;
458         }
459         if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
460             reverse_bits(xm->data, xm->bytes_per_line * xm->height);
461     }
462
463     if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
464         reverse_bits(xi->data, xi->bytes_per_line * xi->height);
465
466     *data = g_new(RrPixel32, pw * ph);
467     RrIncreaseDepth(inst, *data, xi);
468
469     if (mask) {
470         /* apply transparency from the mask */
471         di = 0;
472         for (i = 0, y = 0; y < ph; ++y) {
473             for (x = 0; x < pw; ++x, ++i) {
474                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
475                     (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
476             }
477             di += xm->bytes_per_line;
478         }
479     }
480
481     *w = pw;
482     *h = ph;
483
484     XDestroyImage(xi);
485     if (mask)
486         XDestroyImage(xm);
487
488     return TRUE;
489 }