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.
31 #include <X11/Xutil.h>
32 #include <X11/Xft/Xft.h>
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;
45 RrRect tarea; /* area in which to draw textures */
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"); */
55 if (a->surface.grad == RR_SURFACE_PARENTREL &&
56 (a->surface.parentx >= a->surface.parent->w ||
57 a->surface.parenty >= a->surface.parent->h))
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);
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);
80 g_free(a->surface.pixel_data);
81 a->surface.pixel_data = g_new(RrPixel32, w * h);
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) {
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,
106 RrColormap(a->inst));
108 RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
110 case RR_TEXTURE_LINE_ART:
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);
124 case RR_TEXTURE_MASK:
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);
133 case RR_TEXTURE_RGBA:
134 g_assert(!transferred);
135 RrImageDraw(a->surface.pixel_data,
136 &a->texture[i].data.rgba,
146 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced) ||
149 pixel_data_to_pixmap(a, 0, 0, w, h);
156 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
160 oldp = RrPaintPixmap(a, w, h);
161 XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
162 XClearWindow(RrDisplay(a->inst), win);
163 /* free this after changing the visible pixmap */
164 if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
167 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
171 out = g_new0(RrAppearance, 1);
173 out->textures = numtex;
174 out->surface.bevel_light_adjust = 128;
175 out->surface.bevel_dark_adjust = 64;
176 if (numtex) out->texture = g_new0(RrTexture, numtex);
181 void RrAppearanceRemoveTextures(RrAppearance *a)
187 void RrAppearanceAddTextures(RrAppearance *a, gint numtex)
189 g_assert(a->textures == 0);
191 a->textures = numtex;
192 if (numtex) a->texture = g_new0(RrTexture, numtex);
195 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
197 RrSurface *spo, *spc;
198 RrAppearance *copy = g_new(RrAppearance, 1);
201 copy->inst = orig->inst;
203 spo = &(orig->surface);
204 spc = &(copy->surface);
205 spc->grad = spo->grad;
206 spc->relief = spo->relief;
207 spc->bevel = spo->bevel;
208 if (spo->primary != NULL)
209 spc->primary = RrColorNew(copy->inst,
213 else spc->primary = NULL;
215 if (spo->secondary != NULL)
216 spc->secondary = RrColorNew(copy->inst,
220 else spc->secondary = NULL;
222 if (spo->border_color != NULL)
223 spc->border_color = RrColorNew(copy->inst,
224 spo->border_color->r,
225 spo->border_color->g,
226 spo->border_color->b);
227 else spc->border_color = NULL;
229 if (spo->interlace_color != NULL)
230 spc->interlace_color = RrColorNew(copy->inst,
231 spo->interlace_color->r,
232 spo->interlace_color->g,
233 spo->interlace_color->b);
234 else spc->interlace_color = NULL;
236 if (spo->bevel_dark != NULL)
237 spc->bevel_dark = RrColorNew(copy->inst,
241 else spc->bevel_dark = NULL;
243 if (spo->bevel_light != NULL)
244 spc->bevel_light = RrColorNew(copy->inst,
247 spo->bevel_light->b);
248 else spc->bevel_light = NULL;
250 if (spo->split_primary != NULL)
251 spc->split_primary = RrColorNew(copy->inst,
252 spo->split_primary->r,
253 spo->split_primary->g,
254 spo->split_primary->b);
255 else spc->split_primary = NULL;
257 if (spo->split_secondary != NULL)
258 spc->split_secondary = RrColorNew(copy->inst,
259 spo->split_secondary->r,
260 spo->split_secondary->g,
261 spo->split_secondary->b);
262 else spc->split_secondary = NULL;
264 spc->interlaced = spo->interlaced;
265 spc->bevel_light_adjust = spo->bevel_light_adjust;
266 spc->bevel_dark_adjust = spo->bevel_dark_adjust;
267 spc->border = spo->border;
269 spc->parentx = spc->parenty = 0;
270 spc->pixel_data = NULL;
272 copy->textures = orig->textures;
273 copy->texture = g_memdup(orig->texture,
274 orig->textures * sizeof(RrTexture));
275 for (i = 0; i < copy->textures; ++i)
276 if (copy->texture[i].type == RR_TEXTURE_RGBA) {
277 copy->texture[i].data.rgba.cache = NULL;
280 copy->xftdraw = NULL;
281 copy->w = copy->h = 0;
285 void RrAppearanceFree(RrAppearance *a)
291 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
292 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
293 for (i = 0; i < a->textures; ++i)
294 if (a->texture[i].type == RR_TEXTURE_RGBA) {
295 g_free(a->texture[i].data.rgba.cache);
296 a->texture[i].data.rgba.cache = NULL;
301 RrColorFree(p->primary);
302 RrColorFree(p->secondary);
303 RrColorFree(p->border_color);
304 RrColorFree(p->interlace_color);
305 RrColorFree(p->bevel_dark);
306 RrColorFree(p->bevel_light);
307 RrColorFree(p->split_primary);
308 RrColorFree(p->split_secondary);
309 g_free(p->pixel_data);
310 p->pixel_data = NULL;
316 static void pixel_data_to_pixmap(RrAppearance *l,
317 gint x, gint y, gint w, gint h)
319 RrPixel32 *in, *scratch;
322 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
323 ZPixmap, 0, NULL, w, h, 32, 0);
324 g_assert(im != NULL);
326 in = l->surface.pixel_data;
329 /* this malloc is a complete waste of time on normal 32bpp
330 as reduce_depth just sets im->data = data and returns
332 scratch = g_new(RrPixel32, im->width * im->height);
333 im->data = (gchar*) scratch;
334 RrReduceDepth(l->inst, in, im);
335 XPutImage(RrDisplay(l->inst), out,
336 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
337 im, 0, 0, x, y, w, h);
343 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
345 *l = *t = *r = *b = 0;
347 if (a->surface.grad != RR_SURFACE_PARENTREL) {
348 if (a->surface.relief != RR_RELIEF_FLAT) {
349 switch (a->surface.bevel) {
351 *l = *t = *r = *b = 1;
354 *l = *t = *r = *b = 2;
357 } else if (a->surface.border) {
358 *l = *t = *r = *b = 1;
363 void RrMinSize(RrAppearance *a, gint *w, gint *h)
369 gint RrMinWidth(RrAppearance *a)
376 for (i = 0; i < a->textures; ++i) {
377 switch (a->texture[i].type) {
378 case RR_TEXTURE_NONE:
380 case RR_TEXTURE_MASK:
381 w = MAX(w, a->texture[i].data.mask.mask->width);
383 case RR_TEXTURE_TEXT:
384 m = RrFontMeasureString(a->texture[i].data.text.font,
385 a->texture[i].data.text.string,
386 a->texture[i].data.text.shadow_offset_x,
387 a->texture[i].data.text.shadow_offset_y,
388 a->texture[i].data.text.flow,
389 a->texture[i].data.text.maxwidth);
390 w = MAX(w, m->width);
393 case RR_TEXTURE_RGBA:
394 w += MAX(w, a->texture[i].data.rgba.width);
396 case RR_TEXTURE_LINE_ART:
397 w += MAX(w, MAX(a->texture[i].data.lineart.x1,
398 a->texture[i].data.lineart.x2));
403 RrMargins(a, &l, &t, &r, &b);
411 gint RrMinHeight(RrAppearance *a)
418 for (i = 0; i < a->textures; ++i) {
419 switch (a->texture[i].type) {
420 case RR_TEXTURE_NONE:
422 case RR_TEXTURE_MASK:
423 h = MAX(h, a->texture[i].data.mask.mask->height);
425 case RR_TEXTURE_TEXT:
426 if (a->texture[i].data.text.flow) {
427 g_assert(a->texture[i].data.text.string != NULL);
429 m = RrFontMeasureString
430 (a->texture[i].data.text.font,
431 a->texture[i].data.text.string,
432 a->texture[i].data.text.shadow_offset_x,
433 a->texture[i].data.text.shadow_offset_y,
434 a->texture[i].data.text.flow,
435 a->texture[i].data.text.maxwidth);
436 h += MAX(h, m->height);
442 (a->texture[i].data.text.font,
443 a->texture[i].data.text.shadow_offset_y));
445 case RR_TEXTURE_RGBA:
446 h += MAX(h, a->texture[i].data.rgba.height);
448 case RR_TEXTURE_LINE_ART:
449 h += MAX(h, MAX(a->texture[i].data.lineart.y1,
450 a->texture[i].data.lineart.y2));
455 RrMargins(a, &l, &t, &r, &b);
463 static void reverse_bits(gchar *c, gint n)
466 for (i = 0; i < n; i++, c++)
467 *c = (((*c * 0x0802UL & 0x22110UL) |
468 (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
471 gboolean RrPixmapToRGBA(const RrInstance *inst,
472 Pixmap pmap, Pixmap mask,
473 gint *w, gint *h, RrPixel32 **data)
477 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
478 XImage *xi, *xm = NULL;
480 if (!XGetGeometry(RrDisplay(inst), pmap,
481 &xr, &xx, &xy, &pw, &ph, &xb, &xd))
485 if (!XGetGeometry(RrDisplay(inst), mask,
486 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
488 if (pw != mw || ph != mh || xd != 1)
492 xi = XGetImage(RrDisplay(inst), pmap,
493 0, 0, pw, ph, 0xffffffff, ZPixmap);
498 xm = XGetImage(RrDisplay(inst), mask,
499 0, 0, mw, mh, 0xffffffff, ZPixmap);
504 if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
505 reverse_bits(xm->data, xm->bytes_per_line * xm->height);
508 if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
509 reverse_bits(xi->data, xi->bytes_per_line * xi->height);
511 *data = g_new(RrPixel32, pw * ph);
512 RrIncreaseDepth(inst, *data, xi);
515 /* apply transparency from the mask */
517 for (i = 0, y = 0; y < ph; ++y) {
518 for (x = 0; x < pw; ++x, ++i) {
519 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
520 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
522 di += xm->bytes_per_line;