File: tkWinX.c

package info (click to toggle)
staden 2.0.0%2Bb11-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,584 kB
  • sloc: ansic: 240,605; tcl: 65,360; cpp: 12,854; makefile: 11,203; sh: 3,023; fortran: 2,033; perl: 63; awk: 46
file content (464 lines) | stat: -rw-r--r-- 13,521 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * USED ONLY IN THE WINDOWS VERSION
 *
 * A rewrite of various X functions used by our own Tk widgets that are not
 * available in the Tk X emulation library as used by windows/mac.
 * The routines have been rewritten as other primitives that do exist. This
 * slows them down, but as a first attempt it's valid.
 */


#include <tk.h>
#include <X11/Xlib.h>
#include <tkWinInt.h>
#include "tkWinX.h"



/* 14/1/99 johnt - Optimised Drawing:
   added  XDrawSegments
   re-implemented XDrawPoints
*/

typedef BOOL (CALLBACK *PolyPolyFunc)(HDC,CONST POINT *,CONST DWORD *, DWORD);

static int sp_tkpWinRopModes[] = {
    R2_BLACK,			/* GXclear */
    R2_MASKPEN,			/* GXand */
    R2_MASKPENNOT,		/* GXandReverse */
    R2_COPYPEN,			/* GXcopy */
    R2_MASKNOTPEN,		/* GXandInverted */
    R2_NOT,			/* GXnoop */
    R2_XORPEN,			/* GXxor */
    R2_MERGEPEN,		/* GXor */
    R2_NOTMERGEPEN,		/* GXnor */
    R2_NOTXORPEN,		/* GXequiv */
    R2_NOT,			/* GXinvert */
    R2_MERGEPENNOT,		/* GXorReverse */
    R2_NOTCOPYPEN,		/* GXcopyInverted */
    R2_MERGENOTPEN,		/* GXorInverted */
    R2_NOTMASKPEN,		/* GXnand */
    R2_WHITE			/* GXset */
};

/*
 * The following two raster ops are used to copy the foreground and background
 * bits of a source pattern as defined by a stipple used as the pattern.
 */

#define COPYFG		0x00CA0749 /* dest = (pat & src) | (!pat & dst) */
#define COPYBG		0x00AC0744 /* dest = (!pat & src) | (pat & dst) */

/*
 * Macros used later in the file.
 */

#define MIN(a,b)	((a>b) ? b : a)
#define MAX(a,b)	((a<b) ? b : a)


#ifdef NOTDEF
static POINT *
ConvertPoints(points, npoints, mode, bbox)
    XPoint *points;
    int npoints;
    int mode;			/* CoordModeOrigin or CoordModePrevious. */
    RECT *bbox;			/* Bounding box of points. */
{
    static POINT *winPoints = NULL; /* Array of points that is reused. */
    static int nWinPoints = -1;	    /* Current size of point array. */
    int i;

    /*
     * To avoid paying the cost of a malloc on every drawing routine,
     * we reuse the last array if it is large enough.
     */

    if (npoints > nWinPoints) {
	if (winPoints != NULL) {
	    ckfree((char *) winPoints);
	}
	winPoints = (POINT *) ckalloc(sizeof(POINT) * npoints);
	if (winPoints == NULL) {
	    nWinPoints = -1;
	    return NULL;
	}
	nWinPoints = npoints;
    }

    bbox->left = bbox->right = points[0].x;
    bbox->top = bbox->bottom = points[0].y;

    if (mode == CoordModeOrigin) {
	for (i = 0; i < npoints; i++) {
	    winPoints[i].x = points[i].x;
	    winPoints[i].y = points[i].y;
	    bbox->left = MIN(bbox->left, winPoints[i].x);
	    bbox->right = MAX(bbox->right, winPoints[i].x);
	    bbox->top = MIN(bbox->top, winPoints[i].y);
	    bbox->bottom = MAX(bbox->bottom, winPoints[i].y);
	}
    } else {
	winPoints[0].x = points[0].x;
	winPoints[0].y = points[0].y;
	for (i = 1; i < npoints; i++) {
	    winPoints[i].x = winPoints[i-1].x + points[i].x;
	    winPoints[i].y = winPoints[i-1].y + points[i].y;
	    bbox->left = MIN(bbox->left, winPoints[i].x);
	    bbox->right = MAX(bbox->right, winPoints[i].x);
	    bbox->top = MIN(bbox->top, winPoints[i].y);
	    bbox->bottom = MAX(bbox->bottom, winPoints[i].y);
	}
    }
    return winPoints;
}
#endif

