add _NUM_TYPES to obrender enums, to allow enumeration of the enum values
[dana/openbox.git] / obrender / mask.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    mask.c for the Openbox window manager
4    Copyright (c) 2003-2007   Dana Jansens
5    Copyright (c) 2003        Derek Foreman
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "render.h"
21 #include "color.h"
22 #include "mask.h"
23
24 RrPixmapMask *RrPixmapMaskNew(const RrInstance *inst,
25                               gint w, gint h, const gchar *data)
26 {
27     RrPixmapMask *m = g_slice_new(RrPixmapMask);
28     m->inst = inst;
29     m->width = w;
30     m->height = h;
31     /* round up to nearest byte */
32     m->data = g_memdup(data, (w + 7) / 8 * h);
33     m->mask = XCreateBitmapFromData(RrDisplay(inst), RrRootWindow(inst),
34                                     data, w, h);
35     return m;
36 }
37
38 void RrPixmapMaskFree(RrPixmapMask *m)
39 {
40     if (m) {
41         XFreePixmap(RrDisplay(m->inst), m->mask);
42         g_free(m->data);
43         g_slice_free(RrPixmapMask, m);
44     }
45 }
46
47 void RrPixmapMaskDraw(Pixmap p, const RrTextureMask *m, const RrRect *area)
48 {
49     gint x, y;
50     if (m->mask == NULL) return; /* no mask given */
51
52     /* set the clip region */
53     x = area->x + (area->width - m->mask->width) / 2;
54     y = area->y + (area->height - m->mask->height) / 2;
55
56     if (x < 0) x = 0;
57     if (y < 0) y = 0;
58
59     XSetClipMask(RrDisplay(m->mask->inst), RrColorGC(m->color), m->mask->mask);
60     XSetClipOrigin(RrDisplay(m->mask->inst), RrColorGC(m->color), x, y);
61
62     /* fill in the clipped region */
63     XFillRectangle(RrDisplay(m->mask->inst), p, RrColorGC(m->color), x, y,
64                    x + m->mask->width, y + m->mask->height);
65
66     /* unset the clip region */
67     XSetClipMask(RrDisplay(m->mask->inst), RrColorGC(m->color), None);
68     XSetClipOrigin(RrDisplay(m->mask->inst), RrColorGC(m->color), 0, 0);
69 }
70
71 RrPixmapMask *RrPixmapMaskCopy(const RrPixmapMask *src)
72 {
73     RrPixmapMask *m = g_slice_new(RrPixmapMask);
74     m->inst = src->inst;
75     m->width = src->width;
76     m->height = src->height;
77     /* round up to nearest byte */
78     m->data = g_memdup(src->data, (src->width + 7) / 8 * src->height);
79     m->mask = XCreateBitmapFromData(RrDisplay(m->inst), RrRootWindow(m->inst),
80                                     m->data, m->width, m->height);
81     return m;
82 }