]> Dogcows Code - chaz/openbox/blob - render/render.c
make the text area in alttab list mode the same hight as the icon _with hilite_
[chaz/openbox] / render / render.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
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
7
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.
12
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.
17
18 See the COPYING file for a copy of the GNU General Public License.
19 */
20
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"
28
29 #include <glib.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include <X11/Xft/Xft.h>
33
34 #ifdef HAVE_STDLIB_H
35 # include <stdlib.h>
36 #endif
37
38 static void pixel_data_to_pixmap(RrAppearance *l,
39 gint x, gint y, gint w, gint h);
40
41 Pixmap RrPaintPixmap(RrAppearance *a, gint w, gint h)
42 {
43 gint i, transferred = 0, force_transfer = 0;
44 Pixmap oldp = None;
45 RrRect tarea; /* area in which to draw textures */
46 gboolean resized;
47
48 if (w <= 0 || h <= 0) return None;
49
50 if (a->surface.parentx < 0 || a->surface.parenty < 0) {
51 /* ob_debug("Invalid parent co-ordinates\n"); */
52 return None;
53 }
54
55 if (a->surface.grad == RR_SURFACE_PARENTREL &&
56 (a->surface.parentx >= a->surface.parent->w ||
57 a->surface.parenty >= a->surface.parent->h))
58 {
59 return None;
60 }
61
62 resized = (a->w != w || a->h != h);
63
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));
68
69 g_assert(a->pixmap != None);
70 a->w = w;
71 a->h = h;
72
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);
78
79 if (resized) {
80 g_free(a->surface.pixel_data);
81 a->surface.pixel_data = g_new(RrPixel32, w * h);
82 }
83
84 RrRender(a, w, h);
85
86 {
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);
90 }
91
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);
102 }
103 if (a->xftdraw == NULL) {
104 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
105 RrVisual(a->inst),
106 RrColormap(a->inst));
107 }
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);
116 }
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);
130 }
131 RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
132 break;
133 case RR_TEXTURE_RGBA:
134 g_assert(!transferred);
135 {
136 RrRect narea = tarea;
137 RrTextureRGBA *rgb = &a->texture[i].data.rgba;
138 if (rgb->twidth)
139 narea.width = MIN(tarea.width, rgb->twidth);
140 if (rgb->theight)
141 narea.height = MIN(tarea.height, rgb->theight);
142 narea.x += rgb->tx;
143 narea.y += rgb->ty;
144 RrImageDraw(a->surface.pixel_data,
145 &a->texture[i].data.rgba,
146 a->w, a->h,
147 &narea);
148 }
149 force_transfer = 1;
150 break;
151 }
152 }
153
154 if (!transferred) {
155 transferred = 1;
156 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced) ||
157 force_transfer)
158 {
159 pixel_data_to_pixmap(a, 0, 0, w, h);
160 }
161 }
162
163 return oldp;
164 }
165
166 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
167 {
168 Pixmap oldp;
169
170 oldp = RrPaintPixmap(a, w, h);
171 XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
172 XClearWindow(RrDisplay(a->inst), win);
173 /* free this after changing the visible pixmap */
174 if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
175 }
176
177 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
178 {
179 RrAppearance *out;
180
181 out = g_new0(RrAppearance, 1);
182 out->inst = inst;
183 out->textures = numtex;
184 out->surface.bevel_light_adjust = 128;
185 out->surface.bevel_dark_adjust = 64;
186 if (numtex) out->texture = g_new0(RrTexture, numtex);
187
188 return out;
189 }
190
191 void RrAppearanceRemoveTextures(RrAppearance *a)
192 {
193 g_free(a->texture);
194 a->textures = 0;
195 }
196
197 void RrAppearanceAddTextures(RrAppearance *a, gint numtex)
198 {
199 g_assert(a->textures == 0);
200
201 a->textures = numtex;
202 if (numtex) a->texture = g_new0(RrTexture, numtex);
203 }
204
205 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
206 {
207 RrSurface *spo, *spc;
208 RrAppearance *copy = g_new(RrAppearance, 1);
209 gint i;
210
211 copy->inst = orig->inst;
212
213 spo = &(orig->surface);
214 spc = &(copy->surface);
215 spc->grad = spo->grad;
216 spc->relief = spo->relief;
217 spc->bevel = spo->bevel;
218 if (spo->primary != NULL)
219 spc->primary = RrColorNew(copy->inst,
220 spo->primary->r,
221 spo->primary->g,
222 spo->primary->b);
223 else spc->primary = NULL;
224
225 if (spo->secondary != NULL)
226 spc->secondary = RrColorNew(copy->inst,
227 spo->secondary->r,
228 spo->secondary->g,
229 spo->secondary->b);
230 else spc->secondary = NULL;
231
232 if (spo->border_color != NULL)
233 spc->border_color = RrColorNew(copy->inst,
234 spo->border_color->r,
235 spo->border_color->g,
236 spo->border_color->b);
237 else spc->border_color = NULL;
238
239 if (spo->interlace_color != NULL)
240 spc->interlace_color = RrColorNew(copy->inst,
241 spo->interlace_color->r,
242 spo->interlace_color->g,
243 spo->interlace_color->b);
244 else spc->interlace_color = NULL;
245
246 if (spo->bevel_dark != NULL)
247 spc->bevel_dark = RrColorNew(copy->inst,
248 spo->bevel_dark->r,
249 spo->bevel_dark->g,
250 spo->bevel_dark->b);
251 else spc->bevel_dark = NULL;
252
253 if (spo->bevel_light != NULL)
254 spc->bevel_light = RrColorNew(copy->inst,
255 spo->bevel_light->r,
256 spo->bevel_light->g,
257 spo->bevel_light->b);
258 else spc->bevel_light = NULL;
259
260 if (spo->split_primary != NULL)
261 spc->split_primary = RrColorNew(copy->inst,
262 spo->split_primary->r,
263 spo->split_primary->g,
264 spo->split_primary->b);
265 else spc->split_primary = NULL;
266
267 if (spo->split_secondary != NULL)
268 spc->split_secondary = RrColorNew(copy->inst,
269 spo->split_secondary->r,
270 spo->split_secondary->g,
271 spo->split_secondary->b);
272 else spc->split_secondary = NULL;
273
274 spc->interlaced = spo->interlaced;
275 spc->bevel_light_adjust = spo->bevel_light_adjust;
276 spc->bevel_dark_adjust = spo->bevel_dark_adjust;
277 spc->border = spo->border;
278 spc->parent = NULL;
279 spc->parentx = spc->parenty = 0;
280 spc->pixel_data = NULL;
281
282 copy->textures = orig->textures;
283 copy->texture = g_memdup(orig->texture,
284 orig->textures * sizeof(RrTexture));
285 for (i = 0; i < copy->textures; ++i)
286 if (copy->texture[i].type == RR_TEXTURE_RGBA) {
287 copy->texture[i].data.rgba.cache = NULL;
288 }
289 copy->pixmap = None;
290 copy->xftdraw = NULL;
291 copy->w = copy->h = 0;
292 return copy;
293 }
294
295 void RrAppearanceFree(RrAppearance *a)
296 {
297 gint i;
298
299 if (a) {
300 RrSurface *p;
301 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
302 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
303 for (i = 0; i < a->textures; ++i)
304 if (a->texture[i].type == RR_TEXTURE_RGBA) {
305 g_free(a->texture[i].data.rgba.cache);
306 a->texture[i].data.rgba.cache = NULL;
307 }
308 if (a->textures)
309 g_free(a->texture);
310 p = &a->surface;
311 RrColorFree(p->primary);
312 RrColorFree(p->secondary);
313 RrColorFree(p->border_color);
314 RrColorFree(p->interlace_color);
315 RrColorFree(p->bevel_dark);
316 RrColorFree(p->bevel_light);
317 RrColorFree(p->split_primary);
318 RrColorFree(p->split_secondary);
319 g_free(p->pixel_data);
320 p->pixel_data = NULL;
321 g_free(a);
322 }
323 }
324
325
326 static void pixel_data_to_pixmap(RrAppearance *l,
327 gint x, gint y, gint w, gint h)
328 {
329 RrPixel32 *in, *scratch;
330 Pixmap out;
331 XImage *im = NULL;
332 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
333 ZPixmap, 0, NULL, w, h, 32, 0);
334 g_assert(im != NULL);
335
336 in = l->surface.pixel_data;
337 out = l->pixmap;
338
339 /* this malloc is a complete waste of time on normal 32bpp
340 as reduce_depth just sets im->data = data and returns
341 */
342 scratch = g_new(RrPixel32, im->width * im->height);
343 im->data = (gchar*) scratch;
344 RrReduceDepth(l->inst, in, im);
345 XPutImage(RrDisplay(l->inst), out,
346 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
347 im, 0, 0, x, y, w, h);
348 im->data = NULL;
349 XDestroyImage(im);
350 g_free(scratch);
351 }
352
353 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
354 {
355 *l = *t = *r = *b = 0;
356
357 if (a->surface.grad != RR_SURFACE_PARENTREL) {
358 if (a->surface.relief != RR_RELIEF_FLAT) {
359 switch (a->surface.bevel) {
360 case RR_BEVEL_1:
361 *l = *t = *r = *b = 1;
362 break;
363 case RR_BEVEL_2:
364 *l = *t = *r = *b = 2;
365 break;
366 }
367 } else if (a->surface.border) {
368 *l = *t = *r = *b = 1;
369 }
370 }
371 }
372
373 void RrMinSize(RrAppearance *a, gint *w, gint *h)
374 {
375 *w = RrMinWidth(a);
376 *h = RrMinHeight(a);
377 }
378
379 gint RrMinWidth(RrAppearance *a)
380 {
381 gint i;
382 RrSize *m;
383 gint l, t, r, b;
384 gint w = 0;
385
386 for (i = 0; i < a->textures; ++i) {
387 switch (a->texture[i].type) {
388 case RR_TEXTURE_NONE:
389 break;
390 case RR_TEXTURE_MASK:
391 w = MAX(w, a->texture[i].data.mask.mask->width);
392 break;
393 case RR_TEXTURE_TEXT:
394 m = RrFontMeasureString(a->texture[i].data.text.font,
395 a->texture[i].data.text.string,
396 a->texture[i].data.text.shadow_offset_x,
397 a->texture[i].data.text.shadow_offset_y,
398 a->texture[i].data.text.flow,
399 a->texture[i].data.text.maxwidth);
400 w = MAX(w, m->width);
401 g_free(m);
402 break;
403 case RR_TEXTURE_RGBA:
404 w += MAX(w, a->texture[i].data.rgba.width);
405 break;
406 case RR_TEXTURE_LINE_ART:
407 w += MAX(w, MAX(a->texture[i].data.lineart.x1,
408 a->texture[i].data.lineart.x2));
409 break;
410 }
411 }
412
413 RrMargins(a, &l, &t, &r, &b);
414
415 w += l + r;
416
417 if (w < 1) w = 1;
418 return w;
419 }
420
421 gint RrMinHeight(RrAppearance *a)
422 {
423 gint i;
424 gint l, t, r, b;
425 RrSize *m;
426 gint h = 0;
427
428 for (i = 0; i < a->textures; ++i) {
429 switch (a->texture[i].type) {
430 case RR_TEXTURE_NONE:
431 break;
432 case RR_TEXTURE_MASK:
433 h = MAX(h, a->texture[i].data.mask.mask->height);
434 break;
435 case RR_TEXTURE_TEXT:
436 if (a->texture[i].data.text.flow) {
437 g_assert(a->texture[i].data.text.string != NULL);
438
439 m = RrFontMeasureString
440 (a->texture[i].data.text.font,
441 a->texture[i].data.text.string,
442 a->texture[i].data.text.shadow_offset_x,
443 a->texture[i].data.text.shadow_offset_y,
444 a->texture[i].data.text.flow,
445 a->texture[i].data.text.maxwidth);
446 h += MAX(h, m->height);
447 g_free(m);
448 }
449 else
450 h += MAX(h,
451 RrFontHeight
452 (a->texture[i].data.text.font,
453 a->texture[i].data.text.shadow_offset_y));
454 break;
455 case RR_TEXTURE_RGBA:
456 h += MAX(h, a->texture[i].data.rgba.height);
457 break;
458 case RR_TEXTURE_LINE_ART:
459 h += MAX(h, MAX(a->texture[i].data.lineart.y1,
460 a->texture[i].data.lineart.y2));
461 break;
462 }
463 }
464
465 RrMargins(a, &l, &t, &r, &b);
466
467 h += t + b;
468
469 if (h < 1) h = 1;
470 return h;
471 }
472
473 static void reverse_bits(gchar *c, gint n)
474 {
475 gint i;
476 for (i = 0; i < n; i++, c++)
477 *c = (((*c * 0x0802UL & 0x22110UL) |
478 (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
479 }
480
481 gboolean RrPixmapToRGBA(const RrInstance *inst,
482 Pixmap pmap, Pixmap mask,
483 gint *w, gint *h, RrPixel32 **data)
484 {
485 Window xr;
486 gint xx, xy;
487 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
488 XImage *xi, *xm = NULL;
489
490 if (!XGetGeometry(RrDisplay(inst), pmap,
491 &xr, &xx, &xy, &pw, &ph, &xb, &xd))
492 return FALSE;
493
494 if (mask) {
495 if (!XGetGeometry(RrDisplay(inst), mask,
496 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
497 return FALSE;
498 if (pw != mw || ph != mh || xd != 1)
499 return FALSE;
500 }
501
502 xi = XGetImage(RrDisplay(inst), pmap,
503 0, 0, pw, ph, 0xffffffff, ZPixmap);
504 if (!xi)
505 return FALSE;
506
507 if (mask) {
508 xm = XGetImage(RrDisplay(inst), mask,
509 0, 0, mw, mh, 0xffffffff, ZPixmap);
510 if (!xm) {
511 XDestroyImage(xi);
512 return FALSE;
513 }
514 if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
515 reverse_bits(xm->data, xm->bytes_per_line * xm->height);
516 }
517
518 if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
519 reverse_bits(xi->data, xi->bytes_per_line * xi->height);
520
521 *data = g_new(RrPixel32, pw * ph);
522 RrIncreaseDepth(inst, *data, xi);
523
524 if (mask) {
525 /* apply transparency from the mask */
526 di = 0;
527 for (i = 0, y = 0; y < ph; ++y) {
528 for (x = 0; x < pw; ++x, ++i) {
529 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
530 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
531 }
532 di += xm->bytes_per_line;
533 }
534 }
535
536 *w = pw;
537 *h = ph;
538
539 XDestroyImage(xi);
540 if (mask)
541 XDestroyImage(xm);
542
543 return TRUE;
544 }
This page took 0.061189 seconds and 4 git commands to generate.