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