/* We reuse these within the XDrawPoints and XDrawSegments to save on malloc time and memory usage */
static POINT* segpoints= NULL; /* array of start/end points of each segment */
static DWORD* seglens = NULL;   /* array detailing number of points in each segment (always 2) */
static int nsegpoints = -1;     /* number of segments currently allocated for */
static int nseglens = -1;       /* number of segment lengths currently allocated for */

static void
RenderPolyObject(dc, gc, wPoints, nPoints, mode, pen, func,pRect)
    HDC dc;
    GC gc;
    POINT* wPoints;
    int nPoints;
    int mode;
    HPEN pen;
    PolyPolyFunc func;
    RECT *pRect;
{
    HPEN oldPen;
    HBRUSH oldBrush;

    /* grow the segment lengths array if necessary */
    if (nPoints > (nseglens*2) ) {
	int i;
	if (seglens != NULL) {
	    ckfree((char *) seglens);
	}
	seglens   = (DWORD *) ckalloc(sizeof(DWORD)*nPoints/2);
	if (seglens == NULL) {
	    nseglens = -1;
	    return;
	}
	nseglens = nPoints/2;
	/* fill the segments lengths (always 2) */
	for(i=0;i<nseglens;i++)
	    seglens[i]=2;
    }

    if ((gc->fill_style == FillStippled
	    || gc->fill_style == FillOpaqueStippled)
	    && gc->stipple != None) {

	TkWinDrawable *twdPtr = (TkWinDrawable *)gc->stipple;
	HDC dcMem;
	LONG width, height;
	HBITMAP oldBitmap;
	int i;
	HBRUSH oldMemBrush;

	if (twdPtr->type != TWD_BITMAP) {
	    panic("unexpected drawable type in stipple");
	}

	/*
	 * Grow the bounding box enough to account for wide lines.
	 */

	if (gc->line_width > 1) {
	    pRect->left -= gc->line_width;
	    pRect->top -= gc->line_width;
	    pRect->right += gc->line_width;
	    pRect->bottom += gc->line_width;
	}

	width = pRect->right - pRect->left;
	height = pRect->bottom - pRect->top;

	/*
	 * Select stipple pattern into destination dc.
	 */

	SetBrushOrgEx(dc, gc->ts_x_origin, gc->ts_y_origin, NULL);
	oldBrush = SelectObject(dc, CreatePatternBrush(twdPtr->bitmap.handle));

	/*
	 * Create temporary drawing surface containing a copy of the
	 * destination equal in size to the bounding box of the object.
	 */

	dcMem = CreateCompatibleDC(dc);
	oldBitmap = SelectObject(dcMem, CreateCompatibleBitmap(dc, width,
		height));
	oldPen = SelectObject(dcMem, pen);
	BitBlt(dcMem, 0, 0, width, height, dc, pRect->left, pRect->top, SRCCOPY);

	/*
	 * Translate the object for rendering in the temporary drawing
	 * surface.
	 */

	for (i = 0; i < nPoints; i++) {
	    wPoints[i].x -= pRect->left;
	    wPoints[i].y -= pRect->top;
	}

	/*
	 * Draw the object in the foreground color and copy it to the
	 * destination wherever the pattern is set.
	 */

	SetPolyFillMode(dcMem, (gc->fill_rule == EvenOddRule) ? ALTERNATE
		: WINDING);
	oldMemBrush = SelectObject(dcMem, CreateSolidBrush(gc->foreground));
	(*func)(dcMem, wPoints, seglens, nPoints/2);
	BitBlt(dc, pRect->left, pRect->top, width, height, dcMem, 0, 0, COPYFG);

	/*
	 * If we are rendering an opaque stipple, then draw the polygon in the
	 * background color and copy it to the destination wherever the pattern
	 * is clear.
	 */

	if (gc->fill_style == FillOpaqueStippled) {
	    DeleteObject(SelectObject(dcMem,
		    CreateSolidBrush(gc->background)));
	    (*func)(dcMem, wPoints, seglens, nPoints/2);
	    BitBlt(dc, pRect->left, pRect->top, width, height, dcMem, 0, 0,
		    COPYBG);
	}

	SelectObject(dcMem, oldPen);
	DeleteObject(SelectObject(dcMem, oldMemBrush));
	DeleteObject(SelectObject(dcMem, oldBitmap));
	DeleteDC(dcMem);
    } else {
	oldPen = SelectObject(dc, pen);
	oldBrush = SelectObject(dc, CreateSolidBrush(gc->foreground));
	SetROP2(dc, sp_tkpWinRopModes[gc->function]);

	SetPolyFillMode(dc, (gc->fill_rule == EvenOddRule) ? ALTERNATE
		: WINDING);

	(*func)(dc, wPoints, seglens, nPoints/2);

	SelectObject(dc, oldPen);
    }
    DeleteObject(SelectObject(dc, oldBrush));
}


