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