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 Ben 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.
22 #include <X11/Xutil.h>
38 static void pixel_data_to_pixmap(RrAppearance *l,
39 gint x, gint y, gint w, gint h);
41 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
43 gint i, transferred = 0, sw, sh, partial_w, partial_h;
44 RrPixel32 *source, *dest;
46 RrRect tarea; /* area in which to draw textures */
49 if (w <= 0 || h <= 0) return;
51 if (a->surface.parentx < 0 || a->surface.parenty < 0) {
52 /* ob_debug("Invalid parent co-ordinates\n"); */
55 resized = (a->w != w || a->h != h);
57 oldp = a->pixmap; /* save to free after changing the visible pixmap */
58 a->pixmap = XCreatePixmap(RrDisplay(a->inst),
59 RrRootWindow(a->inst),
60 w, h, RrDepth(a->inst));
62 g_assert(a->pixmap != None);
66 if (a->xftdraw != NULL)
67 XftDrawDestroy(a->xftdraw);
68 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
69 RrVisual(a->inst), RrColormap(a->inst));
70 g_assert(a->xftdraw != NULL);
72 g_free(a->surface.pixel_data);
73 a->surface.pixel_data = g_new(RrPixel32, w * h);
75 if (a->surface.grad == RR_SURFACE_PARENTREL) {
76 g_assert (a->surface.parent);
77 g_assert (a->surface.parent->w);
79 sw = a->surface.parent->w;
80 sh = a->surface.parent->h;
82 if (a->surface.parentx >= sw || a->surface.parenty >= sh) {
86 source = (a->surface.parent->surface.pixel_data +
87 a->surface.parentx + sw * a->surface.parenty);
88 dest = a->surface.pixel_data;
90 if (a->surface.parentx + w > sw) {
91 partial_w = sw - a->surface.parentx;
94 if (a->surface.parenty + h > sh) {
95 partial_h = sh - a->surface.parenty;
98 for (i = 0; i < partial_h; i++, source += sw, dest += w) {
99 memcpy(dest, source, partial_w * sizeof(RrPixel32));
106 RrMargins(a, &l, &t, &r, &b);
107 RECT_SET(tarea, l, t, w - l - r, h - t - b);
110 for (i = 0; i < a->textures; i++) {
111 switch (a->texture[i].type) {
112 case RR_TEXTURE_NONE:
114 case RR_TEXTURE_TEXT:
117 if ((a->surface.grad != RR_SURFACE_SOLID)
118 || (a->surface.interlaced))
119 pixel_data_to_pixmap(a, 0, 0, w, h);
121 if (a->xftdraw == NULL) {
122 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
124 RrColormap(a->inst));
126 RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
128 case RR_TEXTURE_LINE_ART:
131 if ((a->surface.grad != RR_SURFACE_SOLID)
132 || (a->surface.interlaced))
133 pixel_data_to_pixmap(a, 0, 0, w, h);
135 XDrawLine(RrDisplay(a->inst), a->pixmap,
136 RrColorGC(a->texture[i].data.lineart.color),
137 a->texture[i].data.lineart.x1,
138 a->texture[i].data.lineart.y1,
139 a->texture[i].data.lineart.x2,
140 a->texture[i].data.lineart.y2);
142 case RR_TEXTURE_MASK:
145 if ((a->surface.grad != RR_SURFACE_SOLID)
146 || (a->surface.interlaced))
147 pixel_data_to_pixmap(a, 0, 0, w, h);
149 RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
151 case RR_TEXTURE_RGBA:
152 g_assert(!transferred);
153 RrImageDraw(a->surface.pixel_data,
154 &a->texture[i].data.rgba,
163 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced))
164 pixel_data_to_pixmap(a, 0, 0, w, h);
167 XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
168 XClearWindow(RrDisplay(a->inst), win);
169 if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
172 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
176 out = g_new0(RrAppearance, 1);
178 out->textures = numtex;
179 if (numtex) out->texture = g_new0(RrTexture, numtex);
184 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
186 RrSurface *spo, *spc;
187 RrAppearance *copy = g_new(RrAppearance, 1);
190 copy->inst = orig->inst;
192 spo = &(orig->surface);
193 spc = &(copy->surface);
194 spc->grad = spo->grad;
195 spc->relief = spo->relief;
196 spc->bevel = spo->bevel;
197 if (spo->primary != NULL)
198 spc->primary = RrColorNew(copy->inst,
202 else spc->primary = NULL;
204 if (spo->secondary != NULL)
205 spc->secondary = RrColorNew(copy->inst,
209 else spc->secondary = NULL;
211 if (spo->border_color != NULL)
212 spc->border_color = RrColorNew(copy->inst,
213 spo->border_color->r,
214 spo->border_color->g,
215 spo->border_color->b);
216 else spc->border_color = NULL;
218 if (spo->interlace_color != NULL)
219 spc->interlace_color = RrColorNew(copy->inst,
220 spo->interlace_color->r,
221 spo->interlace_color->g,
222 spo->interlace_color->b);
223 else spc->interlace_color = NULL;
225 if (spo->bevel_dark != NULL)
226 spc->bevel_dark = RrColorNew(copy->inst,
230 else spc->bevel_dark = NULL;
232 if (spo->bevel_light != NULL)
233 spc->bevel_light = RrColorNew(copy->inst,
236 spo->bevel_light->b);
237 else spc->bevel_light = NULL;
239 spc->interlaced = spo->interlaced;
240 spc->border = spo->border;
242 spc->parentx = spc->parenty = 0;
243 spc->pixel_data = NULL;
245 copy->textures = orig->textures;
246 copy->texture = g_memdup(orig->texture,
247 orig->textures * sizeof(RrTexture));
248 for (i = 0; i < copy->textures; ++i)
249 if (copy->texture[i].type == RR_TEXTURE_RGBA) {
250 copy->texture[i].data.rgba.cache = NULL;
253 copy->xftdraw = NULL;
254 copy->w = copy->h = 0;
258 void RrAppearanceFree(RrAppearance *a)
264 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
265 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
266 for (i = 0; i < a->textures; ++i)
267 if (a->texture[i].type == RR_TEXTURE_RGBA) {
268 g_free(a->texture[i].data.rgba.cache);
269 a->texture[i].data.rgba.cache = NULL;
274 RrColorFree(p->primary);
275 RrColorFree(p->secondary);
276 RrColorFree(p->border_color);
277 RrColorFree(p->interlace_color);
278 RrColorFree(p->bevel_dark);
279 RrColorFree(p->bevel_light);
280 g_free(p->pixel_data);
281 p->pixel_data = NULL;
287 static void pixel_data_to_pixmap(RrAppearance *l,
288 gint x, gint y, gint w, gint h)
290 RrPixel32 *in, *scratch;
293 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
294 ZPixmap, 0, NULL, w, h, 32, 0);
295 g_assert(im != NULL);
297 in = l->surface.pixel_data;
300 /* this malloc is a complete waste of time on normal 32bpp
301 as reduce_depth just sets im->data = data and returns
303 scratch = g_new(RrPixel32, im->width * im->height);
304 im->data = (gchar*) scratch;
305 RrReduceDepth(l->inst, in, im);
306 XPutImage(RrDisplay(l->inst), out,
307 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
308 im, 0, 0, x, y, w, h);
314 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
316 *l = *t = *r = *b = 0;
318 if (a->surface.grad != RR_SURFACE_PARENTREL) {
319 if (a->surface.relief != RR_RELIEF_FLAT) {
320 switch (a->surface.bevel) {
322 *l = *t = *r = *b = 1;
325 *l = *t = *r = *b = 2;
328 } else if (a->surface.border) {
329 *l = *t = *r = *b = 1;
334 void RrMinsize(RrAppearance *a, gint *w, gint *h)
341 for (i = 0; i < a->textures; ++i) {
342 switch (a->texture[i].type) {
343 case RR_TEXTURE_NONE:
345 case RR_TEXTURE_MASK:
346 *w = MAX(*w, a->texture[i].data.mask.mask->width);
347 *h = MAX(*h, a->texture[i].data.mask.mask->height);
349 case RR_TEXTURE_TEXT:
350 m = RrFontMeasureString(a->texture[i].data.text.font,
351 a->texture[i].data.text.string,
352 a->texture[i].data.text.shadow_offset_x,
353 a->texture[i].data.text.shadow_offset_y);
354 *w = MAX(*w, m->width + 4);
355 m->height = RrFontHeight(a->texture[i].data.text.font,
356 a->texture[i].data.text.shadow_offset_y);
357 *h += MAX(*h, m->height);
360 case RR_TEXTURE_RGBA:
361 *w += MAX(*w, a->texture[i].data.rgba.width);
362 *h += MAX(*h, a->texture[i].data.rgba.height);
364 case RR_TEXTURE_LINE_ART:
365 *w += MAX(*w, MAX(a->texture[i].data.lineart.x1,
366 a->texture[i].data.lineart.x2));
367 *h += MAX(*h, MAX(a->texture[i].data.lineart.y1,
368 a->texture[i].data.lineart.y2));
373 RrMargins(a, &l, &t, &r, &b);
382 static void reverse_bits(gchar *c, gint n)
385 for (i = 0; i < n; i++, c++)
386 *c = (((*c * 0x0802UL & 0x22110UL) |
387 (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
390 gboolean RrPixmapToRGBA(const RrInstance *inst,
391 Pixmap pmap, Pixmap mask,
392 gint *w, gint *h, RrPixel32 **data)
396 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
397 XImage *xi, *xm = NULL;
399 if (!XGetGeometry(RrDisplay(inst), pmap,
400 &xr, &xx, &xy, &pw, &ph, &xb, &xd))
404 if (!XGetGeometry(RrDisplay(inst), mask,
405 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
407 if (pw != mw || ph != mh || xd != 1)
411 xi = XGetImage(RrDisplay(inst), pmap,
412 0, 0, pw, ph, 0xffffffff, ZPixmap);
417 xm = XGetImage(RrDisplay(inst), mask,
418 0, 0, mw, mh, 0xffffffff, ZPixmap);
423 if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
424 reverse_bits(xm->data, xm->bytes_per_line * xm->height);
427 if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
428 reverse_bits(xi->data, xi->bytes_per_line * xi->height);
430 *data = g_new(RrPixel32, pw * ph);
431 RrIncreaseDepth(inst, *data, xi);
434 /* apply transparency from the mask */
436 for (i = 0, y = 0; y < ph; ++y) {
437 for (x = 0; x < pw; ++x, ++i) {
438 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
439 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
441 di += xm->bytes_per_line;