Debian Codegraph @

pTk/mTk/tixWin/tixWinXpm.c

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