wmaker: minor fixes for the size of an aperçu
[wmaker-crm.git] / WINGs / wpixmap.c
blob272e56ac919658579740aa23e9c58ea65c5c3b19
2 #include "WINGsP.h"
4 #include <wraster.h>
6 WMPixmap *WMRetainPixmap(WMPixmap * pixmap)
8 if (pixmap)
9 pixmap->refCount++;
11 return pixmap;
14 void WMReleasePixmap(WMPixmap * pixmap)
16 wassertr(pixmap != NULL);
18 pixmap->refCount--;
20 if (pixmap->refCount < 1) {
21 if (pixmap->pixmap)
22 XFreePixmap(pixmap->screen->display, pixmap->pixmap);
23 if (pixmap->mask)
24 XFreePixmap(pixmap->screen->display, pixmap->mask);
25 wfree(pixmap);
29 WMPixmap *WMCreatePixmap(WMScreen * scrPtr, int width, int height, int depth, Bool masked)
31 WMPixmap *pixPtr;
33 pixPtr = wmalloc(sizeof(WMPixmap));
34 pixPtr->screen = scrPtr;
35 pixPtr->width = width;
36 pixPtr->height = height;
37 pixPtr->depth = depth;
38 pixPtr->refCount = 1;
40 pixPtr->pixmap = XCreatePixmap(scrPtr->display, W_DRAWABLE(scrPtr), width, height, depth);
41 if (masked) {
42 pixPtr->mask = XCreatePixmap(scrPtr->display, W_DRAWABLE(scrPtr), width, height, 1);
43 } else {
44 pixPtr->mask = None;
47 return pixPtr;
50 WMPixmap *WMCreatePixmapFromXPixmaps(WMScreen * scrPtr, Pixmap pixmap, Pixmap mask,
51 int width, int height, int depth)
53 WMPixmap *pixPtr;
55 pixPtr = wmalloc(sizeof(WMPixmap));
56 pixPtr->screen = scrPtr;
57 pixPtr->pixmap = pixmap;
58 pixPtr->mask = mask;
59 pixPtr->width = width;
60 pixPtr->height = height;
61 pixPtr->depth = depth;
62 pixPtr->refCount = 1;
64 return pixPtr;
67 WMPixmap *WMCreatePixmapFromFile(WMScreen * scrPtr, const char *fileName)
69 WMPixmap *pixPtr;
70 RImage *image;
72 image = RLoadImage(scrPtr->rcontext, fileName, 0);
73 if (!image)
74 return NULL;
76 pixPtr = WMCreatePixmapFromRImage(scrPtr, image, 127);
78 RReleaseImage(image);
80 return pixPtr;
83 WMPixmap *WMCreatePixmapFromRImage(WMScreen * scrPtr, RImage * image, int threshold)
85 WMPixmap *pixPtr;
86 Pixmap pixmap, mask;
88 if (!RConvertImageMask(scrPtr->rcontext, image, &pixmap, &mask, threshold)) {
89 return NULL;
92 pixPtr = wmalloc(sizeof(WMPixmap));
93 pixPtr->screen = scrPtr;
94 pixPtr->pixmap = pixmap;
95 pixPtr->mask = mask;
96 pixPtr->width = image->width;
97 pixPtr->height = image->height;
98 pixPtr->depth = scrPtr->depth;
99 pixPtr->refCount = 1;
101 return pixPtr;
104 WMPixmap *WMCreateBlendedPixmapFromRImage(WMScreen * scrPtr, RImage * image, const RColor * color)
106 WMPixmap *pixPtr;
107 RImage *copy;
109 copy = RCloneImage(image);
110 if (!copy)
111 return NULL;
113 RCombineImageWithColor(copy, color);
114 pixPtr = WMCreatePixmapFromRImage(scrPtr, copy, 0);
115 RReleaseImage(copy);
117 return pixPtr;
120 WMPixmap *WMCreateBlendedPixmapFromFile(WMScreen * scrPtr, const char *fileName, const RColor * color)
122 return WMCreateScaledBlendedPixmapFromFile(scrPtr, fileName, color, 0, 0);
125 WMPixmap *WMCreateScaledBlendedPixmapFromFile(WMScreen *scrPtr, const char *fileName, const RColor *color,
126 unsigned int width, unsigned int height)
128 WMPixmap *pixPtr;
129 RImage *image;
131 image = RLoadImage(scrPtr->rcontext, fileName, 0);
132 if (!image)
133 return NULL;
135 /* scale it if needed to fit in the specified box */
136 if ((width > 0) && (height > 0) && ((image->width > width) || (image->height > height))) {
137 int new_width, new_height;
138 RImage *new_image;
140 new_width = image->width;
141 new_height = image->height;
142 if (new_width > width) {
143 new_width = width;
144 new_height = width * image->height / image->width;
146 if (new_height > height) {
147 new_width = height * image->width / image->height;
148 new_height = height;
151 new_image = RScaleImage(image, new_width, new_height);
152 RReleaseImage(image);
153 image = new_image;
156 RCombineImageWithColor(image, color);
157 pixPtr = WMCreatePixmapFromRImage(scrPtr, image, 0);
158 RReleaseImage(image);
160 return pixPtr;
163 WMPixmap *WMCreatePixmapFromXPMData(WMScreen * scrPtr, char **data)
165 WMPixmap *pixPtr;
166 RImage *image;
168 image = RGetImageFromXPMData(scrPtr->rcontext, data);
169 if (!image)
170 return NULL;
172 pixPtr = WMCreatePixmapFromRImage(scrPtr, image, 127);
174 RReleaseImage(image);
176 return pixPtr;
179 Pixmap WMGetPixmapXID(WMPixmap * pixmap)
181 wassertrv(pixmap != NULL, None);
183 return pixmap->pixmap;
186 Pixmap WMGetPixmapMaskXID(WMPixmap * pixmap)
188 wassertrv(pixmap != NULL, None);
190 return pixmap->mask;
193 WMSize WMGetPixmapSize(WMPixmap * pixmap)
195 WMSize size = { 0, 0 };
197 wassertrv(pixmap != NULL, size);
199 size.width = pixmap->width;
200 size.height = pixmap->height;
202 return size;
205 WMPixmap *WMGetSystemPixmap(WMScreen * scr, int image)
207 switch (image) {
208 case WSIReturnArrow:
209 return WMRetainPixmap(scr->buttonArrow);
211 case WSIHighlightedReturnArrow:
212 return WMRetainPixmap(scr->pushedButtonArrow);
214 case WSIScrollerDimple:
215 return WMRetainPixmap(scr->scrollerDimple);
217 case WSIArrowLeft:
218 return WMRetainPixmap(scr->leftArrow);
220 case WSIHighlightedArrowLeft:
221 return WMRetainPixmap(scr->hiLeftArrow);
223 case WSIArrowRight:
224 return WMRetainPixmap(scr->rightArrow);
226 case WSIHighlightedArrowRight:
227 return WMRetainPixmap(scr->hiRightArrow);
229 case WSIArrowUp:
230 return WMRetainPixmap(scr->upArrow);
232 case WSIHighlightedArrowUp:
233 return WMRetainPixmap(scr->hiUpArrow);
235 case WSIArrowDown:
236 return WMRetainPixmap(scr->downArrow);
238 case WSIHighlightedArrowDown:
239 return WMRetainPixmap(scr->hiDownArrow);
241 case WSICheckMark:
242 return WMRetainPixmap(scr->checkMark);
244 default:
245 return NULL;
249 void WMDrawPixmap(WMPixmap * pixmap, Drawable d, int x, int y)
251 WMScreen *scr = pixmap->screen;
253 XSetClipMask(scr->display, scr->clipGC, pixmap->mask);
254 XSetClipOrigin(scr->display, scr->clipGC, x, y);
256 XCopyArea(scr->display, pixmap->pixmap, d, scr->clipGC, 0, 0, pixmap->width, pixmap->height, x, y);