wmbattery: add autogen.
[dockapps.git] / libdockapp / src / dapixmap.c
blob7b37f65336c2995fa85c901e4491286a680232b8
1 /*
2 * Copyright (c) 1999-2005 Alfredo K. Kojima, Alban Hertroys
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <X11/Xlib.h>
24 #include <X11/xpm.h>
25 #include <X11/extensions/shape.h>
26 #include <X11/Xatom.h>
28 #include "dockapp.h"
29 #include "daargs.h"
31 extern struct DAContext *_daContext;
33 /* Local typedef */
34 typedef enum {
35 daXpmSourceData,
36 daXpmSourceFile
37 } daXpmSource;
39 /* Function prototype */
40 Bool _daMakePixmap(daXpmSource source,
41 char **data, Pixmap *pixmap, Pixmap *mask,
42 unsigned short *width, unsigned short *height);
46 void
47 DASetShapeWithOffset(Pixmap shapeMask, int x_ofs, int y_ofs)
49 DASetShapeWithOffsetForWindow(DAWindow, shapeMask, x_ofs, y_ofs);
52 void
53 DASetShapeWithOffsetForWindow(Window window, Pixmap shapeMask,
54 int x_ofs, int y_ofs)
56 XShapeCombineMask(DADisplay, window, ShapeBounding, -x_ofs, -y_ofs,
57 shapeMask, ShapeSet);
58 XFlush(DADisplay);
61 void
62 DASetPixmap(Pixmap pixmap)
64 DASetPixmapForWindow(DAWindow, pixmap);
67 void
68 DASetPixmapForWindow(Window window, Pixmap pixmap)
70 XSetWindowBackgroundPixmap(DADisplay, window, pixmap);
71 XClearWindow(DADisplay, window);
72 XFlush(DADisplay);
75 Pixmap
76 DAMakePixmap(void)
78 return (XCreatePixmap(DADisplay, DAWindow,
79 _daContext->width, _daContext->height,
80 DADepth));
83 Pixmap
84 DAMakeShape(void)
86 return (XCreatePixmap(DADisplay, DAWindow,
87 _daContext->width, _daContext->height,
88 1));
91 Pixmap
92 DAMakeShapeFromData(char *data, unsigned int width, unsigned int height)
94 return (XCreateBitmapFromData(DADisplay, DAWindow, data,
95 width, height));
98 Pixmap
99 DAMakeShapeFromFile(char *filename)
101 Pixmap shape;
102 unsigned int width, height;
103 int result, x_hot, y_hot;
105 result = XReadBitmapFile(DADisplay, DAWindow, filename, &width, &height,
106 &shape, &x_hot, &y_hot);
108 switch (result) {
109 case BitmapOpenFailed:
110 DAWarning("bitmap file %s cannot be opened.", filename);
111 break;
113 case BitmapFileInvalid:
114 DAWarning("bitmap file %s not valid.", filename);
115 break;
117 case BitmapNoMemory:
118 DAWarning("insufficient memory to open bitmap file %s.",
119 filename);
120 break;
123 return shape;
126 Bool
127 DAMakePixmapFromData(char **data, Pixmap *pixmap, Pixmap *mask,
128 unsigned short *width, unsigned short *height)
130 return _daMakePixmap(daXpmSourceData, data,
131 pixmap, mask,
132 width, height);
135 Bool
136 DAMakePixmapFromFile(char *filename, Pixmap *pixmap, Pixmap *mask,
137 unsigned short *width, unsigned short *height)
139 if (access(filename, R_OK) < 0)
140 return False;
142 return _daMakePixmap(daXpmSourceFile, (char **)filename,
143 pixmap, mask,
144 width, height);
149 Bool
150 _daMakePixmap(daXpmSource source,
151 char **data, Pixmap *pixmap, Pixmap *mask,
152 unsigned short *width, unsigned short *height)
154 XpmAttributes xpmAttr;
156 xpmAttr.valuemask = XpmCloseness;
157 xpmAttr.closeness = 40000;
160 if (source == daXpmSourceData
161 && (XpmCreatePixmapFromData(
162 DADisplay, DAWindow, data, pixmap, mask, &xpmAttr) != 0))
163 return False;
165 else if (source == daXpmSourceFile
166 && (XpmReadFileToPixmap(
167 DADisplay, DAWindow, (char *)data, pixmap, mask, &xpmAttr) != 0))
168 return False;
170 *width = xpmAttr.width;
171 *height = xpmAttr.height;
173 return True;