more namespacing with Rr*
[mikachu/openbox.git] / render / render.c
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3
4 #include "render.h"
5 #include "gradient.h"
6 #include "font.h"
7 #include "mask.h"
8 #include "color.h"
9 #include "image.h"
10 #include "theme.h"
11
12 #include <glib.h>
13
14 #ifdef HAVE_STDLIB_H
15 #  include <stdlib.h>
16 #endif
17
18 static void RrPixel32_to_pixmap(RrAppearance *l, gint x, gint y, gint w, gint h);
19
20 void RrPaint(RrAppearance *l, Window win, gint w, gint h)
21 {
22     int i, transferred = 0, sw;
23     RrPixel32 *source, *dest;
24     Pixmap oldp;
25     Rect tarea; /* area in which to draw textures */
26     gboolean resized;
27
28     if (w <= 0 || h <= 0) return;
29
30     resized = (l->w != w || l->h != h);
31
32     if (resized) {
33         oldp = l->pixmap; /* save to free after changing the visible pixmap */
34         l->pixmap = XCreatePixmap(RrDisplay(l->inst),
35                                   RrRootWindow(l->inst),
36                                   w, h, RrDepth(l->inst));
37     } else
38         oldp = None;
39
40     g_assert(l->pixmap != None);
41     l->w = w;
42     l->h = h;
43
44     if (l->xftdraw != NULL)
45         XftDrawDestroy(l->xftdraw);
46     l->xftdraw = XftDrawCreate(RrDisplay(l->inst), l->pixmap,
47                                RrVisual(l->inst), RrColormap(l->inst));
48     g_assert(l->xftdraw != NULL);
49
50     g_free(l->surface.RrPixel_data);
51     l->surface.RrPixel_data = g_new(RrPixel32, w * h);
52
53     if (l->surface.grad == RR_SURFACE_PARENTREL) {
54         g_assert (l->surface.parent);
55         g_assert (l->surface.parent->w);
56
57         sw = l->surface.parent->w;
58         source = (l->surface.parent->surface.RrPixel_data + l->surface.parentx +
59                   sw * l->surface.parenty);
60         dest = l->surface.RrPixel_data;
61         for (i = 0; i < h; i++, source += sw, dest += w) {
62             memcpy(dest, source, w * sizeof(RrPixel32));
63         }
64     } else if (l->surface.grad == RR_SURFACE_SOLID)
65         gradient_solid(l, 0, 0, w, h);
66     else
67         gradient_render(&l->surface, w, h);
68
69     RECT_SET(tarea, 0, 0, w, h);
70     if (l->surface.grad != RR_SURFACE_PARENTREL) {
71         if (l->surface.relief != RR_RELIEF_FLAT) {
72             switch (l->surface.bevel) {
73             case RR_BEVEL_1:
74                 tarea.x += 1; tarea.y += 1;
75                 tarea.width -= 2; tarea.height -= 2;
76                 break;
77             case RR_BEVEL_2:
78                 tarea.x += 2; tarea.y += 2;
79                 tarea.width -= 4; tarea.height -= 4;
80                 break;
81             }
82         } else if (l->surface.border) {
83             tarea.x += 1; tarea.y += 1;
84             tarea.width -= 2; tarea.height -= 2;
85         }
86     }
87
88     for (i = 0; i < l->textures; i++) {
89         switch (l->texture[i].type) {
90         case RR_TEXTURE_NONE:
91             break;
92         case RR_TEXTURE_TEXT:
93             if (!transferred) {
94                 transferred = 1;
95                 if (l->surface.grad != RR_SURFACE_SOLID)
96                     RrPixel32_to_pixmap(l, 0, 0, w, h);
97             }
98             if (l->xftdraw == NULL) {
99                 l->xftdraw = XftDrawCreate(RrDisplay(l->inst), l->pixmap, 
100                                            RrVisual(l->inst),
101                                            RrColormap(l->inst));
102             }
103             RrFontDraw(l->xftdraw, &l->texture[i].data.text, &tarea);
104         break;
105         case RR_TEXTURE_MASK:
106             if (!transferred) {
107                 transferred = 1;
108                 if (l->surface.grad != RR_SURFACE_SOLID)
109                     RrPixel32_to_pixmap(l, 0, 0, w, h);
110             }
111             if (l->texture[i].data.mask.color->gc == None)
112                 color_allocate_gc(l->texture[i].data.mask.color);
113             RrPixmapMaskDraw(l->pixmap, &l->texture[i].data.mask, &tarea);
114         break;
115         case RR_TEXTURE_RGBA:
116             RrImageDraw(l->surface.RrPixel_data,
117                         &l->texture[i].data.rgba, &tarea);
118         break;
119         }
120     }
121
122     if (!transferred) {
123         transferred = 1;
124         if (l->surface.grad != RR_SURFACE_SOLID)
125             RrPixel32_to_pixmap(l, 0, 0, w, h);
126     }
127
128
129     XSetWindowBackgroundPixmap(RrDisplay(l->inst), win, l->pixmap);
130     XClearWindow(RrDisplay(l->inst), win);
131     if (oldp) XFreePixmap(RrDisplay(l->inst), oldp);
132 }
133
134 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
135 {
136   RrAppearance *out;
137
138   out = g_new0(RrAppearance, 1);
139   out->inst = inst;
140   out->textures = numtex;
141   if (numtex) out->texture = g_new0(RrTexture, numtex);
142
143   return out;
144 }
145
146 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
147 {
148     RrSurface *spo, *spc;
149     RrAppearance *copy = g_new(RrAppearance, 1);
150
151     copy->inst = orig->inst;
152
153     spo = &(orig->surface);
154     spc = &(copy->surface);
155     spc->grad = spo->grad;
156     spc->relief = spo->relief;
157     spc->bevel = spo->bevel;
158     if (spo->primary != NULL)
159         spc->primary = RrColorNew(copy->inst,
160                                   spo->primary->r,
161                                   spo->primary->g, 
162                                   spo->primary->b);
163     else spc->primary = NULL;
164
165     if (spo->secondary != NULL)
166         spc->secondary = RrColorNew(copy->inst,
167                                     spo->secondary->r,
168                                     spo->secondary->g,
169                                     spo->secondary->b);
170     else spc->secondary = NULL;
171
172     if (spo->border_color != NULL)
173         spc->border_color = RrColorNew(copy->inst,
174                                        spo->border_color->r,
175                                        spo->border_color->g,
176                                        spo->border_color->b);
177     else spc->border_color = NULL;
178
179     if (spo->bevel_dark != NULL)
180         spc->bevel_dark = RrColorNew(copy->inst,
181                                      spo->bevel_dark->r,
182                                      spo->bevel_dark->g,
183                                      spo->bevel_dark->b);
184     else spc->bevel_dark = NULL;
185
186     if (spo->bevel_light != NULL)
187         spc->bevel_light = RrColorNew(copy->inst,
188                                       spo->bevel_light->r,
189                                       spo->bevel_light->g,
190                                       spo->bevel_light->b);
191     else spc->bevel_light = NULL;
192
193     spc->interlaced = spo->interlaced;
194     spc->border = spo->border;
195     spc->RrPixel_data = NULL;
196
197     copy->textures = orig->textures;
198     copy->texture = g_memdup(orig->texture,
199                              orig->textures * sizeof(RrTexture));
200     copy->pixmap = None;
201     copy->xftdraw = NULL;
202     copy->w = copy->h = 0;
203     return copy;
204 }
205
206 void RrAppearanceFree(RrAppearance *a)
207 {
208     if (a) {
209         RrSurface *p;
210         if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
211         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
212         if (a->textures)
213             g_free(a->texture);
214         p = &a->surface;
215         RrColorFree(p->primary);
216         RrColorFree(p->secondary);
217         RrColorFree(p->border_color);
218         RrColorFree(p->bevel_dark);
219         RrColorFree(p->bevel_light);
220         g_free(p->RrPixel_data);
221
222         g_free(a);
223     }
224 }
225
226
227 static void RrPixel32_to_pixmap(RrAppearance *l, gint x, gint y, gint w, gint h)
228 {
229     RrPixel32 *in, *scratch;
230     Pixmap out;
231     XImage *im = NULL;
232     im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
233                       ZPixmap, 0, NULL, w, h, 32, 0);
234     g_assert(im != NULL);
235
236     in = l->surface.RrPixel_data;
237     out = l->pixmap;
238
239     im->byte_order = RrEndian;
240 /* this malloc is a complete waste of time on normal 32bpp
241    as reduce_depth just sets im->data = data and returns
242 */
243     scratch = g_new(RrPixel32, im->width * im->height);
244     im->data = (char*) scratch;
245     reduce_depth(l->inst, in, im);
246     XPutImage(RrDisplay(l->inst), out,
247               DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
248               im, 0, 0, x, y, w, h);
249     im->data = NULL;
250     XDestroyImage(im);
251     g_free(scratch);
252 }
253
254 void RrMinsize(RrAppearance *l, gint *w, gint *h)
255 {
256     gint i;
257     gint m;
258     *w = *h = 0;
259
260     for (i = 0; i < l->textures; ++i) {
261         switch (l->texture[i].type) {
262         case RR_TEXTURE_NONE:
263             break;
264         case RR_TEXTURE_MASK:
265             *w = MAX(*w, l->texture[i].data.mask.mask->width);
266             *h = MAX(*h, l->texture[i].data.mask.mask->height);
267             break;
268         case RR_TEXTURE_TEXT:
269             m = RrFontMeasureString(l->texture[i].data.text.font,
270                                     l->texture[i].data.text.string,
271                                     l->texture[i].data.text.shadow,
272                                     l->texture[i].data.text.offset);
273             *w = MAX(*w, m);
274             m = RrFontHeight(l->texture[i].data.text.font,
275                              l->texture[i].data.text.shadow,
276                              l->texture[i].data.text.offset);
277             *h += MAX(*h, m);
278             break;
279         case RR_TEXTURE_RGBA:
280             *w += MAX(*w, l->texture[i].data.rgba.width);
281             *h += MAX(*h, l->texture[i].data.rgba.height);
282             break;
283         }
284     }
285
286     if (l->surface.relief != RR_RELIEF_FLAT) {
287         switch (l->surface.bevel) {
288         case RR_BEVEL_1:
289             *w += 2;
290             *h += 2;
291             break;
292         case RR_BEVEL_2:
293             *w += 4;
294             *h += 4;
295             break;
296         }
297     } else if (l->surface.border) {
298         *w += 2;
299         *h += 2;
300     }
301
302     if (*w < 1) *w = 1;
303     if (*h < 1) *h = 1;
304 }
305
306 gboolean RrPixmapToRGBA(const RrInstance *inst,
307                         Pixmap pmap, Pixmap mask,
308                         gint *w, gint *h, RrPixel32 **data)
309 {
310     Window xr;
311     gint xx, xy;
312     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
313     XImage *xi, *xm = NULL;
314
315     if (!XGetGeometry(RrDisplay(inst),
316                       pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
317         return FALSE;
318     if (mask) {
319         if (!XGetGeometry(RrDisplay(inst), mask,
320                           &xr, &xx, &xy, &mw, &mh, &xb, &xd))
321             return FALSE;
322         if (pw != mw || ph != mh || xd != 1)
323             return FALSE;
324     }
325
326     xi = XGetImage(RrDisplay(inst), pmap,
327                    0, 0, pw, ph, 0xffffffff, ZPixmap);
328     if (!xi)
329         return FALSE;
330
331     if (mask) {
332         xm = XGetImage(RrDisplay(inst), mask,
333                        0, 0, mw, mh, 0xffffffff, ZPixmap);
334         if (!xm)
335             return FALSE;
336     }
337
338     *data = g_new(RrPixel32, pw * ph);
339     increase_depth(inst, *data, xi);
340
341     if (mask) {
342         /* apply transparency from the mask */
343         di = 0;
344         for (i = 0, y = 0; y < ph; ++y) {
345             for (x = 0; x < pw; ++x, ++i) {
346                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
347                     (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
348             }
349             di += xm->bytes_per_line;
350         }
351     }
352
353     *w = pw;
354     *h = ph;
355
356     return TRUE;
357 }