Change to the linux kernel coding style
[wmaker-crm.git] / src / pixmap.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/*
9af1c6c4 dan1998-10-21 14:43:47 +00002 * Window Maker window manager
6830b057 dan2004-10-12 21:28:27 +00003 *
4153e2fd dan2003-01-16 23:30:45 +00004 * Copyright (c) 1997-2003 Alfredo K. Kojima
6830b057 dan2004-10-12 21:28:27 +00005 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00006 * 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.
10 *
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.
15 *
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
6830b057 dan2004-10-12 21:28:27 +000018 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
9d2e6ef9 scottc1998-09-29 22:36:29 +000019 * USA.
20 */
21
22#include "wconfig.h"
23
24#include <X11/Xlib.h>
25#include <X11/Xutil.h>
26
27#include <wraster.h>
28
29#include <stdlib.h>
30#include <string.h>
31#include "WindowMaker.h"
32#include "wcore.h"
33
9d2e6ef9 scottc1998-09-29 22:36:29 +000034/*
6830b057 dan2004-10-12 21:28:27 +000035 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +000036 * wPixmapCreateFromXPMData--
37 * Creates a WPixmap structure and initializes it with the supplied
38 * XPM structure data.
6830b057 dan2004-10-12 21:28:27 +000039 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000040 * Returns:
41 * A WPixmap structure or NULL on failure.
6830b057 dan2004-10-12 21:28:27 +000042 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000043 * Notes:
44 * DEF_XPM_CLOSENESS specifies the XpmCloseness
6830b057 dan2004-10-12 21:28:27 +000045 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +000046 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020047WPixmap *wPixmapCreateFromXPMData(WScreen * scr, char **data)
9d2e6ef9 scottc1998-09-29 22:36:29 +000048{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020049 RImage *image;
50 WPixmap *pix;
9d2e6ef9 scottc1998-09-29 22:36:29 +000051
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020052 image = RGetImageFromXPMData(scr->rcontext, data);
53 if (!image)
54 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000055
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020056 pix = wmalloc(sizeof(WPixmap));
57 memset(pix, 0, sizeof(WPixmap));
9d2e6ef9 scottc1998-09-29 22:36:29 +000058
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020059 RConvertImageMask(scr->rcontext, image, &pix->image, &pix->mask, 128);
6830b057 dan2004-10-12 21:28:27 +000060
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020061 pix->width = image->width;
62 pix->height = image->height;
63 pix->depth = scr->w_depth;
6830b057 dan2004-10-12 21:28:27 +000064
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020065 RReleaseImage(image);
6830b057 dan2004-10-12 21:28:27 +000066
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020067 return pix;
9d2e6ef9 scottc1998-09-29 22:36:29 +000068}
69
9d2e6ef9 scottc1998-09-29 22:36:29 +000070/*
6830b057 dan2004-10-12 21:28:27 +000071 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +000072 * wPixmapCreateFromXBMData--
73 * Creates a WPixmap structure and initializes it with the supplied
74 * XBM structure data, size and mask.
6830b057 dan2004-10-12 21:28:27 +000075 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000076 * Returns:
77 * A WPixmap structure or NULL on failure.
6830b057 dan2004-10-12 21:28:27 +000078 *
79 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +000080 */
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020081WPixmap *wPixmapCreateFromXBMData(WScreen * scr, char *data, char *mask,
82 int width, int height, unsigned long fg, unsigned long bg)
9d2e6ef9 scottc1998-09-29 22:36:29 +000083{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020084 WPixmap *pix;
85
86 pix = wmalloc(sizeof(WPixmap));
87 memset(pix, 0, sizeof(WPixmap));
88 pix->image = XCreatePixmapFromBitmapData(dpy, scr->w_win, data, width, height, fg, bg, scr->w_depth);
89 if (pix->image == None) {
90 wfree(pix);
91 return NULL;
92 }
93 if (mask) {
94 pix->mask = XCreateBitmapFromData(dpy, scr->w_win, mask, width, height);
95 } else {
96 pix->mask = None;
97 }
98 pix->width = width;
99 pix->height = height;
100 pix->depth = scr->w_depth;
101 return pix;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000102}
103
9af1c6c4 dan1998-10-21 14:43:47 +0000104#ifdef unused
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200105WPixmap *wPixmapCreateFromBitmap(WScreen * scr, Pixmap bitmap, Pixmap mask, unsigned long fg, unsigned long bg)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000106{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200107 WPixmap *pix;
108 XImage *img, *img2;
109 Window foo;
110 int bar, x, y;
111 Pixmap pixmap;
112 unsigned int width, height, baz, d;
113
114 if (!XGetGeometry(dpy, bitmap, &foo, &bar, &bar, &width, &height, &baz, &d) || d != 1) {
115 return NULL;
116 }
117 img = XGetImage(dpy, bitmap, 0, 0, width, height, AllPlanes, XYPixmap);
118 if (!img)
119 return NULL;
120
121 img2 = XCreateImage(dpy, scr->w_visual, scr->w_depth, ZPixmap, 0, NULL, width, height, 8, 0);
122 if (!img2) {
123 XDestroyImage(img);
124 return NULL;
125 }
126
127 pixmap = XCreatePixmap(dpy, scr->w_win, width, height, scr->w_depth);
128 if (pixmap == None) {
129 XDestroyImage(img);
130 XDestroyImage(img2);
131 return NULL;
132 }
133
134 img2->data = wmalloc(height * img2->bytes_per_line);
135
136 for (y = 0; y < height; y++) {
137 for (x = 0; x < width; x++) {
138 if (XGetPixel(img, x, y) == 0) {
139 XPutPixel(img2, x, y, bg);
140 } else {
141 XPutPixel(img2, x, y, fg);
142 }
143 }
144 }
145 XSetClipMask(dpy, scr->copy_gc, None);
146 XPutImage(dpy, pixmap, scr->copy_gc, img2, 0, 0, 0, 0, width, height);
147 XDestroyImage(img);
148 XDestroyImage(img2);
149
150 pix = wmalloc(sizeof(WPixmap));
151 memset(pix, 0, sizeof(WPixmap));
152 pix->image = pixmap;
153 pix->mask = mask;
154 pix->width = width;
155 pix->height = height;
156 pix->depth = scr->w_depth;
157 return pix;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000158}
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200159#endif /* unused */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000160
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200161WPixmap *wPixmapCreate(WScreen * scr, Pixmap image, Pixmap mask)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000162{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200163 WPixmap *pix;
164 Window foo;
165 int bar;
166 unsigned int width, height, depth, baz;
167
168 pix = wmalloc(sizeof(WPixmap));
169 memset(pix, 0, sizeof(WPixmap));
170 pix->image = image;
171 pix->mask = mask;
172 if (!XGetGeometry(dpy, image, &foo, &bar, &bar, &width, &height, &baz, &depth)) {
173 wwarning("XGetGeometry() failed during wPixmapCreate()");
174 wfree(pix);
175 return NULL;
176 }
177 pix->width = width;
178 pix->height = height;
179 pix->depth = depth;
180 return pix;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000181}
182
183#if 0
184/*
6830b057 dan2004-10-12 21:28:27 +0000185 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000186 * wPixmapLoadXBMFile--
187 * Creates a WPixmap structure and loads a XBM file into it with
188 * an optional mask file. If a mask is not wanted, mask_path should be
189 * NULL.
6830b057 dan2004-10-12 21:28:27 +0000190 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000191 * Returns:
192 * A WPixmap structure or NULL on failure.
6830b057 dan2004-10-12 21:28:27 +0000193 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000194 * Notes:
195 * If the mask bitmap is not successfully loaded the operation
196 * continues as no mask was supplied.
6830b057 dan2004-10-12 21:28:27 +0000197 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000198 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200199WPixmap *wPixmapLoadXBMFile(WScreen * scr, char *path, char *mask_path)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000200{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200201 WPixmap *pix;
202 int junk;
203
204 if (!path)
205 return NULL;
206
207 pix = wmalloc(sizeof(WPixmap));
208 memset(pix, 0, sizeof(WPixmap));
209
210 if (XReadBitmapFile(dpy, scr->w_win, path, (unsigned *)&(pix->width),
211 (unsigned *)&(pix->height), &(pix->image), &junk, &junk) != BitmapSuccess) {
212 wfree(pix);
213 return NULL;
214 }
215 if (mask_path != NULL) {
216 if (XReadBitmapFile(dpy, scr->w_win, path, (unsigned *)&junk,
217 (unsigned *)&junk, &(pix->mask), &junk, &junk) != BitmapSuccess) {
218 wwarning(_("could not load mask bitmap file \"%s\". Won't use mask"), mask_path);
219 pix->mask = None;
220 }
221 } else {
222 pix->mask = None;
223 }
224 pix->depth = 1;
225 return pix;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000226}
227
6830b057 dan2004-10-12 21:28:27 +0000228#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000229
230/*
6830b057 dan2004-10-12 21:28:27 +0000231 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000232 * wPixmapDestroy--
233 * Destroys a WPixmap structure and the pixmap/mask it holds.
6830b057 dan2004-10-12 21:28:27 +0000234 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000235 * Returns:
236 * None
237 *----------------------------------------------------------------------
238 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200239void wPixmapDestroy(WPixmap * pix)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000240{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200241 if (!pix->shared) {
242 if (pix->mask && !pix->client_owned_mask) {
243 XFreePixmap(dpy, pix->mask);
244 }
245
246 if (pix->image && !pix->client_owned) {
247 XFreePixmap(dpy, pix->image);
248 }
249 }
250 wfree(pix);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000251}