*** empty log message ***
[wmaker-crm.git] / src / pixmap.c
blobc97d7fb147540393253229c143833edc4a26eda2
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
27 #include <wraster.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "WindowMaker.h"
32 #include "wcore.h"
37 *----------------------------------------------------------------------
38 * wPixmapCreateFromXPMData--
39 * Creates a WPixmap structure and initializes it with the supplied
40 * XPM structure data.
42 * Returns:
43 * A WPixmap structure or NULL on failure.
45 * Notes:
46 * DEF_XPM_CLOSENESS specifies the XpmCloseness
47 *----------------------------------------------------------------------
49 WPixmap *
50 wPixmapCreateFromXPMData(WScreen *scr, char **data)
52 RImage *image;
53 WPixmap *pix;
55 image = RGetImageFromXPMData(scr->rcontext, data);
56 if (!image)
57 return NULL;
59 pix = wmalloc(sizeof(WPixmap));
60 memset(pix, 0, sizeof(WPixmap));
62 RConvertImageMask(scr->rcontext, image, &pix->image, &pix->mask, 128);
64 pix->width = image->width;
65 pix->height = image->height;
66 pix->depth = scr->w_depth;
68 RDestroyImage(image);
70 return pix;
75 *----------------------------------------------------------------------
76 * wPixmapCreateFromXBMData--
77 * Creates a WPixmap structure and initializes it with the supplied
78 * XBM structure data, size and mask.
80 * Returns:
81 * A WPixmap structure or NULL on failure.
83 *----------------------------------------------------------------------
85 WPixmap *
86 wPixmapCreateFromXBMData(WScreen *scr, char *data, char *mask,
87 int width, int height, unsigned long fg,
88 unsigned long bg)
90 WPixmap *pix;
92 pix = wmalloc(sizeof(WPixmap));
93 memset(pix, 0, sizeof(WPixmap));
94 pix->image = XCreatePixmapFromBitmapData(dpy, scr->w_win, data, width,
95 height, fg, bg, scr->w_depth);
96 if (pix->image==None) {
97 free(pix);
98 return NULL;
100 if (mask) {
101 pix->mask = XCreateBitmapFromData(dpy, scr->w_win, mask, width,
102 height);
103 } else {
104 pix->mask = None;
106 pix->width = width;
107 pix->height = height;
108 pix->depth = scr->w_depth;
109 return pix;
113 #ifdef unused
114 WPixmap*
115 wPixmapCreateFromBitmap(WScreen *scr, Pixmap bitmap, Pixmap mask,
116 unsigned long fg, unsigned long bg)
118 WPixmap *pix;
119 XImage *img, *img2;
120 Window foo;
121 int bar, x, y;
122 Pixmap pixmap;
123 unsigned int width, height, baz, d;
125 if (!XGetGeometry(dpy, bitmap, &foo, &bar, &bar, &width, &height, &baz,
126 &d) || d!=1) {
127 return NULL;
129 img = XGetImage(dpy, bitmap, 0, 0, width, height, AllPlanes, XYPixmap);
130 if (!img)
131 return NULL;
133 img2=XCreateImage(dpy, scr->w_visual, scr->w_depth, ZPixmap,
134 0, NULL, width, height, 8, 0);
135 if (!img2) {
136 XDestroyImage(img);
137 return NULL;
140 pixmap = XCreatePixmap(dpy, scr->w_win, width, height, scr->w_depth);
141 if (pixmap==None) {
142 XDestroyImage(img);
143 XDestroyImage(img2);
144 return NULL;
147 img2->data = wmalloc(height * img2->bytes_per_line);
149 for (y=0; y<height; y++) {
150 for (x=0; x<width; x++) {
151 if (XGetPixel(img, x, y)==0) {
152 XPutPixel(img2, x, y, bg);
153 } else {
154 XPutPixel(img2, x, y, fg);
158 XSetClipMask(dpy, scr->copy_gc, None);
159 XPutImage(dpy, pixmap, scr->copy_gc, img2, 0, 0, 0, 0, width, height);
160 XDestroyImage(img);
161 XDestroyImage(img2);
163 pix = wmalloc(sizeof(WPixmap));
164 memset(pix, 0, sizeof(WPixmap));
165 pix->image = pixmap;
166 pix->mask = mask;
167 pix->width = width;
168 pix->height = height;
169 pix->depth = scr->w_depth;
170 return pix;
172 #endif /* unused */
174 WPixmap*
175 wPixmapCreate(WScreen *scr, Pixmap image, Pixmap mask)
177 WPixmap *pix;
178 Window foo;
179 int bar;
180 unsigned int width, height, depth, baz;
182 pix = wmalloc(sizeof(WPixmap));
183 memset(pix, 0, sizeof(WPixmap));
184 pix->image = image;
185 pix->mask = mask;
186 if (!XGetGeometry(dpy, image, &foo, &bar, &bar, &width, &height, &baz,
187 &depth)) {
188 wwarning("XGetGeometry() failed during wPixmapCreate()");
189 free(pix);
190 return NULL;
192 pix->width = width;
193 pix->height = height;
194 pix->depth = depth;
195 return pix;
198 #if 0
200 *----------------------------------------------------------------------
201 * wPixmapLoadXBMFile--
202 * Creates a WPixmap structure and loads a XBM file into it with
203 * an optional mask file. If a mask is not wanted, mask_path should be
204 * NULL.
206 * Returns:
207 * A WPixmap structure or NULL on failure.
209 * Notes:
210 * If the mask bitmap is not successfully loaded the operation
211 * continues as no mask was supplied.
212 *----------------------------------------------------------------------
214 WPixmap *
215 wPixmapLoadXBMFile(WScreen *scr, char *path, char *mask_path)
217 WPixmap *pix;
218 int junk;
220 if (!path) return NULL;
222 pix = wmalloc(sizeof(WPixmap));
223 memset(pix, 0, sizeof(WPixmap));
225 if (XReadBitmapFile(dpy, scr->w_win, path, (unsigned *)&(pix->width),
226 (unsigned *)&(pix->height),
227 &(pix->image), &junk, &junk)!=BitmapSuccess) {
228 free(pix);
229 return NULL;
231 if (mask_path!=NULL) {
232 if (XReadBitmapFile(dpy, scr->w_win, path, (unsigned *)&junk,
233 (unsigned *)&junk, &(pix->mask),
234 &junk, &junk) !=BitmapSuccess) {
235 wwarning(_("could not load mask bitmap file \"%s\". Won't use mask"),
236 mask_path);
237 pix->mask = None;
239 } else {
240 pix->mask = None;
242 pix->depth = 1;
243 return pix;
246 #endif
249 *----------------------------------------------------------------------
250 * wPixmapDestroy--
251 * Destroys a WPixmap structure and the pixmap/mask it holds.
253 * Returns:
254 * None
255 *----------------------------------------------------------------------
257 void
258 wPixmapDestroy(WPixmap *pix)
260 if (!pix->shared) {
261 if (pix->mask && !pix->client_owned_mask) {
262 XFreePixmap(dpy, pix->mask);
265 if (pix->image && !pix->client_owned) {
266 XFreePixmap(dpy, pix->image);
269 free(pix);