void
XDrawPoints(display, d, gc, points, npoints, mode)
    Display* display;
    Drawable d;
    GC gc;
    XPoint* points;
    int npoints;
    int mode;
{
    HPEN pen;
    TkWinDCState state;
    HDC dc;
    int i,j;
    RECT bbox;

    if (d == None) {
	return;
    }

    /* allocate polypoints array if existing memory is not big enough */
    /* each X point takes 2 Windows points, as an Xpoint is drawn as a windows line */
    if (npoints > (nsegpoints/2)) {
	if (segpoints != NULL)
	    ckfree((char *) segpoints);
	segpoints = (POINT*) ckalloc(sizeof(POINT)*npoints*2);
	if (segpoints == NULL ) {
	    nsegpoints = -1;
	    return;
	}
	nsegpoints = npoints*2;
    }

    /* Fill in the array */
    bbox.left = bbox.right  = points[0].x;
    bbox.top  = bbox.bottom = points[0].y;
    if (mode == CoordModeOrigin) {
	for (i=0,j=0; i < npoints; i++,j+=2) {
	    segpoints[j].x = points[i].x;
	    segpoints[j].y = points[i].y;
	    segpoints[j+1].x = points[i].x+1;
	    segpoints[j+1].y = points[i].y+1;
	    bbox.left = MIN(bbox.left, points[i].x);
	    bbox.right = MAX(bbox.right, points[i].x+1);
	    bbox.top = MIN(bbox.top, points[i].y);
	    bbox.bottom = MAX(bbox.bottom, points[i].y+1);
	}
    } else {
	segpoints[0].x = points[0].x;
	segpoints[0].y = points[0].y;
	segpoints[1].x = points[0].x+1;
	segpoints[1].y = points[0].y+1;
	for (i=1,j=2; i < npoints; i++,j+=2) {
	    segpoints[j].x = points[i-1].x + points[i].x;
	    segpoints[j].y = points[i-1].y + points[i].y;
	    segpoints[j+1].x = segpoints[j].x+1;
	    segpoints[j+1].y = segpoints[j].y+1;
	    bbox.left = MIN(bbox.left, points[i].x);
	    bbox.right = MAX(bbox.right, points[i].x+1);
	    bbox.top = MIN(bbox.top, points[i].y);
	    bbox.bottom = MAX(bbox.bottom, points[i].y+1);
	}
    }

    dc = TkWinGetDrawableDC(display, d, &state);

    pen = CreatePen(PS_SOLID, 1, gc->foreground);

    RenderPolyObject(dc, gc, segpoints, npoints*2, mode, pen, PolyPolyline,&bbox);
    DeleteObject(pen);

    TkWinReleaseDrawableDC(d, dc, &state);
}

