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 Pixmap RrPaintPixmap(RrAppearance *a, 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 None;
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);
170 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
174 oldp = RrPaintPixmap(a, w, h);
175 XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
176 XClearWindow(RrDisplay(a->inst), win);
177 /* free this after changing the visible pixmap */
178 if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
181 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
185 out = g_new0(RrAppearance, 1);
187 out->textures = numtex;
188 if (numtex) out->texture = g_new0(RrTexture, numtex);
193 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
195 RrSurface *spo, *spc;
196 RrAppearance *copy = g_new(RrAppearance, 1);
199 copy->inst = orig->inst;
201 spo = &(orig->surface);
202 spc = &(copy->surface);
203 spc->grad = spo->grad;
204 spc->relief = spo->relief;
205 spc->bevel = spo->bevel;
206 if (spo->primary != NULL)
207 spc->primary = RrColorNew(copy->inst,
211 else spc->primary = NULL;
213 if (spo->secondary != NULL)
214 spc->secondary = RrColorNew(copy->inst,
218 else spc->secondary = NULL;
220 if (spo->border_color != NULL)
221 spc->border_color = RrColorNew(copy->inst,
222 spo->border_color->r,
223 spo->border_color->g,
224 spo->border_color->b);
225 else spc->border_color = NULL;
227 if (spo->interlace_color != NULL)
228 spc->interlace_color = RrColorNew(copy->inst,
229 spo->interlace_color->r,
230 spo->interlace_color->g,
231 spo->interlace_color->b);
232 else spc->interlace_color = NULL;
234 if (spo->bevel_dark != NULL)
235 spc->bevel_dark = RrColorNew(copy->inst,
239 else spc->bevel_dark = NULL;
241 if (spo->bevel_light != NULL)
242 spc->bevel_light = RrColorNew(copy->inst,
245 spo->bevel_light->b);
246 else spc->bevel_light = NULL;
248 spc->interlaced = spo->interlaced;
249 spc->border = spo->border;
251 spc->parentx = spc->parenty = 0;
252 spc->pixel_data = NULL;
254 copy->textures = orig->textures;
255 copy->texture = g_memdup(orig->texture,
256 orig->textures * sizeof(RrTexture));
257 for (i = 0; i < copy->textures; ++i)
258 if (copy->texture[i].type == RR_TEXTURE_RGBA) {
259 copy->texture[i].data.rgba.cache = NULL;
262 copy->xftdraw = NULL;
263 copy->w = copy->h = 0;
267 void RrAppearanceFree(RrAppearance *a)
273 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
274 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
275 for (i = 0; i < a->textures; ++i)
276 if (a->texture[i].type == RR_TEXTURE_RGBA) {
277 g_free(a->texture[i].data.rgba.cache);
278 a->texture[i].data.rgba.cache = NULL;
283 RrColorFree(p->primary);
284 RrColorFree(p->secondary);
285 RrColorFree(p->border_color);
286 RrColorFree(p->interlace_color);
287 RrColorFree(p->bevel_dark);
288 RrColorFree(p->bevel_light);
289 g_free(p->pixel_data);
290 p->pixel_data = NULL;
296 static void pixel_data_to_pixmap(RrAppearance *l,
297 gint x, gint y, gint w, gint h)
299 RrPixel32 *in, *scratch;
302 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
303 ZPixmap, 0, NULL, w, h, 32, 0);
304 g_assert(im != NULL);
306 in = l->surface.pixel_data;
309 /* this malloc is a complete waste of time on normal 32bpp
310 as reduce_depth just sets im->data = data and returns
312 scratch = g_new(RrPixel32, im->width * im->height);
313 im->data = (gchar*) scratch;
314 RrReduceDepth(l->inst, in, im);
315 XPutImage(RrDisplay(l->inst), out,
316 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
317 im, 0, 0, x, y, w, h);
323 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
325 *l = *t = *r = *b = 0;
327 if (a->surface.grad != RR_SURFACE_PARENTREL) {
328 if (a->surface.relief != RR_RELIEF_FLAT) {
329 switch (a->surface.bevel) {
331 *l = *t = *r = *b = 1;
334 *l = *t = *r = *b = 2;
337 } else if (a->surface.border) {
338 *l = *t = *r = *b = 1;
343 void RrMinsize(RrAppearance *a, gint *w, gint *h)
350 for (i = 0; i < a->textures; ++i) {
351 switch (a->texture[i].type) {
352 case RR_TEXTURE_NONE:
354 case RR_TEXTURE_MASK:
355 *w = MAX(*w, a->texture[i].data.mask.mask->width);
356 *h = MAX(*h, a->texture[i].data.mask.mask->height);
358 case RR_TEXTURE_TEXT:
359 m = RrFontMeasureString(a->texture[i].data.text.font,
360 a->texture[i].data.text.string,
361 a->texture[i].data.text.shadow_offset_x,
362 a->texture[i].data.text.shadow_offset_y);
363 *w = MAX(*w, m->width + 4);
364 m->height = RrFontHeight(a->texture[i].data.text.font,
365 a->texture[i].data.text.shadow_offset_y);
366 *h += MAX(*h, m->height);
369 case RR_TEXTURE_RGBA:
370 *w += MAX(*w, a->texture[i].data.rgba.width);
371 *h += MAX(*h, a->texture[i].data.rgba.height);
373 case RR_TEXTURE_LINE_ART:
374 *w += MAX(*w, MAX(a->texture[i].data.lineart.x1,
375 a->texture[i].data.lineart.x2));
376 *h += MAX(*h, MAX(a->texture[i].data.lineart.y1,
377 a->texture[i].data.lineart.y2));
382 RrMargins(a, &l, &t, &r, &b);
391 static void reverse_bits(gchar *c, gint n)
394 for (i = 0; i < n; i++, c++)
395 *c = (((*c * 0x0802UL & 0x22110UL) |
396 (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
399 gboolean RrPixmapToRGBA(const RrInstance *inst,
400 Pixmap pmap, Pixmap mask,
401 gint *w, gint *h, RrPixel32 **data)
405 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
406 XImage *xi, *xm = NULL;
408 if (!XGetGeometry(RrDisplay(inst), pmap,
409 &xr, &xx, &xy, &pw, &ph, &xb, &xd))
413 if (!XGetGeometry(RrDisplay(inst), mask,
414 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
416 if (pw != mw || ph != mh || xd != 1)
420 xi = XGetImage(RrDisplay(inst), pmap,
421 0, 0, pw, ph, 0xffffffff, ZPixmap);
426 xm = XGetImage(RrDisplay(inst), mask,
427 0, 0, mw, mh, 0xffffffff, ZPixmap);
432 if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
433 reverse_bits(xm->data, xm->bytes_per_line * xm->height);
436 if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
437 reverse_bits(xi->data, xi->bytes_per_line * xi->height);
439 *data = g_new(RrPixel32, pw * ph);
440 RrIncreaseDepth(inst, *data, xi);
443 /* apply transparency from the mask */
445 for (i = 0, y = 0; y < ph; ++y) {
446 for (x = 0; x < pw; ++x, ++i) {
447 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
448 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
450 di += xm->bytes_per_line;