OSDN Git Service

Merge branch 'master' of git://github.com/monaka/binutils
[pf3gnuchains/pf3gnuchains3x.git] / tk / generic / tkCanvPoly.c
1 /* 
2  * tkCanvPoly.c --
3  *
4  *      This file implements polygon items for canvas widgets.
5  *
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.
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$
14  */
15
16 #include <stdio.h>
17 #include "tkInt.h"
18 #include "tkPort.h"
19 #include "tkCanvas.h"
20
21 /*
22  * The structure below defines the record for each polygon item.
23  */
24
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 */
38     Tk_TSOffset tsoffset;
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. */
51 } PolygonItem;
52
53 /*
54  * Information used for parsing configuration specs:
55  */
56
57 static Tk_CustomOption smoothOption = {
58     (Tk_OptionParseProc *) TkSmoothParseProc,
59     TkSmoothPrintProc, (ClientData) NULL
60 };
61 static Tk_CustomOption stateOption = {
62     (Tk_OptionParseProc *) TkStateParseProc,
63     TkStatePrintProc, (ClientData) 2
64 };
65 static Tk_CustomOption tagsOption = {
66     (Tk_OptionParseProc *) Tk_CanvasTagsParseProc,
67     Tk_CanvasTagsPrintProc, (ClientData) NULL
68 };
69 static Tk_CustomOption dashOption = {
70     (Tk_OptionParseProc *) TkCanvasDashParseProc,
71     TkCanvasDashPrintProc, (ClientData) NULL
72 };
73 static Tk_CustomOption offsetOption = {
74     (Tk_OptionParseProc *) TkOffsetParseProc,
75     TkOffsetPrintProc,
76     (ClientData) (TK_OFFSET_RELATIVE|TK_OFFSET_INDEX)
77 };
78 static Tk_CustomOption pixelOption = {
79     (Tk_OptionParseProc *) TkPixelParseProc,
80     TkPixelPrintProc, (ClientData) NULL
81 };
82
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),
89         TK_CONFIG_NULL_OK},
90     {TK_CONFIG_COLOR, "-activeoutline", (char *) NULL, (char *) NULL,
91         (char *) NULL, Tk_Offset(PolygonItem, outline.activeColor),
92         TK_CONFIG_NULL_OK},
93     {TK_CONFIG_BITMAP, "-activeoutlinestipple", (char *) NULL, (char *) NULL,
94         (char *) NULL, Tk_Offset(PolygonItem, outline.activeStipple),
95         TK_CONFIG_NULL_OK},
96     {TK_CONFIG_BITMAP, "-activestipple", (char *) NULL, (char *) NULL,
97         (char *) NULL, Tk_Offset(PolygonItem, activeFillStipple),
98         TK_CONFIG_NULL_OK},
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),
113         TK_CONFIG_NULL_OK},
114     {TK_CONFIG_COLOR, "-disabledoutline", (char *) NULL, (char *) NULL,
115         (char *) NULL, Tk_Offset(PolygonItem, outline.disabledColor),
116         TK_CONFIG_NULL_OK},
117     {TK_CONFIG_BITMAP, "-disabledoutlinestipple", (char *) NULL, (char *) NULL,
118         (char *) NULL, Tk_Offset(PolygonItem, outline.disabledStipple),
119         TK_CONFIG_NULL_OK},
120     {TK_CONFIG_BITMAP, "-disabledstipple", (char *) NULL, (char *) NULL,
121         (char *) NULL, Tk_Offset(PolygonItem, disabledFillStipple),
122         TK_CONFIG_NULL_OK},
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),
135         TK_CONFIG_NULL_OK},
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),
141         TK_CONFIG_NULL_OK},
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,
149         &stateOption},
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,
158         (char *) NULL, 0, 0}
159 };
160
161 /*
162  * Prototypes for procedures defined in this file:
163  */
164
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));
199
200 /*
201  * The structures below defines the polygon item type by means
202  * of procedures that can be invoked by generic item code.
203  */
204
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 */
226 };
227
228 /*
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).
232  */
233
234 #define MAX_STATIC_POINTS 200
235 \f
236 /*
237  *--------------------------------------------------------------
238  *
239  * CreatePolygon --
240  *
241  *      This procedure is invoked to create a new polygon item in
242  *      a canvas.
243  *
244  * Results:
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
249  *      caller.
250  *
251  * Side effects:
252  *      A new polygon item is created.
253  *
254  *--------------------------------------------------------------
255  */
256
257 static int
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. */
265 {
266     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
267     int i;
268
269     /*
270      * Carry out initialization that is needed in order to clean
271      * up after errors during the the remainder of this procedure.
272      */
273
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;
292
293     /*
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.
297      */
298
299     for (i = 0; i < objc; i++) {
300         char *arg = Tcl_GetString(objv[i]);
301         if ((arg[0] == '-') && (arg[1] >= 'a') && (arg[1] <= 'z')) {
302             break;
303         }
304     }
305     if (i && PolygonCoords(interp, canvas, itemPtr, i, objv) != TCL_OK) {
306         goto error;
307     }
308
309     if (ConfigurePolygon(interp, canvas, itemPtr, objc-i, objv+i, 0)
310             == TCL_OK) {
311         return TCL_OK;
312     }
313
314     error:
315     DeletePolygon(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
316     return TCL_ERROR;
317 }
318 \f
319 /*
320  *--------------------------------------------------------------
321  *
322  * PolygonCoords --
323  *
324  *      This procedure is invoked to process the "coords" widget
325  *      command on polygons.  See the user documentation for details
326  *      on what it does.
327  *
328  * Results:
329  *      Returns TCL_OK or TCL_ERROR, and sets the interp's result.
330  *
331  * Side effects:
332  *      The coordinates for the given item may be changed.
333  *
334  *--------------------------------------------------------------
335  */
336
337 static int
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
344                                          * objv. */
345     Tcl_Obj *CONST objv[];              /* Array of coordinates: x1, y1,
346                                          * x2, y2, ... */
347 {
348     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
349     int i, numPoints;
350
351     if (objc == 0) {
352         /*
353          * Print the coords used to create the polygon.  If we auto
354          * closed the polygon then we don't report the last point.
355          */
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);
360         }
361         Tcl_SetObjResult(interp, obj);
362         return TCL_OK;
363     }
364     if (objc == 1) {
365         if (Tcl_ListObjGetElements(interp, objv[0], &objc,
366                 (Tcl_Obj ***) &objv) != TCL_OK) {
367             return TCL_ERROR;
368         }
369     }
370     if (objc & 1) {
371         Tcl_AppendResult(interp,
372                 "odd number of coordinates specified for polygon",
373                 (char *) NULL);
374         return TCL_ERROR;
375     } else {
376         numPoints = objc/2;
377         if (polyPtr->pointsAllocated <= numPoints) {
378             if (polyPtr->coordPtr != NULL) {
379                 ckfree((char *) polyPtr->coordPtr);
380             }
381
382             /*
383              * One extra point gets allocated here, because we always
384              * add another point to close the polygon.
385              */
386
387             polyPtr->coordPtr = (double *) ckalloc((unsigned)
388                     (sizeof(double) * (objc+2)));
389             polyPtr->pointsAllocated = numPoints+1;
390         }
391         for (i = objc-1; i >= 0; i--) {
392             if (Tk_CanvasGetCoordFromObj(interp, canvas, objv[i],
393                     &polyPtr->coordPtr[i]) != TCL_OK) {
394                 return TCL_ERROR;
395             }
396         }
397         polyPtr->numPoints = numPoints;
398         polyPtr->autoClosed = 0;
399
400         /*
401          * Close the polygon if it isn't already closed.
402          */
403     
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];
410         }
411         ComputePolygonBbox(canvas, polyPtr);
412     }
413     return TCL_OK;
414 }
415 \f
416 /*
417  *--------------------------------------------------------------
418  *
419  * ConfigurePolygon --
420  *
421  *      This procedure is invoked to configure various aspects
422  *      of a polygon item such as its background color.
423  *
424  * Results:
425  *      A standard Tcl result code.  If an error occurs, then
426  *      an error message is left in the interp's result.
427  *
428  * Side effects:
429  *      Configuration information, such as colors and stipple
430  *      patterns, may be set for itemPtr.
431  *
432  *--------------------------------------------------------------
433  */
434
435 static int
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. */
443 {
444     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
445     XGCValues gcValues;
446     GC newGC;
447     unsigned long mask;
448     Tk_Window tkwin;
449     XColor *color;
450     Pixmap stipple;
451     Tk_State state;
452
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)) {
456         return TCL_ERROR;
457     }
458
459     /*
460      * A few of the options require additional processing, such as
461      * graphics contexts.
462      */
463
464     state = itemPtr->state;
465
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;
473     } else {
474         itemPtr->redraw_flags &= ~TK_ITEM_STATE_DEPENDANT;
475     }
476
477     if(state == TK_STATE_NULL) {
478         state = ((TkCanvas *)canvas)->canvas_state;
479     }
480     if (state==TK_STATE_HIDDEN) {
481         ComputePolygonBbox(canvas, polyPtr);
482         return TCL_OK;
483     }
484
485     mask = Tk_ConfigOutlineGC(&gcValues, canvas, itemPtr, &(polyPtr->outline));
486     if (mask) {
487         gcValues.cap_style = CapRound;
488         gcValues.join_style = polyPtr->joinStyle;
489         mask |= GCCapStyle|GCJoinStyle;
490         newGC = Tk_GetGC(tkwin, mask, &gcValues);
491     } else {
492         newGC = None;
493     }
494     if (polyPtr->outline.gc != None) {
495         Tk_FreeGC(Tk_Display(tkwin), polyPtr->outline.gc);
496     }
497     polyPtr->outline.gc = newGC;
498
499     color = polyPtr->fillColor;
500     stipple = polyPtr->fillStipple;
501     if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
502         if (polyPtr->activeFillColor!=NULL) {
503             color = polyPtr->activeFillColor;
504         }
505         if (polyPtr->activeFillStipple!=None) {
506             stipple = polyPtr->activeFillStipple;
507         }
508     } else if (state==TK_STATE_DISABLED) {
509         if (polyPtr->disabledFillColor!=NULL) {
510             color = polyPtr->disabledFillColor;
511         }
512         if (polyPtr->disabledFillStipple!=None) {
513             stipple = polyPtr->disabledFillStipple;
514         }
515     }
516
517     if (color == NULL) {
518         newGC = None;
519     } else {
520         gcValues.foreground = color->pixel;
521         mask = GCForeground;
522         if (stipple != None) {
523             gcValues.stipple = stipple;
524             gcValues.fill_style = FillStippled;
525             mask |= GCStipple|GCFillStyle;
526         }
527         newGC = Tk_GetGC(tkwin, mask, &gcValues);
528     }
529     if (polyPtr->fillGC != None) {
530         Tk_FreeGC(Tk_Display(tkwin), polyPtr->fillGC);
531     }
532     polyPtr->fillGC = newGC;
533
534     /*
535      * Keep spline parameters within reasonable limits.
536      */
537
538     if (polyPtr->splineSteps < 1) {
539         polyPtr->splineSteps = 1;
540     } else if (polyPtr->splineSteps > 100) {
541         polyPtr->splineSteps = 100;
542     }
543
544     ComputePolygonBbox(canvas, polyPtr);
545     return TCL_OK;
546 }
547 \f
548 /*
549  *--------------------------------------------------------------
550  *
551  * DeletePolygon --
552  *
553  *      This procedure is called to clean up the data structure
554  *      associated with a polygon item.
555  *
556  * Results:
557  *      None.
558  *
559  * Side effects:
560  *      Resources associated with itemPtr are released.
561  *
562  *--------------------------------------------------------------
563  */
564
565 static void
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
570                                          * canvas. */
571 {
572     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
573
574     Tk_DeleteOutline(display,&(polyPtr->outline));
575     if (polyPtr->coordPtr != NULL) {
576         ckfree((char *) polyPtr->coordPtr);
577     }
578     if (polyPtr->fillColor != NULL) {
579         Tk_FreeColor(polyPtr->fillColor);
580     }
581     if (polyPtr->activeFillColor != NULL) {
582         Tk_FreeColor(polyPtr->activeFillColor);
583     }
584     if (polyPtr->disabledFillColor != NULL) {
585         Tk_FreeColor(polyPtr->disabledFillColor);
586     }
587     if (polyPtr->fillStipple != None) {
588         Tk_FreeBitmap(display, polyPtr->fillStipple);
589     }
590     if (polyPtr->activeFillStipple != None) {
591         Tk_FreeBitmap(display, polyPtr->activeFillStipple);
592     }
593     if (polyPtr->disabledFillStipple != None) {
594         Tk_FreeBitmap(display, polyPtr->disabledFillStipple);
595     }
596     if (polyPtr->fillGC != None) {
597         Tk_FreeGC(display, polyPtr->fillGC);
598     }
599 }
600 \f
601 /*
602  *--------------------------------------------------------------
603  *
604  * ComputePolygonBbox --
605  *
606  *      This procedure is invoked to compute the bounding box of
607  *      all the pixels that may be drawn as part of a polygon.
608  *
609  * Results:
610  *      None.
611  *
612  * Side effects:
613  *      The fields x1, y1, x2, and y2 are updated in the header
614  *      for itemPtr.
615  *
616  *--------------------------------------------------------------
617  */
618
619 static void
620 ComputePolygonBbox(canvas, polyPtr)
621     Tk_Canvas canvas;                   /* Canvas that contains item. */
622     PolygonItem *polyPtr;               /* Item whose bbox is to be
623                                          * recomputed. */
624 {
625     double *coordPtr;
626     int i;
627     double width;
628     Tk_State state = polyPtr->header.state;
629     Tk_TSOffset *tsoffset;
630
631     if(state == TK_STATE_NULL) {
632         state = ((TkCanvas *)canvas)->canvas_state;
633     }
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;
638         return;
639     }
640     if (((TkCanvas *)canvas)->currentItemPtr == (Tk_Item *)polyPtr) {
641         if (polyPtr->outline.activeWidth>width) {
642             width = polyPtr->outline.activeWidth;
643         }
644     } else if (state==TK_STATE_DISABLED) {
645         if (polyPtr->outline.disabledWidth>0.0) {
646             width = polyPtr->outline.disabledWidth;
647         }
648     }
649
650     coordPtr = polyPtr->coordPtr;
651     polyPtr->header.x1 = polyPtr->header.x2 = (int) *coordPtr;
652     polyPtr->header.y1 = polyPtr->header.y2 = (int) coordPtr[1];
653
654     /*
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.
662      */
663
664     for (i = 1, coordPtr = polyPtr->coordPtr+2; i < polyPtr->numPoints-1;
665             i++, coordPtr += 2) {
666         TkIncludePoint((Tk_Item *) polyPtr, coordPtr);
667     }
668
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;
674             if (index < 0) {
675                 index = 0;
676             }
677         }
678         index %= (polyPtr->numPoints - polyPtr->autoClosed) * 2;
679         if (index <0) {
680             index += (polyPtr->numPoints - polyPtr->autoClosed) * 2;
681         }
682         tsoffset->xoffset = (int) (polyPtr->coordPtr[index] + 0.5);
683         tsoffset->yoffset = (int) (polyPtr->coordPtr[index+1] + 0.5);
684     } else {
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;
691         }
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;
698         }
699     }
700
701     if (polyPtr->outline.gc != None) {
702         tsoffset = &polyPtr->outline.tsoffset;
703         if (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;
708                 }
709                 index %= (polyPtr->numPoints - 1) * 2;
710                 if (index <0) {
711                     index += (polyPtr->numPoints - 1) * 2;
712                 }
713                 tsoffset->xoffset = (int) (polyPtr->coordPtr[index] + 0.5);
714                 tsoffset->yoffset = (int) (polyPtr->coordPtr[index+1] + 0.5);
715             } else {
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;
722                 }
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;
729                 }
730             }
731         }
732
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;
738
739         /*
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.
743          */
744
745         if (polyPtr->joinStyle == JoinMiter) {
746             double miter[4];
747             int j;
748             coordPtr = polyPtr->coordPtr;
749             if (polyPtr->numPoints>3) {
750                 if (TkGetMiterPoints(coordPtr+2*(polyPtr->numPoints-2),
751                         coordPtr, coordPtr+2, width,
752                         miter, miter+2)) {
753                     for (j = 0; j < 4; j += 2) {
754                         TkIncludePoint((Tk_Item *) polyPtr, miter+j);
755                     }
756                 }
757              }
758             for (i = polyPtr->numPoints ; i >= 3;
759                     i--, coordPtr += 2) {
760     
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);
765                     }
766                 }
767             }
768         }
769     }
770
771     /*
772      * Add one more pixel of fudge factor just to be safe (e.g.
773      * X may round differently than we do).
774      */
775
776     polyPtr->header.x1 -= 1;
777     polyPtr->header.x2 += 1;
778     polyPtr->header.y1 -= 1;
779     polyPtr->header.y2 += 1;
780 }
781 \f
782 /*
783  *--------------------------------------------------------------
784  *
785  * TkFillPolygon --
786  *
787  *      This procedure is invoked to convert a polygon to screen
788  *      coordinates and display it using a particular GC.
789  *
790  * Results:
791  *      None.
792  *
793  * Side effects:
794  *      ItemPtr is drawn in drawable using the transformation
795  *      information in canvas.
796  *
797  *--------------------------------------------------------------
798  */
799
800 void
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
810                                          * polygon. */
811     GC gc;                              /* Graphics context for drawing. */
812     GC outlineGC;                       /* If not None, use this to draw an
813                                          * outline around the polygon after
814                                          * filling it. */
815 {
816     XPoint staticPoints[MAX_STATIC_POINTS];
817     XPoint *pointPtr;
818     XPoint *pPtr;
819     int i;
820
821     /*
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.
825      */
826
827     if (numPoints <= MAX_STATIC_POINTS) {
828         pointPtr = staticPoints;
829     } else {
830         pointPtr = (XPoint *) ckalloc((unsigned) (numPoints * sizeof(XPoint)));
831     }
832
833     for (i = 0, pPtr = pointPtr; i < numPoints; i += 1, coordPtr += 2, pPtr++) {
834         Tk_CanvasDrawableCoords(canvas, coordPtr[0], coordPtr[1], &pPtr->x,
835                 &pPtr->y);
836     }
837
838     /*
839      * Display polygon, then free up polygon storage if it was dynamically
840      * allocated.
841      */
842
843     if (gc != None && numPoints>3) {
844         XFillPolygon(display, drawable, gc, pointPtr, numPoints, Complex,
845                 CoordModeOrigin);
846     }
847     if (outlineGC != None) {
848         XDrawLines(display, drawable, outlineGC, pointPtr,
849             numPoints, CoordModeOrigin);
850     }
851     if (pointPtr != staticPoints) {
852         ckfree((char *) pointPtr);
853     }
854 }
855 \f
856 /*
857  *--------------------------------------------------------------
858  *
859  * DisplayPolygon --
860  *
861  *      This procedure is invoked to draw a polygon item in a given
862  *      drawable.
863  *
864  * Results:
865  *      None.
866  *
867  * Side effects:
868  *      ItemPtr is drawn in drawable using the transformation
869  *      information in canvas.
870  *
871  *--------------------------------------------------------------
872  */
873
874 static void
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
880                                          * item. */
881     int x, y, width, height;            /* Describes region of canvas that
882                                          * must be redisplayed (not used). */
883 {
884     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
885     Tk_State state = itemPtr->state;
886     Pixmap stipple = polyPtr->fillStipple;
887     double linewidth = polyPtr->outline.width;
888
889     if (((polyPtr->fillGC == None) && (polyPtr->outline.gc == None)) ||
890             (polyPtr->numPoints < 1) ||
891             (polyPtr->numPoints < 3 && polyPtr->outline.gc == None)) {
892         return;
893     }
894
895     if(state == TK_STATE_NULL) {
896         state = ((TkCanvas *)canvas)->canvas_state;
897     }
898     if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
899         if (polyPtr->outline.activeWidth>linewidth) {
900             linewidth = polyPtr->outline.activeWidth;
901         }
902         if (polyPtr->activeFillStipple != None) {
903             stipple = polyPtr->activeFillStipple;
904         }
905     } else if (state==TK_STATE_DISABLED) {
906         if (polyPtr->outline.disabledWidth>0.0) {
907             linewidth = polyPtr->outline.disabledWidth;
908         }
909         if (polyPtr->disabledFillStipple != None) {
910             stipple = polyPtr->disabledFillStipple;
911         }
912     }
913     /*
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
916      * read-only.
917      */
918
919     if ((stipple != None) && (polyPtr->fillGC != None)) {
920         Tk_TSOffset *tsoffset = &polyPtr->tsoffset;
921         int w=0; int h=0;
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) {
926                 w /= 2;
927             } else {
928                 w = 0;
929             }
930             if (flags & TK_OFFSET_MIDDLE) {
931                 h /= 2;
932             } else {
933                 h = 0;
934             }
935         }
936         tsoffset->xoffset -= w;
937         tsoffset->yoffset -= h;
938         Tk_CanvasSetOffset(canvas, polyPtr->fillGC, tsoffset);
939         tsoffset->xoffset += w;
940         tsoffset->yoffset += h;
941     }
942     Tk_ChangeOutlineGC(canvas, itemPtr, &(polyPtr->outline));
943
944     if(polyPtr->numPoints < 3) {
945         short x,y;
946         int intLineWidth = (int) (linewidth + 0.5);
947         if (intLineWidth < 1) {
948             intLineWidth = 1;
949         }
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,
955                 0, 64*360);
956     } else if (!polyPtr->smooth || polyPtr->numPoints < 4) {
957         TkFillPolygon(canvas, polyPtr->coordPtr, polyPtr->numPoints,
958                     display, drawable, polyPtr->fillGC, polyPtr->outline.gc);
959     } else {
960         int numPoints;
961         XPoint staticPoints[MAX_STATIC_POINTS];
962         XPoint *pointPtr;
963
964         /*
965          * This is a smoothed polygon.  Display using a set of generated
966          * spline points rather than the original points.
967          */
968
969         numPoints = polyPtr->smooth->coordProc(canvas, (double *) NULL,
970                 polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,
971                 (double *) NULL);
972         if (numPoints <= MAX_STATIC_POINTS) {
973             pointPtr = staticPoints;
974         } else {
975             pointPtr = (XPoint *) ckalloc((unsigned)
976                     (numPoints * sizeof(XPoint)));
977         }
978         numPoints = polyPtr->smooth->coordProc(canvas, polyPtr->coordPtr,
979                 polyPtr->numPoints, polyPtr->splineSteps, pointPtr,
980                 (double *) NULL);
981         if (polyPtr->fillGC != None) {
982             XFillPolygon(display, drawable, polyPtr->fillGC, pointPtr,
983                     numPoints, Complex, CoordModeOrigin);
984         }
985         if (polyPtr->outline.gc != None) {
986             XDrawLines(display, drawable, polyPtr->outline.gc, pointPtr,
987                     numPoints, CoordModeOrigin);
988         }
989         if (pointPtr != staticPoints) {
990             ckfree((char *) pointPtr);
991         }
992     }
993     Tk_ResetOutlineGC(canvas, itemPtr, &(polyPtr->outline));
994     if ((stipple != None) && (polyPtr->fillGC != None)) {
995         XSetTSOrigin(display, polyPtr->fillGC, 0, 0);
996     }
997 }
998 \f
999 /*
1000  *--------------------------------------------------------------
1001  *
1002  * PolygonInsert --
1003  *
1004  *      Insert coords into a polugon item at a given index.
1005  *
1006  * Results:
1007  *      None.
1008  *
1009  * Side effects:
1010  *      The coords in the given item is modified.
1011  *
1012  *--------------------------------------------------------------
1013  */
1014
1015 static void
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. */
1022 {
1023     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1024     int length, objc, i;
1025     Tcl_Obj **objv;
1026     double *new;
1027     Tk_State state = itemPtr->state;
1028
1029     if (state == TK_STATE_NULL) {
1030         state = ((TkCanvas *)canvas)->canvas_state;
1031     }
1032
1033     if (!obj || (Tcl_ListObjGetElements((Tcl_Interp *) NULL, obj, &objc, &objv) != TCL_OK)
1034             || !objc || objc&1) {
1035         return;
1036     }
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];
1043     }
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);
1048             return;
1049         }
1050     }
1051
1052     for(i=beforeThis; i<length; i++) {
1053         new[i+objc] = polyPtr->coordPtr[i];
1054     }
1055     if(polyPtr->coordPtr) ckfree((char *) polyPtr->coordPtr);
1056     length+=objc;
1057     polyPtr->coordPtr = new;
1058     polyPtr->numPoints = (length/2) + polyPtr->autoClosed;
1059
1060     /*
1061      * Close the polygon if it isn't already closed, or remove autoclosing
1062      * if the user's coordinates are now closed.
1063      */
1064
1065     if (polyPtr->autoClosed) {
1066         if ((new[length-2] == new[0]) && (new[length-1] == new[1])) {
1067             polyPtr->autoClosed = 0;
1068             polyPtr->numPoints--;
1069         }
1070     }
1071     else {
1072         if ((new[length-2] != new[0]) || (new[length-1] != new[1])) {
1073             polyPtr->autoClosed = 1;
1074             polyPtr->numPoints++;
1075         }
1076     }
1077
1078     new[length] = new[0];
1079     new[length+1] = new[1];
1080     if (((length-objc)>3) && (state != TK_STATE_HIDDEN)) {
1081         /*
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.
1088          */
1089         double width;
1090         int j;
1091         itemPtr->redraw_flags |= TK_ITEM_DONT_REDRAW;
1092
1093         /*
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.
1099          */
1100
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) {
1108                 j=i;
1109                 if(j<0) j+=length;
1110                 if(j>=length) j-=length;
1111                 TkIncludePoint(itemPtr, polyPtr->coordPtr+j);
1112         }
1113         width = polyPtr->outline.width;
1114         if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1115                 if (polyPtr->outline.activeWidth>width) {
1116                     width = polyPtr->outline.activeWidth;
1117                 }
1118         } else if (state==TK_STATE_DISABLED) {
1119                 if (polyPtr->outline.disabledWidth>0.0) {
1120                     width = polyPtr->outline.disabledWidth;
1121                 }
1122         }
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);
1128     }
1129
1130     ComputePolygonBbox(canvas, polyPtr);
1131 }
1132 \f
1133 /*
1134  *--------------------------------------------------------------
1135  *
1136  * PolygonDeleteCoords --
1137  *
1138  *      Delete one or more coordinates from a polygon item.
1139  *
1140  * Results:
1141  *      None.
1142  *
1143  * Side effects:
1144  *      Characters between "first" and "last", inclusive, get
1145  *      deleted from itemPtr.
1146  *
1147  *--------------------------------------------------------------
1148  */
1149
1150 static void
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. */
1156 {
1157     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1158     int count, i;
1159     int length = 2*(polyPtr->numPoints - polyPtr->autoClosed);
1160
1161     while(first>=length) first-=length;
1162     while(first<0)       first+=length;
1163     while(last>=length)  last-=length;
1164     while(last<0)        last+=length;
1165
1166     first &= -2;
1167     last &= -2;
1168
1169     count = last + 2 - first;
1170     if(count<=0) count +=length;
1171
1172     if(count >= length) {
1173         polyPtr->numPoints = 0;
1174         if(polyPtr->coordPtr != NULL) {
1175             ckfree((char *) polyPtr->coordPtr);
1176         }
1177         ComputePolygonBbox(canvas, polyPtr);
1178         return;
1179     }
1180
1181     if(last>=first) {
1182         for(i=last+2; i<length; i++) {
1183             polyPtr->coordPtr[i-count] = polyPtr->coordPtr[i];
1184         }
1185     } else {
1186         for(i=last; i<=first; i++) {
1187             polyPtr->coordPtr[i-last] = polyPtr->coordPtr[i];
1188         }
1189     }
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);
1194 }
1195 \f
1196 /*
1197  *--------------------------------------------------------------
1198  *
1199  * PolygonToPoint --
1200  *
1201  *      Computes the distance from a given point to a given
1202  *      polygon, in canvas units.
1203  *
1204  * Results:
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.
1209  *
1210  * Side effects:
1211  *      None.
1212  *
1213  *--------------------------------------------------------------
1214  */
1215
1216         /* ARGSUSED */
1217 static double
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. */
1222 {
1223     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1224     double *coordPtr, *polyPoints;
1225     double staticSpace[2*MAX_STATIC_POINTS];
1226     double poly[10];
1227     double radius;
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. */
1233     double width;
1234     Tk_State state = itemPtr->state;
1235
1236     bestDist = 1.0e36;
1237
1238     if(state == TK_STATE_NULL) {
1239         state = ((TkCanvas *)canvas)->canvas_state;
1240     }
1241     width = polyPtr->outline.width;
1242     if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1243         if (polyPtr->outline.activeWidth>width) {
1244             width = polyPtr->outline.activeWidth;
1245         }
1246     } else if (state==TK_STATE_DISABLED) {
1247         if (polyPtr->outline.disabledWidth>0.0) {
1248             width = polyPtr->outline.disabledWidth;
1249         }
1250     }
1251     radius = width/2.0;
1252
1253     /*
1254      * Handle smoothed polygons by generating an expanded set of points
1255      * against which to do the check.
1256      */
1257
1258     if ((polyPtr->smooth) && (polyPtr->numPoints>2)) {
1259         numPoints = polyPtr->smooth->coordProc(canvas, (double *) NULL,
1260                 polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,
1261                 (double *) NULL);
1262         if (numPoints <= MAX_STATIC_POINTS) {
1263             polyPoints = staticSpace;
1264         } else {
1265             polyPoints = (double *) ckalloc((unsigned)
1266                     (2*numPoints*sizeof(double)));
1267         }
1268         numPoints = polyPtr->smooth->coordProc(canvas, polyPtr->coordPtr,
1269                 polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,
1270                 polyPoints);
1271     } else {
1272         numPoints = polyPtr->numPoints;
1273         polyPoints = polyPtr->coordPtr;
1274     }
1275
1276     bestDist = TkPolygonToPoint(polyPoints, numPoints, pointPtr);
1277     if (bestDist<=0.0) {
1278         goto donepoint;
1279     }
1280     if ((polyPtr->outline.gc != None) && (polyPtr->joinStyle == JoinRound)) {
1281         dist = bestDist - radius;
1282         if (dist <= 0.0) {
1283             bestDist = 0.0;
1284             goto donepoint;
1285         } else {
1286             bestDist = dist;
1287         }
1288     }
1289
1290     if ((polyPtr->outline.gc == None) || (width <= 1)) goto donepoint;
1291
1292     /*
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.
1297      */
1298
1299     changedMiterToBevel = 0;
1300     for (count = numPoints, coordPtr = polyPoints; count >= 2;
1301             count--, coordPtr += 2) {
1302
1303         /*
1304          * If rounding is done around the first point then compute
1305          * the distance between the point and the point.
1306          */
1307
1308         if (polyPtr->joinStyle == JoinRound) {
1309             dist = hypot(coordPtr[0] - pointPtr[0], coordPtr[1] - pointPtr[1])
1310                     - radius;
1311             if (dist <= 0.0) {
1312                 bestDist = 0.0;
1313                 goto donepoint;
1314             } else if (dist < bestDist) {
1315                 bestDist = dist;
1316             }
1317         }
1318
1319         /*
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.
1323          */
1324
1325         if (count == numPoints) {
1326             TkGetButtPoints(coordPtr+2, coordPtr, (double) width,
1327                     0, poly, poly+2);
1328         } else if ((polyPtr->joinStyle == JoinMiter) && !changedMiterToBevel) {
1329             poly[0] = poly[6];
1330             poly[1] = poly[7];
1331             poly[2] = poly[4];
1332             poly[3] = poly[5];
1333         } else {
1334             TkGetButtPoints(coordPtr+2, coordPtr, (double) width, 0,
1335                     poly, poly+2);
1336
1337             /*
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.
1342              */
1343
1344             if ((polyPtr->joinStyle == JoinBevel) || changedMiterToBevel) {
1345                 poly[8] = poly[0];
1346                 poly[9] = poly[1];
1347                 dist = TkPolygonToPoint(poly, 5, pointPtr);
1348                 if (dist <= 0.0) {
1349                     bestDist = 0.0;
1350                     goto donepoint;
1351                 } else if (dist < bestDist) {
1352                     bestDist = dist;
1353                 }
1354                 changedMiterToBevel = 0;
1355             }
1356         }
1357         if (count == 2) {
1358             TkGetButtPoints(coordPtr, coordPtr+2, (double) width,
1359                     0, poly+4, poly+6);
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,
1365                         0, poly+4, poly+6);
1366             }
1367         } else {
1368             TkGetButtPoints(coordPtr, coordPtr+2, (double) width, 0,
1369                     poly+4, poly+6);
1370         }
1371         poly[8] = poly[0];
1372         poly[9] = poly[1];
1373         dist = TkPolygonToPoint(poly, 5, pointPtr);
1374         if (dist <= 0.0) {
1375             bestDist = 0.0;
1376             goto donepoint;
1377         } else if (dist < bestDist) {
1378             bestDist = dist;
1379         }
1380     }
1381
1382     donepoint:
1383     if ((polyPoints != staticSpace) && polyPoints != polyPtr->coordPtr) {
1384         ckfree((char *) polyPoints);
1385     }
1386     return bestDist;
1387 }
1388 \f
1389 /*
1390  *--------------------------------------------------------------
1391  *
1392  * PolygonToArea --
1393  *
1394  *      This procedure is called to determine whether an item
1395  *      lies entirely inside, entirely outside, or overlapping
1396  *      a given rectangular area.
1397  *
1398  * Results:
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.
1402  *
1403  * Side effects:
1404  *      None.
1405  *
1406  *--------------------------------------------------------------
1407  */
1408
1409         /* ARGSUSED */
1410 static int
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
1416                                  * area.  */
1417 {
1418     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1419     double *coordPtr;
1420     double staticSpace[2*MAX_STATIC_POINTS];
1421     double *polyPoints, poly[10];
1422     double radius;
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. */ 
1433     double width;
1434     Tk_State state = itemPtr->state;
1435
1436     if(state == TK_STATE_NULL) {
1437         state = ((TkCanvas *)canvas)->canvas_state;
1438     }
1439
1440     width = polyPtr->outline.width;
1441     if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1442         if (polyPtr->outline.activeWidth>width) {
1443             width = polyPtr->outline.activeWidth;
1444         }
1445     } else if (state==TK_STATE_DISABLED) {
1446         if (polyPtr->outline.disabledWidth>0.0) {
1447             width = polyPtr->outline.disabledWidth;
1448         }
1449     }
1450
1451     radius = width/2.0;
1452     inside = -1;
1453
1454     if ((state==TK_STATE_HIDDEN) || polyPtr->numPoints<2) {
1455         return -1;
1456     } else if (polyPtr->numPoints <3) {
1457         double oval[4];
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);
1463     }
1464     /*
1465      * Handle smoothed polygons by generating an expanded set of points
1466      * against which to do the check.
1467      */
1468
1469     if (polyPtr->smooth) {
1470         numPoints = polyPtr->smooth->coordProc(canvas, (double *) NULL,
1471                 polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,
1472                 (double *) NULL);
1473         if (numPoints <= MAX_STATIC_POINTS) {
1474             polyPoints = staticSpace;
1475         } else {
1476             polyPoints = (double *) ckalloc((unsigned)
1477                     (2*numPoints*sizeof(double)));
1478         }
1479         numPoints = polyPtr->smooth->coordProc(canvas, polyPtr->coordPtr,
1480                 polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,
1481                 polyPoints);
1482     } else {
1483         numPoints = polyPtr->numPoints;
1484         polyPoints = polyPtr->coordPtr;
1485     }
1486
1487     /*
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.
1491      */
1492     inside = TkPolygonToArea(polyPoints, numPoints, rectPtr);
1493     if (inside==0) goto donearea;
1494
1495     if (polyPtr->outline.gc == None) goto donearea ;
1496
1497     /*
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
1501      * and caps.
1502      */
1503
1504     changedMiterToBevel = 0;
1505     for (count = numPoints, coordPtr = polyPoints; count >= 2;
1506             count--, coordPtr += 2) {
1507  
1508         /*
1509          * If rounding is done around the first point of the edge
1510          * then test a circular region around the point with the
1511          * area.
1512          */
1513
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) {
1520                 inside = 0;
1521                 goto donearea;
1522             }
1523         }
1524
1525         /*
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.
1529          */
1530
1531         if (count == numPoints) {
1532             TkGetButtPoints(coordPtr+2, coordPtr, width,
1533                     0, poly, poly+2);
1534         } else if ((polyPtr->joinStyle == JoinMiter) && !changedMiterToBevel) {
1535             poly[0] = poly[6];
1536             poly[1] = poly[7];
1537             poly[2] = poly[4];
1538             poly[3] = poly[5];
1539         } else {
1540             TkGetButtPoints(coordPtr+2, coordPtr, width, 0,
1541                     poly, poly+2);
1542
1543             /*
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.
1548              */
1549
1550             if ((polyPtr->joinStyle == JoinBevel) || changedMiterToBevel) {
1551                 poly[8] = poly[0];
1552                 poly[9] = poly[1];
1553                 if (TkPolygonToArea(poly, 5, rectPtr) != inside) {
1554                     inside = 0;
1555                     goto donearea;
1556                 }
1557                 changedMiterToBevel = 0;
1558             }
1559         }
1560         if (count == 2) {
1561             TkGetButtPoints(coordPtr, coordPtr+2, width,
1562                     0, poly+4, poly+6);
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,
1568                         0, poly+4, poly+6);
1569             }
1570         } else {
1571             TkGetButtPoints(coordPtr, coordPtr+2, width, 0,
1572                     poly+4, poly+6);
1573         }
1574         poly[8] = poly[0];
1575         poly[9] = poly[1];
1576         if (TkPolygonToArea(poly, 5, rectPtr) != inside) {
1577             inside = 0;
1578             goto donearea;
1579         }
1580     }
1581
1582     donearea:
1583     if ((polyPoints != staticSpace) && (polyPoints != polyPtr->coordPtr)) {
1584         ckfree((char *) polyPoints);
1585     }
1586     return inside;
1587 }
1588 \f
1589 /*
1590  *--------------------------------------------------------------
1591  *
1592  * ScalePolygon --
1593  *
1594  *      This procedure is invoked to rescale a polygon item.
1595  *
1596  * Results:
1597  *      None.
1598  *
1599  * Side effects:
1600  *      The polygon referred to by itemPtr is rescaled so that the
1601  *      following transformation is applied to all point
1602  *      coordinates:
1603  *              x' = originX + scaleX*(x-originX)
1604  *              y' = originY + scaleY*(y-originY)
1605  *
1606  *--------------------------------------------------------------
1607  */
1608
1609 static void
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. */
1616 {
1617     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1618     double *coordPtr;
1619     int i;
1620
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);
1625     }
1626     ComputePolygonBbox(canvas, polyPtr);
1627 }
1628 \f
1629 /*
1630  *--------------------------------------------------------------
1631  *
1632  * GetPolygonIndex --
1633  *
1634  *      Parse an index into a polygon item and return either its value
1635  *      or an error.
1636  *
1637  * Results:
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
1641  *      interp->result.
1642  *
1643  * Side effects:
1644  *      None.
1645  *
1646  *--------------------------------------------------------------
1647  */
1648
1649 static int
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
1654                                  * specified. */
1655     Tcl_Obj *obj;               /* Specification of a particular coord
1656                                  * in itemPtr's line. */
1657     int *indexPtr;              /* Where to store converted index. */
1658 {
1659     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1660     size_t length;
1661     char *string = Tcl_GetStringFromObj(obj, (int *) &length);
1662
1663     if (string[0] == 'e') {
1664         if (strncmp(string, "end", length) == 0) {
1665             *indexPtr = 2*(polyPtr->numPoints - polyPtr->autoClosed);
1666         } else {
1667             badIndex:
1668
1669             /*
1670              * Some of the paths here leave messages in interp->result,
1671              * so we have to clear it out before storing our own message.
1672              */
1673
1674             Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
1675             Tcl_AppendResult(interp, "bad index \"", string, "\"",
1676                     (char *) NULL);
1677             return TCL_ERROR;
1678         }
1679     } else if (string[0] == '@') {
1680         int i;
1681         double x ,y, bestDist, dist, *coordPtr;
1682         char *end, *p;
1683
1684         p = string+1;
1685         x = strtod(p, &end);
1686         if ((end == p) || (*end != ',')) {
1687             goto badIndex;
1688         }
1689         p = end+1;
1690         y = strtod(p, &end);
1691         if ((end == p) || (*end != 0)) {
1692             goto badIndex;
1693         }
1694         bestDist = 1.0e36;
1695         coordPtr = polyPtr->coordPtr;
1696         *indexPtr = 0;
1697         for(i=0; i<(polyPtr->numPoints-1); i++) {
1698             dist = hypot(coordPtr[0] - x, coordPtr[1] - y);
1699             if (dist<bestDist) {
1700                 bestDist = dist;
1701                 *indexPtr = 2*i;
1702             }
1703             coordPtr += 2;
1704         }
1705     } else {
1706         int count = 2*(polyPtr->numPoints - polyPtr->autoClosed);
1707         if (Tcl_GetIntFromObj(interp, obj, indexPtr) != TCL_OK) {
1708             goto badIndex;
1709         }
1710         *indexPtr &= -2; /* if odd, make it even */
1711         if (count) {
1712             if (*indexPtr > 0) {
1713                 *indexPtr = ((*indexPtr - 2) % count) + 2;
1714             } else {
1715                 *indexPtr = -((-(*indexPtr)) % count);
1716             }
1717         } else {
1718             *indexPtr = 0;
1719         }
1720     }
1721     return TCL_OK;
1722 }
1723 \f
1724 /*
1725  *--------------------------------------------------------------
1726  *
1727  * TranslatePolygon --
1728  *
1729  *      This procedure is called to move a polygon by a given
1730  *      amount.
1731  *
1732  * Results:
1733  *      None.
1734  *
1735  * Side effects:
1736  *      The position of the polygon is offset by (xDelta, yDelta),
1737  *      and the bounding box is updated in the generic part of the
1738  *      item structure.
1739  *
1740  *--------------------------------------------------------------
1741  */
1742
1743 static void
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
1748                                          * moved. */
1749 {
1750     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1751     double *coordPtr;
1752     int i;
1753
1754     for (i = 0, coordPtr = polyPtr->coordPtr; i < polyPtr->numPoints;
1755             i++, coordPtr += 2) {
1756         *coordPtr += deltaX;
1757         coordPtr[1] += deltaY;
1758     }
1759     ComputePolygonBbox(canvas, polyPtr);
1760 }
1761 \f
1762 /*
1763  *--------------------------------------------------------------
1764  *
1765  * PolygonToPostscript --
1766  *
1767  *      This procedure is called to generate Postscript for
1768  *      polygon items.
1769  *
1770  * Results:
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.
1776  *
1777  * Side effects:
1778  *      None.
1779  *
1780  *--------------------------------------------------------------
1781  */
1782
1783 static int
1784 PolygonToPostscript(interp, canvas, itemPtr, prepass)
1785     Tcl_Interp *interp;                 /* Leave Postscript or error message
1786                                          * here. */
1787     Tk_Canvas canvas;                   /* Information about overall canvas. */
1788     Tk_Item *itemPtr;                   /* Item for which Postscript is
1789                                          * wanted. */
1790     int prepass;                        /* 1 means this is a prepass to
1791                                          * collect font information;  0 means
1792                                          * final Postscript is being created. */
1793 {
1794     PolygonItem *polyPtr = (PolygonItem *) itemPtr;
1795     char *style;
1796     XColor *color;
1797     XColor *fillColor;
1798     Pixmap stipple;
1799     Pixmap fillStipple;
1800     Tk_State state = itemPtr->state;
1801     double width;
1802
1803     if (polyPtr->numPoints<2 || polyPtr->coordPtr==NULL) {
1804         return TCL_OK;
1805     }
1806
1807     if(state == TK_STATE_NULL) {
1808         state = ((TkCanvas *)canvas)->canvas_state;
1809     }
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;
1818         }
1819         if (polyPtr->outline.activeColor!=NULL) {
1820             color = polyPtr->outline.activeColor;
1821         }
1822         if (polyPtr->outline.activeStipple!=None) {
1823             stipple = polyPtr->outline.activeStipple;
1824         }
1825         if (polyPtr->activeFillColor!=NULL) {
1826             fillColor = polyPtr->activeFillColor;
1827         }
1828         if (polyPtr->activeFillStipple!=None) {
1829             fillStipple = polyPtr->activeFillStipple;
1830         }
1831     } else if (state==TK_STATE_DISABLED) {
1832         if (polyPtr->outline.disabledWidth>0.0) {
1833             width = polyPtr->outline.disabledWidth;
1834         }
1835         if (polyPtr->outline.disabledColor!=NULL) {
1836             color = polyPtr->outline.disabledColor;
1837         }
1838         if (polyPtr->outline.disabledStipple!=None) {
1839             stipple = polyPtr->outline.disabledStipple;
1840         }
1841         if (polyPtr->disabledFillColor!=NULL) {
1842             fillColor = polyPtr->disabledFillColor;
1843         }
1844         if (polyPtr->disabledFillStipple!=None) {
1845             fillStipple = polyPtr->disabledFillStipple;
1846         }
1847     }
1848     if (polyPtr->numPoints==2) {
1849         char string[128];
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)
1856                 != TCL_OK) {
1857             return TCL_ERROR;
1858         }
1859         if (stipple != None) {
1860             Tcl_AppendResult(interp, "clip ", (char *) NULL);
1861             if (Tk_CanvasPsStipple(interp, canvas, stipple) != TCL_OK) {
1862                 return TCL_ERROR;
1863             }
1864         } else {
1865             Tcl_AppendResult(interp, "fill\n", (char *) NULL);
1866         }
1867         return TCL_OK;
1868     }
1869
1870     /*
1871      * Fill the area of the polygon.
1872      */
1873
1874     if (fillColor != NULL && polyPtr->numPoints>3) {
1875         if (!polyPtr->smooth || !polyPtr->smooth->postscriptProc) {
1876             Tk_CanvasPsPath(interp, canvas, polyPtr->coordPtr,
1877                     polyPtr->numPoints);
1878         } else {
1879             polyPtr->smooth->postscriptProc(interp, canvas, polyPtr->coordPtr,
1880                     polyPtr->numPoints, polyPtr->splineSteps);
1881         }
1882         if (Tk_CanvasPsColor(interp, canvas, fillColor) != TCL_OK) {
1883             return TCL_ERROR;
1884         }
1885         if (fillStipple != None) {
1886             Tcl_AppendResult(interp, "eoclip ", (char *) NULL);
1887             if (Tk_CanvasPsStipple(interp, canvas, fillStipple)
1888                     != TCL_OK) {
1889                 return TCL_ERROR;
1890             }
1891             if (color != NULL) {
1892                 Tcl_AppendResult(interp, "grestore gsave\n", (char *) NULL);
1893             }
1894         } else {
1895             Tcl_AppendResult(interp, "eofill\n", (char *) NULL);
1896         }
1897     }
1898
1899     /*
1900      * Now draw the outline, if there is one.
1901      */
1902
1903     if (color != NULL) {
1904
1905         if (!polyPtr->smooth || !polyPtr->smooth->postscriptProc) {
1906             Tk_CanvasPsPath(interp, canvas, polyPtr->coordPtr,
1907                 polyPtr->numPoints);
1908         } else {
1909             polyPtr->smooth->postscriptProc(interp, canvas, polyPtr->coordPtr,
1910                 polyPtr->numPoints, polyPtr->splineSteps);
1911         }
1912
1913         if (polyPtr->joinStyle == JoinRound) {
1914             style = "1";
1915         } else if (polyPtr->joinStyle == JoinBevel) {
1916             style = "2";
1917         } else {
1918             style = "0";
1919         }
1920         Tcl_AppendResult(interp, style," setlinejoin 1 setlinecap\n",
1921                 (char *) NULL);
1922         if (Tk_CanvasPsOutline(canvas, itemPtr,
1923                 &(polyPtr->outline)) != TCL_OK) {
1924             return TCL_ERROR;
1925         }
1926     }
1927     return TCL_OK;
1928 }