Debian Codegraph @

pTk/mTk/additions/imgWinPmap.c

raw
1 /*
2 *imgWinPmap.c --
3 *
4 * Implement the Windows specific function calls for the pixmap
5 * image type.
6 *
7 * Copyright (c) 1996, Expert Interface Technologies
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 */
13
14 #include <windows.h>
15 #include "tkInt.h"
16 #include "tkWinInt.h"
17 #include <stdlib.h>
18 #include <imgPmap.h>
19 #include "imgInt.h"
20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22
23 typedef struct PixmapData {
24 HDC bitmapDC; /* Bitmap used on Windows platforms */
25 HDC maskDC; /* Mask used on Windows platforms */
26 HBITMAP bitmap, bitmapOld;
27 HBITMAP maskBm, maskBmOld;
28 } PixmapData;
29
30 static void CopyTransparent _ANSI_ARGS_((Display* display,
31 HDC srcDC, Drawable dest,
32 int src_x, int src_y, int width,
33 int height, int dest_x, int dest_y,
34 HDC maskDC));
35
36
37 /*----------------------------------------------------------------------
38 * ImgInitPixmapInstance --
39 *
40 * Initializes the platform-specific data of a pixmap instance
41 *
42 *----------------------------------------------------------------------
43 */
44
45 void
46 ImgInitPixmapInstance(masterPtr, instancePtr)
47 PixmapMaster *masterPtr; /* Pointer to master for image. */
48 PixmapInstance *instancePtr;/* The pixmap instance. */
49 {
50 PixmapData * dataPtr;
51
52 dataPtr = (PixmapData *)ckalloc(sizeof(PixmapData));
53 dataPtr->maskDC = NULL;
54 dataPtr->bitmapDC = NULL;
55
56 instancePtr->clientData = (ClientData)dataPtr;
57 }
58
59
60 /*----------------------------------------------------------------------
61 * ImgXpmAllocTmpBuffer --
62 *
63 * Allocate a temporary space to draw the image.
64 *
65 *----------------------------------------------------------------------
66 */
67 void
68 ImgXpmAllocTmpBuffer(masterPtr, instancePtr, imagePtr, maskPtr)
69 PixmapMaster * masterPtr;
70 PixmapInstance * instancePtr;
71 XImage ** imagePtr;
72 XImage ** maskPtr;
73 {
74 XImage * image = NULL, * mask = NULL;
75 Display *display = Tk_Display(instancePtr->tkwin);
76 int depth;
77
78 depth = Tk_Depth(instancePtr->tkwin);
79
80 instancePtr->pixmap = Tk_GetPixmap(display,
81 Tk_WindowId(instancePtr->tkwin),
82 masterPtr->size[0], masterPtr->size[1], depth);
83
84 mask = (XImage*)ckalloc(sizeof(XImage));
85
86 mask->width = masterPtr->size[0];
87 mask->height = masterPtr->size[1];
88 mask->bytes_per_line = (mask->width+7)/8;
89
90 mask->data =
91 (char *)ckalloc(mask->bytes_per_line * masterPtr->size[1]);
92
93 *imagePtr = image;
94 *maskPtr = mask;
95 }
96
97
98 void
99 ImgXpmFreeTmpBuffer(masterPtr, instancePtr, image, mask)
100 PixmapMaster * masterPtr;
101 PixmapInstance * instancePtr;
102 XImage * image;
103 XImage * mask;
104 {
105 if (image) {
106 ckfree((char*)image->data);
107 image->data = NULL;
108 XDestroyImage(image);
109 }
110 if (mask) {
111 ckfree((char*)mask->data);
112 mask->data = NULL;
113 ckfree((char*)mask);
114 }
115 }
116
117
118 /*----------------------------------------------------------------------
119 * ImgXpmSetPixel --
120 *
121 * Sets the pixel at the given (x,y) coordinate to be the given
122 * color.
123 *----------------------------------------------------------------------
124 */
125 void
126 ImgXpmSetPixel(instancePtr, image, mask, x, y, colorPtr, isTranspPtr)
127 PixmapInstance * instancePtr;
128 XImage * image;
129 XImage * mask;
130 int x;
131 int y;
132 XColor * colorPtr;
133 int * isTranspPtr;
134 {
135 char * p;
136 int n;
137 GC gc;
138 XGCValues gcValues;
139 Display *display = Tk_Display(instancePtr->tkwin);
140
141 if (colorPtr != NULL) {
142 gcValues.foreground = colorPtr->pixel;
143 gc = Tk_GetGC(instancePtr->tkwin, GCForeground, &gcValues);
144 XDrawRectangle(display, instancePtr->pixmap, gc, x, y, 1, 1);
145 Tk_FreeGC(display, gc);
146 }
147
148 p = mask->data;
149 p+= y*(mask->bytes_per_line);
150 p+= x/8;
151 n = x%8;
152
153 if (colorPtr != NULL) {
154 *p |= (1 << (7-n));
155 } else {
156 *p &= ~(1 << (7-n));
157 *isTranspPtr = 1;
158 }
159 }
160
161 /*----------------------------------------------------------------------
162 * ImgXpmRealizePixmap --
163 *
164 * On Unix: Create the pixmap from the buffer.
165 * On Windows: Free the mask if there are no transparent pixels.
166 *----------------------------------------------------------------------
167 */
168 void
169 ImgXpmRealizePixmap(masterPtr, instancePtr, image, mask, isTransp)
170 PixmapMaster * masterPtr;
171 PixmapInstance * instancePtr;
172 XImage * image;
173 XImage * mask;
174 {
175 Display *display = Tk_Display(instancePtr->tkwin);
176 PixmapData *dataPtr = (PixmapData*)instancePtr->clientData;
177 HDC dc, bitmapDC;
178 TkWinDCState dcState;
179 HBITMAP bitmap, bitmapOld;
180 int w, h;
181
182 w = masterPtr->size[0];
183 h = masterPtr->size[1];
184
185 dc = TkWinGetDrawableDC(display, instancePtr->pixmap, &dcState);
186 bitmapDC = CreateCompatibleDC(dc);
187
188 bitmap = CreateCompatibleBitmap(dc, w, h);
189 bitmapOld = SelectObject(bitmapDC, bitmap);
190
191 BitBlt(bitmapDC, 0, 0, w, h, dc, 0, 0, SRCCOPY);
192
193 if (isTransp) {
194 HDC maskDC;
195 HBITMAP maskBm, maskBmOld;
196
197 /*
198 * There are transparent pixels. We need a mask.
199 */
200 maskDC = CreateCompatibleDC(dc);
201 maskBm = CreateBitmap(w, h, 1, 1, (CONST VOID*)mask->data);
202 maskBmOld = SelectObject(maskDC, maskBm);
203
204 BitBlt(bitmapDC, 0, 0, w, h, maskDC, 0, 0, SRCAND);
205 BitBlt(maskDC, 0, 0, w, h, maskDC, 0, 0, NOTSRCCOPY);
206
207 TkWinReleaseDrawableDC(instancePtr->pixmap, dc, &dcState);
208 dataPtr->maskDC = maskDC;
209 dataPtr->maskBm = maskBm;
210 dataPtr->maskBmOld = maskBmOld;
211 } else {
212 dataPtr->maskDC = NULL;
213 }
214 dataPtr->bitmapDC = bitmapDC;
215 dataPtr->bitmap = bitmap;
216 dataPtr->bitmapOld = bitmapOld;
217 }
218
219 void
220 ImgXpmFreeInstanceData(instancePtr, delete)
221 PixmapInstance *instancePtr; /* Pixmap instance. */
222 int delete; /* Should the instance data structure
223 * be deleted as well? */
224 {
225 PixmapData *dataPtr = (PixmapData*)instancePtr->clientData;
226
227 if (dataPtr->maskDC != NULL) {
228 DeleteObject(SelectObject(dataPtr->maskDC,
229 dataPtr->maskBmOld));
230 DeleteDC(dataPtr->maskDC);
231 dataPtr->maskDC = NULL;
232 }
233 if (dataPtr->bitmapDC != NULL) {
234 DeleteObject(SelectObject(dataPtr->bitmapDC,
235 dataPtr->bitmapOld));
236 DeleteDC(dataPtr->bitmapDC);
237 dataPtr->bitmapDC = NULL;
238 }
239 if (delete) {
240 ckfree((char*)dataPtr);
241 instancePtr->clientData = NULL;
242 }
243 }
244
245 void
246 ImgpXpmDisplay(clientData, display, drawable, imageX, imageY, width,
247 height, drawableX, drawableY)
248 ClientData clientData; /* Pointer to PixmapInstance structure for
249 * for instance to be displayed. */
250 Display *display; /* Display on which to draw image. */
251 Drawable drawable; /* Pixmap or window in which to draw image. */
252 int imageX, imageY; /* Upper-left corner of region within image
253 * to draw. */
254 int width, height; /* Dimensions of region within image to draw.*/
255 int drawableX, drawableY; /* Coordinates within drawable that
256 * correspond to imageX and imageY. */
257 {
258 PixmapInstance *instancePtr = (PixmapInstance *) clientData;
259 PixmapData *dataPtr = (PixmapData*)instancePtr->clientData;
260
261 CopyTransparent(display, dataPtr->bitmapDC, drawable,
262 imageX, imageY, width, height,
263 drawableX, drawableY, dataPtr->maskDC);
264 }
265
266 static void
267 CopyTransparent(display, srcDC, dest, src_x, src_y, width, height, dest_x,
268 dest_y, maskDC)
269 Display* display;
270 HDC srcDC;
271 Drawable dest;
272 int src_x;
273 int src_y;
274 int width;
275 int height;
276 int dest_x;
277 int dest_y;
278 HDC maskDC;
279 {
280 HDC destDC;
281 TkWinDCState destState;
282
283 destDC = TkWinGetDrawableDC(display, dest, &destState);
284
285 if (maskDC) {
286 BitBlt(destDC, dest_x, dest_y, width, height, maskDC, src_x, src_y,
287 SRCAND);
288 BitBlt(destDC, dest_x, dest_y, width, height, srcDC, src_x, src_y,
289 SRCPAINT);
290 } else {
291 BitBlt(destDC, dest_x, dest_y, width, height, srcDC, src_x, src_y,
292 SRCCOPY);
293 }
294
295 TkWinReleaseDrawableDC(dest, destDC, &destState);
296 }
297