18 static void pixel_data_to_pixmap(RrAppearance *l,
19 gint x, gint y, gint w, gint h);
21 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
23 int i, transferred = 0, sw;
24 RrPixel32 *source, *dest;
26 RrRect tarea; /* area in which to draw textures */
29 if (w <= 0 || h <= 0) return;
31 resized = (a->w != w || a->h != h);
33 oldp = a->pixmap; /* save to free after changing the visible pixmap */
34 a->pixmap = XCreatePixmap(RrDisplay(a->inst),
35 RrRootWindow(a->inst),
36 w, h, RrDepth(a->inst));
38 g_assert(a->pixmap != None);
42 if (a->xftdraw != NULL)
43 XftDrawDestroy(a->xftdraw);
44 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
45 RrVisual(a->inst), RrColormap(a->inst));
46 g_assert(a->xftdraw != NULL);
48 g_free(a->surface.pixel_data);
49 a->surface.pixel_data = g_new(RrPixel32, w * h);
51 if (a->surface.grad == RR_SURFACE_PARENTREL) {
52 g_assert (a->surface.parent);
53 g_assert (a->surface.parent->w);
55 sw = a->surface.parent->w;
56 source = (a->surface.parent->surface.pixel_data +
57 a->surface.parentx + sw * a->surface.parenty);
58 dest = a->surface.pixel_data;
59 for (i = 0; i < h; i++, source += sw, dest += w) {
60 memcpy(dest, source, w * sizeof(RrPixel32));
67 RrMargins(a, &l, &t, &r, &b);
68 RECT_SET(tarea, l, t, w - l - r, h - t - b);
71 for (i = 0; i < a->textures; i++) {
72 switch (a->texture[i].type) {
78 if (a->surface.grad != RR_SURFACE_SOLID)
79 pixel_data_to_pixmap(a, 0, 0, w, h);
81 if (a->xftdraw == NULL) {
82 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
86 RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
88 case RR_TEXTURE_LINE_ART:
91 if (a->surface.grad != RR_SURFACE_SOLID)
92 pixel_data_to_pixmap(a, 0, 0, w, h);
94 XDrawLine(RrDisplay(a->inst), a->pixmap,
95 RrColorGC(a->texture[i].data.lineart.color),
96 a->texture[i].data.lineart.x1,
97 a->texture[i].data.lineart.y1,
98 a->texture[i].data.lineart.x2,
99 a->texture[i].data.lineart.y2);
101 case RR_TEXTURE_MASK:
104 if (a->surface.grad != RR_SURFACE_SOLID)
105 pixel_data_to_pixmap(a, 0, 0, w, h);
107 RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
109 case RR_TEXTURE_RGBA:
110 g_assert(!transferred);
111 RrImageDraw(a->surface.pixel_data,
112 &a->texture[i].data.rgba,
121 if (a->surface.grad != RR_SURFACE_SOLID)
122 pixel_data_to_pixmap(a, 0, 0, w, h);
125 XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
126 XClearWindow(RrDisplay(a->inst), win);
127 if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
130 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
134 out = g_new0(RrAppearance, 1);
136 out->textures = numtex;
137 if (numtex) out->texture = g_new0(RrTexture, numtex);
142 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
144 RrSurface *spo, *spc;
145 RrAppearance *copy = g_new(RrAppearance, 1);
148 copy->inst = orig->inst;
150 spo = &(orig->surface);
151 spc = &(copy->surface);
152 spc->grad = spo->grad;
153 spc->relief = spo->relief;
154 spc->bevel = spo->bevel;
155 if (spo->primary != NULL)
156 spc->primary = RrColorNew(copy->inst,
160 else spc->primary = NULL;
162 if (spo->secondary != NULL)
163 spc->secondary = RrColorNew(copy->inst,
167 else spc->secondary = NULL;
169 if (spo->border_color != NULL)
170 spc->border_color = RrColorNew(copy->inst,
171 spo->border_color->r,
172 spo->border_color->g,
173 spo->border_color->b);
174 else spc->border_color = NULL;
176 if (spo->interlace_color != NULL)
177 spc->interlace_color = RrColorNew(copy->inst,
178 spo->interlace_color->r,
179 spo->interlace_color->g,
180 spo->interlace_color->b);
181 else spc->interlace_color = NULL;
183 if (spo->bevel_dark != NULL)
184 spc->bevel_dark = RrColorNew(copy->inst,
188 else spc->bevel_dark = NULL;
190 if (spo->bevel_light != NULL)
191 spc->bevel_light = RrColorNew(copy->inst,
194 spo->bevel_light->b);
195 else spc->bevel_light = NULL;
197 spc->interlaced = spo->interlaced;
198 spc->border = spo->border;
200 spc->parentx = spc->parenty = 0;
201 spc->pixel_data = NULL;
203 copy->textures = orig->textures;
204 copy->texture = g_memdup(orig->texture,
205 orig->textures * sizeof(RrTexture));
206 for (i = 0; i < copy->textures; ++i)
207 if (copy->texture[i].type == RR_TEXTURE_RGBA) {
208 copy->texture[i].data.rgba.cache = NULL;
211 copy->xftdraw = NULL;
212 copy->w = copy->h = 0;
216 void RrAppearanceFree(RrAppearance *a)
222 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
223 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
224 for (i = 0; i < a->textures; ++i)
225 if (a->texture[i].type == RR_TEXTURE_RGBA) {
226 g_free(a->texture[i].data.rgba.cache);
227 a->texture[i].data.rgba.cache = NULL;
232 RrColorFree(p->primary);
233 RrColorFree(p->secondary);
234 RrColorFree(p->border_color);
235 RrColorFree(p->interlace_color);
236 RrColorFree(p->bevel_dark);
237 RrColorFree(p->bevel_light);
238 g_free(p->pixel_data);
245 static void pixel_data_to_pixmap(RrAppearance *l,
246 gint x, gint y, gint w, gint h)
248 RrPixel32 *in, *scratch;
251 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
252 ZPixmap, 0, NULL, w, h, 32, 0);
253 g_assert(im != NULL);
255 in = l->surface.pixel_data;
258 /* this malloc is a complete waste of time on normal 32bpp
259 as reduce_depth just sets im->data = data and returns
261 scratch = g_new(RrPixel32, im->width * im->height);
262 im->data = (char*) scratch;
263 RrReduceDepth(l->inst, in, im);
264 XPutImage(RrDisplay(l->inst), out,
265 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
266 im, 0, 0, x, y, w, h);
272 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
274 *l = *t = *r = *b = 0;
276 if (a->surface.grad != RR_SURFACE_PARENTREL) {
277 if (a->surface.relief != RR_RELIEF_FLAT) {
278 switch (a->surface.bevel) {
280 *l = *t = *r = *b = 1;
283 *l = *t = *r = *b = 2;
286 } else if (a->surface.border) {
287 *l = *t = *r = *b = 1;
292 void RrMinsize(RrAppearance *a, gint *w, gint *h)
299 for (i = 0; i < a->textures; ++i) {
300 switch (a->texture[i].type) {
301 case RR_TEXTURE_NONE:
303 case RR_TEXTURE_MASK:
304 *w = MAX(*w, a->texture[i].data.mask.mask->width);
305 *h = MAX(*h, a->texture[i].data.mask.mask->height);
307 case RR_TEXTURE_TEXT:
308 m = RrFontMeasureString(a->texture[i].data.text.font,
309 a->texture[i].data.text.string);
311 m = RrFontHeight(a->texture[i].data.text.font);
314 case RR_TEXTURE_RGBA:
315 *w += MAX(*w, a->texture[i].data.rgba.width);
316 *h += MAX(*h, a->texture[i].data.rgba.height);
318 case RR_TEXTURE_LINE_ART:
319 *w += MAX(*w, MAX(a->texture[i].data.lineart.x1,
320 a->texture[i].data.lineart.x2));
321 *h += MAX(*h, MAX(a->texture[i].data.lineart.y1,
322 a->texture[i].data.lineart.y2));
327 RrMargins(a, &l, &t, &r, &b);
336 gboolean RrPixmapToRGBA(const RrInstance *inst,
337 Pixmap pmap, Pixmap mask,
338 gint *w, gint *h, RrPixel32 **data)
342 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
343 XImage *xi, *xm = NULL;
345 if (!XGetGeometry(RrDisplay(inst),
346 pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
349 if (!XGetGeometry(RrDisplay(inst), mask,
350 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
352 if (pw != mw || ph != mh || xd != 1)
356 xi = XGetImage(RrDisplay(inst), pmap,
357 0, 0, pw, ph, 0xffffffff, ZPixmap);
362 xm = XGetImage(RrDisplay(inst), mask,
363 0, 0, mw, mh, 0xffffffff, ZPixmap);
368 *data = g_new(RrPixel32, pw * ph);
369 RrIncreaseDepth(inst, *data, xi);
372 /* apply transparency from the mask */
374 for (i = 0, y = 0; y < ph; ++y) {
375 for (x = 0; x < pw; ++x, ++i) {
376 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
377 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
379 di += xm->bytes_per_line;