void
XDrawSegments(display, d, gc, segments, nsegs)
    Display* display;
    Drawable d;
    GC gc;
    XSegment* segments;
    int nsegs;
{
    HPEN pen;
    TkWinDCState state;
    HDC dc;
    int i,j;
    RECT bbox;

    if (d == None) {
	return;
    }

    /* allocate polypoints array if existing memory is not big enough */
    /* each X segment takes 2 Windows points */
    if (nsegs > (nsegpoints/2)) {
	if (segpoints != NULL)
	    ckfree((char *) segpoints);
	segpoints = (POINT*) ckalloc(sizeof(POINT)*nsegs*2);
	if (segpoints == NULL ) {
	    nsegpoints = -1;
	    return;
	}
	nsegpoints = nsegs*2;
    }
    /* Fill in the array */
    bbox.left = bbox.right  = segments[0].x1;
    bbox.top  = bbox.bottom = segments[0].y1;
    for (i=0,j=0; i < nsegs; i++,j+=2) {
	segpoints[j].x = segments[i].x1;
	segpoints[j].y = segments[i].y1;
	segpoints[j+1].x = segments[i].x2;
	segpoints[j+1].y = segments[i].y2;
	bbox.left = MIN(bbox.left, segments[i].x1);
	bbox.right = MAX(bbox.right, segments[i].x1);
	bbox.top = MIN(bbox.top, segments[i].y1);
	bbox.bottom = MAX(bbox.bottom, segments[i].y1);
	bbox.left = MIN(bbox.left, segments[i].x2);
	bbox.right = MAX(bbox.right, segments[i].x2);
	bbox.top = MIN(bbox.top, segments[i].y2);
	bbox.bottom = MAX(bbox.bottom, segments[i].y2);
    }

    dc = TkWinGetDrawableDC(display, d, &state);

    if ( gc->line_width > 1) {
	LOGBRUSH lb;
	DWORD style;

	lb.lbStyle = BS_SOLID;
	lb.lbColor = gc->foreground;
	lb.lbHatch = 0;

	style = PS_GEOMETRIC|PS_COSMETIC;
	switch (gc->cap_style) {
	    case CapNotLast:
	    case CapButt:
		style |= PS_ENDCAP_FLAT;
		break;
	    case CapRound:
		style |= PS_ENDCAP_ROUND;
		break;
	    default:
		style |= PS_ENDCAP_SQUARE;
		break;
	}
	pen = ExtCreatePen(style, gc->line_width, &lb, 0, NULL);
    } else {
	pen = CreatePen(PS_SOLID, gc->line_width, gc->foreground);
    }
    RenderPolyObject(dc, gc, segpoints, nsegs*2, CoordModeOrigin, pen, PolyPolyline,&bbox);
    DeleteObject(pen);

    TkWinReleaseDrawableDC(d, dc, &state);
}

/* XDrawPoint - use XDrawLine */
void XDrawPoint(Display *dsp, Drawable d, GC gc, int x, int y) {
    XDrawLine(dsp, d, gc, x, y, x+1, y+1);
}


/* XDrawRectangles - use XDrawRectangle */
void XDrawRectangles(Display *dsp, Drawable d, GC gc,
		     XRectangle *rectangles, int nrectangles) {
    int i;

    for (i=0; i<nrectangles; i++, rectangles++)
	XDrawRectangle(dsp, d, gc,
		       rectangles->x, rectangles->y,
		       rectangles->width, rectangles->height);
}

/* XSetClipRectangles - unimplemented */
int XSetClipRectangles( Display* d, GC gc, int x, int y, XRectangle* r, int w, int z )
{
   return 0;
}


/*
 * MJ: A version of this function is already provided by TK, so we'll defer
 * to its implementation, instead of this one to avoid link errors.
 */
#ifdef USE_CUSTOM_XCHANGEGC
void XChangeGC(Display *dsp, GC gp, unsigned long mask, XGCValues *values) {
    if (mask & GCFunction)          gp->function = values->function;
    if (mask & GCPlaneMask)         gp->plane_mask = values->plane_mask;
    if (mask & GCForeground)        gp->foreground = values->foreground;
    if (mask & GCBackground)        gp->background = values->background;
    if (mask & GCLineWidth)         gp->line_width = values->line_width;
    if (mask & GCLineStyle)         gp->line_style = values->line_style;
    if (mask & GCCapStyle)          gp->cap_style = values->cap_style;
    if (mask & GCJoinStyle)         gp->join_style = values->join_style;
    if (mask & GCFillStyle)         gp->fill_style = values->fill_style;
    if (mask & GCArcMode)           gp->arc_mode = values->arc_mode;
    if (mask & GCTile)              gp->tile = values->tile;
    if (mask & GCStipple)           gp->stipple = values->stipple;
    if (mask & GCTileStipXOrigin)   gp->ts_x_origin = values->ts_x_origin;
    if (mask & GCTileStipYOrigin)   gp->ts_y_origin = values->ts_y_origin;
    if (mask & GCFont)              gp->font = values->font;
    if (mask & GCSubwindowMode)
	gp->subwindow_mode = values->subwindow_mode;
    if (mask & GCGraphicsExposures)
	gp->graphics_exposures = values->graphics_exposures;
    if (mask & GCClipXOrigin)       gp->clip_x_origin = values->clip_x_origin;
    if (mask & GCClipYOrigin)       gp->clip_y_origin = values->clip_y_origin;
    if (mask & GCDashOffset)        gp->dash_offset = values->dash_offset;
    if (mask & GCDashList)          gp->dashes = values->dashes;
}
#endif