]> icculus.org git repositories - dana/openbox.git/blob - render/render.c
make bevel strengths theme-selectable
[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_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   out->surface.bevel_light_adjust = 128;
175   out->surface.bevel_dark_adjust = 64;
176   if (numtex) out->texture = g_new0(RrTexture, numtex);
177
178   return out;
179 }
180
181 void RrAppearanceAddTextures(RrAppearance *a, gint numtex)
182 {
183     g_assert(a->textures == 0);
184
185     a->textures = numtex;
186     if (numtex) a->texture = g_new0(RrTexture, numtex);
187 }
188
189 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
190 {
191     RrSurface *spo, *spc;
192     RrAppearance *copy = g_new(RrAppearance, 1);
193     gint i;
194
195     copy->inst = orig->inst;
196
197     spo = &(orig->surface);
198     spc = &(copy->surface);
199     spc->grad = spo->grad;
200     spc->relief = spo->relief;
201     spc->bevel = spo->bevel;
202     if (spo->primary != NULL)
203         spc->primary = RrColorNew(copy->inst,
204                                   spo->primary->r,
205                                   spo->primary->g, 
206                                   spo->primary->b);
207     else spc->primary = NULL;
208
209     if (spo->secondary != NULL)
210         spc->secondary = RrColorNew(copy->inst,
211                                     spo->secondary->r,
212                                     spo->secondary->g,
213                                     spo->secondary->b);
214     else spc->secondary = NULL;
215
216     if (spo->border_color != NULL)
217         spc->border_color = RrColorNew(copy->inst,
218                                        spo->border_color->r,
219                                        spo->border_color->g,
220                                        spo->border_color->b);
221     else spc->border_color = NULL;
222
223     if (spo->interlace_color != NULL)
224         spc->interlace_color = RrColorNew(copy->inst,
225                                        spo->interlace_color->r,
226                                        spo->interlace_color->g,
227                                        spo->interlace_color->b);
228     else spc->interlace_color = NULL;
229
230     if (spo->bevel_dark != NULL)
231         spc->bevel_dark = RrColorNew(copy->inst,
232                                      spo->bevel_dark->r,
233                                      spo->bevel_dark->g,
234                                      spo->bevel_dark->b);
235     else spc->bevel_dark = NULL;
236
237     if (spo->bevel_light != NULL)
238         spc->bevel_light = RrColorNew(copy->inst,
239                                       spo->bevel_light->r,
240                                       spo->bevel_light->g,
241                                       spo->bevel_light->b);
242     else spc->bevel_light = NULL;
243
244     spc->interlaced = spo->interlaced;
245     spc->bevel_light_adjust = spo->bevel_light_adjust;
246     spc->bevel_dark_adjust = spo->bevel_dark_adjust;
247     spc->border = spo->border;
248     spc->parent = NULL;
249     spc->parentx = spc->parenty = 0;
250     spc->pixel_data = NULL;
251
252     copy->textures = orig->textures;
253     copy->texture = g_memdup(orig->texture,
254                              orig->textures * sizeof(RrTexture));
255     for (i = 0; i < copy->textures; ++i)
256         if (copy->texture[i].type == RR_TEXTURE_RGBA) {
257             copy->texture[i].data.rgba.cache = NULL;
258         }
259     copy->pixmap = None;
260     copy->xftdraw = NULL;
261     copy->w = copy->h = 0;
262     return copy;
263 }
264
265 void RrAppearanceFree(RrAppearance *a)
266 {
267     gint i;
268
269     if (a) {
270         RrSurface *p;
271         if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
272         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
273         for (i = 0; i < a->textures; ++i)
274             if (a->texture[i].type == RR_TEXTURE_RGBA) {
275                 g_free(a->texture[i].data.rgba.cache);
276                 a->texture[i].data.rgba.cache = NULL;
277             }
278         if (a->textures)
279             g_free(a->texture);
280         p = &a->surface;
281         RrColorFree(p->primary);
282         RrColorFree(p->secondary);
283         RrColorFree(p->border_color);
284         RrColorFree(p->interlace_color);
285         RrColorFree(p->bevel_dark);
286         RrColorFree(p->bevel_light);
287         g_free(p->pixel_data);
288         p->pixel_data = NULL;
289         g_free(a);
290     }
291 }
292
293
294 static void pixel_data_to_pixmap(RrAppearance *l,
295                                  gint x, gint y, gint w, gint h)
296 {
297     RrPixel32 *in, *scratch;
298     Pixmap out;
299     XImage *im = NULL;
300     im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
301                       ZPixmap, 0, NULL, w, h, 32, 0);
302     g_assert(im != NULL);
303
304     in = l->surface.pixel_data;
305     out = l->pixmap;
306
307 /* this malloc is a complete waste of time on normal 32bpp
308    as reduce_depth just sets im->data = data and returns
309 */
310     scratch = g_new(RrPixel32, im->width * im->height);
311     im->data = (gchar*) scratch;
312     RrReduceDepth(l->inst, in, im);
313     XPutImage(RrDisplay(l->inst), out,
314               DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
315               im, 0, 0, x, y, w, h);
316     im->data = NULL;
317     XDestroyImage(im);
318     g_free(scratch);
319 }
320
321 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
322 {
323     *l = *t = *r = *b = 0;
324
325     if (a->surface.grad != RR_SURFACE_PARENTREL) {
326         if (a->surface.relief != RR_RELIEF_FLAT) {
327             switch (a->surface.bevel) {
328             case RR_BEVEL_1:
329                 *l = *t = *r = *b = 1;
330                 break;
331             case RR_BEVEL_2:
332                 *l = *t = *r = *b = 2;
333                 break;
334             }
335         } else if (a->surface.border) {
336             *l = *t = *r = *b = 1;
337         }
338     }
339 }
340
341 void RrMinSize(RrAppearance *a, gint *w, gint *h)
342 {
343     *w = RrMinWidth(a);
344     *h = RrMinHeight(a);
345 }
346
347 gint RrMinWidth(RrAppearance *a)
348 {
349     gint i;
350     RrSize *m;
351     gint l, t, r, b;
352     gint w = 0;
353
354     for (i = 0; i < a->textures; ++i) {
355         switch (a->texture[i].type) {
356         case RR_TEXTURE_NONE:
357             break;
358         case RR_TEXTURE_MASK:
359             w = MAX(w, a->texture[i].data.mask.mask->width);
360             break;
361         case RR_TEXTURE_TEXT:
362             m = RrFontMeasureString(a->texture[i].data.text.font,
363                                     a->texture[i].data.text.string, 
364                                     a->texture[i].data.text.shadow_offset_x,
365                                     a->texture[i].data.text.shadow_offset_y);
366             w = MAX(w, m->width);
367             g_free(m);
368             break;
369         case RR_TEXTURE_RGBA:
370             w += MAX(w, a->texture[i].data.rgba.width);
371             break;
372         case RR_TEXTURE_LINE_ART:
373             w += MAX(w, MAX(a->texture[i].data.lineart.x1,
374                             a->texture[i].data.lineart.x2));
375             break;
376         }
377     }
378
379     RrMargins(a, &l, &t, &r, &b);
380
381     w += l + r;
382
383     if (w < 1) w = 1;
384     return w;
385 }
386
387 gint RrMinHeight(RrAppearance *a)
388 {
389     gint i;
390     gint l, t, r, b;
391     gint h = 0;
392
393     for (i = 0; i < a->textures; ++i) {
394         switch (a->texture[i].type) {
395         case RR_TEXTURE_NONE:
396             break;
397         case RR_TEXTURE_MASK:
398             h = MAX(h, a->texture[i].data.mask.mask->height);
399             break;
400         case RR_TEXTURE_TEXT:
401             h += MAX(h, RrFontHeight(a->texture[i].data.text.font,
402                                      a->texture[i].data.text.shadow_offset_y));
403             break;
404         case RR_TEXTURE_RGBA:
405             h += MAX(h, a->texture[i].data.rgba.height);
406             break;
407         case RR_TEXTURE_LINE_ART:
408             h += MAX(h, MAX(a->texture[i].data.lineart.y1,
409                             a->texture[i].data.lineart.y2));
410             break;
411         }
412     }
413
414     RrMargins(a, &l, &t, &r, &b);
415
416     h += t + b;
417
418     if (h < 1) h = 1;
419     return h;
420 }
421
422 static void reverse_bits(gchar *c, gint n)
423 {
424     gint i;
425     for (i = 0; i < n; i++, c++)
426         *c = (((*c * 0x0802UL & 0x22110UL) |
427                (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
428 }
429
430 gboolean RrPixmapToRGBA(const RrInstance *inst,
431                         Pixmap pmap, Pixmap mask,
432                         gint *w, gint *h, RrPixel32 **data)
433 {
434     Window xr;
435     gint xx, xy;
436     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
437     XImage *xi, *xm = NULL;
438
439     if (!XGetGeometry(RrDisplay(inst), pmap,
440                       &xr, &xx, &xy, &pw, &ph, &xb, &xd))
441         return FALSE;
442
443     if (mask) {
444         if (!XGetGeometry(RrDisplay(inst), mask,
445                           &xr, &xx, &xy, &mw, &mh, &xb, &xd))
446             return FALSE;
447         if (pw != mw || ph != mh || xd != 1)
448             return FALSE;
449     }
450
451     xi = XGetImage(RrDisplay(inst), pmap,
452                    0, 0, pw, ph, 0xffffffff, ZPixmap);
453     if (!xi)
454         return FALSE;
455
456     if (mask) {
457         xm = XGetImage(RrDisplay(inst), mask,
458                        0, 0, mw, mh, 0xffffffff, ZPixmap);
459         if (!xm) {
460             XDestroyImage(xi);
461             return FALSE;
462         }
463         if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
464             reverse_bits(xm->data, xm->bytes_per_line * xm->height);
465     }
466
467     if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
468         reverse_bits(xi->data, xi->bytes_per_line * xi->height);
469
470     *data = g_new(RrPixel32, pw * ph);
471     RrIncreaseDepth(inst, *data, xi);
472
473     if (mask) {
474         /* apply transparency from the mask */
475         di = 0;
476         for (i = 0, y = 0; y < ph; ++y) {
477             for (x = 0; x < pw; ++x, ++i) {
478                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
479                     (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
480             }
481             di += xm->bytes_per_line;
482         }
483     }
484
485     *w = pw;
486     *h = ph;
487
488     XDestroyImage(xi);
489     if (mask)
490         XDestroyImage(xm);
491
492     return TRUE;
493 }