4 * This file implements polygon items for canvas widgets.
6 * Copyright (c) 1991-1994 The Regents of the University of California.
7 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
8 * Copyright (c) 1998-2000 Ajuba Solutions.
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
22 * The structure below defines the record for each polygon item.
25 typedef struct PolygonItem {
26 Tk_Item header; /* Generic stuff that's the same for all
27 * types. MUST BE FIRST IN STRUCTURE. */
28 Tk_Outline outline; /* Outline structure */
29 int numPoints; /* Number of points in polygon.
30 * Polygon is always closed. */
31 int pointsAllocated; /* Number of points for which space is
32 * allocated at *coordPtr. */
33 double *coordPtr; /* Pointer to malloc-ed array containing
34 * x- and y-coords of all points in polygon.
35 * X-coords are even-valued indices, y-coords
36 * are corresponding odd-valued indices. */
37 int joinStyle; /* Join style for outline */
39 XColor *fillColor; /* Foreground color for polygon. */
40 XColor *activeFillColor; /* Foreground color for polygon if state is active. */
41 XColor *disabledFillColor; /* Foreground color for polygon if state is disabled. */
42 Pixmap fillStipple; /* Stipple bitmap for filling polygon. */
43 Pixmap activeFillStipple; /* Stipple bitmap for filling polygon if state is active. */
44 Pixmap disabledFillStipple; /* Stipple bitmap for filling polygon if state is disabled. */
45 GC fillGC; /* Graphics context for filling polygon. */
46 Tk_SmoothMethod *smooth; /* Non-zero means draw shape smoothed (i.e.
47 * with Bezier splines). */
48 int splineSteps; /* Number of steps in each spline segment. */
49 int autoClosed; /* Zero means the given polygon was closed,
50 one means that we auto closed it. */
54 * Information used for parsing configuration specs:
57 static Tk_CustomOption smoothOption = {
58 (Tk_OptionParseProc *) TkSmoothParseProc,
59 TkSmoothPrintProc, (ClientData) NULL
61 static Tk_CustomOption stateOption = {
62 (Tk_OptionParseProc *) TkStateParseProc,
63 TkStatePrintProc, (ClientData) 2
65 static Tk_CustomOption tagsOption = {
66 (Tk_OptionParseProc *) Tk_CanvasTagsParseProc,
67 Tk_CanvasTagsPrintProc, (ClientData) NULL
69 static Tk_CustomOption dashOption = {
70 (Tk_OptionParseProc *) TkCanvasDashParseProc,
71 TkCanvasDashPrintProc, (ClientData) NULL
73 static Tk_CustomOption offsetOption = {
74 (Tk_OptionParseProc *) TkOffsetParseProc,
76 (ClientData) (TK_OFFSET_RELATIVE|TK_OFFSET_INDEX)
78 static Tk_CustomOption pixelOption = {
79 (Tk_OptionParseProc *) TkPixelParseProc,
80 TkPixelPrintProc, (ClientData) NULL
83 static Tk_ConfigSpec configSpecs[] = {
84 {TK_CONFIG_CUSTOM, "-activedash", (char *) NULL, (char *) NULL,
85 (char *) NULL, Tk_Offset(PolygonItem, outline.activeDash),
86 TK_CONFIG_NULL_OK, &dashOption},
87 {TK_CONFIG_COLOR, "-activefill", (char *) NULL, (char *) NULL,
88 (char *) NULL, Tk_Offset(PolygonItem, activeFillColor),
90 {TK_CONFIG_COLOR, "-activeoutline", (char *) NULL, (char *) NULL,
91 (char *) NULL, Tk_Offset(PolygonItem, outline.activeColor),
93 {TK_CONFIG_BITMAP, "-activeoutlinestipple", (char *) NULL, (char *) NULL,
94 (char *) NULL, Tk_Offset(PolygonItem, outline.activeStipple),
96 {TK_CONFIG_BITMAP, "-activestipple", (char *) NULL, (char *) NULL,
97 (char *) NULL, Tk_Offset(PolygonItem, activeFillStipple),
99 {TK_CONFIG_CUSTOM, "-activewidth", (char *) NULL, (char *) NULL,
100 "0.0", Tk_Offset(PolygonItem, outline.activeWidth),
101 TK_CONFIG_DONT_SET_DEFAULT, &pixelOption},
102 {TK_CONFIG_CUSTOM, "-dash", (char *) NULL, (char *) NULL,
103 (char *) NULL, Tk_Offset(PolygonItem, outline.dash),
104 TK_CONFIG_NULL_OK, &dashOption},
105 {TK_CONFIG_PIXELS, "-dashoffset", (char *) NULL, (char *) NULL,
106 "0", Tk_Offset(PolygonItem, outline.offset),
107 TK_CONFIG_DONT_SET_DEFAULT},
108 {TK_CONFIG_CUSTOM, "-disableddash", (char *) NULL, (char *) NULL,
109 (char *) NULL, Tk_Offset(PolygonItem, outline.disabledDash),
110 TK_CONFIG_NULL_OK, &dashOption},
111 {TK_CONFIG_COLOR, "-disabledfill", (char *) NULL, (char *) NULL,
112 (char *) NULL, Tk_Offset(PolygonItem, disabledFillColor),
114 {TK_CONFIG_COLOR, "-disabledoutline", (char *) NULL, (char *) NULL,
115 (char *) NULL, Tk_Offset(PolygonItem, outline.disabledColor),
117 {TK_CONFIG_BITMAP, "-disabledoutlinestipple", (char *) NULL, (char *) NULL,
118 (char *) NULL, Tk_Offset(PolygonItem, outline.disabledStipple),
120 {TK_CONFIG_BITMAP, "-disabledstipple", (char *) NULL, (char *) NULL,
121 (char *) NULL, Tk_Offset(PolygonItem, disabledFillStipple),
123 {TK_CONFIG_CUSTOM, "-disabledwidth", (char *) NULL, (char *) NULL,
124 "0.0", Tk_Offset(PolygonItem, outline.disabledWidth),
125 TK_CONFIG_DONT_SET_DEFAULT, &pixelOption},
126 {TK_CONFIG_COLOR, "-fill", (char *) NULL, (char *) NULL,
127 "black", Tk_Offset(PolygonItem, fillColor), TK_CONFIG_NULL_OK},
128 {TK_CONFIG_JOIN_STYLE, "-joinstyle", (char *) NULL, (char *) NULL,
129 "round", Tk_Offset(PolygonItem, joinStyle), TK_CONFIG_DONT_SET_DEFAULT},
130 {TK_CONFIG_CUSTOM, "-offset", (char *) NULL, (char *) NULL,
131 "0,0", Tk_Offset(PolygonItem, tsoffset),
132 TK_CONFIG_NULL_OK, &offsetOption},
133 {TK_CONFIG_COLOR, "-outline", (char *) NULL, (char *) NULL,
134 (char *) NULL, Tk_Offset(PolygonItem, outline.color),
136 {TK_CONFIG_CUSTOM, "-outlineoffset", (char *) NULL, (char *) NULL,
137 "0,0", Tk_Offset(PolygonItem, outline.tsoffset),
138 TK_CONFIG_NULL_OK, &offsetOption},
139 {TK_CONFIG_BITMAP, "-outlinestipple", (char *) NULL, (char *) NULL,
140 (char *) NULL, Tk_Offset(PolygonItem, outline.stipple),
142 {TK_CONFIG_CUSTOM, "-smooth", (char *) NULL, (char *) NULL,
143 "0", Tk_Offset(PolygonItem, smooth),
144 TK_CONFIG_DONT_SET_DEFAULT, &smoothOption},
145 {TK_CONFIG_INT, "-splinesteps", (char *) NULL, (char *) NULL,
146 "12", Tk_Offset(PolygonItem, splineSteps), TK_CONFIG_DONT_SET_DEFAULT},
147 {TK_CONFIG_CUSTOM, "-state", (char *) NULL, (char *) NULL,
148 (char *) NULL, Tk_Offset(Tk_Item, state), TK_CONFIG_NULL_OK,
150 {TK_CONFIG_BITMAP, "-stipple", (char *) NULL, (char *) NULL,
151 (char *) NULL, Tk_Offset(PolygonItem, fillStipple), TK_CONFIG_NULL_OK},
152 {TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
153 (char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
154 {TK_CONFIG_CUSTOM, "-width", (char *) NULL, (char *) NULL,
155 "1.0", Tk_Offset(PolygonItem, outline.width),
156 TK_CONFIG_DONT_SET_DEFAULT, &pixelOption},
157 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
162 * Prototypes for procedures defined in this file:
165 static void ComputePolygonBbox _ANSI_ARGS_((Tk_Canvas canvas,
166 PolygonItem *polyPtr));
167 static int ConfigurePolygon _ANSI_ARGS_((Tcl_Interp *interp,
168 Tk_Canvas canvas, Tk_Item *itemPtr, int objc,
169 Tcl_Obj *CONST objv[], int flags));
170 static int CreatePolygon _ANSI_ARGS_((Tcl_Interp *interp,
171 Tk_Canvas canvas, struct Tk_Item *itemPtr,
172 int objc, Tcl_Obj *CONST objv[]));
173 static void DeletePolygon _ANSI_ARGS_((Tk_Canvas canvas,
174 Tk_Item *itemPtr, Display *display));
175 static void DisplayPolygon _ANSI_ARGS_((Tk_Canvas canvas,
176 Tk_Item *itemPtr, Display *display, Drawable dst,
177 int x, int y, int width, int height));
178 static int GetPolygonIndex _ANSI_ARGS_((Tcl_Interp *interp,
179 Tk_Canvas canvas, Tk_Item *itemPtr,
180 Tcl_Obj *obj, int *indexPtr));
181 static int PolygonCoords _ANSI_ARGS_((Tcl_Interp *interp,
182 Tk_Canvas canvas, Tk_Item *itemPtr,
183 int objc, Tcl_Obj *CONST objv[]));
184 static void PolygonDeleteCoords _ANSI_ARGS_((Tk_Canvas canvas,
185 Tk_Item *itemPtr, int first, int last));
186 static void PolygonInsert _ANSI_ARGS_((Tk_Canvas canvas,
187 Tk_Item *itemPtr, int beforeThis, Tcl_Obj *obj));
188 static int PolygonToArea _ANSI_ARGS_((Tk_Canvas canvas,
189 Tk_Item *itemPtr, double *rectPtr));
190 static double PolygonToPoint _ANSI_ARGS_((Tk_Canvas canvas,
191 Tk_Item *itemPtr, double *pointPtr));
192 static int PolygonToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
193 Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
194 static void ScalePolygon _ANSI_ARGS_((Tk_Canvas canvas,
195 Tk_Item *itemPtr, double originX, double originY,
196 double scaleX, double scaleY));
197 static void TranslatePolygon _ANSI_ARGS_((Tk_Canvas canvas,
198 Tk_Item *itemPtr, double deltaX, double deltaY));
201 * The structures below defines the polygon item type by means
202 * of procedures that can be invoked by generic item code.
205 Tk_ItemType tkPolygonType = {
206 "polygon", /* name */
207 sizeof(PolygonItem), /* itemSize */
208 CreatePolygon, /* createProc */
209 configSpecs, /* configSpecs */
210 ConfigurePolygon, /* configureProc */
211 PolygonCoords, /* coordProc */
212 DeletePolygon, /* deleteProc */
213 DisplayPolygon, /* displayProc */
214 TK_CONFIG_OBJS, /* flags */
215 PolygonToPoint, /* pointProc */
216 PolygonToArea, /* areaProc */
217 PolygonToPostscript, /* postscriptProc */
218 ScalePolygon, /* scaleProc */
219 TranslatePolygon, /* translateProc */
220 (Tk_ItemIndexProc *) GetPolygonIndex,/* indexProc */
221 (Tk_ItemCursorProc *) NULL, /* icursorProc */
222 (Tk_ItemSelectionProc *) NULL, /* selectionProc */
223 (Tk_ItemInsertProc *) PolygonInsert,/* insertProc */
224 PolygonDeleteCoords, /* dTextProc */
225 (Tk_ItemType *) NULL, /* nextPtr */
229 * The definition below determines how large are static arrays
230 * used to hold spline points (splines larger than this have to
231 * have their arrays malloc-ed).
234 #define MAX_STATIC_POINTS 200
237 *--------------------------------------------------------------
241 * This procedure is invoked to create a new polygon item in
245 * A standard Tcl return value. If an error occurred in
246 * creating the item, then an error message is left in
247 * the interp's result; in this case itemPtr is
248 * left uninitialized, so it can be safely freed by the
252 * A new polygon item is created.
254 *--------------------------------------------------------------
258 CreatePolygon(interp, canvas, itemPtr, objc, objv)
259 Tcl_Interp *interp; /* Interpreter for error reporting. */
260 Tk_Canvas canvas; /* Canvas to hold new item. */
261 Tk_Item *itemPtr; /* Record to hold new item; header
262 * has been initialized by caller. */
263 int objc; /* Number of arguments in objv. */
264 Tcl_Obj *CONST objv[]; /* Arguments describing polygon. */
266 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
270 * Carry out initialization that is needed in order to clean
271 * up after errors during the the remainder of this procedure.
274 Tk_CreateOutline(&(polyPtr->outline));
275 polyPtr->numPoints = 0;
276 polyPtr->pointsAllocated = 0;
277 polyPtr->coordPtr = NULL;
278 polyPtr->joinStyle = JoinRound;
279 polyPtr->tsoffset.flags = 0;
280 polyPtr->tsoffset.xoffset = 0;
281 polyPtr->tsoffset.yoffset = 0;
282 polyPtr->fillColor = NULL;
283 polyPtr->activeFillColor = NULL;
284 polyPtr->disabledFillColor = NULL;
285 polyPtr->fillStipple = None;
286 polyPtr->activeFillStipple = None;
287 polyPtr->disabledFillStipple = None;
288 polyPtr->fillGC = None;
289 polyPtr->smooth = (Tk_SmoothMethod *) NULL;
290 polyPtr->splineSteps = 12;
291 polyPtr->autoClosed = 0;
294 * Count the number of points and then parse them into a point
295 * array. Leading arguments are assumed to be points if they
296 * start with a digit or a minus sign followed by a digit.
299 for (i = 0; i < objc; i++) {
300 char *arg = Tcl_GetString(objv[i]);
301 if ((arg[0] == '-') && (arg[1] >= 'a') && (arg[1] <= 'z')) {
305 if (i && PolygonCoords(interp, canvas, itemPtr, i, objv) != TCL_OK) {
309 if (ConfigurePolygon(interp, canvas, itemPtr, objc-i, objv+i, 0)
315 DeletePolygon(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
320 *--------------------------------------------------------------
324 * This procedure is invoked to process the "coords" widget
325 * command on polygons. See the user documentation for details
329 * Returns TCL_OK or TCL_ERROR, and sets the interp's result.
332 * The coordinates for the given item may be changed.
334 *--------------------------------------------------------------
338 PolygonCoords(interp, canvas, itemPtr, objc, objv)
339 Tcl_Interp *interp; /* Used for error reporting. */
340 Tk_Canvas canvas; /* Canvas containing item. */
341 Tk_Item *itemPtr; /* Item whose coordinates are to be
342 * read or modified. */
343 int objc; /* Number of coordinates supplied in
345 Tcl_Obj *CONST objv[]; /* Array of coordinates: x1, y1,
348 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
353 * Print the coords used to create the polygon. If we auto
354 * closed the polygon then we don't report the last point.
356 Tcl_Obj *subobj, *obj = Tcl_NewObj();
357 for (i = 0; i < 2*(polyPtr->numPoints - polyPtr->autoClosed); i++) {
358 subobj = Tcl_NewDoubleObj(polyPtr->coordPtr[i]);
359 Tcl_ListObjAppendElement(interp, obj, subobj);
361 Tcl_SetObjResult(interp, obj);
365 if (Tcl_ListObjGetElements(interp, objv[0], &objc,
366 (Tcl_Obj ***) &objv) != TCL_OK) {
371 Tcl_AppendResult(interp,
372 "odd number of coordinates specified for polygon",
377 if (polyPtr->pointsAllocated <= numPoints) {
378 if (polyPtr->coordPtr != NULL) {
379 ckfree((char *) polyPtr->coordPtr);
383 * One extra point gets allocated here, because we always
384 * add another point to close the polygon.
387 polyPtr->coordPtr = (double *) ckalloc((unsigned)
388 (sizeof(double) * (objc+2)));
389 polyPtr->pointsAllocated = numPoints+1;
391 for (i = objc-1; i >= 0; i--) {
392 if (Tk_CanvasGetCoordFromObj(interp, canvas, objv[i],
393 &polyPtr->coordPtr[i]) != TCL_OK) {
397 polyPtr->numPoints = numPoints;
398 polyPtr->autoClosed = 0;
401 * Close the polygon if it isn't already closed.
404 if (objc>2 && ((polyPtr->coordPtr[objc-2] != polyPtr->coordPtr[0])
405 || (polyPtr->coordPtr[objc-1] != polyPtr->coordPtr[1]))) {
406 polyPtr->autoClosed = 1;
407 polyPtr->numPoints++;
408 polyPtr->coordPtr[objc] = polyPtr->coordPtr[0];
409 polyPtr->coordPtr[objc+1] = polyPtr->coordPtr[1];
411 ComputePolygonBbox(canvas, polyPtr);
417 *--------------------------------------------------------------
419 * ConfigurePolygon --
421 * This procedure is invoked to configure various aspects
422 * of a polygon item such as its background color.
425 * A standard Tcl result code. If an error occurs, then
426 * an error message is left in the interp's result.
429 * Configuration information, such as colors and stipple
430 * patterns, may be set for itemPtr.
432 *--------------------------------------------------------------
436 ConfigurePolygon(interp, canvas, itemPtr, objc, objv, flags)
437 Tcl_Interp *interp; /* Interpreter for error reporting. */
438 Tk_Canvas canvas; /* Canvas containing itemPtr. */
439 Tk_Item *itemPtr; /* Polygon item to reconfigure. */
440 int objc; /* Number of elements in objv. */
441 Tcl_Obj *CONST objv[]; /* Arguments describing things to configure. */
442 int flags; /* Flags to pass to Tk_ConfigureWidget. */
444 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
453 tkwin = Tk_CanvasTkwin(canvas);
454 if (TCL_OK != Tk_ConfigureWidget(interp, tkwin, configSpecs, objc,
455 (CONST char **) objv, (char *) polyPtr, flags|TK_CONFIG_OBJS)) {
460 * A few of the options require additional processing, such as
464 state = itemPtr->state;
466 if (polyPtr->outline.activeWidth > polyPtr->outline.width ||
467 polyPtr->outline.activeDash.number != 0 ||
468 polyPtr->outline.activeColor != NULL ||
469 polyPtr->outline.activeStipple != None ||
470 polyPtr->activeFillColor != NULL ||
471 polyPtr->activeFillStipple != None) {
472 itemPtr->redraw_flags |= TK_ITEM_STATE_DEPENDANT;
474 itemPtr->redraw_flags &= ~TK_ITEM_STATE_DEPENDANT;
477 if(state == TK_STATE_NULL) {
478 state = ((TkCanvas *)canvas)->canvas_state;
480 if (state==TK_STATE_HIDDEN) {
481 ComputePolygonBbox(canvas, polyPtr);
485 mask = Tk_ConfigOutlineGC(&gcValues, canvas, itemPtr, &(polyPtr->outline));
487 gcValues.cap_style = CapRound;
488 gcValues.join_style = polyPtr->joinStyle;
489 mask |= GCCapStyle|GCJoinStyle;
490 newGC = Tk_GetGC(tkwin, mask, &gcValues);
494 if (polyPtr->outline.gc != None) {
495 Tk_FreeGC(Tk_Display(tkwin), polyPtr->outline.gc);
497 polyPtr->outline.gc = newGC;
499 color = polyPtr->fillColor;
500 stipple = polyPtr->fillStipple;
501 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
502 if (polyPtr->activeFillColor!=NULL) {
503 color = polyPtr->activeFillColor;
505 if (polyPtr->activeFillStipple!=None) {
506 stipple = polyPtr->activeFillStipple;
508 } else if (state==TK_STATE_DISABLED) {
509 if (polyPtr->disabledFillColor!=NULL) {
510 color = polyPtr->disabledFillColor;
512 if (polyPtr->disabledFillStipple!=None) {
513 stipple = polyPtr->disabledFillStipple;
520 gcValues.foreground = color->pixel;
522 if (stipple != None) {
523 gcValues.stipple = stipple;
524 gcValues.fill_style = FillStippled;
525 mask |= GCStipple|GCFillStyle;
527 newGC = Tk_GetGC(tkwin, mask, &gcValues);
529 if (polyPtr->fillGC != None) {
530 Tk_FreeGC(Tk_Display(tkwin), polyPtr->fillGC);
532 polyPtr->fillGC = newGC;
535 * Keep spline parameters within reasonable limits.
538 if (polyPtr->splineSteps < 1) {
539 polyPtr->splineSteps = 1;
540 } else if (polyPtr->splineSteps > 100) {
541 polyPtr->splineSteps = 100;
544 ComputePolygonBbox(canvas, polyPtr);
549 *--------------------------------------------------------------
553 * This procedure is called to clean up the data structure
554 * associated with a polygon item.
560 * Resources associated with itemPtr are released.
562 *--------------------------------------------------------------
566 DeletePolygon(canvas, itemPtr, display)
567 Tk_Canvas canvas; /* Info about overall canvas widget. */
568 Tk_Item *itemPtr; /* Item that is being deleted. */
569 Display *display; /* Display containing window for
572 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
574 Tk_DeleteOutline(display,&(polyPtr->outline));
575 if (polyPtr->coordPtr != NULL) {
576 ckfree((char *) polyPtr->coordPtr);
578 if (polyPtr->fillColor != NULL) {
579 Tk_FreeColor(polyPtr->fillColor);
581 if (polyPtr->activeFillColor != NULL) {
582 Tk_FreeColor(polyPtr->activeFillColor);
584 if (polyPtr->disabledFillColor != NULL) {
585 Tk_FreeColor(polyPtr->disabledFillColor);
587 if (polyPtr->fillStipple != None) {
588 Tk_FreeBitmap(display, polyPtr->fillStipple);
590 if (polyPtr->activeFillStipple != None) {
591 Tk_FreeBitmap(display, polyPtr->activeFillStipple);
593 if (polyPtr->disabledFillStipple != None) {
594 Tk_FreeBitmap(display, polyPtr->disabledFillStipple);
596 if (polyPtr->fillGC != None) {
597 Tk_FreeGC(display, polyPtr->fillGC);
602 *--------------------------------------------------------------
604 * ComputePolygonBbox --
606 * This procedure is invoked to compute the bounding box of
607 * all the pixels that may be drawn as part of a polygon.
613 * The fields x1, y1, x2, and y2 are updated in the header
616 *--------------------------------------------------------------
620 ComputePolygonBbox(canvas, polyPtr)
621 Tk_Canvas canvas; /* Canvas that contains item. */
622 PolygonItem *polyPtr; /* Item whose bbox is to be
628 Tk_State state = polyPtr->header.state;
629 Tk_TSOffset *tsoffset;
631 if(state == TK_STATE_NULL) {
632 state = ((TkCanvas *)canvas)->canvas_state;
634 width = polyPtr->outline.width;
635 if (polyPtr->coordPtr == NULL || (polyPtr->numPoints < 1) || (state==TK_STATE_HIDDEN)) {
636 polyPtr->header.x1 = polyPtr->header.x2 =
637 polyPtr->header.y1 = polyPtr->header.y2 = -1;
640 if (((TkCanvas *)canvas)->currentItemPtr == (Tk_Item *)polyPtr) {
641 if (polyPtr->outline.activeWidth>width) {
642 width = polyPtr->outline.activeWidth;
644 } else if (state==TK_STATE_DISABLED) {
645 if (polyPtr->outline.disabledWidth>0.0) {
646 width = polyPtr->outline.disabledWidth;
650 coordPtr = polyPtr->coordPtr;
651 polyPtr->header.x1 = polyPtr->header.x2 = (int) *coordPtr;
652 polyPtr->header.y1 = polyPtr->header.y2 = (int) coordPtr[1];
655 * Compute the bounding box of all the points in the polygon,
656 * then expand in all directions by the outline's width to take
657 * care of butting or rounded corners and projecting or
658 * rounded caps. This expansion is an overestimate (worst-case
659 * is square root of two over two) but it's simple. Don't do
660 * anything special for curves. This causes an additional
661 * overestimate in the bounding box, but is faster.
664 for (i = 1, coordPtr = polyPtr->coordPtr+2; i < polyPtr->numPoints-1;
665 i++, coordPtr += 2) {
666 TkIncludePoint((Tk_Item *) polyPtr, coordPtr);
669 tsoffset = &polyPtr->tsoffset;
670 if (tsoffset->flags & TK_OFFSET_INDEX) {
671 int index = tsoffset->flags & ~TK_OFFSET_INDEX;
672 if (tsoffset->flags == INT_MAX) {
673 index = (polyPtr->numPoints - polyPtr->autoClosed) * 2;
678 index %= (polyPtr->numPoints - polyPtr->autoClosed) * 2;
680 index += (polyPtr->numPoints - polyPtr->autoClosed) * 2;
682 tsoffset->xoffset = (int) (polyPtr->coordPtr[index] + 0.5);
683 tsoffset->yoffset = (int) (polyPtr->coordPtr[index+1] + 0.5);
685 if (tsoffset->flags & TK_OFFSET_LEFT) {
686 tsoffset->xoffset = polyPtr->header.x1;
687 } else if (tsoffset->flags & TK_OFFSET_CENTER) {
688 tsoffset->xoffset = (polyPtr->header.x1 + polyPtr->header.x2)/2;
689 } else if (tsoffset->flags & TK_OFFSET_RIGHT) {
690 tsoffset->xoffset = polyPtr->header.x2;
692 if (tsoffset->flags & TK_OFFSET_TOP) {
693 tsoffset->yoffset = polyPtr->header.y1;
694 } else if (tsoffset->flags & TK_OFFSET_MIDDLE) {
695 tsoffset->yoffset = (polyPtr->header.y1 + polyPtr->header.y2)/2;
696 } else if (tsoffset->flags & TK_OFFSET_BOTTOM) {
697 tsoffset->yoffset = polyPtr->header.y2;
701 if (polyPtr->outline.gc != None) {
702 tsoffset = &polyPtr->outline.tsoffset;
704 if (tsoffset->flags & TK_OFFSET_INDEX) {
705 int index = tsoffset->flags & ~TK_OFFSET_INDEX;
706 if (tsoffset->flags == INT_MAX) {
707 index = (polyPtr->numPoints - 1) * 2;
709 index %= (polyPtr->numPoints - 1) * 2;
711 index += (polyPtr->numPoints - 1) * 2;
713 tsoffset->xoffset = (int) (polyPtr->coordPtr[index] + 0.5);
714 tsoffset->yoffset = (int) (polyPtr->coordPtr[index+1] + 0.5);
716 if (tsoffset->flags & TK_OFFSET_LEFT) {
717 tsoffset->xoffset = polyPtr->header.x1;
718 } else if (tsoffset->flags & TK_OFFSET_CENTER) {
719 tsoffset->xoffset = (polyPtr->header.x1 + polyPtr->header.x2)/2;
720 } else if (tsoffset->flags & TK_OFFSET_RIGHT) {
721 tsoffset->xoffset = polyPtr->header.x2;
723 if (tsoffset->flags & TK_OFFSET_TOP) {
724 tsoffset->yoffset = polyPtr->header.y1;
725 } else if (tsoffset->flags & TK_OFFSET_MIDDLE) {
726 tsoffset->yoffset = (polyPtr->header.y1 + polyPtr->header.y2)/2;
727 } else if (tsoffset->flags & TK_OFFSET_BOTTOM) {
728 tsoffset->yoffset = polyPtr->header.y2;
733 i = (int) ((width+1.5)/2.0);
734 polyPtr->header.x1 -= i;
735 polyPtr->header.x2 += i;
736 polyPtr->header.y1 -= i;
737 polyPtr->header.y2 += i;
740 * For mitered lines, make a second pass through all the points.
741 * Compute the locations of the two miter vertex points and add
742 * those into the bounding box.
745 if (polyPtr->joinStyle == JoinMiter) {
748 coordPtr = polyPtr->coordPtr;
749 if (polyPtr->numPoints>3) {
750 if (TkGetMiterPoints(coordPtr+2*(polyPtr->numPoints-2),
751 coordPtr, coordPtr+2, width,
753 for (j = 0; j < 4; j += 2) {
754 TkIncludePoint((Tk_Item *) polyPtr, miter+j);
758 for (i = polyPtr->numPoints ; i >= 3;
759 i--, coordPtr += 2) {
761 if (TkGetMiterPoints(coordPtr, coordPtr+2, coordPtr+4,
762 width, miter, miter+2)) {
763 for (j = 0; j < 4; j += 2) {
764 TkIncludePoint((Tk_Item *) polyPtr, miter+j);
772 * Add one more pixel of fudge factor just to be safe (e.g.
773 * X may round differently than we do).
776 polyPtr->header.x1 -= 1;
777 polyPtr->header.x2 += 1;
778 polyPtr->header.y1 -= 1;
779 polyPtr->header.y2 += 1;
783 *--------------------------------------------------------------
787 * This procedure is invoked to convert a polygon to screen
788 * coordinates and display it using a particular GC.
794 * ItemPtr is drawn in drawable using the transformation
795 * information in canvas.
797 *--------------------------------------------------------------
801 TkFillPolygon(canvas, coordPtr, numPoints, display, drawable, gc, outlineGC)
802 Tk_Canvas canvas; /* Canvas whose coordinate system
803 * is to be used for drawing. */
804 double *coordPtr; /* Array of coordinates for polygon:
805 * x1, y1, x2, y2, .... */
806 int numPoints; /* Twice this many coordinates are
807 * present at *coordPtr. */
808 Display *display; /* Display on which to draw polygon. */
809 Drawable drawable; /* Pixmap or window in which to draw
811 GC gc; /* Graphics context for drawing. */
812 GC outlineGC; /* If not None, use this to draw an
813 * outline around the polygon after
816 XPoint staticPoints[MAX_STATIC_POINTS];
822 * Build up an array of points in screen coordinates. Use a
823 * static array unless the polygon has an enormous number of points;
824 * in this case, dynamically allocate an array.
827 if (numPoints <= MAX_STATIC_POINTS) {
828 pointPtr = staticPoints;
830 pointPtr = (XPoint *) ckalloc((unsigned) (numPoints * sizeof(XPoint)));
833 for (i = 0, pPtr = pointPtr; i < numPoints; i += 1, coordPtr += 2, pPtr++) {
834 Tk_CanvasDrawableCoords(canvas, coordPtr[0], coordPtr[1], &pPtr->x,
839 * Display polygon, then free up polygon storage if it was dynamically
843 if (gc != None && numPoints>3) {
844 XFillPolygon(display, drawable, gc, pointPtr, numPoints, Complex,
847 if (outlineGC != None) {
848 XDrawLines(display, drawable, outlineGC, pointPtr,
849 numPoints, CoordModeOrigin);
851 if (pointPtr != staticPoints) {
852 ckfree((char *) pointPtr);
857 *--------------------------------------------------------------
861 * This procedure is invoked to draw a polygon item in a given
868 * ItemPtr is drawn in drawable using the transformation
869 * information in canvas.
871 *--------------------------------------------------------------
875 DisplayPolygon(canvas, itemPtr, display, drawable, x, y, width, height)
876 Tk_Canvas canvas; /* Canvas that contains item. */
877 Tk_Item *itemPtr; /* Item to be displayed. */
878 Display *display; /* Display on which to draw item. */
879 Drawable drawable; /* Pixmap or window in which to draw
881 int x, y, width, height; /* Describes region of canvas that
882 * must be redisplayed (not used). */
884 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
885 Tk_State state = itemPtr->state;
886 Pixmap stipple = polyPtr->fillStipple;
887 double linewidth = polyPtr->outline.width;
889 if (((polyPtr->fillGC == None) && (polyPtr->outline.gc == None)) ||
890 (polyPtr->numPoints < 1) ||
891 (polyPtr->numPoints < 3 && polyPtr->outline.gc == None)) {
895 if(state == TK_STATE_NULL) {
896 state = ((TkCanvas *)canvas)->canvas_state;
898 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
899 if (polyPtr->outline.activeWidth>linewidth) {
900 linewidth = polyPtr->outline.activeWidth;
902 if (polyPtr->activeFillStipple != None) {
903 stipple = polyPtr->activeFillStipple;
905 } else if (state==TK_STATE_DISABLED) {
906 if (polyPtr->outline.disabledWidth>0.0) {
907 linewidth = polyPtr->outline.disabledWidth;
909 if (polyPtr->disabledFillStipple != None) {
910 stipple = polyPtr->disabledFillStipple;
914 * If we're stippling then modify the stipple offset in the GC. Be
915 * sure to reset the offset when done, since the GC is supposed to be
919 if ((stipple != None) && (polyPtr->fillGC != None)) {
920 Tk_TSOffset *tsoffset = &polyPtr->tsoffset;
922 int flags = tsoffset->flags;
923 if (!(flags & TK_OFFSET_INDEX) && (flags & (TK_OFFSET_CENTER|TK_OFFSET_MIDDLE))) {
924 Tk_SizeOfBitmap(display, stipple, &w, &h);
925 if (flags & TK_OFFSET_CENTER) {
930 if (flags & TK_OFFSET_MIDDLE) {
936 tsoffset->xoffset -= w;
937 tsoffset->yoffset -= h;
938 Tk_CanvasSetOffset(canvas, polyPtr->fillGC, tsoffset);
939 tsoffset->xoffset += w;
940 tsoffset->yoffset += h;
942 Tk_ChangeOutlineGC(canvas, itemPtr, &(polyPtr->outline));
944 if(polyPtr->numPoints < 3) {
946 int intLineWidth = (int) (linewidth + 0.5);
947 if (intLineWidth < 1) {
950 Tk_CanvasDrawableCoords(canvas, polyPtr->coordPtr[0],
951 polyPtr->coordPtr[1], &x,&y);
952 XFillArc(display, drawable, polyPtr->outline.gc,
953 x - intLineWidth/2, y - intLineWidth/2,
954 (unsigned int)intLineWidth+1, (unsigned int)intLineWidth+1,
956 } else if (!polyPtr->smooth || polyPtr->numPoints < 4) {
957 TkFillPolygon(canvas, polyPtr->coordPtr, polyPtr->numPoints,
958 display, drawable, polyPtr->fillGC, polyPtr->outline.gc);
961 XPoint staticPoints[MAX_STATIC_POINTS];
965 * This is a smoothed polygon. Display using a set of generated
966 * spline points rather than the original points.
969 numPoints = polyPtr->smooth->coordProc(canvas, (double *) NULL,
970 polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,
972 if (numPoints <= MAX_STATIC_POINTS) {
973 pointPtr = staticPoints;
975 pointPtr = (XPoint *) ckalloc((unsigned)
976 (numPoints * sizeof(XPoint)));
978 numPoints = polyPtr->smooth->coordProc(canvas, polyPtr->coordPtr,
979 polyPtr->numPoints, polyPtr->splineSteps, pointPtr,
981 if (polyPtr->fillGC != None) {
982 XFillPolygon(display, drawable, polyPtr->fillGC, pointPtr,
983 numPoints, Complex, CoordModeOrigin);
985 if (polyPtr->outline.gc != None) {
986 XDrawLines(display, drawable, polyPtr->outline.gc, pointPtr,
987 numPoints, CoordModeOrigin);
989 if (pointPtr != staticPoints) {
990 ckfree((char *) pointPtr);
993 Tk_ResetOutlineGC(canvas, itemPtr, &(polyPtr->outline));
994 if ((stipple != None) && (polyPtr->fillGC != None)) {
995 XSetTSOrigin(display, polyPtr->fillGC, 0, 0);
1000 *--------------------------------------------------------------
1004 * Insert coords into a polugon item at a given index.
1010 * The coords in the given item is modified.
1012 *--------------------------------------------------------------
1016 PolygonInsert(canvas, itemPtr, beforeThis, obj)
1017 Tk_Canvas canvas; /* Canvas containing text item. */
1018 Tk_Item *itemPtr; /* Line item to be modified. */
1019 int beforeThis; /* Index before which new coordinates
1020 * are to be inserted. */
1021 Tcl_Obj *obj; /* New coordinates to be inserted. */
1023 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1024 int length, objc, i;
1027 Tk_State state = itemPtr->state;
1029 if (state == TK_STATE_NULL) {
1030 state = ((TkCanvas *)canvas)->canvas_state;
1033 if (!obj || (Tcl_ListObjGetElements((Tcl_Interp *) NULL, obj, &objc, &objv) != TCL_OK)
1034 || !objc || objc&1) {
1037 length = 2*(polyPtr->numPoints - polyPtr->autoClosed);
1038 while(beforeThis>length) beforeThis-=length;
1039 while(beforeThis<0) beforeThis+=length;
1040 new = (double *) ckalloc((unsigned)(sizeof(double) * (length + 2 + objc)));
1041 for (i=0; i<beforeThis; i++) {
1042 new[i] = polyPtr->coordPtr[i];
1044 for (i=0; i<objc; i++) {
1045 if (Tcl_GetDoubleFromObj((Tcl_Interp *) NULL,objv[i],
1046 new+(i+beforeThis))!=TCL_OK) {
1047 ckfree((char *) new);
1052 for(i=beforeThis; i<length; i++) {
1053 new[i+objc] = polyPtr->coordPtr[i];
1055 if(polyPtr->coordPtr) ckfree((char *) polyPtr->coordPtr);
1057 polyPtr->coordPtr = new;
1058 polyPtr->numPoints = (length/2) + polyPtr->autoClosed;
1061 * Close the polygon if it isn't already closed, or remove autoclosing
1062 * if the user's coordinates are now closed.
1065 if (polyPtr->autoClosed) {
1066 if ((new[length-2] == new[0]) && (new[length-1] == new[1])) {
1067 polyPtr->autoClosed = 0;
1068 polyPtr->numPoints--;
1072 if ((new[length-2] != new[0]) || (new[length-1] != new[1])) {
1073 polyPtr->autoClosed = 1;
1074 polyPtr->numPoints++;
1078 new[length] = new[0];
1079 new[length+1] = new[1];
1080 if (((length-objc)>3) && (state != TK_STATE_HIDDEN)) {
1082 * This is some optimizing code that will result that only the part
1083 * of the polygon that changed (and the objects that are overlapping
1084 * with that part) need to be redrawn. A special flag is set that
1085 * instructs the general canvas code not to redraw the whole
1086 * object. If this flag is not set, the canvas will do the redrawing,
1087 * otherwise I have to do it here.
1091 itemPtr->redraw_flags |= TK_ITEM_DONT_REDRAW;
1094 * The header elements that normally are used for the
1095 * bounding box, are now used to calculate the bounding
1096 * box for only the part that has to be redrawn. That
1097 * doesn't matter, because afterwards the bounding
1098 * box has to be re-calculated anyway.
1101 itemPtr->x1 = itemPtr->x2 = (int) polyPtr->coordPtr[beforeThis];
1102 itemPtr->y1 = itemPtr->y2 = (int) polyPtr->coordPtr[beforeThis+1];
1103 beforeThis-=2; objc+=4;
1104 if(polyPtr->smooth) {
1105 beforeThis-=2; objc+=4;
1106 } /* be carefull; beforeThis could now be negative */
1107 for(i=beforeThis; i<beforeThis+objc; i+=2) {
1110 if(j>=length) j-=length;
1111 TkIncludePoint(itemPtr, polyPtr->coordPtr+j);
1113 width = polyPtr->outline.width;
1114 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1115 if (polyPtr->outline.activeWidth>width) {
1116 width = polyPtr->outline.activeWidth;
1118 } else if (state==TK_STATE_DISABLED) {
1119 if (polyPtr->outline.disabledWidth>0.0) {
1120 width = polyPtr->outline.disabledWidth;
1123 itemPtr->x1 -= (int) width; itemPtr->y1 -= (int) width;
1124 itemPtr->x2 += (int) width; itemPtr->y2 += (int) width;
1125 Tk_CanvasEventuallyRedraw(canvas,
1126 itemPtr->x1, itemPtr->y1,
1127 itemPtr->x2, itemPtr->y2);
1130 ComputePolygonBbox(canvas, polyPtr);
1134 *--------------------------------------------------------------
1136 * PolygonDeleteCoords --
1138 * Delete one or more coordinates from a polygon item.
1144 * Characters between "first" and "last", inclusive, get
1145 * deleted from itemPtr.
1147 *--------------------------------------------------------------
1151 PolygonDeleteCoords(canvas, itemPtr, first, last)
1152 Tk_Canvas canvas; /* Canvas containing itemPtr. */
1153 Tk_Item *itemPtr; /* Item in which to delete characters. */
1154 int first; /* Index of first character to delete. */
1155 int last; /* Index of last character to delete. */
1157 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1159 int length = 2*(polyPtr->numPoints - polyPtr->autoClosed);
1161 while(first>=length) first-=length;
1162 while(first<0) first+=length;
1163 while(last>=length) last-=length;
1164 while(last<0) last+=length;
1169 count = last + 2 - first;
1170 if(count<=0) count +=length;
1172 if(count >= length) {
1173 polyPtr->numPoints = 0;
1174 if(polyPtr->coordPtr != NULL) {
1175 ckfree((char *) polyPtr->coordPtr);
1177 ComputePolygonBbox(canvas, polyPtr);
1182 for(i=last+2; i<length; i++) {
1183 polyPtr->coordPtr[i-count] = polyPtr->coordPtr[i];
1186 for(i=last; i<=first; i++) {
1187 polyPtr->coordPtr[i-last] = polyPtr->coordPtr[i];
1190 polyPtr->coordPtr[length-count] = polyPtr->coordPtr[0];
1191 polyPtr->coordPtr[length-count+1] = polyPtr->coordPtr[1];
1192 polyPtr->numPoints -= count/2;
1193 ComputePolygonBbox(canvas, polyPtr);
1197 *--------------------------------------------------------------
1201 * Computes the distance from a given point to a given
1202 * polygon, in canvas units.
1205 * The return value is 0 if the point whose x and y coordinates
1206 * are pointPtr[0] and pointPtr[1] is inside the polygon. If the
1207 * point isn't inside the polygon then the return value is the
1208 * distance from the point to the polygon.
1213 *--------------------------------------------------------------
1218 PolygonToPoint(canvas, itemPtr, pointPtr)
1219 Tk_Canvas canvas; /* Canvas containing item. */
1220 Tk_Item *itemPtr; /* Item to check against point. */
1221 double *pointPtr; /* Pointer to x and y coordinates. */
1223 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1224 double *coordPtr, *polyPoints;
1225 double staticSpace[2*MAX_STATIC_POINTS];
1228 double bestDist, dist;
1229 int numPoints, count;
1230 int changedMiterToBevel; /* Non-zero means that a mitered corner
1231 * had to be treated as beveled after all
1232 * because the angle was < 11 degrees. */
1234 Tk_State state = itemPtr->state;
1238 if(state == TK_STATE_NULL) {
1239 state = ((TkCanvas *)canvas)->canvas_state;
1241 width = polyPtr->outline.width;
1242 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1243 if (polyPtr->outline.activeWidth>width) {
1244 width = polyPtr->outline.activeWidth;
1246 } else if (state==TK_STATE_DISABLED) {
1247 if (polyPtr->outline.disabledWidth>0.0) {
1248 width = polyPtr->outline.disabledWidth;
1254 * Handle smoothed polygons by generating an expanded set of points
1255 * against which to do the check.
1258 if ((polyPtr->smooth) && (polyPtr->numPoints>2)) {
1259 numPoints = polyPtr->smooth->coordProc(canvas, (double *) NULL,
1260 polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,
1262 if (numPoints <= MAX_STATIC_POINTS) {
1263 polyPoints = staticSpace;
1265 polyPoints = (double *) ckalloc((unsigned)
1266 (2*numPoints*sizeof(double)));
1268 numPoints = polyPtr->smooth->coordProc(canvas, polyPtr->coordPtr,
1269 polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,
1272 numPoints = polyPtr->numPoints;
1273 polyPoints = polyPtr->coordPtr;
1276 bestDist = TkPolygonToPoint(polyPoints, numPoints, pointPtr);
1277 if (bestDist<=0.0) {
1280 if ((polyPtr->outline.gc != None) && (polyPtr->joinStyle == JoinRound)) {
1281 dist = bestDist - radius;
1290 if ((polyPtr->outline.gc == None) || (width <= 1)) goto donepoint;
1293 * The overall idea is to iterate through all of the edges of
1294 * the line, computing a polygon for each edge and testing the
1295 * point against that polygon. In addition, there are additional
1296 * tests to deal with rounded joints and caps.
1299 changedMiterToBevel = 0;
1300 for (count = numPoints, coordPtr = polyPoints; count >= 2;
1301 count--, coordPtr += 2) {
1304 * If rounding is done around the first point then compute
1305 * the distance between the point and the point.
1308 if (polyPtr->joinStyle == JoinRound) {
1309 dist = hypot(coordPtr[0] - pointPtr[0], coordPtr[1] - pointPtr[1])
1314 } else if (dist < bestDist) {
1320 * Compute the polygonal shape corresponding to this edge,
1321 * consisting of two points for the first point of the edge
1322 * and two points for the last point of the edge.
1325 if (count == numPoints) {
1326 TkGetButtPoints(coordPtr+2, coordPtr, (double) width,
1328 } else if ((polyPtr->joinStyle == JoinMiter) && !changedMiterToBevel) {
1334 TkGetButtPoints(coordPtr+2, coordPtr, (double) width, 0,
1338 * If this line uses beveled joints, then check the distance
1339 * to a polygon comprising the last two points of the previous
1340 * polygon and the first two from this polygon; this checks
1341 * the wedges that fill the mitered joint.
1344 if ((polyPtr->joinStyle == JoinBevel) || changedMiterToBevel) {
1347 dist = TkPolygonToPoint(poly, 5, pointPtr);
1351 } else if (dist < bestDist) {
1354 changedMiterToBevel = 0;
1358 TkGetButtPoints(coordPtr, coordPtr+2, (double) width,
1360 } else if (polyPtr->joinStyle == JoinMiter) {
1361 if (TkGetMiterPoints(coordPtr, coordPtr+2, coordPtr+4,
1362 (double) width, poly+4, poly+6) == 0) {
1363 changedMiterToBevel = 1;
1364 TkGetButtPoints(coordPtr, coordPtr+2, (double) width,
1368 TkGetButtPoints(coordPtr, coordPtr+2, (double) width, 0,
1373 dist = TkPolygonToPoint(poly, 5, pointPtr);
1377 } else if (dist < bestDist) {
1383 if ((polyPoints != staticSpace) && polyPoints != polyPtr->coordPtr) {
1384 ckfree((char *) polyPoints);
1390 *--------------------------------------------------------------
1394 * This procedure is called to determine whether an item
1395 * lies entirely inside, entirely outside, or overlapping
1396 * a given rectangular area.
1399 * -1 is returned if the item is entirely outside the area
1400 * given by rectPtr, 0 if it overlaps, and 1 if it is entirely
1401 * inside the given area.
1406 *--------------------------------------------------------------
1411 PolygonToArea(canvas, itemPtr, rectPtr)
1412 Tk_Canvas canvas; /* Canvas containing item. */
1413 Tk_Item *itemPtr; /* Item to check against polygon. */
1414 double *rectPtr; /* Pointer to array of four coordinates
1415 * (x1, y1, x2, y2) describing rectangular
1418 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1420 double staticSpace[2*MAX_STATIC_POINTS];
1421 double *polyPoints, poly[10];
1423 int numPoints, count;
1424 int changedMiterToBevel; /* Non-zero means that a mitered corner
1425 * had to be treated as beveled after all
1426 * because the angle was < 11 degrees. */
1427 int inside; /* Tentative guess about what to return,
1428 * based on all points seen so far: one
1429 * means everything seen so far was
1430 * inside the area; -1 means everything
1431 * was outside the area. 0 means overlap
1432 * has been found. */
1434 Tk_State state = itemPtr->state;
1436 if(state == TK_STATE_NULL) {
1437 state = ((TkCanvas *)canvas)->canvas_state;
1440 width = polyPtr->outline.width;
1441 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1442 if (polyPtr->outline.activeWidth>width) {
1443 width = polyPtr->outline.activeWidth;
1445 } else if (state==TK_STATE_DISABLED) {
1446 if (polyPtr->outline.disabledWidth>0.0) {
1447 width = polyPtr->outline.disabledWidth;
1454 if ((state==TK_STATE_HIDDEN) || polyPtr->numPoints<2) {
1456 } else if (polyPtr->numPoints <3) {
1458 oval[0] = polyPtr->coordPtr[0]-radius;
1459 oval[1] = polyPtr->coordPtr[1]-radius;
1460 oval[2] = polyPtr->coordPtr[0]+radius;
1461 oval[3] = polyPtr->coordPtr[1]+radius;
1462 return TkOvalToArea(oval, rectPtr);
1465 * Handle smoothed polygons by generating an expanded set of points
1466 * against which to do the check.
1469 if (polyPtr->smooth) {
1470 numPoints = polyPtr->smooth->coordProc(canvas, (double *) NULL,
1471 polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,
1473 if (numPoints <= MAX_STATIC_POINTS) {
1474 polyPoints = staticSpace;
1476 polyPoints = (double *) ckalloc((unsigned)
1477 (2*numPoints*sizeof(double)));
1479 numPoints = polyPtr->smooth->coordProc(canvas, polyPtr->coordPtr,
1480 polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,
1483 numPoints = polyPtr->numPoints;
1484 polyPoints = polyPtr->coordPtr;
1488 * Simple test to see if we are in the polygon. Polygons are
1489 * different from othe canvas items in that they register points
1490 * being inside even if it isn't filled.
1492 inside = TkPolygonToArea(polyPoints, numPoints, rectPtr);
1493 if (inside==0) goto donearea;
1495 if (polyPtr->outline.gc == None) goto donearea ;
1498 * Iterate through all of the edges of the line, computing a polygon
1499 * for each edge and testing the area against that polygon. In
1500 * addition, there are additional tests to deal with rounded joints
1504 changedMiterToBevel = 0;
1505 for (count = numPoints, coordPtr = polyPoints; count >= 2;
1506 count--, coordPtr += 2) {
1509 * If rounding is done around the first point of the edge
1510 * then test a circular region around the point with the
1514 if (polyPtr->joinStyle == JoinRound) {
1515 poly[0] = coordPtr[0] - radius;
1516 poly[1] = coordPtr[1] - radius;
1517 poly[2] = coordPtr[0] + radius;
1518 poly[3] = coordPtr[1] + radius;
1519 if (TkOvalToArea(poly, rectPtr) != inside) {
1526 * Compute the polygonal shape corresponding to this edge,
1527 * consisting of two points for the first point of the edge
1528 * and two points for the last point of the edge.
1531 if (count == numPoints) {
1532 TkGetButtPoints(coordPtr+2, coordPtr, width,
1534 } else if ((polyPtr->joinStyle == JoinMiter) && !changedMiterToBevel) {
1540 TkGetButtPoints(coordPtr+2, coordPtr, width, 0,
1544 * If the last joint was beveled, then also check a
1545 * polygon comprising the last two points of the previous
1546 * polygon and the first two from this polygon; this checks
1547 * the wedges that fill the beveled joint.
1550 if ((polyPtr->joinStyle == JoinBevel) || changedMiterToBevel) {
1553 if (TkPolygonToArea(poly, 5, rectPtr) != inside) {
1557 changedMiterToBevel = 0;
1561 TkGetButtPoints(coordPtr, coordPtr+2, width,
1563 } else if (polyPtr->joinStyle == JoinMiter) {
1564 if (TkGetMiterPoints(coordPtr, coordPtr+2, coordPtr+4,
1565 width, poly+4, poly+6) == 0) {
1566 changedMiterToBevel = 1;
1567 TkGetButtPoints(coordPtr, coordPtr+2, width,
1571 TkGetButtPoints(coordPtr, coordPtr+2, width, 0,
1576 if (TkPolygonToArea(poly, 5, rectPtr) != inside) {
1583 if ((polyPoints != staticSpace) && (polyPoints != polyPtr->coordPtr)) {
1584 ckfree((char *) polyPoints);
1590 *--------------------------------------------------------------
1594 * This procedure is invoked to rescale a polygon item.
1600 * The polygon referred to by itemPtr is rescaled so that the
1601 * following transformation is applied to all point
1603 * x' = originX + scaleX*(x-originX)
1604 * y' = originY + scaleY*(y-originY)
1606 *--------------------------------------------------------------
1610 ScalePolygon(canvas, itemPtr, originX, originY, scaleX, scaleY)
1611 Tk_Canvas canvas; /* Canvas containing polygon. */
1612 Tk_Item *itemPtr; /* Polygon to be scaled. */
1613 double originX, originY; /* Origin about which to scale rect. */
1614 double scaleX; /* Amount to scale in X direction. */
1615 double scaleY; /* Amount to scale in Y direction. */
1617 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1621 for (i = 0, coordPtr = polyPtr->coordPtr; i < polyPtr->numPoints;
1622 i++, coordPtr += 2) {
1623 *coordPtr = originX + scaleX*(*coordPtr - originX);
1624 coordPtr[1] = originY + scaleY*(coordPtr[1] - originY);
1626 ComputePolygonBbox(canvas, polyPtr);
1630 *--------------------------------------------------------------
1632 * GetPolygonIndex --
1634 * Parse an index into a polygon item and return either its value
1638 * A standard Tcl result. If all went well, then *indexPtr is
1639 * filled in with the index (into itemPtr) corresponding to
1640 * string. Otherwise an error message is left in
1646 *--------------------------------------------------------------
1650 GetPolygonIndex(interp, canvas, itemPtr, obj, indexPtr)
1651 Tcl_Interp *interp; /* Used for error reporting. */
1652 Tk_Canvas canvas; /* Canvas containing item. */
1653 Tk_Item *itemPtr; /* Item for which the index is being
1655 Tcl_Obj *obj; /* Specification of a particular coord
1656 * in itemPtr's line. */
1657 int *indexPtr; /* Where to store converted index. */
1659 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1661 char *string = Tcl_GetStringFromObj(obj, (int *) &length);
1663 if (string[0] == 'e') {
1664 if (strncmp(string, "end", length) == 0) {
1665 *indexPtr = 2*(polyPtr->numPoints - polyPtr->autoClosed);
1670 * Some of the paths here leave messages in interp->result,
1671 * so we have to clear it out before storing our own message.
1674 Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
1675 Tcl_AppendResult(interp, "bad index \"", string, "\"",
1679 } else if (string[0] == '@') {
1681 double x ,y, bestDist, dist, *coordPtr;
1685 x = strtod(p, &end);
1686 if ((end == p) || (*end != ',')) {
1690 y = strtod(p, &end);
1691 if ((end == p) || (*end != 0)) {
1695 coordPtr = polyPtr->coordPtr;
1697 for(i=0; i<(polyPtr->numPoints-1); i++) {
1698 dist = hypot(coordPtr[0] - x, coordPtr[1] - y);
1699 if (dist<bestDist) {
1706 int count = 2*(polyPtr->numPoints - polyPtr->autoClosed);
1707 if (Tcl_GetIntFromObj(interp, obj, indexPtr) != TCL_OK) {
1710 *indexPtr &= -2; /* if odd, make it even */
1712 if (*indexPtr > 0) {
1713 *indexPtr = ((*indexPtr - 2) % count) + 2;
1715 *indexPtr = -((-(*indexPtr)) % count);
1725 *--------------------------------------------------------------
1727 * TranslatePolygon --
1729 * This procedure is called to move a polygon by a given
1736 * The position of the polygon is offset by (xDelta, yDelta),
1737 * and the bounding box is updated in the generic part of the
1740 *--------------------------------------------------------------
1744 TranslatePolygon(canvas, itemPtr, deltaX, deltaY)
1745 Tk_Canvas canvas; /* Canvas containing item. */
1746 Tk_Item *itemPtr; /* Item that is being moved. */
1747 double deltaX, deltaY; /* Amount by which item is to be
1750 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1754 for (i = 0, coordPtr = polyPtr->coordPtr; i < polyPtr->numPoints;
1755 i++, coordPtr += 2) {
1756 *coordPtr += deltaX;
1757 coordPtr[1] += deltaY;
1759 ComputePolygonBbox(canvas, polyPtr);
1763 *--------------------------------------------------------------
1765 * PolygonToPostscript --
1767 * This procedure is called to generate Postscript for
1771 * The return value is a standard Tcl result. If an error
1772 * occurs in generating Postscript then an error message is
1773 * left in the interp's result, replacing whatever used
1774 * to be there. If no error occurs, then Postscript for the
1775 * item is appended to the result.
1780 *--------------------------------------------------------------
1784 PolygonToPostscript(interp, canvas, itemPtr, prepass)
1785 Tcl_Interp *interp; /* Leave Postscript or error message
1787 Tk_Canvas canvas; /* Information about overall canvas. */
1788 Tk_Item *itemPtr; /* Item for which Postscript is
1790 int prepass; /* 1 means this is a prepass to
1791 * collect font information; 0 means
1792 * final Postscript is being created. */
1794 PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1800 Tk_State state = itemPtr->state;
1803 if (polyPtr->numPoints<2 || polyPtr->coordPtr==NULL) {
1807 if(state == TK_STATE_NULL) {
1808 state = ((TkCanvas *)canvas)->canvas_state;
1810 width = polyPtr->outline.width;
1811 color = polyPtr->outline.color;
1812 stipple = polyPtr->fillStipple;
1813 fillColor = polyPtr->fillColor;
1814 fillStipple = polyPtr->fillStipple;
1815 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1816 if (polyPtr->outline.activeWidth>width) {
1817 width = polyPtr->outline.activeWidth;
1819 if (polyPtr->outline.activeColor!=NULL) {
1820 color = polyPtr->outline.activeColor;
1822 if (polyPtr->outline.activeStipple!=None) {
1823 stipple = polyPtr->outline.activeStipple;
1825 if (polyPtr->activeFillColor!=NULL) {
1826 fillColor = polyPtr->activeFillColor;
1828 if (polyPtr->activeFillStipple!=None) {
1829 fillStipple = polyPtr->activeFillStipple;
1831 } else if (state==TK_STATE_DISABLED) {
1832 if (polyPtr->outline.disabledWidth>0.0) {
1833 width = polyPtr->outline.disabledWidth;
1835 if (polyPtr->outline.disabledColor!=NULL) {
1836 color = polyPtr->outline.disabledColor;
1838 if (polyPtr->outline.disabledStipple!=None) {
1839 stipple = polyPtr->outline.disabledStipple;
1841 if (polyPtr->disabledFillColor!=NULL) {
1842 fillColor = polyPtr->disabledFillColor;
1844 if (polyPtr->disabledFillStipple!=None) {
1845 fillStipple = polyPtr->disabledFillStipple;
1848 if (polyPtr->numPoints==2) {
1850 sprintf(string, "%.15g %.15g translate %.15g %.15g",
1851 polyPtr->coordPtr[0], Tk_CanvasPsY(canvas, polyPtr->coordPtr[1]),
1852 width/2.0, width/2.0);
1853 Tcl_AppendResult(interp, "matrix currentmatrix\n",string,
1854 " scale 1 0 moveto 0 0 1 0 360 arc\nsetmatrix\n", (char *) NULL);
1855 if (Tk_CanvasPsColor(interp, canvas, color)
1859 if (stipple != None) {
1860 Tcl_AppendResult(interp, "clip ", (char *) NULL);
1861 if (Tk_CanvasPsStipple(interp, canvas, stipple) != TCL_OK) {
1865 Tcl_AppendResult(interp, "fill\n", (char *) NULL);
1871 * Fill the area of the polygon.
1874 if (fillColor != NULL && polyPtr->numPoints>3) {
1875 if (!polyPtr->smooth || !polyPtr->smooth->postscriptProc) {
1876 Tk_CanvasPsPath(interp, canvas, polyPtr->coordPtr,
1877 polyPtr->numPoints);
1879 polyPtr->smooth->postscriptProc(interp, canvas, polyPtr->coordPtr,
1880 polyPtr->numPoints, polyPtr->splineSteps);
1882 if (Tk_CanvasPsColor(interp, canvas, fillColor) != TCL_OK) {
1885 if (fillStipple != None) {
1886 Tcl_AppendResult(interp, "eoclip ", (char *) NULL);
1887 if (Tk_CanvasPsStipple(interp, canvas, fillStipple)
1891 if (color != NULL) {
1892 Tcl_AppendResult(interp, "grestore gsave\n", (char *) NULL);
1895 Tcl_AppendResult(interp, "eofill\n", (char *) NULL);
1900 * Now draw the outline, if there is one.
1903 if (color != NULL) {
1905 if (!polyPtr->smooth || !polyPtr->smooth->postscriptProc) {
1906 Tk_CanvasPsPath(interp, canvas, polyPtr->coordPtr,
1907 polyPtr->numPoints);
1909 polyPtr->smooth->postscriptProc(interp, canvas, polyPtr->coordPtr,
1910 polyPtr->numPoints, polyPtr->splineSteps);
1913 if (polyPtr->joinStyle == JoinRound) {
1915 } else if (polyPtr->joinStyle == JoinBevel) {
1920 Tcl_AppendResult(interp, style," setlinejoin 1 setlinecap\n",
1922 if (Tk_CanvasPsOutline(canvas, itemPtr,
1923 &(polyPtr->outline)) != TCL_OK) {