formatting
[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 pixel_data_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     RrRect 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     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
38     g_assert(l->pixmap != None);
39     l->w = w;
40     l->h = h;
41
42     if (l->xftdraw != NULL)
43         XftDrawDestroy(l->xftdraw);
44     l->xftdraw = XftDrawCreate(RrDisplay(l->inst), l->pixmap,
45                                RrVisual(l->inst), RrColormap(l->inst));
46     g_assert(l->xftdraw != NULL);
47
48     g_free(l->surface.pixel_data);
49     l->surface.pixel_data = g_new(RrPixel32, w * h);
50
51     if (l->surface.grad == RR_SURFACE_PARENTREL) {
52         g_assert (l->surface.parent);
53         g_assert (l->surface.parent->w);
54
55         sw = l->surface.parent->w;
56         source = (l->surface.parent->surface.pixel_data +
57                   l->surface.parentx + sw * l->surface.parenty);
58         dest = l->surface.pixel_data;
59         for (i = 0; i < h; i++, source += sw, dest += w) {
60             memcpy(dest, source, w * sizeof(RrPixel32));
61         }
62     } else
63         RrRender(l, w, h);
64
65     RECT_SET(tarea, 0, 0, w, h);
66     if (l->surface.grad != RR_SURFACE_PARENTREL) {
67         if (l->surface.relief != RR_RELIEF_FLAT) {
68             switch (l->surface.bevel) {
69             case RR_BEVEL_1:
70                 tarea.x += 1; tarea.y += 1;
71                 tarea.width -= 2; tarea.height -= 2;
72                 break;
73             case RR_BEVEL_2:
74                 tarea.x += 2; tarea.y += 2;
75                 tarea.width -= 4; tarea.height -= 4;
76                 break;
77             }
78         } else if (l->surface.border) {
79             tarea.x += 1; tarea.y += 1;
80             tarea.width -= 2; tarea.height -= 2;
81         }
82     }
83
84     for (i = 0; i < l->textures; i++) {
85         switch (l->texture[i].type) {
86         case RR_TEXTURE_NONE:
87             break;
88         case RR_TEXTURE_TEXT:
89             if (!transferred) {
90                 transferred = 1;
91                 if (l->surface.grad != RR_SURFACE_SOLID)
92                     pixel_data_to_pixmap(l, 0, 0, w, h);
93             }
94             if (l->xftdraw == NULL) {
95                 l->xftdraw = XftDrawCreate(RrDisplay(l->inst), l->pixmap, 
96                                            RrVisual(l->inst),
97                                            RrColormap(l->inst));
98             }
99             RrFontDraw(l->xftdraw, &l->texture[i].data.text, &tarea);
100         break;
101         case RR_TEXTURE_MASK:
102             if (!transferred) {
103                 transferred = 1;
104                 if (l->surface.grad != RR_SURFACE_SOLID)
105                     pixel_data_to_pixmap(l, 0, 0, w, h);
106             }
107             if (l->texture[i].data.mask.color->gc == None)
108                 RrColorAllocateGC(l->texture[i].data.mask.color);
109             RrPixmapMaskDraw(l->pixmap, &l->texture[i].data.mask, &tarea);
110         break;
111         case RR_TEXTURE_RGBA:
112             RrImageDraw(l->surface.pixel_data,
113                         &l->texture[i].data.rgba, &tarea);
114         break;
115         }
116     }
117
118     if (!transferred) {
119         transferred = 1;
120         if (l->surface.grad != RR_SURFACE_SOLID)
121             pixel_data_to_pixmap(l, 0, 0, w, h);
122     }
123
124
125     XSetWindowBackgroundPixmap(RrDisplay(l->inst), win, l->pixmap);
126     XClearWindow(RrDisplay(l->inst), win);
127     if (oldp) XFreePixmap(RrDisplay(l->inst), oldp);
128 }
129
130 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
131 {
132   RrAppearance *out;
133
134   out = g_new0(RrAppearance, 1);
135   out->inst = inst;
136   out->textures = numtex;
137   if (numtex) out->texture = g_new0(RrTexture, numtex);
138
139   return out;
140 }
141
142 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
143 {
144     RrSurface *spo, *spc;
145     RrAppearance *copy = g_new(RrAppearance, 1);
146
147     copy->inst = orig->inst;
148
149     spo = &(orig->surface);
150     spc = &(copy->surface);
151     spc->grad = spo->grad;
152     spc->relief = spo->relief;
153     spc->bevel = spo->bevel;
154     if (spo->primary != NULL)
155         spc->primary = RrColorNew(copy->inst,
156                                   spo->primary->r,
157                                   spo->primary->g, 
158                                   spo->primary->b);
159     else spc->primary = NULL;
160
161     if (spo->secondary != NULL)
162         spc->secondary = RrColorNew(copy->inst,
163                                     spo->secondary->r,
164                                     spo->secondary->g,
165                                     spo->secondary->b);
166     else spc->secondary = NULL;
167
168     if (spo->border_color != NULL)
169         spc->border_color = RrColorNew(copy->inst,
170                                        spo->border_color->r,
171                                        spo->border_color->g,
172                                        spo->border_color->b);
173     else spc->border_color = NULL;
174
175     if (spo->bevel_dark != NULL)
176         spc->bevel_dark = RrColorNew(copy->inst,
177                                      spo->bevel_dark->r,
178                                      spo->bevel_dark->g,
179                                      spo->bevel_dark->b);
180     else spc->bevel_dark = NULL;
181
182     if (spo->bevel_light != NULL)
183         spc->bevel_light = RrColorNew(copy->inst,
184                                       spo->bevel_light->r,
185                                       spo->bevel_light->g,
186                                       spo->bevel_light->b);
187     else spc->bevel_light = NULL;
188
189     spc->interlaced = spo->interlaced;
190     spc->border = spo->border;
191     spc->parent = NULL;
192     spc->parentx = spc->parenty = 0;
193     spc->pixel_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->pixel_data);
219
220         g_free(a);
221     }
222 }
223
224
225 static void pixel_data_to_pixmap(RrAppearance *l,
226                                  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.pixel_data;
236     out = l->pixmap;
237
238     im->byte_order = LSBFirst;
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             *w = MAX(*w, m);
271             m = RrFontHeight(l->texture[i].data.text.font);
272             *h += MAX(*h, m);
273             break;
274         case RR_TEXTURE_RGBA:
275             *w += MAX(*w, l->texture[i].data.rgba.width);
276             *h += MAX(*h, l->texture[i].data.rgba.height);
277             break;
278         }
279     }
280
281     if (l->surface.relief != RR_RELIEF_FLAT) {
282         switch (l->surface.bevel) {
283         case RR_BEVEL_1:
284             *w += 2;
285             *h += 2;
286             break;
287         case RR_BEVEL_2:
288             *w += 4;
289             *h += 4;
290             break;
291         }
292     } else if (l->surface.border) {
293         *w += 2;
294         *h += 2;
295     }
296
297     if (*w < 1) *w = 1;
298     if (*h < 1) *h = 1;
299 }
300
301 gboolean RrPixmapToRGBA(const RrInstance *inst,
302                         Pixmap pmap, Pixmap mask,
303                         gint *w, gint *h, RrPixel32 **data)
304 {
305     Window xr;
306     gint xx, xy;
307     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
308     XImage *xi, *xm = NULL;
309
310     if (!XGetGeometry(RrDisplay(inst),
311                       pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
312         return FALSE;
313     if (mask) {
314         if (!XGetGeometry(RrDisplay(inst), mask,
315                           &xr, &xx, &xy, &mw, &mh, &xb, &xd))
316             return FALSE;
317         if (pw != mw || ph != mh || xd != 1)
318             return FALSE;
319     }
320
321     xi = XGetImage(RrDisplay(inst), pmap,
322                    0, 0, pw, ph, 0xffffffff, ZPixmap);
323     if (!xi)
324         return FALSE;
325
326     if (mask) {
327         xm = XGetImage(RrDisplay(inst), mask,
328                        0, 0, mw, mh, 0xffffffff, ZPixmap);
329         if (!xm)
330             return FALSE;
331     }
332
333     *data = g_new(RrPixel32, pw * ph);
334     RrIncreaseDepth(inst, *data, xi);
335
336     if (mask) {
337         /* apply transparency from the mask */
338         di = 0;
339         for (i = 0, y = 0; y < ph; ++y) {
340             for (x = 0; x < pw; ++x, ++i) {
341                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
342                     (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
343             }
344             di += xm->bytes_per_line;
345         }
346     }
347
348     *w = pw;
349     *h = ph;
350
351     return TRUE;
352 }