]> Dogcows Code - chaz/openbox/blob - render/render.c
we use the offsets we get from the server, we should use the byte order too..
[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 XDrawLine(RrDisplay(l->inst), l->pixmap,
108 RrColorGC(l->texture[i].data.lineart.color),
109 l->texture[i].data.lineart.x1,
110 l->texture[i].data.lineart.y1,
111 l->texture[i].data.lineart.x2,
112 l->texture[i].data.lineart.y2);
113 break;
114 case RR_TEXTURE_MASK:
115 if (!transferred) {
116 transferred = 1;
117 if (l->surface.grad != RR_SURFACE_SOLID)
118 pixel_data_to_pixmap(l, 0, 0, w, h);
119 }
120 RrPixmapMaskDraw(l->pixmap, &l->texture[i].data.mask, &tarea);
121 break;
122 case RR_TEXTURE_RGBA:
123 g_assert(!transferred);
124 RrImageDraw(l->surface.pixel_data,
125 &l->texture[i].data.rgba, &tarea);
126 break;
127 }
128 }
129
130 if (!transferred) {
131 transferred = 1;
132 if (l->surface.grad != RR_SURFACE_SOLID)
133 pixel_data_to_pixmap(l, 0, 0, w, h);
134 }
135
136 XSetWindowBackgroundPixmap(RrDisplay(l->inst), win, l->pixmap);
137 XClearWindow(RrDisplay(l->inst), win);
138 if (oldp) XFreePixmap(RrDisplay(l->inst), oldp);
139 }
140
141 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
142 {
143 RrAppearance *out;
144
145 out = g_new0(RrAppearance, 1);
146 out->inst = inst;
147 out->textures = numtex;
148 if (numtex) out->texture = g_new0(RrTexture, numtex);
149
150 return out;
151 }
152
153 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
154 {
155 RrSurface *spo, *spc;
156 RrAppearance *copy = g_new(RrAppearance, 1);
157 gint i;
158
159 copy->inst = orig->inst;
160
161 spo = &(orig->surface);
162 spc = &(copy->surface);
163 spc->grad = spo->grad;
164 spc->relief = spo->relief;
165 spc->bevel = spo->bevel;
166 if (spo->primary != NULL)
167 spc->primary = RrColorNew(copy->inst,
168 spo->primary->r,
169 spo->primary->g,
170 spo->primary->b);
171 else spc->primary = NULL;
172
173 if (spo->secondary != NULL)
174 spc->secondary = RrColorNew(copy->inst,
175 spo->secondary->r,
176 spo->secondary->g,
177 spo->secondary->b);
178 else spc->secondary = NULL;
179
180 if (spo->border_color != NULL)
181 spc->border_color = RrColorNew(copy->inst,
182 spo->border_color->r,
183 spo->border_color->g,
184 spo->border_color->b);
185 else spc->border_color = NULL;
186
187 if (spo->interlace_color != NULL)
188 spc->interlace_color = RrColorNew(copy->inst,
189 spo->interlace_color->r,
190 spo->interlace_color->g,
191 spo->interlace_color->b);
192 else spc->interlace_color = NULL;
193
194 if (spo->bevel_dark != NULL)
195 spc->bevel_dark = RrColorNew(copy->inst,
196 spo->bevel_dark->r,
197 spo->bevel_dark->g,
198 spo->bevel_dark->b);
199 else spc->bevel_dark = NULL;
200
201 if (spo->bevel_light != NULL)
202 spc->bevel_light = RrColorNew(copy->inst,
203 spo->bevel_light->r,
204 spo->bevel_light->g,
205 spo->bevel_light->b);
206 else spc->bevel_light = NULL;
207
208 spc->interlaced = spo->interlaced;
209 spc->border = spo->border;
210 spc->parent = NULL;
211 spc->parentx = spc->parenty = 0;
212 spc->pixel_data = NULL;
213
214 copy->textures = orig->textures;
215 copy->texture = g_memdup(orig->texture,
216 orig->textures * sizeof(RrTexture));
217 for (i = 0; i < copy->textures; ++i)
218 if (copy->texture[i].type == RR_TEXTURE_RGBA) {
219 g_free(copy->texture[i].data.rgba.cache);
220 copy->texture[i].data.rgba.cache = NULL;
221 }
222 copy->pixmap = None;
223 copy->xftdraw = NULL;
224 copy->w = copy->h = 0;
225 return copy;
226 }
227
228 void RrAppearanceFree(RrAppearance *a)
229 {
230 gint i;
231
232 if (a) {
233 RrSurface *p;
234 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
235 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
236 for (i = 0; i < a->textures; ++i)
237 if (a->texture[i].type == RR_TEXTURE_RGBA) {
238 g_free(a->texture[i].data.rgba.cache);
239 a->texture[i].data.rgba.cache = NULL;
240 }
241 if (a->textures)
242 g_free(a->texture);
243 p = &a->surface;
244 RrColorFree(p->primary);
245 RrColorFree(p->secondary);
246 RrColorFree(p->border_color);
247 RrColorFree(p->interlace_color);
248 RrColorFree(p->bevel_dark);
249 RrColorFree(p->bevel_light);
250 g_free(p->pixel_data);
251
252 g_free(a);
253 }
254 }
255
256
257 static void pixel_data_to_pixmap(RrAppearance *l,
258 gint x, gint y, gint w, gint h)
259 {
260 RrPixel32 *in, *scratch;
261 Pixmap out;
262 XImage *im = NULL;
263 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
264 ZPixmap, 0, NULL, w, h, 32, 0);
265 g_assert(im != NULL);
266
267 in = l->surface.pixel_data;
268 out = l->pixmap;
269
270 /* this malloc is a complete waste of time on normal 32bpp
271 as reduce_depth just sets im->data = data and returns
272 */
273 scratch = g_new(RrPixel32, im->width * im->height);
274 im->data = (char*) scratch;
275 RrReduceDepth(l->inst, in, im);
276 XPutImage(RrDisplay(l->inst), out,
277 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
278 im, 0, 0, x, y, w, h);
279 im->data = NULL;
280 XDestroyImage(im);
281 g_free(scratch);
282 }
283
284 void RrMinsize(RrAppearance *l, gint *w, gint *h)
285 {
286 gint i;
287 gint m;
288 *w = *h = 0;
289
290 for (i = 0; i < l->textures; ++i) {
291 switch (l->texture[i].type) {
292 case RR_TEXTURE_NONE:
293 break;
294 case RR_TEXTURE_MASK:
295 *w = MAX(*w, l->texture[i].data.mask.mask->width);
296 *h = MAX(*h, l->texture[i].data.mask.mask->height);
297 break;
298 case RR_TEXTURE_TEXT:
299 m = RrFontMeasureString(l->texture[i].data.text.font,
300 l->texture[i].data.text.string);
301 *w = MAX(*w, m);
302 m = RrFontHeight(l->texture[i].data.text.font);
303 *h += MAX(*h, m);
304 break;
305 case RR_TEXTURE_RGBA:
306 *w += MAX(*w, l->texture[i].data.rgba.width);
307 *h += MAX(*h, l->texture[i].data.rgba.height);
308 break;
309 case RR_TEXTURE_LINE_ART:
310 *w += MAX(*w, MAX(l->texture[i].data.lineart.x1,
311 l->texture[i].data.lineart.x2));
312 *h += MAX(*h, MAX(l->texture[i].data.lineart.y1,
313 l->texture[i].data.lineart.y2));
314 break;
315 }
316 }
317
318 if (l->surface.relief != RR_RELIEF_FLAT) {
319 switch (l->surface.bevel) {
320 case RR_BEVEL_1:
321 *w += 2;
322 *h += 2;
323 break;
324 case RR_BEVEL_2:
325 *w += 4;
326 *h += 4;
327 break;
328 }
329 } else if (l->surface.border) {
330 *w += 2;
331 *h += 2;
332 }
333
334 if (*w < 1) *w = 1;
335 if (*h < 1) *h = 1;
336 }
337
338 gboolean RrPixmapToRGBA(const RrInstance *inst,
339 Pixmap pmap, Pixmap mask,
340 gint *w, gint *h, RrPixel32 **data)
341 {
342 Window xr;
343 gint xx, xy;
344 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
345 XImage *xi, *xm = NULL;
346
347 if (!XGetGeometry(RrDisplay(inst),
348 pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
349 return FALSE;
350 if (mask) {
351 if (!XGetGeometry(RrDisplay(inst), mask,
352 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
353 return FALSE;
354 if (pw != mw || ph != mh || xd != 1)
355 return FALSE;
356 }
357
358 xi = XGetImage(RrDisplay(inst), pmap,
359 0, 0, pw, ph, 0xffffffff, ZPixmap);
360 if (!xi)
361 return FALSE;
362
363 if (mask) {
364 xm = XGetImage(RrDisplay(inst), mask,
365 0, 0, mw, mh, 0xffffffff, ZPixmap);
366 if (!xm)
367 return FALSE;
368 }
369
370 *data = g_new(RrPixel32, pw * ph);
371 RrIncreaseDepth(inst, *data, xi);
372
373 if (mask) {
374 /* apply transparency from the mask */
375 di = 0;
376 for (i = 0, y = 0; y < ph; ++y) {
377 for (x = 0; x < pw; ++x, ++i) {
378 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
379 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
380 }
381 di += xm->bytes_per_line;
382 }
383 }
384
385 *w = pw;
386 *h = ph;
387
388 return TRUE;
389 }
This page took 0.054498 seconds and 5 git commands to generate.