Don't show prompts inside the message handler to prevent recursion
[openbox.git] / obrender / render.c
blobfe9a2a80dd27669b8888b226ab0b62655e52b23a
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 render.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6 Copyright (c) 2003 Derek Foreman
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 See the COPYING file for a copy of the GNU General Public License.
21 #include "render.h"
22 #include "gradient.h"
23 #include "font.h"
24 #include "mask.h"
25 #include "color.h"
26 #include "image.h"
27 #include "theme.h"
29 #include <glib.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include <X11/Xft/Xft.h>
34 #ifdef HAVE_STDLIB_H
35 # include <stdlib.h>
36 #endif
38 static void pixel_data_to_pixmap(RrAppearance *l,
39 gint x, gint y, gint w, gint h);
41 Pixmap RrPaintPixmap(RrAppearance *a, gint w, gint h)
43 gint i, transferred = 0, force_transfer = 0;
44 Pixmap oldp = None;
45 RrRect tarea; /* area in which to draw textures */
46 gboolean resized;
48 if (w <= 0 || h <= 0) return None;
50 if (a->surface.parentx < 0 || a->surface.parenty < 0) {
51 /* ob_debug("Invalid parent co-ordinates\n"); */
52 return None;
55 if (a->surface.grad == RR_SURFACE_PARENTREL &&
56 (a->surface.parentx >= a->surface.parent->w ||
57 a->surface.parenty >= a->surface.parent->h))
59 return None;
62 resized = (a->w != w || a->h != h);
64 oldp = a->pixmap; /* save to free after changing the visible pixmap */
65 a->pixmap = XCreatePixmap(RrDisplay(a->inst),
66 RrRootWindow(a->inst),
67 w, h, RrDepth(a->inst));
69 g_assert(a->pixmap != None);
70 a->w = w;
71 a->h = h;
73 if (a->xftdraw != NULL)
74 XftDrawDestroy(a->xftdraw);
75 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
76 RrVisual(a->inst), RrColormap(a->inst));
77 g_assert(a->xftdraw != NULL);
79 if (resized) {
80 g_free(a->surface.pixel_data);
81 a->surface.pixel_data = g_new(RrPixel32, w * h);
84 RrRender(a, w, h);
87 gint l, t, r, b;
88 RrMargins(a, &l, &t, &r, &b);
89 RECT_SET(tarea, l, t, w - l - r, h - t - b);
92 for (i = 0; i < a->textures; i++) {
93 switch (a->texture[i].type) {
94 case RR_TEXTURE_NONE:
95 break;
96 case RR_TEXTURE_TEXT:
97 if (!transferred) {
98 transferred = 1;
99 if ((a->surface.grad != RR_SURFACE_SOLID)
100 || (a->surface.interlaced))
101 pixel_data_to_pixmap(a, 0, 0, w, h);
103 if (a->xftdraw == NULL) {
104 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
105 RrVisual(a->inst),
106 RrColormap(a->inst));
108 RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
109 break;
110 case RR_TEXTURE_LINE_ART:
111 if (!transferred) {
112 transferred = 1;
113 if ((a->surface.grad != RR_SURFACE_SOLID)
114 || (a->surface.interlaced))
115 pixel_data_to_pixmap(a, 0, 0, w, h);
117 XDrawLine(RrDisplay(a->inst), a->pixmap,
118 RrColorGC(a->texture[i].data.lineart.color),
119 a->texture[i].data.lineart.x1,
120 a->texture[i].data.lineart.y1,
121 a->texture[i].data.lineart.x2,
122 a->texture[i].data.lineart.y2);
123 break;
124 case RR_TEXTURE_MASK:
125 if (!transferred) {
126 transferred = 1;
127 if ((a->surface.grad != RR_SURFACE_SOLID)
128 || (a->surface.interlaced))
129 pixel_data_to_pixmap(a, 0, 0, w, h);
131 RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
132 break;
133 case RR_TEXTURE_IMAGE:
134 g_assert(!transferred);
136 RrRect narea = tarea;
137 RrTextureImage *img = &a->texture[i].data.image;
138 narea.x += img->tx;
139 narea.width -= img->tx;
140 narea.y += img->ty;
141 narea.height -= img->ty;
142 if (img->twidth)
143 narea.width = MIN(narea.width, img->twidth);
144 if (img->theight)
145 narea.height = MIN(narea.height, img->theight);
146 RrImageDrawImage(a->surface.pixel_data,
147 &a->texture[i].data.image,
148 a->w, a->h,
149 &narea);
151 force_transfer = 1;
152 break;
153 case RR_TEXTURE_RGBA:
154 g_assert(!transferred);
156 RrRect narea = tarea;
157 RrTextureRGBA *rgb = &a->texture[i].data.rgba;
158 narea.x += rgb->tx;
159 narea.width -= rgb->tx;
160 narea.y += rgb->ty;
161 narea.height -= rgb->ty;
162 if (rgb->twidth)
163 narea.width = MIN(narea.width, rgb->twidth);
164 if (rgb->theight)
165 narea.height = MIN(narea.height, rgb->theight);
166 RrImageDrawRGBA(a->surface.pixel_data,
167 &a->texture[i].data.rgba,
168 a->w, a->h,
169 &narea);
171 force_transfer = 1;
172 break;
173 case RR_TEXTURE_NUM_TYPES:
174 g_assert_not_reached();
178 if (!transferred) {
179 transferred = 1;
180 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced) ||
181 force_transfer)
183 pixel_data_to_pixmap(a, 0, 0, w, h);
187 return oldp;
190 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
192 Pixmap oldp;
194 oldp = RrPaintPixmap(a, w, h);
195 XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
196 XClearWindow(RrDisplay(a->inst), win);
197 /* free this after changing the visible pixmap */
198 if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
201 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
203 RrAppearance *out;
205 out = g_slice_new0(RrAppearance);
206 out->inst = inst;
207 out->textures = numtex;
208 out->surface.bevel_light_adjust = 128;
209 out->surface.bevel_dark_adjust = 64;
210 if (numtex) out->texture = g_new0(RrTexture, numtex);
212 return out;
215 void RrAppearanceRemoveTextures(RrAppearance *a)
217 g_free(a->texture);
218 a->textures = 0;
221 void RrAppearanceAddTextures(RrAppearance *a, gint numtex)
223 g_assert(a->textures == 0);
225 a->textures = numtex;
226 if (numtex) a->texture = g_new0(RrTexture, numtex);
229 void RrAppearanceClearTextures(RrAppearance *a)
231 memset(a->texture, 0, a->textures * sizeof(RrTexture));
234 /* deep copy of orig, means reset ref to 1 on copy
235 * and copy each thing memwise. */
236 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
238 RrSurface *spo, *spc;
239 RrAppearance *copy = g_slice_new(RrAppearance);
241 copy->inst = orig->inst;
243 spo = &(orig->surface);
244 spc = &(copy->surface);
245 spc->grad = spo->grad;
246 spc->relief = spo->relief;
247 spc->bevel = spo->bevel;
248 if (spo->primary != NULL)
249 spc->primary = RrColorNew(copy->inst,
250 spo->primary->r,
251 spo->primary->g,
252 spo->primary->b);
253 else spc->primary = NULL;
255 if (spo->secondary != NULL)
256 spc->secondary = RrColorNew(copy->inst,
257 spo->secondary->r,
258 spo->secondary->g,
259 spo->secondary->b);
260 else spc->secondary = NULL;
262 if (spo->border_color != NULL)
263 spc->border_color = RrColorNew(copy->inst,
264 spo->border_color->r,
265 spo->border_color->g,
266 spo->border_color->b);
267 else spc->border_color = NULL;
269 if (spo->interlace_color != NULL)
270 spc->interlace_color = RrColorNew(copy->inst,
271 spo->interlace_color->r,
272 spo->interlace_color->g,
273 spo->interlace_color->b);
274 else spc->interlace_color = NULL;
276 if (spo->bevel_dark != NULL)
277 spc->bevel_dark = RrColorNew(copy->inst,
278 spo->bevel_dark->r,
279 spo->bevel_dark->g,
280 spo->bevel_dark->b);
281 else spc->bevel_dark = NULL;
283 if (spo->bevel_light != NULL)
284 spc->bevel_light = RrColorNew(copy->inst,
285 spo->bevel_light->r,
286 spo->bevel_light->g,
287 spo->bevel_light->b);
288 else spc->bevel_light = NULL;
290 if (spo->split_primary != NULL)
291 spc->split_primary = RrColorNew(copy->inst,
292 spo->split_primary->r,
293 spo->split_primary->g,
294 spo->split_primary->b);
295 else spc->split_primary = NULL;
297 if (spo->split_secondary != NULL)
298 spc->split_secondary = RrColorNew(copy->inst,
299 spo->split_secondary->r,
300 spo->split_secondary->g,
301 spo->split_secondary->b);
302 else spc->split_secondary = NULL;
304 spc->interlaced = spo->interlaced;
305 spc->bevel_light_adjust = spo->bevel_light_adjust;
306 spc->bevel_dark_adjust = spo->bevel_dark_adjust;
307 spc->border = spo->border;
308 spc->parent = NULL;
309 spc->parentx = spc->parenty = 0;
310 spc->pixel_data = NULL;
312 copy->textures = orig->textures;
313 copy->texture = g_memdup(orig->texture,
314 orig->textures * sizeof(RrTexture));
315 copy->pixmap = None;
316 copy->xftdraw = NULL;
317 copy->w = copy->h = 0;
318 return copy;
321 /* now decrements ref counter, and frees only if ref <= 0 */
322 void RrAppearanceFree(RrAppearance *a)
324 if (a) {
325 RrSurface *p;
326 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
327 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
328 if (a->textures)
329 g_free(a->texture);
330 p = &a->surface;
331 RrColorFree(p->primary);
332 RrColorFree(p->secondary);
333 RrColorFree(p->border_color);
334 RrColorFree(p->interlace_color);
335 RrColorFree(p->bevel_dark);
336 RrColorFree(p->bevel_light);
337 RrColorFree(p->split_primary);
338 RrColorFree(p->split_secondary);
339 g_free(p->pixel_data);
340 p->pixel_data = NULL;
341 g_slice_free(RrAppearance, a);
345 static void pixel_data_to_pixmap(RrAppearance *l,
346 gint x, gint y, gint w, gint h)
348 RrPixel32 *in, *scratch;
349 Pixmap out;
350 XImage *im = NULL;
351 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
352 ZPixmap, 0, NULL, w, h, 32, 0);
353 g_assert(im != NULL);
355 in = l->surface.pixel_data;
356 out = l->pixmap;
358 /* this malloc is a complete waste of time on normal 32bpp
359 as reduce_depth just sets im->data = data and returns
361 scratch = g_new(RrPixel32, im->width * im->height);
362 im->data = (gchar*) scratch;
363 RrReduceDepth(l->inst, in, im);
364 XPutImage(RrDisplay(l->inst), out,
365 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
366 im, 0, 0, x, y, w, h);
367 im->data = NULL;
368 XDestroyImage(im);
369 g_free(scratch);
372 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
374 *l = *t = *r = *b = 0;
376 if (a->surface.grad != RR_SURFACE_PARENTREL) {
377 if (a->surface.relief != RR_RELIEF_FLAT) {
378 switch (a->surface.bevel) {
379 case RR_BEVEL_1:
380 *l = *t = *r = *b = 1;
381 break;
382 case RR_BEVEL_2:
383 *l = *t = *r = *b = 2;
384 break;
385 case RR_BEVEL_NUM_TYPES:
386 g_assert_not_reached();
388 } else if (a->surface.border) {
389 *l = *t = *r = *b = 1;
394 void RrMinSize(RrAppearance *a, gint *w, gint *h)
396 *w = RrMinWidth(a);
397 *h = RrMinHeight(a);
400 gint RrMinWidth(RrAppearance *a)
402 gint i;
403 RrSize *m;
404 gint l, t, r, b;
405 gint w = 0;
407 RrMargins(a, &l, &t, &r, &b);
409 for (i = 0; i < a->textures; ++i) {
410 switch (a->texture[i].type) {
411 case RR_TEXTURE_NONE:
412 break;
413 case RR_TEXTURE_MASK:
414 w = MAX(w, a->texture[i].data.mask.mask->width);
415 break;
416 case RR_TEXTURE_TEXT:
417 m = RrFontMeasureString(a->texture[i].data.text.font,
418 a->texture[i].data.text.string,
419 a->texture[i].data.text.shadow_offset_x,
420 a->texture[i].data.text.shadow_offset_y,
421 a->texture[i].data.text.flow,
422 a->texture[i].data.text.maxwidth);
423 w = MAX(w, m->width);
424 g_slice_free(RrSize, m);
425 break;
426 case RR_TEXTURE_RGBA:
427 w += MAX(w, a->texture[i].data.rgba.width);
428 break;
429 case RR_TEXTURE_IMAGE:
430 /* images resize so they don't contribute anything to the min */
431 break;
432 case RR_TEXTURE_LINE_ART:
433 w = MAX(w, MAX(a->texture[i].data.lineart.x1 - l - r,
434 a->texture[i].data.lineart.x2 - l - r));
435 break;
436 case RR_TEXTURE_NUM_TYPES:
437 g_assert_not_reached();
441 w += l + r;
443 if (w < 1) w = 1;
444 return w;
447 gint RrMinHeight(RrAppearance *a)
449 gint i;
450 gint l, t, r, b;
451 RrSize *m;
452 gint h = 0;
454 RrMargins(a, &l, &t, &r, &b);
456 for (i = 0; i < a->textures; ++i) {
457 switch (a->texture[i].type) {
458 case RR_TEXTURE_NONE:
459 break;
460 case RR_TEXTURE_MASK:
461 h = MAX(h, a->texture[i].data.mask.mask->height);
462 break;
463 case RR_TEXTURE_TEXT:
464 if (a->texture[i].data.text.flow) {
465 g_assert(a->texture[i].data.text.string != NULL);
467 m = RrFontMeasureString
468 (a->texture[i].data.text.font,
469 a->texture[i].data.text.string,
470 a->texture[i].data.text.shadow_offset_x,
471 a->texture[i].data.text.shadow_offset_y,
472 a->texture[i].data.text.flow,
473 a->texture[i].data.text.maxwidth);
474 h += MAX(h, m->height);
475 g_slice_free(RrSize, m);
477 else
478 h += MAX(h,
479 RrFontHeight
480 (a->texture[i].data.text.font,
481 a->texture[i].data.text.shadow_offset_y));
482 break;
483 case RR_TEXTURE_RGBA:
484 h += MAX(h, a->texture[i].data.rgba.height);
485 break;
486 case RR_TEXTURE_IMAGE:
487 /* images resize so they don't contribute anything to the min */
488 break;
489 case RR_TEXTURE_LINE_ART:
490 h = MAX(h, MAX(a->texture[i].data.lineart.y1 - t - b,
491 a->texture[i].data.lineart.y2 - t - b));
492 break;
493 case RR_TEXTURE_NUM_TYPES:
494 g_assert_not_reached();
498 h += t + b;
500 if (h < 1) h = 1;
501 return h;
504 static void reverse_bits(gchar *c, gint n)
506 gint i;
507 for (i = 0; i < n; i++, c++)
508 *c = (((*c * 0x0802UL & 0x22110UL) |
509 (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
512 gboolean RrPixmapToRGBA(const RrInstance *inst,
513 Pixmap pmap, Pixmap mask,
514 gint *w, gint *h, RrPixel32 **data)
516 Window xr;
517 gint xx, xy;
518 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
519 XImage *xi, *xm = NULL;
521 if (!XGetGeometry(RrDisplay(inst), pmap,
522 &xr, &xx, &xy, &pw, &ph, &xb, &xd))
523 return FALSE;
525 if (mask) {
526 if (!XGetGeometry(RrDisplay(inst), mask,
527 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
528 return FALSE;
529 if (pw != mw || ph != mh || xd != 1)
530 return FALSE;
533 xi = XGetImage(RrDisplay(inst), pmap,
534 0, 0, pw, ph, 0xffffffff, ZPixmap);
535 if (!xi)
536 return FALSE;
538 if (mask) {
539 xm = XGetImage(RrDisplay(inst), mask,
540 0, 0, mw, mh, 0xffffffff, ZPixmap);
541 if (!xm) {
542 XDestroyImage(xi);
543 return FALSE;
545 if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
546 reverse_bits(xm->data, xm->bytes_per_line * xm->height);
549 if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
550 reverse_bits(xi->data, xi->bytes_per_line * xi->height);
552 *data = g_new(RrPixel32, pw * ph);
553 RrIncreaseDepth(inst, *data, xi);
555 if (mask) {
556 /* apply transparency from the mask */
557 di = 0;
558 for (i = 0, y = 0; y < ph; ++y) {
559 for (x = 0; x < pw; ++x, ++i) {
560 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
561 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
563 di += xm->bytes_per_line;
567 *w = pw;
568 *h = ph;
570 XDestroyImage(xi);
571 if (mask)
572 XDestroyImage(xm);
574 return TRUE;