Remove double newlines.
[dana/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_IMAGE:
134             g_assert(!transferred);
135             {
136                 RrRect narea = tarea;
137                 RrTextureImage *img = &a->texture[i].data.image;
138                 if (img->twidth)
139                     narea.width = MIN(tarea.width, img->twidth);
140                 if (img->theight)
141                     narea.height = MIN(tarea.height, img->theight);
142                 narea.x += img->tx;
143                 narea.y += img->ty;
144                 RrImageDrawImage(a->surface.pixel_data,
145                                  &a->texture[i].data.image,
146                                  a->w, a->h,
147                                  &narea);
148             }
149             force_transfer = 1;
150             break;
151         case RR_TEXTURE_RGBA:
152             g_assert(!transferred);
153             {
154                 RrRect narea = tarea;
155                 RrTextureRGBA *rgb = &a->texture[i].data.rgba;
156                 if (rgb->twidth)
157                     narea.width = MIN(tarea.width, rgb->twidth);
158                 if (rgb->theight)
159                     narea.height = MIN(tarea.height, rgb->theight);
160                 narea.x += rgb->tx;
161                 narea.y += rgb->ty;
162                 RrImageDrawRGBA(a->surface.pixel_data,
163                                 &a->texture[i].data.rgba,
164                                 a->w, a->h,
165                                 &narea);
166             }
167             force_transfer = 1;
168         break;
169         }
170     }
171
172     if (!transferred) {
173         transferred = 1;
174         if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced) ||
175             force_transfer)
176         {
177             pixel_data_to_pixmap(a, 0, 0, w, h);
178         }
179     }
180
181     return oldp;
182 }
183
184 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
185 {
186     Pixmap oldp;
187
188     oldp = RrPaintPixmap(a, w, h);
189     XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
190     XClearWindow(RrDisplay(a->inst), win);
191     /* free this after changing the visible pixmap */
192     if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
193 }
194
195 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
196 {
197   RrAppearance *out;
198
199   out = g_new0(RrAppearance, 1);
200   out->inst = inst;
201   out->textures = numtex;
202   out->surface.bevel_light_adjust = 128;
203   out->surface.bevel_dark_adjust = 64;
204   if (numtex) out->texture = g_new0(RrTexture, numtex);
205
206   return out;
207 }
208
209 void RrAppearanceRemoveTextures(RrAppearance *a)
210 {
211     g_free(a->texture);
212     a->textures = 0;
213 }
214
215 void RrAppearanceAddTextures(RrAppearance *a, gint numtex)
216 {
217     g_assert(a->textures == 0);
218
219     a->textures = numtex;
220     if (numtex) a->texture = g_new0(RrTexture, numtex);
221 }
222
223 void RrAppearanceClearTextures(RrAppearance *a)
224 {
225     memset(a->texture, 0, a->textures * sizeof(RrTexture));
226 }
227
228 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
229 {
230     RrSurface *spo, *spc;
231     RrAppearance *copy = g_new(RrAppearance, 1);
232
233     copy->inst = orig->inst;
234
235     spo = &(orig->surface);
236     spc = &(copy->surface);
237     spc->grad = spo->grad;
238     spc->relief = spo->relief;
239     spc->bevel = spo->bevel;
240     if (spo->primary != NULL)
241         spc->primary = RrColorNew(copy->inst,
242                                   spo->primary->r,
243                                   spo->primary->g,
244                                   spo->primary->b);
245     else spc->primary = NULL;
246
247     if (spo->secondary != NULL)
248         spc->secondary = RrColorNew(copy->inst,
249                                     spo->secondary->r,
250                                     spo->secondary->g,
251                                     spo->secondary->b);
252     else spc->secondary = NULL;
253
254     if (spo->border_color != NULL)
255         spc->border_color = RrColorNew(copy->inst,
256                                        spo->border_color->r,
257                                        spo->border_color->g,
258                                        spo->border_color->b);
259     else spc->border_color = NULL;
260
261     if (spo->interlace_color != NULL)
262         spc->interlace_color = RrColorNew(copy->inst,
263                                        spo->interlace_color->r,
264                                        spo->interlace_color->g,
265                                        spo->interlace_color->b);
266     else spc->interlace_color = NULL;
267
268     if (spo->bevel_dark != NULL)
269         spc->bevel_dark = RrColorNew(copy->inst,
270                                      spo->bevel_dark->r,
271                                      spo->bevel_dark->g,
272                                      spo->bevel_dark->b);
273     else spc->bevel_dark = NULL;
274
275     if (spo->bevel_light != NULL)
276         spc->bevel_light = RrColorNew(copy->inst,
277                                       spo->bevel_light->r,
278                                       spo->bevel_light->g,
279                                       spo->bevel_light->b);
280     else spc->bevel_light = NULL;
281
282     if (spo->split_primary != NULL)
283         spc->split_primary = RrColorNew(copy->inst,
284                                         spo->split_primary->r,
285                                         spo->split_primary->g,
286                                         spo->split_primary->b);
287     else spc->split_primary = NULL;
288
289     if (spo->split_secondary != NULL)
290         spc->split_secondary = RrColorNew(copy->inst,
291                                         spo->split_secondary->r,
292                                         spo->split_secondary->g,
293                                         spo->split_secondary->b);
294     else spc->split_secondary = NULL;
295
296     spc->interlaced = spo->interlaced;
297     spc->bevel_light_adjust = spo->bevel_light_adjust;
298     spc->bevel_dark_adjust = spo->bevel_dark_adjust;
299     spc->border = spo->border;
300     spc->parent = NULL;
301     spc->parentx = spc->parenty = 0;
302     spc->pixel_data = NULL;
303
304     copy->textures = orig->textures;
305     copy->texture = g_memdup(orig->texture,
306                              orig->textures * sizeof(RrTexture));
307     copy->pixmap = None;
308     copy->xftdraw = NULL;
309     copy->w = copy->h = 0;
310     return copy;
311 }
312
313 void RrAppearanceFree(RrAppearance *a)
314 {
315     if (a) {
316         RrSurface *p;
317         if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
318         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
319         if (a->textures)
320             g_free(a->texture);
321         p = &a->surface;
322         RrColorFree(p->primary);
323         RrColorFree(p->secondary);
324         RrColorFree(p->border_color);
325         RrColorFree(p->interlace_color);
326         RrColorFree(p->bevel_dark);
327         RrColorFree(p->bevel_light);
328         RrColorFree(p->split_primary);
329         RrColorFree(p->split_secondary);
330         g_free(p->pixel_data);
331         p->pixel_data = NULL;
332         g_free(a);
333     }
334 }
335
336 static void pixel_data_to_pixmap(RrAppearance *l,
337                                  gint x, gint y, gint w, gint h)
338 {
339     RrPixel32 *in, *scratch;
340     Pixmap out;
341     XImage *im = NULL;
342     im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
343                       ZPixmap, 0, NULL, w, h, 32, 0);
344     g_assert(im != NULL);
345
346     in = l->surface.pixel_data;
347     out = l->pixmap;
348
349 /* this malloc is a complete waste of time on normal 32bpp
350    as reduce_depth just sets im->data = data and returns
351 */
352     scratch = g_new(RrPixel32, im->width * im->height);
353     im->data = (gchar*) scratch;
354     RrReduceDepth(l->inst, in, im);
355     XPutImage(RrDisplay(l->inst), out,
356               DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
357               im, 0, 0, x, y, w, h);
358     im->data = NULL;
359     XDestroyImage(im);
360     g_free(scratch);
361 }
362
363 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
364 {
365     *l = *t = *r = *b = 0;
366
367     if (a->surface.grad != RR_SURFACE_PARENTREL) {
368         if (a->surface.relief != RR_RELIEF_FLAT) {
369             switch (a->surface.bevel) {
370             case RR_BEVEL_1:
371                 *l = *t = *r = *b = 1;
372                 break;
373             case RR_BEVEL_2:
374                 *l = *t = *r = *b = 2;
375                 break;
376             }
377         } else if (a->surface.border) {
378             *l = *t = *r = *b = 1;
379         }
380     }
381 }
382
383 void RrMinSize(RrAppearance *a, gint *w, gint *h)
384 {
385     *w = RrMinWidth(a);
386     *h = RrMinHeight(a);
387 }
388
389 gint RrMinWidth(RrAppearance *a)
390 {
391     gint i;
392     RrSize *m;
393     gint l, t, r, b;
394     gint w = 0;
395
396     RrMargins(a, &l, &t, &r, &b);
397
398     for (i = 0; i < a->textures; ++i) {
399         switch (a->texture[i].type) {
400         case RR_TEXTURE_NONE:
401             break;
402         case RR_TEXTURE_MASK:
403             w = MAX(w, a->texture[i].data.mask.mask->width);
404             break;
405         case RR_TEXTURE_TEXT:
406             m = RrFontMeasureString(a->texture[i].data.text.font,
407                                     a->texture[i].data.text.string,
408                                     a->texture[i].data.text.shadow_offset_x,
409                                     a->texture[i].data.text.shadow_offset_y,
410                                     a->texture[i].data.text.flow,
411                                     a->texture[i].data.text.maxwidth);
412             w = MAX(w, m->width);
413             g_free(m);
414             break;
415         case RR_TEXTURE_RGBA:
416             w += MAX(w, a->texture[i].data.rgba.width);
417             break;
418         case RR_TEXTURE_IMAGE:
419             /* images resize so they don't contribute anything to the min */
420             break;
421         case RR_TEXTURE_LINE_ART:
422             w = MAX(w, MAX(a->texture[i].data.lineart.x1 - l - r,
423                            a->texture[i].data.lineart.x2 - l - r));
424             break;
425         }
426     }
427
428     w += l + r;
429
430     if (w < 1) w = 1;
431     return w;
432 }
433
434 gint RrMinHeight(RrAppearance *a)
435 {
436     gint i;
437     gint l, t, r, b;
438     RrSize *m;
439     gint h = 0;
440
441     RrMargins(a, &l, &t, &r, &b);
442
443     for (i = 0; i < a->textures; ++i) {
444         switch (a->texture[i].type) {
445         case RR_TEXTURE_NONE:
446             break;
447         case RR_TEXTURE_MASK:
448             h = MAX(h, a->texture[i].data.mask.mask->height);
449             break;
450         case RR_TEXTURE_TEXT:
451             if (a->texture[i].data.text.flow) {
452                 g_assert(a->texture[i].data.text.string != NULL);
453
454                 m = RrFontMeasureString
455                     (a->texture[i].data.text.font,
456                      a->texture[i].data.text.string,
457                      a->texture[i].data.text.shadow_offset_x,
458                      a->texture[i].data.text.shadow_offset_y,
459                      a->texture[i].data.text.flow,
460                      a->texture[i].data.text.maxwidth);
461                 h += MAX(h, m->height);
462                 g_free(m);
463             }
464             else
465                 h += MAX(h,
466                          RrFontHeight
467                          (a->texture[i].data.text.font,
468                           a->texture[i].data.text.shadow_offset_y));
469             break;
470         case RR_TEXTURE_RGBA:
471             h += MAX(h, a->texture[i].data.rgba.height);
472             break;
473         case RR_TEXTURE_IMAGE:
474             /* images resize so they don't contribute anything to the min */
475             break;
476         case RR_TEXTURE_LINE_ART:
477             h = MAX(h, MAX(a->texture[i].data.lineart.y1 - t - b,
478                            a->texture[i].data.lineart.y2 - t - b));
479             break;
480         }
481     }
482
483     h += t + b;
484
485     if (h < 1) h = 1;
486     return h;
487 }
488
489 static void reverse_bits(gchar *c, gint n)
490 {
491     gint i;
492     for (i = 0; i < n; i++, c++)
493         *c = (((*c * 0x0802UL & 0x22110UL) |
494                (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
495 }
496
497 gboolean RrPixmapToRGBA(const RrInstance *inst,
498                         Pixmap pmap, Pixmap mask,
499                         gint *w, gint *h, RrPixel32 **data)
500 {
501     Window xr;
502     gint xx, xy;
503     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
504     XImage *xi, *xm = NULL;
505
506     if (!XGetGeometry(RrDisplay(inst), pmap,
507                       &xr, &xx, &xy, &pw, &ph, &xb, &xd))
508         return FALSE;
509
510     if (mask) {
511         if (!XGetGeometry(RrDisplay(inst), mask,
512                           &xr, &xx, &xy, &mw, &mh, &xb, &xd))
513             return FALSE;
514         if (pw != mw || ph != mh || xd != 1)
515             return FALSE;
516     }
517
518     xi = XGetImage(RrDisplay(inst), pmap,
519                    0, 0, pw, ph, 0xffffffff, ZPixmap);
520     if (!xi)
521         return FALSE;
522
523     if (mask) {
524         xm = XGetImage(RrDisplay(inst), mask,
525                        0, 0, mw, mh, 0xffffffff, ZPixmap);
526         if (!xm) {
527             XDestroyImage(xi);
528             return FALSE;
529         }
530         if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
531             reverse_bits(xm->data, xm->bytes_per_line * xm->height);
532     }
533
534     if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
535         reverse_bits(xi->data, xi->bytes_per_line * xi->height);
536
537     *data = g_new(RrPixel32, pw * ph);
538     RrIncreaseDepth(inst, *data, xi);
539
540     if (mask) {
541         /* apply transparency from the mask */
542         di = 0;
543         for (i = 0, y = 0; y < ph; ++y) {
544             for (x = 0; x < pw; ++x, ++i) {
545                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
546                     (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
547             }
548             di += xm->bytes_per_line;
549         }
550     }
551
552     *w = pw;
553     *h = ph;
554
555     XDestroyImage(xi);
556     if (mask)
557         XDestroyImage(xm);
558
559     return TRUE;
560 }