Debian Codegraph @

pTk/mTk/win/tkWinDraw.c

raw
1 /*
2 * tkWinDraw.c --
3 *
4 * This file contains the Xlib emulation functions pertaining to
5 * actually drawing objects on a window.
6 *
7 * Copyright (c) 1995 Sun Microsystems, Inc.
8 * Copyright (c) 1994 Software Research Associates, Inc.
9 *
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 *
13 * RCS: @(#) $Id: tkWinDraw.c,v 1.12.2.1 2003/11/11 00:26:33 hobbs Exp $
14 */
15
16 #include "tkWinInt.h"
17
18 /*
19 * These macros convert between X's bizarre angle units to radians.
20 */
21
22 #define PI 3.14159265358979
23 #define XAngleToRadians(a) ((double)(a) / 64 * PI / 180);
24
25 /*
26 * Translation table between X gc functions and Win32 raster op modes.
27 */
28
29 int tkpWinRopModes[] = {
30 R2_BLACK, /* GXclear */
31 R2_MASKPEN, /* GXand */
32 R2_MASKPENNOT, /* GXandReverse */
33 R2_COPYPEN, /* GXcopy */
34 R2_MASKNOTPEN, /* GXandInverted */
35 R2_NOT, /* GXnoop */
36 R2_XORPEN, /* GXxor */
37 R2_MERGEPEN, /* GXor */
38 R2_NOTMERGEPEN, /* GXnor */
39 R2_NOTXORPEN, /* GXequiv */
40 R2_NOT, /* GXinvert */
41 R2_MERGEPENNOT, /* GXorReverse */
42 R2_NOTCOPYPEN, /* GXcopyInverted */
43 R2_MERGENOTPEN, /* GXorInverted */
44 R2_NOTMASKPEN, /* GXnand */
45 R2_WHITE /* GXset */
46 };
47
48 /*
49 * Translation table between X gc functions and Win32 BitBlt op modes. Some
50 * of the operations defined in X don't have names, so we have to construct
51 * new opcodes for those functions. This is arcane and probably not all that
52 * useful, but at least it's accurate.
53 */
54
55 #define NOTSRCAND (DWORD)0x00220326 /* dest = (NOT source) AND dest */
56 #define NOTSRCINVERT (DWORD)0x00990066 /* dest = (NOT source) XOR dest */
57 #define SRCORREVERSE (DWORD)0x00DD0228 /* dest = source OR (NOT dest) */
58 #define SRCNAND (DWORD)0x007700E6 /* dest = NOT (source AND dest) */
59
60 int tkpWinBltModes[] = {
61 BLACKNESS, /* GXclear */
62 SRCAND, /* GXand */
63 SRCERASE, /* GXandReverse */
64 SRCCOPY, /* GXcopy */
65 NOTSRCAND, /* GXandInverted */
66 PATCOPY, /* GXnoop */
67 SRCINVERT, /* GXxor */
68 SRCPAINT, /* GXor */
69 NOTSRCERASE, /* GXnor */
70 NOTSRCINVERT, /* GXequiv */
71 DSTINVERT, /* GXinvert */
72 SRCORREVERSE, /* GXorReverse */
73 NOTSRCCOPY, /* GXcopyInverted */
74 MERGEPAINT, /* GXorInverted */
75 SRCNAND, /* GXnand */
76 WHITENESS /* GXset */
77 };
78
79 /*
80 * The following raster op uses the source bitmap as a mask for the
81 * pattern. This is used to draw in a foreground color but leave the
82 * background color transparent.
83 */
84
85 #define MASKPAT 0x00E20746 /* dest = (src & pat) | (!src & dst) */
86
87 /*
88 * The following two raster ops are used to copy the foreground and background
89 * bits of a source pattern as defined by a stipple used as the pattern.
90 */
91
92 #define COPYFG 0x00CA0749 /* dest = (pat & src) | (!pat & dst) */
93 #define COPYBG 0x00AC0744 /* dest = (!pat & src) | (pat & dst) */
94
95 /*
96 * Macros used later in the file.
97 */
98
99 #define MIN(a,b) ((a>b) ? b : a)
100 #define MAX(a,b) ((a<b) ? b : a)
101
102 /*
103 * The followng typedef is used to pass Windows GDI drawing functions.
104 */
105
106 typedef BOOL (CALLBACK *WinDrawFunc) _ANSI_ARGS_((HDC dc,
107 CONST POINT* points, int npoints));
108
109 typedef struct ThreadSpecificData {
110 POINT *winPoints; /* Array of points that is reused. */
111 int nWinPoints; /* Current size of point array. */
112 } ThreadSpecificData;
113 static Tcl_ThreadDataKey dataKey;
114
115 /*
116 * Forward declarations for procedures defined in this file:
117 */
118
119 static POINT * ConvertPoints _ANSI_ARGS_((XPoint *points, int npoints,
120 int mode, RECT *bbox));
121 static void DrawOrFillArc _ANSI_ARGS_((Display *display,
122 Drawable d, GC gc, int x, int y,
123 unsigned int width, unsigned int height,
124 int start, int extent, int fill));
125 static void RenderObject _ANSI_ARGS_((HDC dc, GC gc,
126 XPoint* points, int npoints, int mode, HPEN pen,
127 WinDrawFunc func));
128 static HPEN SetUpGraphicsPort _ANSI_ARGS_((GC gc));
129
130 /*
131 *----------------------------------------------------------------------
132 *
133 * TkWinGetDrawableDC --
134 *
135 * Retrieve the DC from a drawable.
136 *
137 * Results:
138 * Returns the window DC for windows. Returns a new memory DC
139 * for pixmaps.
140 *
141 * Side effects:
142 * Sets up the palette for the device context, and saves the old
143 * device context state in the passed in TkWinDCState structure.
144 *
145 *----------------------------------------------------------------------
146 */
147
148 HDC
149 TkWinGetDrawableDC(display, d, state)
150 Display *display;
151 Drawable d;
152 TkWinDCState* state;
153 {
154 HDC dc;
155 TkWinDrawable *twdPtr = (TkWinDrawable *)d;
156 Colormap cmap;
157
158 if (twdPtr->type == TWD_WINDOW) {
159 TkWindow *winPtr = twdPtr->window.winPtr;
160
161 dc = GetDC(twdPtr->window.handle);
162 if (winPtr == NULL) {
163 cmap = DefaultColormap(display, DefaultScreen(display));
164 } else {
165 cmap = winPtr->atts.colormap;
166 }
167 } else if (twdPtr->type == TWD_WINDC) {
168 dc = twdPtr->winDC.hdc;
169 cmap = DefaultColormap(display, DefaultScreen(display));
170 } else {
171 dc = CreateCompatibleDC(NULL);
172 SelectObject(dc, twdPtr->bitmap.handle);
173 cmap = twdPtr->bitmap.colormap;
174 }
175 state->palette = TkWinSelectPalette(dc, cmap);
176 state->bkmode = GetBkMode(dc);
177 LangNoteDC(dc,1);
178 return dc;
179 }
180
181 /*
182 *----------------------------------------------------------------------
183 *
184 * TkWinReleaseDrawableDC --
185 *
186 * Frees the resources associated with a drawable's DC.
187 *
188 * Results:
189 * None.
190 *
191 * Side effects:
192 * Restores the old bitmap handle to the memory DC for pixmaps.
193 *
194 *----------------------------------------------------------------------
195 */
196
197 void
198 TkWinReleaseDrawableDC(d, dc, state)
199 Drawable d;
200 HDC dc;
201 TkWinDCState *state;
202 {
203 TkWinDrawable *twdPtr = (TkWinDrawable *)d;
204 SetBkMode(dc, state->bkmode);
205 SelectPalette(dc, state->palette, TRUE);
206 RealizePalette(dc);
207 if (twdPtr->type == TWD_WINDOW) {
208 ReleaseDC(TkWinGetHWND(d), dc);
209 } else if (twdPtr->type == TWD_BITMAP) {
210 DeleteDC(dc);
211 }
212 LangNoteDC(dc,-1);
213 }
214
215 /*
216 *----------------------------------------------------------------------
217 *
218 * ConvertPoints --
219 *
220 * Convert an array of X points to an array of Win32 points.
221 *
222 * Results:
223 * Returns the converted array of POINTs.
224 *
225 * Side effects:
226 * Allocates a block of memory in thread local storage that
227 * should not be freed.
228 *
229 *----------------------------------------------------------------------
230 */
231
232 static POINT *
233 ConvertPoints(points, npoints, mode, bbox)
234 XPoint *points;
235 int npoints;
236 int mode; /* CoordModeOrigin or CoordModePrevious. */
237 RECT *bbox; /* Bounding box of points. */
238 {
239 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
240 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
241 int i;
242
243 /*
244 * To avoid paying the cost of a malloc on every drawing routine,
245 * we reuse the last array if it is large enough.
246 */
247
248 if (npoints > tsdPtr->nWinPoints) {
249 if (tsdPtr->winPoints != NULL) {
250 ckfree((char *) tsdPtr->winPoints);
251 }
252 tsdPtr->winPoints = (POINT *) ckalloc(sizeof(POINT) * npoints);
253 if (tsdPtr->winPoints == NULL) {
254 tsdPtr->nWinPoints = -1;
255 return NULL;
256 }
257 tsdPtr->nWinPoints = npoints;
258 }
259
260 bbox->left = bbox->right = points[0].x;
261 bbox->top = bbox->bottom = points[0].y;
262
263 if (mode == CoordModeOrigin) {
264 for (i = 0; i < npoints; i++) {
265 tsdPtr->winPoints[i].x = points[i].x;
266 tsdPtr->winPoints[i].y = points[i].y;
267 bbox->left = MIN(bbox->left, tsdPtr->winPoints[i].x);
268 bbox->right = MAX(bbox->right, tsdPtr->winPoints[i].x);
269 bbox->top = MIN(bbox->top, tsdPtr->winPoints[i].y);
270 bbox->bottom = MAX(bbox->bottom, tsdPtr->winPoints[i].y);
271 }
272 } else {
273 tsdPtr->winPoints[0].x = points[0].x;
274 tsdPtr->winPoints[0].y = points[0].y;
275 for (i = 1; i < npoints; i++) {
276 tsdPtr->winPoints[i].x = tsdPtr->winPoints[i-1].x + points[i].x;
277 tsdPtr->winPoints[i].y = tsdPtr->winPoints[i-1].y + points[i].y;
278 bbox->left = MIN(bbox->left, tsdPtr->winPoints[i].x);
279 bbox->right = MAX(bbox->right, tsdPtr->winPoints[i].x);
280 bbox->top = MIN(bbox->top, tsdPtr->winPoints[i].y);
281 bbox->bottom = MAX(bbox->bottom, tsdPtr->winPoints[i].y);
282 }
283 }
284 return tsdPtr->winPoints;
285 }
286
287 /*
288 *----------------------------------------------------------------------
289 *
290 * XCopyArea --
291 *
292 * Copies data from one drawable to another using block transfer
293 * routines.
294 *
295 * Results:
296 * None.
297 *
298 * Side effects:
299 * Data is moved from a window or bitmap to a second window or
300 * bitmap.
301 *
302 *----------------------------------------------------------------------
303 */
304
305 void
306 XCopyArea(display, src, dest, gc, src_x, src_y, width, height, dest_x, dest_y)
307 Display* display;
308 Drawable src;
309 Drawable dest;
310 GC gc;
311 int src_x, src_y;
312 unsigned int width, height;
313 int dest_x, dest_y;
314 {
315 HDC srcDC, destDC;
316 TkWinDCState srcState, destState;
317 TkpClipMask *clipPtr = (TkpClipMask*)gc->clip_mask;
318
319 srcDC = TkWinGetDrawableDC(display, src, &srcState);
320
321 if (src != dest) {
322 destDC = TkWinGetDrawableDC(display, dest, &destState);
323 } else {
324 destDC = srcDC;
325 }
326
327 if (clipPtr && clipPtr->type == TKP_CLIP_REGION) {
328 SelectClipRgn(destDC, (HRGN) clipPtr->value.region);
329 OffsetClipRgn(destDC, gc->clip_x_origin, gc->clip_y_origin);
330 }
331
332 BitBlt(destDC, dest_x, dest_y, width, height, srcDC, src_x, src_y,
333 tkpWinBltModes[gc->function]);
334
335 SelectClipRgn(destDC, NULL);
336
337 if (src != dest) {
338 TkWinReleaseDrawableDC(dest, destDC, &destState);
339 }
340 TkWinReleaseDrawableDC(src, srcDC, &srcState);
341 }
342
343 /*
344 *----------------------------------------------------------------------
345 *
346 * XCopyPlane --
347 *
348 * Copies a bitmap from a source drawable to a destination
349 * drawable. The plane argument specifies which bit plane of
350 * the source contains the bitmap. Note that this implementation
351 * ignores the gc->function.
352 *
353 * Results:
354 * None.
355 *
356 * Side effects:
357 * Changes the destination drawable.
358 *
359 *----------------------------------------------------------------------
360 */
361
362 void
363 XCopyPlane(display, src, dest, gc, src_x, src_y, width, height, dest_x,
364 dest_y, plane)
365 Display* display;
366 Drawable src;
367 Drawable dest;
368 GC gc;
369 int src_x, src_y;
370 unsigned int width, height;
371 int dest_x, dest_y;
372 unsigned long plane;
373 {
374 HDC srcDC, destDC;
375 TkWinDCState srcState, destState;
376 HBRUSH bgBrush, fgBrush, oldBrush;
377 TkpClipMask *clipPtr = (TkpClipMask*)gc->clip_mask;
378
379 display->request++;
380
381 if (plane != 1) {
382 panic("Unexpected plane specified for XCopyPlane");
383 }
384
385 srcDC = TkWinGetDrawableDC(display, src, &srcState);
386
387 if (src != dest) {
388 destDC = TkWinGetDrawableDC(display, dest, &destState);
389 } else {
390 destDC = srcDC;
391 }
392
393 if (clipPtr == NULL || clipPtr->type == TKP_CLIP_REGION) {
394
395 /*
396 * Case 1: opaque bitmaps. Windows handles the conversion
397 * from one bit to multiple bits by setting 0 to the
398 * foreground color, and 1 to the background color (seems
399 * backwards, but there you are).
400 */
401
402 if (clipPtr && clipPtr->type == TKP_CLIP_REGION) {
403 SelectClipRgn(destDC, (HRGN) clipPtr->value.region);
404 OffsetClipRgn(destDC, gc->clip_x_origin, gc->clip_y_origin);
405 }
406
407 SetBkMode(destDC, OPAQUE);
408 SetBkColor(destDC, gc->foreground);
409 SetTextColor(destDC, gc->background);
410 BitBlt(destDC, dest_x, dest_y, width, height, srcDC, src_x, src_y,
411 SRCCOPY);
412
413 SelectClipRgn(destDC, NULL);
414 } else if (clipPtr->type == TKP_CLIP_PIXMAP) {
415 if (clipPtr->value.pixmap == src) {
416
417 /*
418 * Case 2: transparent bitmaps are handled by setting the
419 * destination to the foreground color whenever the source
420 * pixel is set.
421 */
422
423 fgBrush = CreateSolidBrush(gc->foreground);
424 oldBrush = SelectObject(destDC, fgBrush);
425 BitBlt(destDC, dest_x, dest_y, width, height, srcDC, src_x, src_y,
426 MASKPAT);
427 SelectObject(destDC, oldBrush);
428 DeleteObject(fgBrush);
429 } else {
430
431 /*
432 * Case 3: two arbitrary bitmaps. Copy the source rectangle
433 * into a color pixmap. Use the result as a brush when
434 * copying the clip mask into the destination.
435 */
436
437 HDC memDC, maskDC;
438 HBITMAP bitmap;
439 TkWinDCState maskState;
440
441 fgBrush = CreateSolidBrush(gc->foreground);
442 bgBrush = CreateSolidBrush(gc->background);
443 maskDC = TkWinGetDrawableDC(display, clipPtr->value.pixmap,
444 &maskState);
445 memDC = CreateCompatibleDC(destDC);
446 bitmap = CreateBitmap(width, height, 1, 1, NULL);
447 SelectObject(memDC, bitmap);
448
449 /*
450 * Set foreground bits. We create a new bitmap containing
451 * (source AND mask), then use it to set the foreground color
452 * into the destination.
453 */
454
455 BitBlt(memDC, 0, 0, width, height, srcDC, src_x, src_y, SRCCOPY);
456 BitBlt(memDC, 0, 0, width, height, maskDC,
457 dest_x - gc->clip_x_origin, dest_y - gc->clip_y_origin,
458 SRCAND);
459 oldBrush = SelectObject(destDC, fgBrush);
460 BitBlt(destDC, dest_x, dest_y, width, height, memDC, 0, 0,
461 MASKPAT);
462
463 /*
464 * Set background bits. Same as foreground, except we use
465 * ((NOT source) AND mask) and the background brush.
466 */
467
468 BitBlt(memDC, 0, 0, width, height, srcDC, src_x, src_y,
469 NOTSRCCOPY);
470 BitBlt(memDC, 0, 0, width, height, maskDC,
471 dest_x - gc->clip_x_origin, dest_y - gc->clip_y_origin,
472 SRCAND);
473 SelectObject(destDC, bgBrush);
474 BitBlt(destDC, dest_x, dest_y, width, height, memDC, 0, 0,
475 MASKPAT);
476
477 TkWinReleaseDrawableDC(clipPtr->value.pixmap, maskDC, &maskState);
478 SelectObject(destDC, oldBrush);
479 DeleteDC(memDC);
480 DeleteObject(bitmap);
481 DeleteObject(fgBrush);
482 DeleteObject(bgBrush);
483 }
484 }
485 if (src != dest) {
486 TkWinReleaseDrawableDC(dest, destDC, &destState);
487 }
488 TkWinReleaseDrawableDC(src, srcDC, &srcState);
489 }
490
491 /*
492 *----------------------------------------------------------------------
493 *
494 * TkPutImage --
495 *
496 * Copies a subimage from an in-memory image to a rectangle of
497 * of the specified drawable.
498 *
499 * Results:
500 * None.
501 *
502 * Side effects:
503 * Draws the image on the specified drawable.
504 *
505 *----------------------------------------------------------------------
506 */
507
508 void
509 TkPutImage(colors, ncolors, display, d, gc, image, src_x, src_y, dest_x,
510 dest_y, width, height)
511 unsigned long *colors; /* Array of pixel values used by this
512 * image. May be NULL. */
513 int ncolors; /* Number of colors used, or 0. */
514 Display* display;
515 Drawable d; /* Destination drawable. */
516 GC gc;
517 XImage* image; /* Source image. */
518 int src_x, src_y; /* Offset of subimage. */
519 int dest_x, dest_y; /* Position of subimage origin in
520 * drawable. */
521 unsigned int width, height; /* Dimensions of subimage. */
522 {
523 HDC dc, dcMem;
524 TkWinDCState state;
525 BITMAPINFO *infoPtr;
526 HBITMAP bitmap;
527 char *data;
528
529 display->request++;
530
531 dc = TkWinGetDrawableDC(display, d, &state);
532 SetROP2(dc, tkpWinRopModes[gc->function]);
533 dcMem = CreateCompatibleDC(dc);
534
535 if (image->bits_per_pixel == 1) {
536 /*
537 * If the image isn't in the right format, we have to copy
538 * it into a new buffer in MSBFirst and word-aligned format.
539 */
540
541 if ((image->bitmap_bit_order != MSBFirst)
542 || (image->bitmap_pad != sizeof(WORD))) {
543 data = TkAlignImageData(image, sizeof(WORD), MSBFirst);
544 bitmap = CreateBitmap(image->width, image->height, 1, 1, data);
545 ckfree(data);
546 } else {
547 bitmap = CreateBitmap(image->width, image->height, 1, 1,
548 image->data);
549 }
550 SetTextColor(dc, gc->foreground);
551 SetBkColor(dc, gc->background);
552 } else {
553 int i, usePalette;
554
555 /*
556 * Do not use a palette for TrueColor images.
557 */
558
559 usePalette = (image->bits_per_pixel < 16);
560
561 if (usePalette) {
562 infoPtr = (BITMAPINFO*) ckalloc(sizeof(BITMAPINFOHEADER)
563 + sizeof(RGBQUAD)*ncolors);
564 } else {
565 infoPtr = (BITMAPINFO*) ckalloc(sizeof(BITMAPINFOHEADER));
566 }
567
568 infoPtr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
569 infoPtr->bmiHeader.biWidth = image->width;
570 infoPtr->bmiHeader.biHeight = -image->height; /* Top-down order */
571 infoPtr->bmiHeader.biPlanes = 1;
572 infoPtr->bmiHeader.biBitCount = image->bits_per_pixel;
573 infoPtr->bmiHeader.biCompression = BI_RGB;
574 infoPtr->bmiHeader.biSizeImage = 0;
575 infoPtr->bmiHeader.biXPelsPerMeter = 0;
576 infoPtr->bmiHeader.biYPelsPerMeter = 0;
577 infoPtr->bmiHeader.biClrImportant = 0;
578
579 if (usePalette) {
580 infoPtr->bmiHeader.biClrUsed = ncolors;
581 for (i = 0; i < ncolors; i++) {
582 infoPtr->bmiColors[i].rgbBlue = GetBValue(colors[i]);
583 infoPtr->bmiColors[i].rgbGreen = GetGValue(colors[i]);
584 infoPtr->bmiColors[i].rgbRed = GetRValue(colors[i]);
585 infoPtr->bmiColors[i].rgbReserved = 0;
586 }
587 } else {
588 infoPtr->bmiHeader.biClrUsed = 0;
589 }
590 bitmap = CreateDIBitmap(dc, &infoPtr->bmiHeader, CBM_INIT,
591 image->data, infoPtr, DIB_RGB_COLORS);
592 ckfree((char *) infoPtr);
593 }
594 if(!bitmap) {
595 panic("Fail to allocate bitmap\n");
596 DeleteDC(dcMem);
597 TkWinReleaseDrawableDC(d, dc, &state);
598 return;
599 }
600 bitmap = SelectObject(dcMem, bitmap);
601 BitBlt(dc, dest_x, dest_y, width, height, dcMem, src_x, src_y, SRCCOPY);
602 DeleteObject(SelectObject(dcMem, bitmap));
603 DeleteDC(dcMem);
604 TkWinReleaseDrawableDC(d, dc, &state);
605 }
606
607 /*
608 *----------------------------------------------------------------------
609 *
610 * XFillRectangles --
611 *
612 * Fill multiple rectangular areas in the given drawable.
613 *
614 * Results:
615 * None.
616 *
617 * Side effects:
618 * Draws onto the specified drawable.
619 *
620 *----------------------------------------------------------------------
621 */
622
623 void
624 XFillRectangles(display, d, gc, rectangles, nrectangles)
625 Display* display;
626 Drawable d;
627 GC gc;
628 XRectangle* rectangles;
629 int nrectangles;
630 {
631 HDC dc;
632 int i;
633 RECT rect;
634 TkWinDCState state;
635 HBRUSH brush, oldBrush;
636
637 if (d == None) {
638 return;
639 }
640
641 dc = TkWinGetDrawableDC(display, d, &state);
642 SetROP2(dc, tkpWinRopModes[gc->function]);
643 brush = CreateSolidBrush(gc->foreground);
644
645 if ((gc->fill_style == FillStippled
646 || gc->fill_style == FillOpaqueStippled)
647 && gc->stipple != None) {
648 TkWinDrawable *twdPtr = (TkWinDrawable *)gc->stipple;
649 HBRUSH stipple;
650 HBITMAP oldBitmap, bitmap;
651 HDC dcMem;
652 HBRUSH bgBrush = CreateSolidBrush(gc->background);
653
654 if (twdPtr->type != TWD_BITMAP) {
655 panic("unexpected drawable type in stipple");
656 }
657
658 /*
659 * Select stipple pattern into destination dc.
660 */
661
662 stipple = CreatePatternBrush(twdPtr->bitmap.handle);
663 SetBrushOrgEx(dc, gc->ts_x_origin, gc->ts_y_origin, NULL);
664 oldBrush = SelectObject(dc, stipple);
665 dcMem = CreateCompatibleDC(dc);
666
667 /*
668 * For each rectangle, create a drawing surface which is the size of
669 * the rectangle and fill it with the background color. Then merge the
670 * result with the stipple pattern.
671 */
672
673 for (i = 0; i < nrectangles; i++) {
674 bitmap = CreateCompatibleBitmap(dc, rectangles[i].width,
675 rectangles[i].height);
676 oldBitmap = SelectObject(dcMem, bitmap);
677 rect.left = 0;
678 rect.top = 0;
679 rect.right = rectangles[i].width;
680 rect.bottom = rectangles[i].height;
681 FillRect(dcMem, &rect, brush);
682 BitBlt(dc, rectangles[i].x, rectangles[i].y, rectangles[i].width,
683 rectangles[i].height, dcMem, 0, 0, COPYFG);
684 if (gc->fill_style == FillOpaqueStippled) {
685 FillRect(dcMem, &rect, bgBrush);
686 BitBlt(dc, rectangles[i].x, rectangles[i].y,
687 rectangles[i].width, rectangles[i].height, dcMem,
688 0, 0, COPYBG);
689 }
690 SelectObject(dcMem, oldBitmap);
691 DeleteObject(bitmap);
692 }
693
694 DeleteDC(dcMem);
695 SelectObject(dc, oldBrush);
696 DeleteObject(stipple);
697 DeleteObject(bgBrush);
698 } else {
699 if (gc->function == GXcopy) {
700 for (i = 0; i < nrectangles; i++) {
701 rect.left = rectangles[i].x;
702 rect.right = rect.left + rectangles[i].width;
703 rect.top = rectangles[i].y;
704 rect.bottom = rect.top + rectangles[i].height;
705 FillRect(dc, &rect, brush);
706 }
707 } else {
708 HPEN newPen = CreatePen(PS_NULL, 0, gc->foreground);
709 HPEN oldPen = SelectObject(dc, newPen);
710 oldBrush = SelectObject(dc, brush);
711
712 for (i = 0; i < nrectangles; i++) {
713 Rectangle(dc, rectangles[i].x, rectangles[i].y,
714 rectangles[i].x + rectangles[i].width + 1,
715 rectangles[i].y + rectangles[i].height + 1);
716 }
717
718 SelectObject(dc, oldBrush);
719 SelectObject(dc, oldPen);
720 DeleteObject(newPen);
721 }
722 }
723 DeleteObject(brush);
724 TkWinReleaseDrawableDC(d, dc, &state);
725 }
726
727 /*
728 *----------------------------------------------------------------------
729 *
730 * RenderObject --
731 *
732 * This function draws a shape using a list of points, a
733 * stipple pattern, and the specified drawing function.
734 *
735 * Results:
736 * None.
737 *
738 * Side effects:
739 * None.
740 *
741 *----------------------------------------------------------------------
742 */
743
744 static void
745 RenderObject(dc, gc, points, npoints, mode, pen, func)
746 HDC dc;
747 GC gc;
748 XPoint* points;
749 int npoints;
750 int mode;
751 HPEN pen;
752 WinDrawFunc func;
753 {
754 RECT rect;
755 HPEN oldPen;
756 HBRUSH oldBrush;
757 POINT *winPoints = ConvertPoints(points, npoints, mode, &rect);
758
759 if ((gc->fill_style == FillStippled
760 || gc->fill_style == FillOpaqueStippled)
761 && gc->stipple != None) {
762
763 TkWinDrawable *twdPtr = (TkWinDrawable *)gc->stipple;
764 HDC dcMem;
765 LONG width, height;
766 HBITMAP oldBitmap;
767 int i;
768 HBRUSH oldMemBrush;
769
770 if (twdPtr->type != TWD_BITMAP) {
771 panic("unexpected drawable type in stipple");
772 }
773
774 /*
775 * Grow the bounding box enough to account for line width.
776 */
777
778 rect.left -= gc->line_width;
779 rect.top -= gc->line_width;
780 rect.right += gc->line_width;
781 rect.bottom += gc->line_width;
782
783 width = rect.right - rect.left;
784 height = rect.bottom - rect.top;
785
786 /*
787 * Select stipple pattern into destination dc.
788 */
789
790 SetBrushOrgEx(dc, gc->ts_x_origin, gc->ts_y_origin, NULL);
791 oldBrush = SelectObject(dc, CreatePatternBrush(twdPtr->bitmap.handle));
792
793 /*
794 * Create temporary drawing surface containing a copy of the
795 * destination equal in size to the bounding box of the object.
796 */
797
798 dcMem = CreateCompatibleDC(dc);
799 oldBitmap = SelectObject(dcMem, CreateCompatibleBitmap(dc, width,
800 height));
801 oldPen = SelectObject(dcMem, pen);
802 BitBlt(dcMem, 0, 0, width, height, dc, rect.left, rect.top, SRCCOPY);
803
804 /*
805 * Translate the object for rendering in the temporary drawing
806 * surface.
807 */
808
809 for (i = 0; i < npoints; i++) {
810 winPoints[i].x -= rect.left;
811 winPoints[i].y -= rect.top;
812 }
813
814 /*
815 * Draw the object in the foreground color and copy it to the
816 * destination wherever the pattern is set.
817 */
818
819 SetPolyFillMode(dcMem, (gc->fill_rule == EvenOddRule) ? ALTERNATE
820 : WINDING);
821 oldMemBrush = SelectObject(dcMem, CreateSolidBrush(gc->foreground));
822 (*func)(dcMem, winPoints, npoints);
823 BitBlt(dc, rect.left, rect.top, width, height, dcMem, 0, 0, COPYFG);
824
825 /*
826 * If we are rendering an opaque stipple, then draw the polygon in the
827 * background color and copy it to the destination wherever the pattern
828 * is clear.
829 */
830
831 if (gc->fill_style == FillOpaqueStippled) {
832 DeleteObject(SelectObject(dcMem,
833 CreateSolidBrush(gc->background)));
834 (*func)(dcMem, winPoints, npoints);
835 BitBlt(dc, rect.left, rect.top, width, height, dcMem, 0, 0,
836 COPYBG);
837 }
838
839 SelectObject(dcMem, oldPen);
840 DeleteObject(SelectObject(dcMem, oldMemBrush));
841 DeleteObject(SelectObject(dcMem, oldBitmap));
842 DeleteDC(dcMem);
843 } else {
844 oldPen = SelectObject(dc, pen);
845 oldBrush = SelectObject(dc, CreateSolidBrush(gc->foreground));
846 SetROP2(dc, tkpWinRopModes[gc->function]);
847
848 SetPolyFillMode(dc, (gc->fill_rule == EvenOddRule) ? ALTERNATE
849 : WINDING);
850
851 (*func)(dc, winPoints, npoints);
852
853 SelectObject(dc, oldPen);
854 }
855 DeleteObject(SelectObject(dc, oldBrush));
856 }
857
858 /*
859 *----------------------------------------------------------------------
860 *
861 * XDrawLines --
862 *
863 * Draw connected lines.
864 *
865 * Results:
866 * None.
867 *
868 * Side effects:
869 * Renders a series of connected lines.
870 *
871 *----------------------------------------------------------------------
872 */
873
874 void
875 XDrawLines(display, d, gc, points, npoints, mode)
876 Display* display;
877 Drawable d;
878 GC gc;
879 XPoint* points;
880 int npoints;
881 int mode;
882 {
883 HPEN pen;
884 TkWinDCState state;
885 HDC dc;
886
887 if (d == None) {
888 return;
889 }
890
891 dc = TkWinGetDrawableDC(display, d, &state);
892
893 pen = SetUpGraphicsPort(gc);
894 SetBkMode(dc, TRANSPARENT);
895 RenderObject(dc, gc, points, npoints, mode, pen, Polyline);
896 DeleteObject(pen);
897
898 TkWinReleaseDrawableDC(d, dc, &state);
899 }
900
901
902 void XDrawPoints(display, d, gc, points, npoints, mode)
903 Display* display;
904 Drawable d;
905 GC gc;
906 XPoint* points;
907 int npoints;
908 int mode;
909 {
910 int i;
911
912 for (i=0; i<npoints; i++) {
913 XDrawLine(display, d, gc, points[i].x, points[i].y,
914 points[i].x, points[i].y);
915 }
916 }
917
918 /*
919 *----------------------------------------------------------------------
920 *
921 * XFillPolygon --
922 *
923 * Draws a filled polygon.
924 *
925 * Results:
926 * None.
927 *
928 * Side effects:
929 * Draws a filled polygon on the specified drawable.
930 *
931 *----------------------------------------------------------------------
932 */
933
934 void
935 XFillPolygon(display, d, gc, points, npoints, shape, mode)
936 Display* display;
937 Drawable d;
938 GC gc;
939 XPoint* points;
940 int npoints;
941 int shape;
942 int mode;
943 {
944 HPEN pen;
945 TkWinDCState state;
946 HDC dc;
947
948 if (d == None) {
949 return;
950 }
951
952 dc = TkWinGetDrawableDC(display, d, &state);
953
954 pen = GetStockObject(NULL_PEN);
955 RenderObject(dc, gc, points, npoints, mode, pen, Polygon);
956
957 TkWinReleaseDrawableDC(d, dc, &state);
958 }
959
960 /*
961 *----------------------------------------------------------------------
962 *
963 * XDrawRectangle --
964 *
965 * Draws a rectangle.
966 *
967 * Results:
968 * None.
969 *
970 * Side effects:
971 * Draws a rectangle on the specified drawable.
972 *
973 *----------------------------------------------------------------------
974 */
975
976 void
977 XDrawRectangle(display, d, gc, x, y, width, height)
978 Display* display;
979 Drawable d;
980 GC gc;
981 int x;
982 int y;
983 unsigned int width;
984 unsigned int height;
985 {
986 HPEN pen, oldPen;
987 TkWinDCState state;
988 HBRUSH oldBrush;
989 HDC dc;
990
991 if (d == None) {
992 return;
993 }
994
995 dc = TkWinGetDrawableDC(display, d, &state);
996
997 pen = SetUpGraphicsPort(gc);
998 SetBkMode(dc, TRANSPARENT);
999 oldPen = SelectObject(dc, pen);
1000 oldBrush = SelectObject(dc, GetStockObject(NULL_BRUSH));
1001 SetROP2(dc, tkpWinRopModes[gc->function]);
1002
1003 Rectangle(dc, x, y, x+width+1, y+height+1);
1004
1005 DeleteObject(SelectObject(dc, oldPen));
1006 SelectObject(dc, oldBrush);
1007 TkWinReleaseDrawableDC(d, dc, &state);
1008 }
1009
1010 /*
1011 *----------------------------------------------------------------------
1012 *
1013 * XDrawArc --
1014 *
1015 * Draw an arc.
1016 *
1017 * Results:
1018 * None.
1019 *
1020 * Side effects:
1021 * Draws an arc on the specified drawable.
1022 *
1023 *----------------------------------------------------------------------
1024 */
1025
1026 void
1027 XDrawArc(display, d, gc, x, y, width, height, start, extent)
1028 Display* display;
1029 Drawable d;
1030 GC gc;
1031 int x;
1032 int y;
1033 unsigned int width;
1034 unsigned int height;
1035 int start;
1036 int extent;
1037 {
1038 display->request++;
1039
1040 DrawOrFillArc(display, d, gc, x, y, width, height, start, extent, 0);
1041 }
1042
1043 /*
1044 *----------------------------------------------------------------------
1045 *
1046 * XFillArc --
1047 *
1048 * Draw a filled arc.
1049 *
1050 * Results:
1051 * None.
1052 *
1053 * Side effects:
1054 * Draws a filled arc on the specified drawable.
1055 *
1056 *----------------------------------------------------------------------
1057 */
1058
1059 void
1060 XFillArc(display, d, gc, x, y, width, height, start, extent)
1061 Display* display;
1062 Drawable d;
1063 GC gc;
1064 int x;
1065 int y;
1066 unsigned int width;
1067 unsigned int height;
1068 int start;
1069 int extent;
1070 {
1071 display->request++;
1072
1073 DrawOrFillArc(display, d, gc, x, y, width, height, start, extent, 1);
1074 }
1075
1076 /*
1077 *----------------------------------------------------------------------
1078 *
1079 * DrawOrFillArc --
1080 *
1081 * This procedure handles the rendering of drawn or filled
1082 * arcs and chords.
1083 *
1084 * Results:
1085 * None.
1086 *
1087 * Side effects:
1088 * Renders the requested arc.
1089 *
1090 *----------------------------------------------------------------------
1091 */
1092
1093 static void
1094 DrawOrFillArc(display, d, gc, x, y, width, height, start, extent, fill)
1095 Display *display;
1096 Drawable d;
1097 GC gc;
1098 int x, y; /* left top */
1099 unsigned int width, height;
1100 int start; /* start: three-o'clock (deg*64) */
1101 int extent; /* extent: relative (deg*64) */
1102 int fill; /* ==0 draw, !=0 fill */
1103 {
1104 HDC dc;
1105 HBRUSH brush, oldBrush;
1106 HPEN pen, oldPen;
1107 TkWinDCState state;
1108 int clockwise = (extent < 0); /* non-zero if clockwise */
1109 int xstart, ystart, xend, yend;
1110 double radian_start, radian_end, xr, yr;
1111
1112 if (d == None) {
1113 return;
1114 }
1115
1116 dc = TkWinGetDrawableDC(display, d, &state);
1117
1118 SetROP2(dc, tkpWinRopModes[gc->function]);
1119
1120 /*
1121 * Compute the absolute starting and ending angles in normalized radians.
1122 * Swap the start and end if drawing clockwise.
1123 */
1124
1125 start = start % (64*360);
1126 if (start < 0) {
1127 start += (64*360);
1128 }
1129 extent = (start+extent) % (64*360);
1130 if (extent < 0) {
1131 extent += (64*360);
1132 }
1133 if (clockwise) {
1134 int tmp = start;
1135 start = extent;
1136 extent = tmp;
1137 }
1138 radian_start = XAngleToRadians(start);
1139 radian_end = XAngleToRadians(extent);
1140
1141 /*
1142 * Now compute points on the radial lines that define the starting and
1143 * ending angles. Be sure to take into account that the y-coordinate
1144 * system is inverted.
1145 */
1146
1147 xr = x + width / 2.0;
1148 yr = y + height / 2.0;
1149 xstart = (int)((xr + cos(radian_start)*width/2.0) + 0.5);
1150 ystart = (int)((yr + sin(-radian_start)*height/2.0) + 0.5);
1151 xend = (int)((xr + cos(radian_end)*width/2.0) + 0.5);
1152 yend = (int)((yr + sin(-radian_end)*height/2.0) + 0.5);
1153
1154 /*
1155 * Now draw a filled or open figure. Note that we have to
1156 * increase the size of the bounding box by one to account for the
1157 * difference in pixel definitions between X and Windows.
1158 */
1159
1160 pen = SetUpGraphicsPort(gc);
1161 oldPen = SelectObject(dc, pen);
1162 if (!fill) {
1163 /*
1164 * Note that this call will leave a gap of one pixel at the
1165 * end of the arc for thin arcs. We can't use ArcTo because
1166 * it's only supported under Windows NT.
1167 */
1168
1169 SetBkMode(dc, TRANSPARENT);
1170 Arc(dc, x, y, x+width+1, y+height+1, xstart, ystart, xend, yend);
1171 } else {
1172 brush = CreateSolidBrush(gc->foreground);
1173 oldBrush = SelectObject(dc, brush);
1174 if (gc->arc_mode == ArcChord) {
1175 Chord(dc, x, y, x+width+1, y+height+1, xstart, ystart, xend, yend);
1176 } else if ( gc->arc_mode == ArcPieSlice ) {
1177 Pie(dc, x, y, x+width+1, y+height+1, xstart, ystart, xend, yend);
1178 }
1179 DeleteObject(SelectObject(dc, oldBrush));
1180 }
1181 DeleteObject(SelectObject(dc, oldPen));
1182 TkWinReleaseDrawableDC(d, dc, &state);
1183 }
1184
1185 /*
1186 *----------------------------------------------------------------------
1187 *
1188 * SetUpGraphicsPort --
1189 *
1190 * Set up the graphics port from the given GC.
1191 *
1192 * Results:
1193 * None.
1194 *
1195 * Side effects:
1196 * The current port is adjusted.
1197 *
1198 *----------------------------------------------------------------------
1199 */
1200
1201 static HPEN
1202 SetUpGraphicsPort(gc)
1203 GC gc;
1204 {
1205 DWORD style;
1206
1207 if (gc->line_style == LineOnOffDash) {
1208 unsigned char *p = (unsigned char *) &(gc->dashes);
1209 /* pointer to the dash-list */
1210
1211 /*
1212 * Below is a simple translation of serveral dash patterns
1213 * to valid windows pen types. Far from complete,
1214 * but I don't know how to do it better.
1215 * Any ideas: <mailto:[email protected]>
1216 */
1217
1218 if (p[1] && p[2]) {
1219 if (!p[3] || p[4]) {
1220 style = PS_DASHDOTDOT; /* -.. */
1221 } else {
1222 style = PS_DASHDOT; /* -. */
1223 }
1224 } else {
1225 if (p[0] > (4 * gc->line_width)) {
1226 style = PS_DASH; /* - */
1227 } else {
1228 style = PS_DOT; /* . */
1229 }
1230 }
1231 } else {
1232 style = PS_SOLID;
1233 }
1234 if (gc->line_width < 2) {
1235 return CreatePen(style, gc->line_width, gc->foreground);
1236 } else {
1237 LOGBRUSH lb;
1238
1239 lb.lbStyle = BS_SOLID;
1240 lb.lbColor = gc->foreground;
1241 lb.lbHatch = 0;
1242
1243 style |= PS_GEOMETRIC;
1244 switch (gc->cap_style) {
1245 case CapNotLast:
1246 case CapButt:
1247 style |= PS_ENDCAP_FLAT;
1248 break;
1249 case CapRound:
1250 style |= PS_ENDCAP_ROUND;
1251 break;
1252 default:
1253 style |= PS_ENDCAP_SQUARE;
1254 break;
1255 }
1256 switch (gc->join_style) {
1257 case JoinMiter:
1258 style |= PS_JOIN_MITER;
1259 break;
1260 case JoinRound:
1261 style |= PS_JOIN_ROUND;
1262 break;
1263 default:
1264 style |= PS_JOIN_BEVEL;
1265 break;
1266 }
1267 return ExtCreatePen(style, gc->line_width, &lb, 0, NULL);
1268 }
1269 }
1270
1271 /*
1272 *----------------------------------------------------------------------
1273 *
1274 * TkScrollWindow --
1275 *
1276 * Scroll a rectangle of the specified window and accumulate
1277 * a damage region.
1278 *
1279 * Results:
1280 * Returns 0 if the scroll genereated no additional damage.
1281 * Otherwise, sets the region that needs to be repainted after
1282 * scrolling and returns 1.
1283 *
1284 * Side effects:
1285 * Scrolls the bits in the window.
1286 *
1287 *----------------------------------------------------------------------
1288 */
1289
1290 int
1291 TkScrollWindow(tkwin, gc, x, y, width, height, dx, dy, damageRgn)
1292 Tk_Window tkwin; /* The window to be scrolled. */
1293 GC gc; /* GC for window to be scrolled. */
1294 int x, y, width, height; /* Position rectangle to be scrolled. */
1295 int dx, dy; /* Distance rectangle should be moved. */
1296 TkRegion damageRgn; /* Region to accumulate damage in. */
1297 {
1298 HWND hwnd = TkWinGetHWND(Tk_WindowId(tkwin));
1299 RECT scrollRect;
1300
1301 scrollRect.left = x;
1302 scrollRect.top = y;
1303 scrollRect.right = x + width;
1304 scrollRect.bottom = y + height;
1305 return (ScrollWindowEx(hwnd, dx, dy, &scrollRect, NULL, (HRGN) damageRgn,
1306 NULL, 0) == NULLREGION) ? 0 : 1;
1307 }
1308
1309 /*
1310 *----------------------------------------------------------------------
1311 *
1312 * TkWinFillRect --
1313 *
1314 * This routine fills a rectangle with the foreground color
1315 * from the specified GC ignoring all other GC values. This
1316 * is the fastest way to fill a drawable with a solid color.
1317 *
1318 * Results:
1319 * None.
1320 *
1321 * Side effects:
1322 * Modifies the contents of the DC drawing surface.
1323 *
1324 *----------------------------------------------------------------------
1325 */
1326
1327 void
1328 TkWinFillRect(dc, x, y, width, height, pixel)
1329 HDC dc;
1330 int x, y, width, height;
1331 int pixel;
1332 {
1333 RECT rect;
1334 COLORREF oldColor;
1335
1336 rect.left = x;
1337 rect.top = y;
1338 rect.right = x + width;
1339 rect.bottom = y + height;
1340 oldColor = SetBkColor(dc, (COLORREF)pixel);
1341 SetBkMode(dc, OPAQUE);
1342 ExtTextOut(dc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
1343 SetBkColor(dc, oldColor);
1344 }
1345
1346 /*
1347 *----------------------------------------------------------------------
1348 *
1349 * TkpDrawHighlightBorder --
1350 *
1351 * This procedure draws a rectangular ring around the outside of
1352 * a widget to indicate that it has received the input focus.
1353 *
1354 * On Windows, we just draw the simple inset ring. On other sytems,
1355 * e.g. the Mac, the focus ring is a little more complicated, so we
1356 * need this abstraction.
1357 *
1358 * Results:
1359 * None.
1360 *
1361 * Side effects:
1362 * A rectangle "width" pixels wide is drawn in "drawable",
1363 * corresponding to the outer area of "tkwin".
1364 *
1365 *----------------------------------------------------------------------
1366 */
1367
1368 void
1369 TkpDrawHighlightBorder(tkwin, fgGC, bgGC, highlightWidth, drawable)
1370 Tk_Window tkwin;
1371 GC fgGC;
1372 GC bgGC;
1373 int highlightWidth;
1374 Drawable drawable;
1375 {
1376 TkDrawInsetFocusHighlight(tkwin, fgGC, highlightWidth, drawable, 0);
1377 }
1378