]> Dogcows Code - chaz/openbox/blob - render/render.c
you can create dialog windows called "prompts" which have a message and some buttons...
[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 RrImageDraw(a->surface.pixel_data,
136 &a->texture[i].data.rgba,
137 a->w, a->h,
138 &tarea);
139 force_transfer = 1;
140 break;
141 }
142 }
143
144 if (!transferred) {
145 transferred = 1;
146 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced) ||
147 force_transfer)
148 {
149 pixel_data_to_pixmap(a, 0, 0, w, h);
150 }
151 }
152
153 return oldp;
154 }
155
156 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
157 {
158 Pixmap oldp;
159
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);
165 }
166
167 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
168 {
169 RrAppearance *out;
170
171 out = g_new0(RrAppearance, 1);
172 out->inst = inst;
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);
177
178 return out;
179 }
180
181 void RrAppearanceRemoveTextures(RrAppearance *a)
182 {
183 gint i;
184
185 g_free(a->texture);
186 a->textures = 0;
187 }
188
189 void RrAppearanceAddTextures(RrAppearance *a, gint numtex)
190 {
191 g_assert(a->textures == 0);
192
193 a->textures = numtex;
194 if (numtex) a->texture = g_new0(RrTexture, numtex);
195 }
196
197 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
198 {
199 RrSurface *spo, *spc;
200 RrAppearance *copy = g_new(RrAppearance, 1);
201 gint i;
202
203 copy->inst = orig->inst;
204
205 spo = &(orig->surface);
206 spc = &(copy->surface);
207 spc->grad = spo->grad;
208 spc->relief = spo->relief;
209 spc->bevel = spo->bevel;
210 if (spo->primary != NULL)
211 spc->primary = RrColorNew(copy->inst,
212 spo->primary->r,
213 spo->primary->g,
214 spo->primary->b);
215 else spc->primary = NULL;
216
217 if (spo->secondary != NULL)
218 spc->secondary = RrColorNew(copy->inst,
219 spo->secondary->r,
220 spo->secondary->g,
221 spo->secondary->b);
222 else spc->secondary = NULL;
223
224 if (spo->border_color != NULL)
225 spc->border_color = RrColorNew(copy->inst,
226 spo->border_color->r,
227 spo->border_color->g,
228 spo->border_color->b);
229 else spc->border_color = NULL;
230
231 if (spo->interlace_color != NULL)
232 spc->interlace_color = RrColorNew(copy->inst,
233 spo->interlace_color->r,
234 spo->interlace_color->g,
235 spo->interlace_color->b);
236 else spc->interlace_color = NULL;
237
238 if (spo->bevel_dark != NULL)
239 spc->bevel_dark = RrColorNew(copy->inst,
240 spo->bevel_dark->r,
241 spo->bevel_dark->g,
242 spo->bevel_dark->b);
243 else spc->bevel_dark = NULL;
244
245 if (spo->bevel_light != NULL)
246 spc->bevel_light = RrColorNew(copy->inst,
247 spo->bevel_light->r,
248 spo->bevel_light->g,
249 spo->bevel_light->b);
250 else spc->bevel_light = NULL;
251
252 if (spo->split_primary != NULL)
253 spc->split_primary = RrColorNew(copy->inst,
254 spo->split_primary->r,
255 spo->split_primary->g,
256 spo->split_primary->b);
257 else spc->split_primary = NULL;
258
259 if (spo->split_secondary != NULL)
260 spc->split_secondary = RrColorNew(copy->inst,
261 spo->split_secondary->r,
262 spo->split_secondary->g,
263 spo->split_secondary->b);
264 else spc->split_secondary = NULL;
265
266 spc->interlaced = spo->interlaced;
267 spc->bevel_light_adjust = spo->bevel_light_adjust;
268 spc->bevel_dark_adjust = spo->bevel_dark_adjust;
269 spc->border = spo->border;
270 spc->parent = NULL;
271 spc->parentx = spc->parenty = 0;
272 spc->pixel_data = NULL;
273
274 copy->textures = orig->textures;
275 copy->texture = g_memdup(orig->texture,
276 orig->textures * sizeof(RrTexture));
277 for (i = 0; i < copy->textures; ++i)
278 if (copy->texture[i].type == RR_TEXTURE_RGBA) {
279 copy->texture[i].data.rgba.cache = NULL;
280 }
281 copy->pixmap = None;
282 copy->xftdraw = NULL;
283 copy->w = copy->h = 0;
284 return copy;
285 }
286
287 void RrAppearanceFree(RrAppearance *a)
288 {
289 gint i;
290
291 if (a) {
292 RrSurface *p;
293 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
294 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
295 for (i = 0; i < a->textures; ++i)
296 if (a->texture[i].type == RR_TEXTURE_RGBA) {
297 g_free(a->texture[i].data.rgba.cache);
298 a->texture[i].data.rgba.cache = NULL;
299 }
300 if (a->textures)
301 g_free(a->texture);
302 p = &a->surface;
303 RrColorFree(p->primary);
304 RrColorFree(p->secondary);
305 RrColorFree(p->border_color);
306 RrColorFree(p->interlace_color);
307 RrColorFree(p->bevel_dark);
308 RrColorFree(p->bevel_light);
309 RrColorFree(p->split_primary);
310 RrColorFree(p->split_secondary);
311 g_free(p->pixel_data);
312 p->pixel_data = NULL;
313 g_free(a);
314 }
315 }
316
317
318 static void pixel_data_to_pixmap(RrAppearance *l,
319 gint x, gint y, gint w, gint h)
320 {
321 RrPixel32 *in, *scratch;
322 Pixmap out;
323 XImage *im = NULL;
324 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
325 ZPixmap, 0, NULL, w, h, 32, 0);
326 g_assert(im != NULL);
327
328 in = l->surface.pixel_data;
329 out = l->pixmap;
330
331 /* this malloc is a complete waste of time on normal 32bpp
332 as reduce_depth just sets im->data = data and returns
333 */
334 scratch = g_new(RrPixel32, im->width * im->height);
335 im->data = (gchar*) scratch;
336 RrReduceDepth(l->inst, in, im);
337 XPutImage(RrDisplay(l->inst), out,
338 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
339 im, 0, 0, x, y, w, h);
340 im->data = NULL;
341 XDestroyImage(im);
342 g_free(scratch);
343 }
344
345 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
346 {
347 *l = *t = *r = *b = 0;
348
349 if (a->surface.grad != RR_SURFACE_PARENTREL) {
350 if (a->surface.relief != RR_RELIEF_FLAT) {
351 switch (a->surface.bevel) {
352 case RR_BEVEL_1:
353 *l = *t = *r = *b = 1;
354 break;
355 case RR_BEVEL_2:
356 *l = *t = *r = *b = 2;
357 break;
358 }
359 } else if (a->surface.border) {
360 *l = *t = *r = *b = 1;
361 }
362 }
363 }
364
365 void RrMinSize(RrAppearance *a, gint *w, gint *h)
366 {
367 *w = RrMinWidth(a);
368 *h = RrMinHeight(a);
369 }
370
371 gint RrMinWidth(RrAppearance *a)
372 {
373 gint i;
374 RrSize *m;
375 gint l, t, r, b;
376 gint w = 0;
377
378 for (i = 0; i < a->textures; ++i) {
379 switch (a->texture[i].type) {
380 case RR_TEXTURE_NONE:
381 break;
382 case RR_TEXTURE_MASK:
383 w = MAX(w, a->texture[i].data.mask.mask->width);
384 break;
385 case RR_TEXTURE_TEXT:
386 m = RrFontMeasureString(a->texture[i].data.text.font,
387 a->texture[i].data.text.string,
388 a->texture[i].data.text.shadow_offset_x,
389 a->texture[i].data.text.shadow_offset_y,
390 a->texture[i].data.text.maxwidth);
391 w = MAX(w, m->width);
392 g_free(m);
393 break;
394 case RR_TEXTURE_RGBA:
395 w += MAX(w, a->texture[i].data.rgba.width);
396 break;
397 case RR_TEXTURE_LINE_ART:
398 w += MAX(w, MAX(a->texture[i].data.lineart.x1,
399 a->texture[i].data.lineart.x2));
400 break;
401 }
402 }
403
404 RrMargins(a, &l, &t, &r, &b);
405
406 w += l + r;
407
408 if (w < 1) w = 1;
409 return w;
410 }
411
412 gint RrMinHeight(RrAppearance *a)
413 {
414 gint i;
415 gint l, t, r, b;
416 gint h = 0;
417
418 for (i = 0; i < a->textures; ++i) {
419 switch (a->texture[i].type) {
420 case RR_TEXTURE_NONE:
421 break;
422 case RR_TEXTURE_MASK:
423 h = MAX(h, a->texture[i].data.mask.mask->height);
424 break;
425 case RR_TEXTURE_TEXT:
426 h += MAX(h, RrFontHeight(a->texture[i].data.text.font,
427 a->texture[i].data.text.shadow_offset_y));
428 break;
429 case RR_TEXTURE_RGBA:
430 h += MAX(h, a->texture[i].data.rgba.height);
431 break;
432 case RR_TEXTURE_LINE_ART:
433 h += MAX(h, MAX(a->texture[i].data.lineart.y1,
434 a->texture[i].data.lineart.y2));
435 break;
436 }
437 }
438
439 RrMargins(a, &l, &t, &r, &b);
440
441 h += t + b;
442
443 if (h < 1) h = 1;
444 return h;
445 }
446
447 static void reverse_bits(gchar *c, gint n)
448 {
449 gint i;
450 for (i = 0; i < n; i++, c++)
451 *c = (((*c * 0x0802UL & 0x22110UL) |
452 (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
453 }
454
455 gboolean RrPixmapToRGBA(const RrInstance *inst,
456 Pixmap pmap, Pixmap mask,
457 gint *w, gint *h, RrPixel32 **data)
458 {
459 Window xr;
460 gint xx, xy;
461 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
462 XImage *xi, *xm = NULL;
463
464 if (!XGetGeometry(RrDisplay(inst), pmap,
465 &xr, &xx, &xy, &pw, &ph, &xb, &xd))
466 return FALSE;
467
468 if (mask) {
469 if (!XGetGeometry(RrDisplay(inst), mask,
470 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
471 return FALSE;
472 if (pw != mw || ph != mh || xd != 1)
473 return FALSE;
474 }
475
476 xi = XGetImage(RrDisplay(inst), pmap,
477 0, 0, pw, ph, 0xffffffff, ZPixmap);
478 if (!xi)
479 return FALSE;
480
481 if (mask) {
482 xm = XGetImage(RrDisplay(inst), mask,
483 0, 0, mw, mh, 0xffffffff, ZPixmap);
484 if (!xm) {
485 XDestroyImage(xi);
486 return FALSE;
487 }
488 if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
489 reverse_bits(xm->data, xm->bytes_per_line * xm->height);
490 }
491
492 if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
493 reverse_bits(xi->data, xi->bytes_per_line * xi->height);
494
495 *data = g_new(RrPixel32, pw * ph);
496 RrIncreaseDepth(inst, *data, xi);
497
498 if (mask) {
499 /* apply transparency from the mask */
500 di = 0;
501 for (i = 0, y = 0; y < ph; ++y) {
502 for (x = 0; x < pw; ++x, ++i) {
503 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
504 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
505 }
506 di += xm->bytes_per_line;
507 }
508 }
509
510 *w = pw;
511 *h = ph;
512
513 XDestroyImage(xi);
514 if (mask)
515 XDestroyImage(xm);
516
517 return TRUE;
518 }
This page took 0.056299 seconds and 4 git commands to generate.