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