]> Dogcows Code - chaz/openbox/blob - render/render.c
couple more xrdb theme format cleanups:
[chaz/openbox] / render / render.c
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3
4 #include "render.h"
5 #include "gradient.h"
6 #include "font.h"
7 #include "mask.h"
8 #include "color.h"
9 #include "image.h"
10 #include "theme.h"
11
12 #include <glib.h>
13
14 #ifdef HAVE_STDLIB_H
15 # include <stdlib.h>
16 #endif
17
18 static void pixel_data_to_pixmap(RrAppearance *l,
19 gint x, gint y, gint w, gint h);
20
21 void RrPaint(RrAppearance *l, Window win, gint w, gint h)
22 {
23 int i, transferred = 0, sw;
24 RrPixel32 *source, *dest;
25 Pixmap oldp;
26 RrRect tarea; /* area in which to draw textures */
27 gboolean resized;
28
29 if (w <= 0 || h <= 0) return;
30
31 resized = (l->w != w || l->h != h);
32
33 oldp = l->pixmap; /* save to free after changing the visible pixmap */
34 l->pixmap = XCreatePixmap(RrDisplay(l->inst),
35 RrRootWindow(l->inst),
36 w, h, RrDepth(l->inst));
37
38 g_assert(l->pixmap != None);
39 l->w = w;
40 l->h = h;
41
42 if (l->xftdraw != NULL)
43 XftDrawDestroy(l->xftdraw);
44 l->xftdraw = XftDrawCreate(RrDisplay(l->inst), l->pixmap,
45 RrVisual(l->inst), RrColormap(l->inst));
46 g_assert(l->xftdraw != NULL);
47
48 g_free(l->surface.pixel_data);
49 l->surface.pixel_data = g_new(RrPixel32, w * h);
50
51 if (l->surface.grad == RR_SURFACE_PARENTREL) {
52 g_assert (l->surface.parent);
53 g_assert (l->surface.parent->w);
54
55 sw = l->surface.parent->w;
56 source = (l->surface.parent->surface.pixel_data +
57 l->surface.parentx + sw * l->surface.parenty);
58 dest = l->surface.pixel_data;
59 for (i = 0; i < h; i++, source += sw, dest += w) {
60 memcpy(dest, source, w * sizeof(RrPixel32));
61 }
62 } else
63 RrRender(l, w, h);
64
65 RECT_SET(tarea, 0, 0, w, h);
66 if (l->surface.grad != RR_SURFACE_PARENTREL) {
67 if (l->surface.relief != RR_RELIEF_FLAT) {
68 switch (l->surface.bevel) {
69 case RR_BEVEL_1:
70 tarea.x += 1; tarea.y += 1;
71 tarea.width -= 2; tarea.height -= 2;
72 break;
73 case RR_BEVEL_2:
74 tarea.x += 2; tarea.y += 2;
75 tarea.width -= 4; tarea.height -= 4;
76 break;
77 }
78 } else if (l->surface.border) {
79 tarea.x += 1; tarea.y += 1;
80 tarea.width -= 2; tarea.height -= 2;
81 }
82 }
83
84 for (i = 0; i < l->textures; i++) {
85 switch (l->texture[i].type) {
86 case RR_TEXTURE_NONE:
87 break;
88 case RR_TEXTURE_TEXT:
89 if (!transferred) {
90 transferred = 1;
91 if (l->surface.grad != RR_SURFACE_SOLID)
92 pixel_data_to_pixmap(l, 0, 0, w, h);
93 }
94 if (l->xftdraw == NULL) {
95 l->xftdraw = XftDrawCreate(RrDisplay(l->inst), l->pixmap,
96 RrVisual(l->inst),
97 RrColormap(l->inst));
98 }
99 RrFontDraw(l->xftdraw, &l->texture[i].data.text, &tarea);
100 break;
101 case RR_TEXTURE_LINE_ART:
102 if (!transferred) {
103 transferred = 1;
104 if (l->surface.grad != RR_SURFACE_SOLID)
105 pixel_data_to_pixmap(l, 0, 0, w, h);
106 }
107 g_message("%d %d -> %d %d",
108 l->texture[i].data.lineart.x1,
109 l->texture[i].data.lineart.y1,
110 l->texture[i].data.lineart.x2,
111 l->texture[i].data.lineart.y2);
112 XDrawLine(RrDisplay(l->inst), l->pixmap,
113 RrColorGC(l->texture[i].data.lineart.color),
114 l->texture[i].data.lineart.x1,
115 l->texture[i].data.lineart.y1,
116 l->texture[i].data.lineart.x2,
117 l->texture[i].data.lineart.y2);
118 break;
119 case RR_TEXTURE_MASK:
120 if (!transferred) {
121 transferred = 1;
122 if (l->surface.grad != RR_SURFACE_SOLID)
123 pixel_data_to_pixmap(l, 0, 0, w, h);
124 }
125 RrPixmapMaskDraw(l->pixmap, &l->texture[i].data.mask, &tarea);
126 break;
127 case RR_TEXTURE_RGBA:
128 g_assert(!transferred);
129 RrImageDraw(l->surface.pixel_data,
130 &l->texture[i].data.rgba, &tarea);
131 break;
132 }
133 }
134
135 if (!transferred) {
136 transferred = 1;
137 if (l->surface.grad != RR_SURFACE_SOLID)
138 pixel_data_to_pixmap(l, 0, 0, w, h);
139 }
140
141 XSetWindowBackgroundPixmap(RrDisplay(l->inst), win, l->pixmap);
142 XClearWindow(RrDisplay(l->inst), win);
143 if (oldp) XFreePixmap(RrDisplay(l->inst), oldp);
144 }
145
146 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
147 {
148 RrAppearance *out;
149
150 out = g_new0(RrAppearance, 1);
151 out->inst = inst;
152 out->textures = numtex;
153 if (numtex) out->texture = g_new0(RrTexture, numtex);
154
155 return out;
156 }
157
158 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
159 {
160 RrSurface *spo, *spc;
161 RrAppearance *copy = g_new(RrAppearance, 1);
162 gint i;
163
164 copy->inst = orig->inst;
165
166 spo = &(orig->surface);
167 spc = &(copy->surface);
168 spc->grad = spo->grad;
169 spc->relief = spo->relief;
170 spc->bevel = spo->bevel;
171 if (spo->primary != NULL)
172 spc->primary = RrColorNew(copy->inst,
173 spo->primary->r,
174 spo->primary->g,
175 spo->primary->b);
176 else spc->primary = NULL;
177
178 if (spo->secondary != NULL)
179 spc->secondary = RrColorNew(copy->inst,
180 spo->secondary->r,
181 spo->secondary->g,
182 spo->secondary->b);
183 else spc->secondary = NULL;
184
185 if (spo->border_color != NULL)
186 spc->border_color = RrColorNew(copy->inst,
187 spo->border_color->r,
188 spo->border_color->g,
189 spo->border_color->b);
190 else spc->border_color = NULL;
191
192 if (spo->interlace_color != NULL)
193 spc->interlace_color = RrColorNew(copy->inst,
194 spo->interlace_color->r,
195 spo->interlace_color->g,
196 spo->interlace_color->b);
197 else spc->interlace_color = NULL;
198
199 if (spo->bevel_dark != NULL)
200 spc->bevel_dark = RrColorNew(copy->inst,
201 spo->bevel_dark->r,
202 spo->bevel_dark->g,
203 spo->bevel_dark->b);
204 else spc->bevel_dark = NULL;
205
206 if (spo->bevel_light != NULL)
207 spc->bevel_light = RrColorNew(copy->inst,
208 spo->bevel_light->r,
209 spo->bevel_light->g,
210 spo->bevel_light->b);
211 else spc->bevel_light = NULL;
212
213 spc->interlaced = spo->interlaced;
214 spc->border = spo->border;
215 spc->parent = NULL;
216 spc->parentx = spc->parenty = 0;
217 spc->pixel_data = NULL;
218
219 copy->textures = orig->textures;
220 copy->texture = g_memdup(orig->texture,
221 orig->textures * sizeof(RrTexture));
222 for (i = 0; i < copy->textures; ++i)
223 if (copy->texture[i].type == RR_TEXTURE_RGBA) {
224 g_free(copy->texture[i].data.rgba.cache);
225 copy->texture[i].data.rgba.cache = NULL;
226 }
227 copy->pixmap = None;
228 copy->xftdraw = NULL;
229 copy->w = copy->h = 0;
230 return copy;
231 }
232
233 void RrAppearanceFree(RrAppearance *a)
234 {
235 gint i;
236
237 if (a) {
238 RrSurface *p;
239 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
240 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
241 for (i = 0; i < a->textures; ++i)
242 if (a->texture[i].type == RR_TEXTURE_RGBA) {
243 g_free(a->texture[i].data.rgba.cache);
244 a->texture[i].data.rgba.cache = NULL;
245 }
246 if (a->textures)
247 g_free(a->texture);
248 p = &a->surface;
249 RrColorFree(p->primary);
250 RrColorFree(p->secondary);
251 RrColorFree(p->border_color);
252 RrColorFree(p->interlace_color);
253 RrColorFree(p->bevel_dark);
254 RrColorFree(p->bevel_light);
255 g_free(p->pixel_data);
256
257 g_free(a);
258 }
259 }
260
261
262 static void pixel_data_to_pixmap(RrAppearance *l,
263 gint x, gint y, gint w, gint h)
264 {
265 RrPixel32 *in, *scratch;
266 Pixmap out;
267 XImage *im = NULL;
268 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
269 ZPixmap, 0, NULL, w, h, 32, 0);
270 g_assert(im != NULL);
271
272 in = l->surface.pixel_data;
273 out = l->pixmap;
274
275 im->byte_order = LSBFirst;
276 /* this malloc is a complete waste of time on normal 32bpp
277 as reduce_depth just sets im->data = data and returns
278 */
279 scratch = g_new(RrPixel32, im->width * im->height);
280 im->data = (char*) scratch;
281 RrReduceDepth(l->inst, in, im);
282 XPutImage(RrDisplay(l->inst), out,
283 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
284 im, 0, 0, x, y, w, h);
285 im->data = NULL;
286 XDestroyImage(im);
287 g_free(scratch);
288 }
289
290 void RrMinsize(RrAppearance *l, gint *w, gint *h)
291 {
292 gint i;
293 gint m;
294 *w = *h = 0;
295
296 for (i = 0; i < l->textures; ++i) {
297 switch (l->texture[i].type) {
298 case RR_TEXTURE_NONE:
299 break;
300 case RR_TEXTURE_MASK:
301 *w = MAX(*w, l->texture[i].data.mask.mask->width);
302 *h = MAX(*h, l->texture[i].data.mask.mask->height);
303 break;
304 case RR_TEXTURE_TEXT:
305 m = RrFontMeasureString(l->texture[i].data.text.font,
306 l->texture[i].data.text.string);
307 *w = MAX(*w, m);
308 m = RrFontHeight(l->texture[i].data.text.font);
309 *h += MAX(*h, m);
310 break;
311 case RR_TEXTURE_RGBA:
312 *w += MAX(*w, l->texture[i].data.rgba.width);
313 *h += MAX(*h, l->texture[i].data.rgba.height);
314 break;
315 case RR_TEXTURE_LINE_ART:
316 *w += MAX(*w, MAX(l->texture[i].data.lineart.x1,
317 l->texture[i].data.lineart.x2));
318 *h += MAX(*h, MAX(l->texture[i].data.lineart.y1,
319 l->texture[i].data.lineart.y2));
320 break;
321 }
322 }
323
324 if (l->surface.relief != RR_RELIEF_FLAT) {
325 switch (l->surface.bevel) {
326 case RR_BEVEL_1:
327 *w += 2;
328 *h += 2;
329 break;
330 case RR_BEVEL_2:
331 *w += 4;
332 *h += 4;
333 break;
334 }
335 } else if (l->surface.border) {
336 *w += 2;
337 *h += 2;
338 }
339
340 if (*w < 1) *w = 1;
341 if (*h < 1) *h = 1;
342 }
343
344 gboolean RrPixmapToRGBA(const RrInstance *inst,
345 Pixmap pmap, Pixmap mask,
346 gint *w, gint *h, RrPixel32 **data)
347 {
348 Window xr;
349 gint xx, xy;
350 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
351 XImage *xi, *xm = NULL;
352
353 if (!XGetGeometry(RrDisplay(inst),
354 pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
355 return FALSE;
356 if (mask) {
357 if (!XGetGeometry(RrDisplay(inst), mask,
358 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
359 return FALSE;
360 if (pw != mw || ph != mh || xd != 1)
361 return FALSE;
362 }
363
364 xi = XGetImage(RrDisplay(inst), pmap,
365 0, 0, pw, ph, 0xffffffff, ZPixmap);
366 if (!xi)
367 return FALSE;
368
369 if (mask) {
370 xm = XGetImage(RrDisplay(inst), mask,
371 0, 0, mw, mh, 0xffffffff, ZPixmap);
372 if (!xm)
373 return FALSE;
374 }
375
376 *data = g_new(RrPixel32, pw * ph);
377 RrIncreaseDepth(inst, *data, xi);
378
379 if (mask) {
380 /* apply transparency from the mask */
381 di = 0;
382 for (i = 0, y = 0; y < ph; ++y) {
383 for (x = 0; x < pw; ++x, ++i) {
384 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
385 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
386 }
387 di += xm->bytes_per_line;
388 }
389 }
390
391 *w = pw;
392 *h = ph;
393
394 return TRUE;
395 }
This page took 0.055517 seconds and 4 git commands to generate.