Debian Codegraph @

pTk/mTk/generic/tkCanvArc.c

raw symbols SCIP
1 /*
2 * tkCanvArc.c --
3 *
4 * This file implements arc items for canvas widgets.
5 *
6 * Copyright (c) 1992-1994 The Regents of the University of California.
7 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tkCanvArc.c,v 1.11 2003/02/09 07:48:22 hobbs Exp $
13 */
14
15 #include "tkPort.h"
16 #include "tkInt.h"
17 #include "tkCanvases.h"
18
19 /*
20 * The structure below defines the record for each arc item.
21 */
22
23 typedef enum {
24 PIESLICE_STYLE, CHORD_STYLE, ARC_STYLE
25 } Style;
26
27 typedef struct ArcItem {
28 Tk_Item header; /* Generic stuff that's the same for all
29 * types. MUST BE FIRST IN STRUCTURE. */
30 Tk_Outline outline; /* Outline structure */
31 double bbox[4]; /* Coordinates (x1, y1, x2, y2) of bounding
32 * box for oval of which arc is a piece. */
33 double start; /* Angle at which arc begins, in degrees
34 * between 0 and 360. */
35 double extent; /* Extent of arc (angular distance from
36 * start to end of arc) in degrees between
37 * -360 and 360. */
38 double *outlinePtr; /* Points to (x,y) coordinates for points
39 * that define one or two closed polygons
40 * representing the portion of the outline
41 * that isn't part of the arc (the V-shape
42 * for a pie slice or a line-like segment
43 * for a chord). Malloc'ed. */
44 int numOutlinePoints; /* Number of points at outlinePtr. Zero
45 * means no space allocated. */
46 Tk_TSOffset tsoffset;
47 XColor *fillColor; /* Color for filling arc (used for drawing
48 * outline too when style is "arc"). NULL
49 * means don't fill arc. */
50 XColor *activeFillColor; /* Color for filling arc (used for drawing
51 * outline too when style is "arc" and state
52 * is "active"). NULL means use fillColor. */
53 XColor *disabledFillColor; /* Color for filling arc (used for drawing
54 * outline too when style is "arc" and state
55 * is "disabled". NULL means use fillColor */
56 Pixmap fillStipple; /* Stipple bitmap for filling item. */
57 Pixmap activeFillStipple; /* Stipple bitmap for filling item if state
58 * is active. */
59 Pixmap disabledFillStipple; /* Stipple bitmap for filling item if state
60 * is disabled. */
61 Style style; /* How to draw arc: arc, chord, or pieslice. */
62 GC fillGC; /* Graphics context for filling item. */
63 double center1[2]; /* Coordinates of center of arc outline at
64 * start (see ComputeArcOutline). */
65 double center2[2]; /* Coordinates of center of arc outline at
66 * start+extent (see ComputeArcOutline). */
67 Tk_Tile fillTile;
68 } ArcItem;
69
70 /*
71 * The definitions below define the sizes of the polygons used to
72 * display outline information for various styles of arcs:
73 */
74
75 #define CHORD_OUTLINE_PTS 7
76 #define PIE_OUTLINE1_PTS 6
77 #define PIE_OUTLINE2_PTS 7
78
79 /*
80 * Information used for parsing configuration specs:
81 */
82
83 static int StyleParseProc _ANSI_ARGS_((
84 ClientData clientData, Tcl_Interp *interp,
85 Tk_Window tkwin, Tcl_Obj * value,
86 char *widgRec, int offset));
87 static Tcl_Obj * StylePrintProc _ANSI_ARGS_((
88 ClientData clientData, Tk_Window tkwin,
89 char *widgRec, int offset,
90 Tcl_FreeProc **freeProcPtr));
91
92 static Tk_CustomOption stateOption = {
93 TkStateParseProc,
94 TkStatePrintProc, (ClientData) 2
95 };
96 static Tk_CustomOption styleOption = {
97 StyleParseProc,
98 StylePrintProc, (ClientData) NULL
99 };
100 static Tk_CustomOption tagsOption = {
101 Tk_CanvasTagsParseProc,
102 Tk_CanvasTagsPrintProc, (ClientData) NULL
103 };
104 static Tk_CustomOption dashOption = {
105 TkCanvasDashParseProc,
106 TkCanvasDashPrintProc, (ClientData) NULL
107 };
108 static Tk_CustomOption tileOption = {
109 Tk_TileParseProc,
110 Tk_TilePrintProc, (ClientData) NULL
111 };
112 static Tk_CustomOption offsetOption = {
113 Tk_OffsetParseProc,
114 Tk_OffsetPrintProc, (ClientData) (TK_OFFSET_RELATIVE)
115 };
116 static Tk_CustomOption pixelOption = {
117 Tk_PixelParseProc,
118 Tk_PixelPrintProc, (ClientData) NULL
119 };
120
121 static Tk_ConfigSpec configSpecs[] = {
122 {TK_CONFIG_CUSTOM, "-activedash", (char *) NULL, (char *) NULL,
123 (char *) NULL, Tk_Offset(ArcItem, outline.activeDash),
124 TK_CONFIG_NULL_OK, &dashOption},
125 {TK_CONFIG_COLOR, "-activefill", (char *) NULL, (char *) NULL,
126 (char *) NULL, Tk_Offset(ArcItem, activeFillColor),
127 TK_CONFIG_NULL_OK},
128 {TK_CONFIG_COLOR, "-activeoutline", (char *) NULL, (char *) NULL,
129 (char *) NULL, Tk_Offset(ArcItem, outline.activeColor),
130 TK_CONFIG_NULL_OK},
131 {TK_CONFIG_BITMAP, "-activeoutlinestipple", (char *) NULL, (char *) NULL,
132 (char *) NULL, Tk_Offset(ArcItem, outline.activeStipple),
133 TK_CONFIG_NULL_OK},
134 {TK_CONFIG_BITMAP, "-activestipple", (char *) NULL, (char *) NULL,
135 (char *) NULL, Tk_Offset(ArcItem, activeFillStipple),
136 TK_CONFIG_NULL_OK},
137 {TK_CONFIG_CUSTOM, "-activewidth", (char *) NULL, (char *) NULL,
138 "0.0", Tk_Offset(ArcItem, outline.activeWidth),
139 TK_CONFIG_DONT_SET_DEFAULT, &pixelOption},
140 {TK_CONFIG_CUSTOM, "-dash", (char *) NULL, (char *) NULL,
141 (char *) NULL, Tk_Offset(ArcItem, outline.dash),
142 TK_CONFIG_NULL_OK, &dashOption},
143 {TK_CONFIG_PIXELS, "-dashoffset", (char *) NULL, (char *) NULL,
144 "0", Tk_Offset(ArcItem, outline.offset), TK_CONFIG_DONT_SET_DEFAULT},
145 {TK_CONFIG_CUSTOM, "-disableddash", (char *) NULL, (char *) NULL,
146 (char *) NULL, Tk_Offset(ArcItem, outline.disabledDash),
147 TK_CONFIG_NULL_OK, &dashOption},
148 {TK_CONFIG_COLOR, "-disabledfill", (char *) NULL, (char *) NULL,
149 (char *) NULL, Tk_Offset(ArcItem, disabledFillColor),
150 TK_CONFIG_NULL_OK},
151 {TK_CONFIG_COLOR, "-disabledoutline", (char *) NULL, (char *) NULL,
152 (char *) NULL, Tk_Offset(ArcItem, outline.disabledColor),
153 TK_CONFIG_NULL_OK},
154 {TK_CONFIG_BITMAP, "-disabledoutlinestipple", (char *) NULL, (char *) NULL,
155 (char *) NULL, Tk_Offset(ArcItem, outline.disabledStipple),
156 TK_CONFIG_NULL_OK},
157 {TK_CONFIG_BITMAP, "-disabledstipple", (char *) NULL, (char *) NULL,
158 (char *) NULL, Tk_Offset(ArcItem, disabledFillStipple),
159 TK_CONFIG_NULL_OK},
160 {TK_CONFIG_CUSTOM, "-disabledwidth", (char *) NULL, (char *) NULL,
161 "0.0", Tk_Offset(ArcItem, outline.disabledWidth),
162 TK_CONFIG_DONT_SET_DEFAULT, &pixelOption},
163 {TK_CONFIG_DOUBLE, "-extent", (char *) NULL, (char *) NULL,
164 "90", Tk_Offset(ArcItem, extent), TK_CONFIG_DONT_SET_DEFAULT},
165 {TK_CONFIG_COLOR, "-fill", (char *) NULL, (char *) NULL,
166 (char *) NULL, Tk_Offset(ArcItem, fillColor), TK_CONFIG_NULL_OK},
167 {TK_CONFIG_CUSTOM, "-offset", (char *) NULL, (char *) NULL,
168 "0 0", Tk_Offset(ArcItem, tsoffset),
169 TK_CONFIG_DONT_SET_DEFAULT, &offsetOption},
170 {TK_CONFIG_COLOR, "-outline", (char *) NULL, (char *) NULL,
171 "black", Tk_Offset(ArcItem, outline.color), TK_CONFIG_NULL_OK},
172 {TK_CONFIG_CUSTOM, "-outlineoffset", (char *) NULL, (char *) NULL,
173 "0 0", Tk_Offset(ArcItem, outline.tsoffset),
174 TK_CONFIG_DONT_SET_DEFAULT, &offsetOption},
175 {TK_CONFIG_BITMAP, "-outlinestipple", (char *) NULL, (char *) NULL,
176 (char *) NULL, Tk_Offset(ArcItem, outline.stipple),
177 TK_CONFIG_NULL_OK},
178 {TK_CONFIG_DOUBLE, "-start", (char *) NULL, (char *) NULL,
179 "0", Tk_Offset(ArcItem, start), TK_CONFIG_DONT_SET_DEFAULT},
180 {TK_CONFIG_CUSTOM, "-state", (char *) NULL, (char *) NULL,
181 (char *) NULL, Tk_Offset(Tk_Item, state), TK_CONFIG_NULL_OK,
182 &stateOption},
183 {TK_CONFIG_BITMAP, "-stipple", (char *) NULL, (char *) NULL,
184 (char *) NULL, Tk_Offset(ArcItem, fillStipple), TK_CONFIG_NULL_OK},
185 {TK_CONFIG_CUSTOM, "-style", (char *) NULL, (char *) NULL,
186 (char *) NULL, Tk_Offset(ArcItem, style), TK_CONFIG_DONT_SET_DEFAULT,
187 &styleOption},
188 {TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
189 (char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
190 {TK_CONFIG_CUSTOM, "-tile", (char *) NULL, (char *) NULL,
191 (char *) NULL, Tk_Offset(ArcItem, fillTile),
192 TK_CONFIG_NULL_OK, &tileOption},
193 {TK_CONFIG_CALLBACK, "-updatecommand", (char *) NULL, (char *) NULL,
194 (char *) NULL, Tk_Offset(Tk_Item, updateCmd), TK_CONFIG_NULL_OK},
195 {TK_CONFIG_CUSTOM, "-width", (char *) NULL, (char *) NULL,
196 "1.0", Tk_Offset(ArcItem, outline.width), TK_CONFIG_DONT_SET_DEFAULT,
197 &pixelOption},
198 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
199 (char *) NULL, 0, 0}
200 };
201
202 /*
203 * Prototypes for procedures defined in this file:
204 */
205
206 static void ComputeArcBbox _ANSI_ARGS_((Tk_Canvas canvas,
207 ArcItem *arcPtr));
208 static int ConfigureArc _ANSI_ARGS_((Tcl_Interp *interp,
209 Tk_Canvas canvas, Tk_Item *itemPtr, int objc,
210 Tcl_Obj *CONST objv[], int flags));
211 static int CreateArc _ANSI_ARGS_((Tcl_Interp *interp,
212 Tk_Canvas canvas, struct Tk_Item *itemPtr,
213 int objc, Tcl_Obj *CONST objv[]));
214 static void DeleteArc _ANSI_ARGS_((Tk_Canvas canvas,
215 Tk_Item *itemPtr, Display *display));
216 static void DisplayArc _ANSI_ARGS_((Tk_Canvas canvas,
217 Tk_Item *itemPtr, Display *display, Drawable dst,
218 int x, int y, int width, int height));
219 static int ArcCoords _ANSI_ARGS_((Tcl_Interp *interp,
220 Tk_Canvas canvas, Tk_Item *itemPtr, int objc,
221 Tcl_Obj *CONST objv[]));
222 static int ArcToArea _ANSI_ARGS_((Tk_Canvas canvas,
223 Tk_Item *itemPtr, double *rectPtr));
224 static double ArcToPoint _ANSI_ARGS_((Tk_Canvas canvas,
225 Tk_Item *itemPtr, double *coordPtr));
226 static int ArcToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
227 Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
228 static void ScaleArc _ANSI_ARGS_((Tk_Canvas canvas,
229 Tk_Item *itemPtr, double originX, double originY,
230 double scaleX, double scaleY));
231 static void TranslateArc _ANSI_ARGS_((Tk_Canvas canvas,
232 Tk_Item *itemPtr, double deltaX, double deltaY));
233 static int AngleInRange _ANSI_ARGS_((double x, double y,
234 double start, double extent));
235 static void ComputeArcOutline _ANSI_ARGS_((Tk_Canvas canvas,
236 ArcItem *arcPtr));
237 static int HorizLineToArc _ANSI_ARGS_((double x1, double x2,
238 double y, double rx, double ry,
239 double start, double extent));
240 static int VertLineToArc _ANSI_ARGS_((double x, double y1,
241 double y2, double rx, double ry,
242 double start, double extent));
243
244 /*
245 * The structures below defines the arc item types by means of procedures
246 * that can be invoked by generic item code.
247 */
248
249 Tk_ItemType tkArcType = {
250 "arc", /* name */
251 sizeof(ArcItem), /* itemSize */
252 CreateArc, /* createProc */
253 configSpecs, /* configSpecs */
254 ConfigureArc, /* configureProc */
255 ArcCoords, /* coordProc */
256 DeleteArc, /* deleteProc */
257 DisplayArc, /* displayProc */
258 TK_CONFIG_OBJS, /* flags */
259 ArcToPoint, /* pointProc */
260 ArcToArea, /* areaProc */
261 ArcToPostscript, /* postscriptProc */
262 ScaleArc, /* scaleProc */
263 TranslateArc, /* translateProc */
264 (Tk_ItemIndexProc *) NULL, /* indexProc */
265 (Tk_ItemCursorProc *) NULL, /* icursorProc */
266 (Tk_ItemSelectionProc *) NULL, /* selectionProc */
267 (Tk_ItemInsertProc *) NULL, /* insertProc */
268 (Tk_ItemDCharsProc *) NULL, /* dTextProc */
269 (Tk_ItemType *) NULL, /* nextPtr */
270 };
271
272 #ifndef PI
273 # define PI 3.14159265358979323846
274 #endif
275
276
277 /*
278 *--------------------------------------------------------------
279 *
280 * CreateArc --
281 *
282 * This procedure is invoked to create a new arc item in
283 * a canvas.
284 *
285 * Results:
286 * A standard Tcl return value. If an error occurred in
287 * creating the item, then an error message is left in
288 * the interp's result; in this case itemPtr is
289 * left uninitialized, so it can be safely freed by the
290 * caller.
291 *
292 * Side effects:
293 * A new arc item is created.
294 *
295 *--------------------------------------------------------------
296 */
297
298 static int
299 CreateArc(interp, canvas, itemPtr, objc, objv)
300 Tcl_Interp *interp; /* Interpreter for error reporting. */
301 Tk_Canvas canvas; /* Canvas to hold new item. */
302 Tk_Item *itemPtr; /* Record to hold new item; header
303 * has been initialized by caller. */
304 int objc; /* Number of arguments in objv. */
305 Tcl_Obj *CONST objv[]; /* Arguments describing arc. */
306 {
307 ArcItem *arcPtr = (ArcItem *) itemPtr;
308 int i;
309
310 if (objc == 0) {
311 panic("canvas did not pass any coords\n");
312 }
313
314 /*
315 * Carry out initialization that is needed in order to clean
316 * up after errors during the the remainder of this procedure.
317 */
318
319 Tk_CreateOutline(&(arcPtr->outline));
320 arcPtr->start = 0;
321 arcPtr->extent = 90;
322 arcPtr->outlinePtr = NULL;
323 arcPtr->numOutlinePoints = 0;
324 arcPtr->tsoffset.flags = 0;
325 arcPtr->tsoffset.xoffset = 0;
326 arcPtr->tsoffset.yoffset = 0;
327 arcPtr->fillColor = NULL;
328 arcPtr->activeFillColor = NULL;
329 arcPtr->disabledFillColor = NULL;
330 arcPtr->fillStipple = None;
331 arcPtr->activeFillStipple = None;
332 arcPtr->disabledFillStipple = None;
333 arcPtr->style = PIESLICE_STYLE;
334 arcPtr->fillGC = None;
335
336 /*
337 * Process the arguments to fill in the item record.
338 */
339
340 for (i = 1; i < objc; i++) {
341 char *arg = Tcl_GetString(objv[i]);
342 if ((arg[0] == '-') && (arg[1] >= 'a') && (arg[1] <= 'z')) {
343 break;
344 }
345 }
346 if (ArcCoords(interp, canvas, itemPtr, i, objv) != TCL_OK) {
347 goto error;
348 }
349 if (ConfigureArc(interp, canvas, itemPtr, objc-i, objv+i, 0) == TCL_OK) {
350 return TCL_OK;
351 }
352 error:
353 DeleteArc(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
354 return TCL_ERROR;
355 }
356
357 /*
358 *--------------------------------------------------------------
359 *
360 * ArcCoords --
361 *
362 * This procedure is invoked to process the "coords" widget
363 * command on arcs. See the user documentation for details
364 * on what it does.
365 *
366 * Results:
367 * Returns TCL_OK or TCL_ERROR, and sets the interp's result.
368 *
369 * Side effects:
370 * The coordinates for the given item may be changed.
371 *
372 *--------------------------------------------------------------
373 */
374
375 static int
376 ArcCoords(interp, canvas, itemPtr, objc, objv)
377 Tcl_Interp *interp; /* Used for error reporting. */
378 Tk_Canvas canvas; /* Canvas containing item. */
379 Tk_Item *itemPtr; /* Item whose coordinates are to be
380 * read or modified. */
381 int objc; /* Number of coordinates supplied in
382 * objv. */
383 Tcl_Obj *CONST objv[]; /* Array of coordinates: x1, y1,
384 * x2, y2, ... */
385 {
386 ArcItem *arcPtr = (ArcItem *) itemPtr;
387
388 if (objc == 0) {
389 Tcl_Obj *obj = Tcl_NewObj();
390 Tcl_Obj *subobj = Tcl_NewDoubleObj(arcPtr->bbox[0]);
391 Tcl_ListObjAppendElement(interp, obj, subobj);
392 subobj = Tcl_NewDoubleObj(arcPtr->bbox[1]);
393 Tcl_ListObjAppendElement(interp, obj, subobj);
394 subobj = Tcl_NewDoubleObj(arcPtr->bbox[2]);
395 Tcl_ListObjAppendElement(interp, obj, subobj);
396 subobj = Tcl_NewDoubleObj(arcPtr->bbox[3]);
397 Tcl_ListObjAppendElement(interp, obj, subobj);
398 Tcl_SetObjResult(interp, obj);
399 } else if ((objc == 1)||(objc == 4)) {
400 if (objc==1) {
401 if (Tcl_ListObjGetElements(interp, objv[0], &objc,
402 (Tcl_Obj ***) &objv) != TCL_OK) {
403 return TCL_ERROR;
404 } else if (objc != 4) {
405 char buf[64 + TCL_INTEGER_SPACE];
406
407 sprintf(buf, "wrong # coordinates: expected 4, got %d", objc);
408 Tcl_SetResult(interp, buf, TCL_VOLATILE);
409 return TCL_ERROR;
410 }
411 }
412 if ((Tk_CanvasGetCoordFromObj(interp, canvas, objv[0],
413 &arcPtr->bbox[0]) != TCL_OK)
414 || (Tk_CanvasGetCoordFromObj(interp, canvas, objv[1],
415 &arcPtr->bbox[1]) != TCL_OK)
416 || (Tk_CanvasGetCoordFromObj(interp, canvas, objv[2],
417 &arcPtr->bbox[2]) != TCL_OK)
418 || (Tk_CanvasGetCoordFromObj(interp, canvas, objv[3],
419 &arcPtr->bbox[3]) != TCL_OK)) {
420 return TCL_ERROR;
421 }
422 ComputeArcBbox(canvas, arcPtr);
423 } else {
424 char buf[64 + TCL_INTEGER_SPACE];
425
426 sprintf(buf, "wrong # coordinates: expected 0 or 4, got %d", objc);
427 Tcl_SetResult(interp, buf, TCL_VOLATILE);
428 return TCL_ERROR;
429 }
430 return TCL_OK;
431 }
432
433 /*
434 *--------------------------------------------------------------
435 *
436 * ConfigureArc --
437 *
438 * This procedure is invoked to configure various aspects
439 * of a arc item, such as its outline and fill colors.
440 *
441 * Results:
442 * A standard Tcl result code. If an error occurs, then
443 * an error message is left in the interp's result.
444 *
445 * Side effects:
446 * Configuration information, such as colors and stipple
447 * patterns, may be set for itemPtr.
448 *
449 *--------------------------------------------------------------
450 */
451
452 static int
453 ConfigureArc(interp, canvas, itemPtr, objc, objv, flags)
454 Tcl_Interp *interp; /* Used for error reporting. */
455 Tk_Canvas canvas; /* Canvas containing itemPtr. */
456 Tk_Item *itemPtr; /* Arc item to reconfigure. */
457 int objc; /* Number of elements in objv. */
458 Tcl_Obj *CONST objv[]; /* Arguments describing things to configure. */
459 int flags; /* Flags to pass to Tk_ConfigureWidget. */
460 {
461 ArcItem *arcPtr = (ArcItem *) itemPtr;
462 XGCValues gcValues;
463 GC newGC;
464 unsigned long mask;
465 int i;
466 Tk_Window tkwin;
467 Tk_TSOffset *tsoffset;
468 XColor *color;
469 Pixmap stipple;
470 Tk_State state;
471
472 tkwin = Tk_CanvasTkwin(canvas);
473 if (TCL_OK != Tk_ConfigureWidget(interp, tkwin, configSpecs, objc,
474 objv, (char *) arcPtr, flags|TK_CONFIG_OBJS)) {
475 return TCL_ERROR;
476 }
477
478 state = Tk_GetItemState(canvas, itemPtr);
479
480 /*
481 * A few of the options require additional processing, such as
482 * style and graphics contexts.
483 */
484
485 if (arcPtr->outline.activeWidth > arcPtr->outline.width ||
486 arcPtr->outline.activeDash.number != 0 ||
487 arcPtr->outline.activeColor != NULL ||
488 arcPtr->outline.activeStipple != None ||
489 arcPtr->activeFillColor != NULL ||
490 arcPtr->activeFillStipple != None) {
491 itemPtr->redraw_flags |= TK_ITEM_STATE_DEPENDANT;
492 } else {
493 itemPtr->redraw_flags &= ~TK_ITEM_STATE_DEPENDANT;
494 }
495
496 tsoffset = &arcPtr->outline.tsoffset;
497 flags = tsoffset->flags;
498 if (flags & TK_OFFSET_LEFT) {
499 tsoffset->xoffset = (int) (arcPtr->bbox[0] + 0.5);
500 } else if (flags & TK_OFFSET_CENTER) {
501 tsoffset->xoffset = (int) ((arcPtr->bbox[0]+arcPtr->bbox[2]+1)/2);
502 } else if (flags & TK_OFFSET_RIGHT) {
503 tsoffset->xoffset = (int) (arcPtr->bbox[2] + 0.5);
504 }
505 if (flags & TK_OFFSET_TOP) {
506 tsoffset->yoffset = (int) (arcPtr->bbox[1] + 0.5);
507 } else if (flags & TK_OFFSET_MIDDLE) {
508 tsoffset->yoffset = (int) ((arcPtr->bbox[1]+arcPtr->bbox[3]+1)/2);
509 } else if (flags & TK_OFFSET_BOTTOM) {
510 tsoffset->yoffset = (int) (arcPtr->bbox[2] + 0.5);
511 }
512
513 i = (int) (arcPtr->start/360.0);
514 arcPtr->start -= i*360.0;
515 if (arcPtr->start < 0) {
516 arcPtr->start += 360.0;
517 }
518 i = (int) (arcPtr->extent/360.0);
519 arcPtr->extent -= i*360.0;
520
521 mask = Tk_ConfigOutlineGC(&gcValues, canvas, itemPtr,
522 &(arcPtr->outline));
523 if (mask) {
524 gcValues.cap_style = CapButt;
525 mask |= GCCapStyle;
526 newGC = Tk_GetGC(tkwin, mask, &gcValues);
527 } else {
528 newGC = None;
529 }
530 if (arcPtr->outline.gc != None) {
531 Tk_FreeGC(Tk_Display(tkwin), arcPtr->outline.gc);
532 }
533 arcPtr->outline.gc = newGC;
534
535 if (state==TK_STATE_HIDDEN) {
536 ComputeArcBbox(canvas, arcPtr);
537 return TCL_OK;
538 }
539
540 color = arcPtr->fillColor;
541 stipple = arcPtr->fillStipple;
542 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
543 if (arcPtr->activeFillColor!=NULL) {
544 color = arcPtr->activeFillColor;
545 }
546 if (arcPtr->activeFillStipple!=None) {
547 stipple = arcPtr->activeFillStipple;
548 }
549 } else if (state==TK_STATE_DISABLED) {
550 if (arcPtr->disabledFillColor!=NULL) {
551 color = arcPtr->disabledFillColor;
552 }
553 if (arcPtr->disabledFillStipple!=None) {
554 stipple = arcPtr->disabledFillStipple;
555 }
556 }
557
558 if (arcPtr->style == ARC_STYLE) {
559 newGC = None;
560 } else if (color == NULL) {
561 newGC = None;
562 } else {
563 gcValues.foreground = color->pixel;
564 if (arcPtr->style == CHORD_STYLE) {
565 gcValues.arc_mode = ArcChord;
566 } else {
567 gcValues.arc_mode = ArcPieSlice;
568 }
569 mask = GCForeground|GCArcMode;
570 if (stipple != None) {
571 gcValues.stipple = stipple;
572 gcValues.fill_style = FillStippled;
573 mask |= GCStipple|GCFillStyle;
574 }
575 newGC = Tk_GetGC(tkwin, mask, &gcValues);
576 }
577 if (arcPtr->fillGC != None) {
578 Tk_FreeGC(Tk_Display(tkwin), arcPtr->fillGC);
579 }
580 arcPtr->fillGC = newGC;
581
582 tsoffset = &arcPtr->tsoffset;
583 flags = tsoffset->flags;
584 if (flags & TK_OFFSET_LEFT) {
585 tsoffset->xoffset = (int) (arcPtr->bbox[0] + 0.5);
586 } else if (flags & TK_OFFSET_CENTER) {
587 tsoffset->xoffset = (int) ((arcPtr->bbox[0]+arcPtr->bbox[2]+1)/2);
588 } else if (flags & TK_OFFSET_RIGHT) {
589 tsoffset->xoffset = (int) (arcPtr->bbox[2] + 0.5);
590 }
591 if (flags & TK_OFFSET_TOP) {
592 tsoffset->yoffset = (int) (arcPtr->bbox[1] + 0.5);
593 } else if (flags & TK_OFFSET_MIDDLE) {
594 tsoffset->yoffset = (int) ((arcPtr->bbox[1]+arcPtr->bbox[3]+1)/2);
595 } else if (flags & TK_OFFSET_BOTTOM) {
596 tsoffset->yoffset = (int) (arcPtr->bbox[3] + 0.5);
597 }
598
599 ComputeArcBbox(canvas, arcPtr);
600 return TCL_OK;
601 }
602
603 /*
604 *--------------------------------------------------------------
605 *
606 * DeleteArc --
607 *
608 * This procedure is called to clean up the data structure
609 * associated with a arc item.
610 *
611 * Results:
612 * None.
613 *
614 * Side effects:
615 * Resources associated with itemPtr are released.
616 *
617 *--------------------------------------------------------------
618 */
619
620 static void
621 DeleteArc(canvas, itemPtr, display)
622 Tk_Canvas canvas; /* Info about overall canvas. */
623 Tk_Item *itemPtr; /* Item that is being deleted. */
624 Display *display; /* Display containing window for
625 * canvas. */
626 {
627 ArcItem *arcPtr = (ArcItem *) itemPtr;
628
629 Tk_DeleteOutline(display, &(arcPtr->outline));
630 if (arcPtr->numOutlinePoints != 0) {
631 ckfree((char *) arcPtr->outlinePtr);
632 }
633 if (arcPtr->fillColor != NULL) {
634 Tk_FreeColor(arcPtr->fillColor);
635 }
636 if (arcPtr->activeFillColor != NULL) {
637 Tk_FreeColor(arcPtr->activeFillColor);
638 }
639 if (arcPtr->disabledFillColor != NULL) {
640 Tk_FreeColor(arcPtr->disabledFillColor);
641 }
642 if (arcPtr->fillStipple != None) {
643 Tk_FreeBitmap(display, arcPtr->fillStipple);
644 }
645 if (arcPtr->activeFillStipple != None) {
646 Tk_FreeBitmap(display, arcPtr->activeFillStipple);
647 }
648 if (arcPtr->disabledFillStipple != None) {
649 Tk_FreeBitmap(display, arcPtr->disabledFillStipple);
650 }
651 if (arcPtr->fillGC != None) {
652 Tk_FreeGC(display, arcPtr->fillGC);
653 }
654 }
655
656 /*
657 *--------------------------------------------------------------
658 *
659 * ComputeArcBbox --
660 *
661 * This procedure is invoked to compute the bounding box of
662 * all the pixels that may be drawn as part of an arc.
663 *
664 * Results:
665 * None.
666 *
667 * Side effects:
668 * The fields x1, y1, x2, and y2 are updated in the header
669 * for itemPtr.
670 *
671 *--------------------------------------------------------------
672 */
673
674 /* ARGSUSED */
675 static void
676 ComputeArcBbox(canvas, arcPtr)
677 Tk_Canvas canvas; /* Canvas that contains item. */
678 ArcItem *arcPtr; /* Item whose bbox is to be
679 * recomputed. */
680 {
681 double tmp, center[2], point[2];
682 double width;
683 Tk_State state = Tk_GetItemState(canvas, &arcPtr->header);
684
685 width = arcPtr->outline.width;
686 if (width < 1.0) {
687 width = 1.0;
688 }
689 if (state==TK_STATE_HIDDEN) {
690 arcPtr->header.x1 = arcPtr->header.x2 =
691 arcPtr->header.y1 = arcPtr->header.y2 = -1;
692 return;
693 } else if (((TkCanvas *)canvas)->currentItemPtr == (Tk_Item *) arcPtr) {
694 if (arcPtr->outline.activeWidth>width) {
695 width = arcPtr->outline.activeWidth;
696 }
697 } else if (state==TK_STATE_DISABLED) {
698 if (arcPtr->outline.disabledWidth>0) {
699 width = arcPtr->outline.disabledWidth;
700 }
701 }
702
703 /*
704 * Make sure that the first coordinates are the lowest ones.
705 */
706
707 if (arcPtr->bbox[1] > arcPtr->bbox[3]) {
708 double tmp;
709 tmp = arcPtr->bbox[3];
710 arcPtr->bbox[3] = arcPtr->bbox[1];
711 arcPtr->bbox[1] = tmp;
712 }
713 if (arcPtr->bbox[0] > arcPtr->bbox[2]) {
714 double tmp;
715 tmp = arcPtr->bbox[2];
716 arcPtr->bbox[2] = arcPtr->bbox[0];
717 arcPtr->bbox[0] = tmp;
718 }
719
720 ComputeArcOutline(canvas,arcPtr);
721
722 /*
723 * To compute the bounding box, start with the the bbox formed
724 * by the two endpoints of the arc. Then add in the center of
725 * the arc's oval (if relevant) and the 3-o'clock, 6-o'clock,
726 * 9-o'clock, and 12-o'clock positions, if they are relevant.
727 */
728
729 arcPtr->header.x1 = arcPtr->header.x2 = (int) arcPtr->center1[0];
730 arcPtr->header.y1 = arcPtr->header.y2 = (int) arcPtr->center1[1];
731 TkIncludePoint((Tk_Item *) arcPtr, arcPtr->center2);
732 center[0] = (arcPtr->bbox[0] + arcPtr->bbox[2])/2;
733 center[1] = (arcPtr->bbox[1] + arcPtr->bbox[3])/2;
734 if (arcPtr->style == PIESLICE_STYLE) {
735 TkIncludePoint((Tk_Item *) arcPtr, center);
736 }
737
738 tmp = -arcPtr->start;
739 if (tmp < 0) {
740 tmp += 360.0;
741 }
742 if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
743 point[0] = arcPtr->bbox[2];
744 point[1] = center[1];
745 TkIncludePoint((Tk_Item *) arcPtr, point);
746 }
747 tmp = 90.0 - arcPtr->start;
748 if (tmp < 0) {
749 tmp += 360.0;
750 }
751 if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
752 point[0] = center[0];
753 point[1] = arcPtr->bbox[1];
754 TkIncludePoint((Tk_Item *) arcPtr, point);
755 }
756 tmp = 180.0 - arcPtr->start;
757 if (tmp < 0) {
758 tmp += 360.0;
759 }
760 if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
761 point[0] = arcPtr->bbox[0];
762 point[1] = center[1];
763 TkIncludePoint((Tk_Item *) arcPtr, point);
764 }
765 tmp = 270.0 - arcPtr->start;
766 if (tmp < 0) {
767 tmp += 360.0;
768 }
769 if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
770 point[0] = center[0];
771 point[1] = arcPtr->bbox[3];
772 TkIncludePoint((Tk_Item *) arcPtr, point);
773 }
774
775 /*
776 * Lastly, expand by the width of the arc (if the arc's outline is
777 * being drawn) and add one extra pixel just for safety.
778 */
779
780 if (arcPtr->outline.gc == None) {
781 tmp = 1;
782 } else {
783 tmp = (int) ((width + 1.0)/2.0 + 1);
784 }
785 arcPtr->header.x1 -= (int) tmp;
786 arcPtr->header.y1 -= (int) tmp;
787 arcPtr->header.x2 += (int) tmp;
788 arcPtr->header.y2 += (int) tmp;
789 }
790
791 /*
792 *--------------------------------------------------------------
793 *
794 * DisplayArc --
795 *
796 * This procedure is invoked to draw an arc item in a given
797 * drawable.
798 *
799 * Results:
800 * None.
801 *
802 * Side effects:
803 * ItemPtr is drawn in drawable using the transformation
804 * information in canvas.
805 *
806 *--------------------------------------------------------------
807 */
808
809 static void
810 DisplayArc(canvas, itemPtr, display, drawable, x, y, width, height)
811 Tk_Canvas canvas; /* Canvas that contains item. */
812 Tk_Item *itemPtr; /* Item to be displayed. */
813 Display *display; /* Display on which to draw item. */
814 Drawable drawable; /* Pixmap or window in which to draw
815 * item. */
816 int x, y, width, height; /* Describes region of canvas that
817 * must be redisplayed (not used). */
818 {
819 ArcItem *arcPtr = (ArcItem *) itemPtr;
820 short x1, y1, x2, y2;
821 int start, extent, dashnumber;
822 double lineWidth;
823 Tk_State state = Tk_GetItemState(canvas, itemPtr);
824
825 Tk_Tile tile;
826 Pixmap stipple;
827
828 lineWidth = arcPtr->outline.width;
829 if (lineWidth < 1.0) {
830 lineWidth = 1.0;
831 }
832 dashnumber = arcPtr->outline.dash.number;
833 stipple = arcPtr->fillStipple;
834 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
835 if (arcPtr->outline.activeWidth>lineWidth) {
836 lineWidth = arcPtr->outline.activeWidth;
837 }
838 if (arcPtr->outline.activeDash.number != 0) {
839 dashnumber = arcPtr->outline.activeDash.number;
840 }
841 if (arcPtr->activeFillStipple != None) {
842 stipple = arcPtr->activeFillStipple;
843 }
844 } else if (state==TK_STATE_DISABLED) {
845 if (arcPtr->outline.disabledWidth > 0) {
846 lineWidth = arcPtr->outline.disabledWidth;
847 }
848 if (arcPtr->outline.disabledDash.number != 0) {
849 dashnumber = arcPtr->outline.disabledDash.number;
850 }
851 if (arcPtr->disabledFillStipple != None) {
852 stipple = arcPtr->disabledFillStipple;
853 }
854 }
855
856 /*
857 * Compute the screen coordinates of the bounding box for the item,
858 * plus integer values for the angles.
859 */
860
861 Tk_CanvasDrawableCoords(canvas, arcPtr->bbox[0], arcPtr->bbox[1],
862 &x1, &y1);
863 Tk_CanvasDrawableCoords(canvas, arcPtr->bbox[2], arcPtr->bbox[3],
864 &x2, &y2);
865 if (x2 <= x1) {
866 x2 = x1+1;
867 }
868 if (y2 <= y1) {
869 y2 = y1+1;
870 }
871 start = (int) ((64*arcPtr->start) + 0.5);
872 extent = (int) ((64*arcPtr->extent) + 0.5);
873
874 /*
875 * Display filled arc first (if wanted), then outline. If the extent
876 * is zero then don't invoke XFillArc or XDrawArc, since this causes
877 * some window servers to crash and should be a no-op anyway.
878 */
879
880 if ((arcPtr->fillGC != None) && (extent != 0)) {
881 if (stipple != None) {
882 int w=0; int h=0;
883 Tk_TSOffset *tsoffset = &arcPtr->tsoffset;
884 int flags = tsoffset->flags;
885 if (flags & (TK_OFFSET_CENTER|TK_OFFSET_MIDDLE)) {
886 Tk_SizeOfBitmap(display, stipple, &w, &h);
887 if (flags & TK_OFFSET_CENTER) {
888 w /= 2;
889 } else {
890 w = 0;
891 }
892 if (flags & TK_OFFSET_MIDDLE) {
893 h /= 2;
894 } else {
895 h = 0;
896 }
897 }
898 tsoffset->xoffset -= w;
899 tsoffset->yoffset -= h;
900 Tk_CanvasSetOffset(canvas, arcPtr->fillGC, tsoffset);
901 if (tsoffset) {
902 tsoffset->xoffset += w;
903 tsoffset->yoffset += h;
904 }
905 }
906 XFillArc(display, drawable, arcPtr->fillGC, x1, y1, (unsigned) (x2-x1),
907 (unsigned) (y2-y1), start, extent);
908 if (stipple != None) {
909 XSetTSOrigin(display, arcPtr->fillGC, 0, 0);
910 }
911 }
912 if (arcPtr->outline.gc != None) {
913 Tk_ChangeOutlineGC(canvas, itemPtr, &(arcPtr->outline));
914
915 if (extent != 0) {
916 XDrawArc(display, drawable, arcPtr->outline.gc, x1, y1,
917 (unsigned) (x2-x1), (unsigned) (y2-y1), start, extent);
918 }
919
920 /*
921 * If the outline width is very thin, don't use polygons to draw
922 * the linear parts of the outline (this often results in nothing
923 * being displayed); just draw lines instead. The same is done if
924 * the outline is dashed, because then polygons don't work.
925 */
926
927 if (lineWidth < 1.5 || dashnumber != 0) {
928 Tk_CanvasDrawableCoords(canvas, arcPtr->center1[0],
929 arcPtr->center1[1], &x1, &y1);
930 Tk_CanvasDrawableCoords(canvas, arcPtr->center2[0],
931 arcPtr->center2[1], &x2, &y2);
932
933 if (arcPtr->style == CHORD_STYLE) {
934 XDrawLine(display, drawable, arcPtr->outline.gc,
935 x1, y1, x2, y2);
936 } else if (arcPtr->style == PIESLICE_STYLE) {
937 short cx, cy;
938
939 Tk_CanvasDrawableCoords(canvas,
940 (arcPtr->bbox[0] + arcPtr->bbox[2])/2.0,
941 (arcPtr->bbox[1] + arcPtr->bbox[3])/2.0, &cx, &cy);
942 XDrawLine(display, drawable, arcPtr->outline.gc,
943 cx, cy, x1, y1);
944 XDrawLine(display, drawable, arcPtr->outline.gc,
945 cx, cy, x2, y2);
946 }
947 } else {
948 if (arcPtr->style == CHORD_STYLE) {
949 TkFillPolygon(canvas, arcPtr->outlinePtr, CHORD_OUTLINE_PTS,
950 display, drawable, arcPtr->outline.gc, None);
951 } else if (arcPtr->style == PIESLICE_STYLE) {
952 TkFillPolygon(canvas, arcPtr->outlinePtr, PIE_OUTLINE1_PTS,
953 display, drawable, arcPtr->outline.gc, None);
954 TkFillPolygon(canvas, arcPtr->outlinePtr + 2*PIE_OUTLINE1_PTS,
955 PIE_OUTLINE2_PTS, display, drawable, arcPtr->outline.gc,
956 None);
957 }
958 }
959
960 Tk_ResetOutlineGC(canvas, itemPtr, &(arcPtr->outline));
961 }
962 }
963
964 /*
965 *--------------------------------------------------------------
966 *
967 * ArcToPoint --
968 *
969 * Computes the distance from a given point to a given
970 * arc, in canvas units.
971 *
972 * Results:
973 * The return value is 0 if the point whose x and y coordinates
974 * are coordPtr[0] and coordPtr[1] is inside the arc. If the
975 * point isn't inside the arc then the return value is the
976 * distance from the point to the arc. If itemPtr is filled,
977 * then anywhere in the interior is considered "inside"; if
978 * itemPtr isn't filled, then "inside" means only the area
979 * occupied by the outline.
980 *
981 * Side effects:
982 * None.
983 *
984 *--------------------------------------------------------------
985 */
986
987 /* ARGSUSED */
988 static double
989 ArcToPoint(canvas, itemPtr, pointPtr)
990 Tk_Canvas canvas; /* Canvas containing item. */
991 Tk_Item *itemPtr; /* Item to check against point. */
992 double *pointPtr; /* Pointer to x and y coordinates. */
993 {
994 ArcItem *arcPtr = (ArcItem *) itemPtr;
995 double vertex[2], pointAngle, diff, dist, newDist;
996 double poly[8], polyDist, width, t1, t2;
997 int filled, angleInRange;
998
999 Tk_State state = Tk_GetItemState(canvas, itemPtr);
1000
1001 width = (double) arcPtr->outline.width;
1002 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1003 if (arcPtr->outline.activeWidth>width) {
1004 width = (double) arcPtr->outline.activeWidth;
1005 }
1006 } else if (state == TK_STATE_DISABLED) {
1007 if (arcPtr->outline.disabledWidth>0) {
1008 width = (double) arcPtr->outline.disabledWidth;
1009 }
1010 }
1011
1012 /*
1013 * See if the point is within the angular range of the arc.
1014 * Remember, X angles are backwards from the way we'd normally
1015 * think of them. Also, compensate for any eccentricity of
1016 * the oval.
1017 */
1018
1019 vertex[0] = (arcPtr->bbox[0] + arcPtr->bbox[2])/2.0;
1020 vertex[1] = (arcPtr->bbox[1] + arcPtr->bbox[3])/2.0;
1021 t1 = arcPtr->bbox[3] - arcPtr->bbox[1];
1022 if (t1 != 0.0) {
1023 t1 = (pointPtr[1] - vertex[1]) / t1;
1024 }
1025 t2 = arcPtr->bbox[2] - arcPtr->bbox[0];
1026 if (t2 != 0.0) {
1027 t2 = (pointPtr[0] - vertex[0]) / t2;
1028 }
1029 if ((t1 == 0.0) && (t2 == 0.0)) {
1030 pointAngle = 0;
1031 } else {
1032 pointAngle = -atan2(t1, t2)*180/PI;
1033 }
1034 diff = pointAngle - arcPtr->start;
1035 diff -= ((int) (diff/360.0) * 360.0);
1036 if (diff < 0) {
1037 diff += 360.0;
1038 }
1039 angleInRange = (diff <= arcPtr->extent) ||
1040 ((arcPtr->extent < 0) && ((diff - 360.0) >= arcPtr->extent));
1041
1042 /*
1043 * Now perform different tests depending on what kind of arc
1044 * we're dealing with.
1045 */
1046
1047 if (arcPtr->style == ARC_STYLE) {
1048 if (angleInRange) {
1049 return TkOvalToPoint(arcPtr->bbox, width,
1050 0, pointPtr);
1051 }
1052 dist = hypot(pointPtr[0] - arcPtr->center1[0],
1053 pointPtr[1] - arcPtr->center1[1]);
1054 newDist = hypot(pointPtr[0] - arcPtr->center2[0],
1055 pointPtr[1] - arcPtr->center2[1]);
1056 if (newDist < dist) {
1057 return newDist;
1058 }
1059 return dist;
1060 }
1061
1062 if ((arcPtr->fillGC != None) || (arcPtr->outline.gc == None)) {
1063 filled = 1;
1064 } else {
1065 filled = 0;
1066 }
1067 if (arcPtr->outline.gc == None) {
1068 width = 0.0;
1069 }
1070
1071 if (arcPtr->style == PIESLICE_STYLE) {
1072 if (width > 1.0) {
1073 dist = TkPolygonToPoint(arcPtr->outlinePtr, PIE_OUTLINE1_PTS,
1074 pointPtr);
1075 newDist = TkPolygonToPoint(arcPtr->outlinePtr + 2*PIE_OUTLINE1_PTS,
1076 PIE_OUTLINE2_PTS, pointPtr);
1077 } else {
1078 dist = TkLineToPoint(vertex, arcPtr->center1, pointPtr);
1079 newDist = TkLineToPoint(vertex, arcPtr->center2, pointPtr);
1080 }
1081 if (newDist < dist) {
1082 dist = newDist;
1083 }
1084 if (angleInRange) {
1085 newDist = TkOvalToPoint(arcPtr->bbox, width, filled, pointPtr);
1086 if (newDist < dist) {
1087 dist = newDist;
1088 }
1089 }
1090 return dist;
1091 }
1092
1093 /*
1094 * This is a chord-style arc. We have to deal specially with the
1095 * triangular piece that represents the difference between a
1096 * chord-style arc and a pie-slice arc (for small angles this piece
1097 * is excluded here where it would be included for pie slices;
1098 * for large angles the piece is included here but would be
1099 * excluded for pie slices).
1100 */
1101
1102 if (width > 1.0) {
1103 dist = TkPolygonToPoint(arcPtr->outlinePtr, CHORD_OUTLINE_PTS,
1104 pointPtr);
1105 } else {
1106 dist = TkLineToPoint(arcPtr->center1, arcPtr->center2, pointPtr);
1107 }
1108 poly[0] = poly[6] = vertex[0];
1109 poly[1] = poly[7] = vertex[1];
1110 poly[2] = arcPtr->center1[0];
1111 poly[3] = arcPtr->center1[1];
1112 poly[4] = arcPtr->center2[0];
1113 poly[5] = arcPtr->center2[1];
1114 polyDist = TkPolygonToPoint(poly, 4, pointPtr);
1115 if (angleInRange) {
1116 if ((arcPtr->extent < -180.0) || (arcPtr->extent > 180.0)
1117 || (polyDist > 0.0)) {
1118 newDist = TkOvalToPoint(arcPtr->bbox, width, filled, pointPtr);
1119 if (newDist < dist) {
1120 dist = newDist;
1121 }
1122 }
1123 } else {
1124 if ((arcPtr->extent < -180.0) || (arcPtr->extent > 180.0)) {
1125 if (filled && (polyDist < dist)) {
1126 dist = polyDist;
1127 }
1128 }
1129 }
1130 return dist;
1131 }
1132
1133 /*
1134 *--------------------------------------------------------------
1135 *
1136 * ArcToArea --
1137 *
1138 * This procedure is called to determine whether an item
1139 * lies entirely inside, entirely outside, or overlapping
1140 * a given area.
1141 *
1142 * Results:
1143 * -1 is returned if the item is entirely outside the area
1144 * given by rectPtr, 0 if it overlaps, and 1 if it is entirely
1145 * inside the given area.
1146 *
1147 * Side effects:
1148 * None.
1149 *
1150 *--------------------------------------------------------------
1151 */
1152
1153 /* ARGSUSED */
1154 static int
1155 ArcToArea(canvas, itemPtr, rectPtr)
1156 Tk_Canvas canvas; /* Canvas containing item. */
1157 Tk_Item *itemPtr; /* Item to check against arc. */
1158 double *rectPtr; /* Pointer to array of four coordinates
1159 * (x1, y1, x2, y2) describing rectangular
1160 * area. */
1161 {
1162 ArcItem *arcPtr = (ArcItem *) itemPtr;
1163 double rx, ry; /* Radii for transformed oval: these define
1164 * an oval centered at the origin. */
1165 double tRect[4]; /* Transformed version of x1, y1, x2, y2,
1166 * for coord. system where arc is centered
1167 * on the origin. */
1168 double center[2], width, angle, tmp;
1169 double points[20], *pointPtr;
1170 int numPoints, filled;
1171 int inside; /* Non-zero means every test so far suggests
1172 * that arc is inside rectangle. 0 means
1173 * every test so far shows arc to be outside
1174 * of rectangle. */
1175 int newInside;
1176 Tk_State state = Tk_GetItemState(canvas, itemPtr);
1177
1178 width = (double) arcPtr->outline.width;
1179 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1180 if (arcPtr->outline.activeWidth>width) {
1181 width = (double) arcPtr->outline.activeWidth;
1182 }
1183 } else if (state==TK_STATE_DISABLED) {
1184 if (arcPtr->outline.disabledWidth>0) {
1185 width = (double) arcPtr->outline.disabledWidth;
1186 }
1187 }
1188
1189 if ((arcPtr->fillGC != None) || (arcPtr->outline.gc == None)) {
1190 filled = 1;
1191 } else {
1192 filled = 0;
1193 }
1194 if (arcPtr->outline.gc == None) {
1195 width = 0.0;
1196 }
1197
1198 /*
1199 * Transform both the arc and the rectangle so that the arc's oval
1200 * is centered on the origin.
1201 */
1202
1203 center[0] = (arcPtr->bbox[0] + arcPtr->bbox[2])/2.0;
1204 center[1] = (arcPtr->bbox[1] + arcPtr->bbox[3])/2.0;
1205 tRect[0] = rectPtr[0] - center[0];
1206 tRect[1] = rectPtr[1] - center[1];
1207 tRect[2] = rectPtr[2] - center[0];
1208 tRect[3] = rectPtr[3] - center[1];
1209 rx = arcPtr->bbox[2] - center[0] + width/2.0;
1210 ry = arcPtr->bbox[3] - center[1] + width/2.0;
1211
1212 /*
1213 * Find the extreme points of the arc and see whether these are all
1214 * inside the rectangle (in which case we're done), partly in and
1215 * partly out (in which case we're done), or all outside (in which
1216 * case we have more work to do). The extreme points include the
1217 * following, which are checked in order:
1218 *
1219 * 1. The outside points of the arc, corresponding to start and
1220 * extent.
1221 * 2. The center of the arc (but only in pie-slice mode).
1222 * 3. The 12, 3, 6, and 9-o'clock positions (but only if the arc
1223 * includes those angles).
1224 */
1225
1226 pointPtr = points;
1227 angle = -arcPtr->start*(PI/180.0);
1228 pointPtr[0] = rx*cos(angle);
1229 pointPtr[1] = ry*sin(angle);
1230 angle += -arcPtr->extent*(PI/180.0);
1231 pointPtr[2] = rx*cos(angle);
1232 pointPtr[3] = ry*sin(angle);
1233 numPoints = 2;
1234 pointPtr += 4;
1235
1236 if ((arcPtr->style == PIESLICE_STYLE) && (arcPtr->extent < 180.0)) {
1237 pointPtr[0] = 0.0;
1238 pointPtr[1] = 0.0;
1239 numPoints++;
1240 pointPtr += 2;
1241 }
1242
1243 tmp = -arcPtr->start;
1244 if (tmp < 0) {
1245 tmp += 360.0;
1246 }
1247 if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
1248 pointPtr[0] = rx;
1249 pointPtr[1] = 0.0;
1250 numPoints++;
1251 pointPtr += 2;
1252 }
1253 tmp = 90.0 - arcPtr->start;
1254 if (tmp < 0) {
1255 tmp += 360.0;
1256 }
1257 if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
1258 pointPtr[0] = 0.0;
1259 pointPtr[1] = -ry;
1260 numPoints++;
1261 pointPtr += 2;
1262 }
1263 tmp = 180.0 - arcPtr->start;
1264 if (tmp < 0) {
1265 tmp += 360.0;
1266 }
1267 if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
1268 pointPtr[0] = -rx;
1269 pointPtr[1] = 0.0;
1270 numPoints++;
1271 pointPtr += 2;
1272 }
1273 tmp = 270.0 - arcPtr->start;
1274 if (tmp < 0) {
1275 tmp += 360.0;
1276 }
1277 if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
1278 pointPtr[0] = 0.0;
1279 pointPtr[1] = ry;
1280 numPoints++;
1281 }
1282
1283 /*
1284 * Now that we've located the extreme points, loop through them all
1285 * to see which are inside the rectangle.
1286 */
1287
1288 inside = (points[0] > tRect[0]) && (points[0] < tRect[2])
1289 && (points[1] > tRect[1]) && (points[1] < tRect[3]);
1290 for (pointPtr = points+2; numPoints > 1; pointPtr += 2, numPoints--) {
1291 newInside = (pointPtr[0] > tRect[0]) && (pointPtr[0] < tRect[2])
1292 && (pointPtr[1] > tRect[1]) && (pointPtr[1] < tRect[3]);
1293 if (newInside != inside) {
1294 return 0;
1295 }
1296 }
1297
1298 if (inside) {
1299 return 1;
1300 }
1301
1302 /*
1303 * So far, oval appears to be outside rectangle, but can't yet tell
1304 * for sure. Next, test each of the four sides of the rectangle
1305 * against the bounding region for the arc. If any intersections
1306 * are found, then return "overlapping". First, test against the
1307 * polygon(s) forming the sides of a chord or pie-slice.
1308 */
1309
1310 if (arcPtr->style == PIESLICE_STYLE) {
1311 if (width >= 1.0) {
1312 if (TkPolygonToArea(arcPtr->outlinePtr, PIE_OUTLINE1_PTS,
1313 rectPtr) != -1) {
1314 return 0;
1315 }
1316 if (TkPolygonToArea(arcPtr->outlinePtr + 2*PIE_OUTLINE1_PTS,
1317 PIE_OUTLINE2_PTS, rectPtr) != -1) {
1318 return 0;
1319 }
1320 } else {
1321 if ((TkLineToArea(center, arcPtr->center1, rectPtr) != -1) ||
1322 (TkLineToArea(center, arcPtr->center2, rectPtr) != -1)) {
1323 return 0;
1324 }
1325 }
1326 } else if (arcPtr->style == CHORD_STYLE) {
1327 if (width >= 1.0) {
1328 if (TkPolygonToArea(arcPtr->outlinePtr, CHORD_OUTLINE_PTS,
1329 rectPtr) != -1) {
1330 return 0;
1331 }
1332 } else {
1333 if (TkLineToArea(arcPtr->center1, arcPtr->center2,
1334 rectPtr) != -1) {
1335 return 0;
1336 }
1337 }
1338 }
1339
1340 /*
1341 * Next check for overlap between each of the four sides and the
1342 * outer perimiter of the arc. If the arc isn't filled, then also
1343 * check the inner perimeter of the arc.
1344 */
1345
1346 if (HorizLineToArc(tRect[0], tRect[2], tRect[1], rx, ry, arcPtr->start,
1347 arcPtr->extent)
1348 || HorizLineToArc(tRect[0], tRect[2], tRect[3], rx, ry,
1349 arcPtr->start, arcPtr->extent)
1350 || VertLineToArc(tRect[0], tRect[1], tRect[3], rx, ry,
1351 arcPtr->start, arcPtr->extent)
1352 || VertLineToArc(tRect[2], tRect[1], tRect[3], rx, ry,
1353 arcPtr->start, arcPtr->extent)) {
1354 return 0;
1355 }
1356 if ((width > 1.0) && !filled) {
1357 rx -= width;
1358 ry -= width;
1359 if (HorizLineToArc(tRect[0], tRect[2], tRect[1], rx, ry, arcPtr->start,
1360 arcPtr->extent)
1361 || HorizLineToArc(tRect[0], tRect[2], tRect[3], rx, ry,
1362 arcPtr->start, arcPtr->extent)
1363 || VertLineToArc(tRect[0], tRect[1], tRect[3], rx, ry,
1364 arcPtr->start, arcPtr->extent)
1365 || VertLineToArc(tRect[2], tRect[1], tRect[3], rx, ry,
1366 arcPtr->start, arcPtr->extent)) {
1367 return 0;
1368 }
1369 }
1370
1371 /*
1372 * The arc still appears to be totally disjoint from the rectangle,
1373 * but it's also possible that the rectangle is totally inside the arc.
1374 * Do one last check, which is to check one point of the rectangle
1375 * to see if it's inside the arc. If it is, we've got overlap. If
1376 * it isn't, the arc's really outside the rectangle.
1377 */
1378
1379 if (ArcToPoint(canvas, itemPtr, rectPtr) == 0.0) {
1380 return 0;
1381 }
1382 return -1;
1383 }
1384
1385 /*
1386 *--------------------------------------------------------------
1387 *
1388 * ScaleArc --
1389 *
1390 * This procedure is invoked to rescale an arc item.
1391 *
1392 * Results:
1393 * None.
1394 *
1395 * Side effects:
1396 * The arc referred to by itemPtr is rescaled so that the
1397 * following transformation is applied to all point
1398 * coordinates:
1399 * x' = originX + scaleX*(x-originX)
1400 * y' = originY + scaleY*(y-originY)
1401 *
1402 *--------------------------------------------------------------
1403 */
1404
1405 static void
1406 ScaleArc(canvas, itemPtr, originX, originY, scaleX, scaleY)
1407 Tk_Canvas canvas; /* Canvas containing arc. */
1408 Tk_Item *itemPtr; /* Arc to be scaled. */
1409 double originX, originY; /* Origin about which to scale rect. */
1410 double scaleX; /* Amount to scale in X direction. */
1411 double scaleY; /* Amount to scale in Y direction. */
1412 {
1413 ArcItem *arcPtr = (ArcItem *) itemPtr;
1414
1415 arcPtr->bbox[0] = originX + scaleX*(arcPtr->bbox[0] - originX);
1416 arcPtr->bbox[1] = originY + scaleY*(arcPtr->bbox[1] - originY);
1417 arcPtr->bbox[2] = originX + scaleX*(arcPtr->bbox[2] - originX);
1418 arcPtr->bbox[3] = originY + scaleY*(arcPtr->bbox[3] - originY);
1419 ComputeArcBbox(canvas, arcPtr);
1420 }
1421
1422 /*
1423 *--------------------------------------------------------------
1424 *
1425 * TranslateArc --
1426 *
1427 * This procedure is called to move an arc by a given amount.
1428 *
1429 * Results:
1430 * None.
1431 *
1432 * Side effects:
1433 * The position of the arc is offset by (xDelta, yDelta), and
1434 * the bounding box is updated in the generic part of the item
1435 * structure.
1436 *
1437 *--------------------------------------------------------------
1438 */
1439
1440 static void
1441 TranslateArc(canvas, itemPtr, deltaX, deltaY)
1442 Tk_Canvas canvas; /* Canvas containing item. */
1443 Tk_Item *itemPtr; /* Item that is being moved. */
1444 double deltaX, deltaY; /* Amount by which item is to be
1445 * moved. */
1446 {
1447 ArcItem *arcPtr = (ArcItem *) itemPtr;
1448
1449 arcPtr->bbox[0] += deltaX;
1450 arcPtr->bbox[1] += deltaY;
1451 arcPtr->bbox[2] += deltaX;
1452 arcPtr->bbox[3] += deltaY;
1453 ComputeArcBbox(canvas, arcPtr);
1454 }
1455
1456 /*
1457 *--------------------------------------------------------------
1458 *
1459 * ComputeArcOutline --
1460 *
1461 * This procedure creates a polygon describing everything in
1462 * the outline for an arc except what's in the curved part.
1463 * For a "pie slice" arc this is a V-shaped chunk, and for
1464 * a "chord" arc this is a linear chunk (with cutaway corners).
1465 * For "arc" arcs, this stuff isn't relevant.
1466 *
1467 * Results:
1468 * None.
1469 *
1470 * Side effects:
1471 * The information at arcPtr->outlinePtr gets modified, and
1472 * storage for arcPtr->outlinePtr may be allocated or freed.
1473 *
1474 *--------------------------------------------------------------
1475 */
1476
1477 static void
1478 ComputeArcOutline(canvas,arcPtr)
1479 Tk_Canvas canvas; /* Information about overall canvas. */
1480 ArcItem *arcPtr; /* Information about arc. */
1481 {
1482 double sin1, cos1, sin2, cos2, angle, width, halfWidth;
1483 double boxWidth, boxHeight;
1484 double vertex[2], corner1[2], corner2[2];
1485 double *outlinePtr;
1486 Tk_State state = Tk_GetItemState(canvas, &arcPtr->header);
1487
1488
1489 /*
1490 * Make sure that the outlinePtr array is large enough to hold
1491 * either a chord or pie-slice outline.
1492 */
1493
1494 if (arcPtr->numOutlinePoints == 0) {
1495 arcPtr->outlinePtr = (double *) ckalloc((unsigned)
1496 (26 * sizeof(double)));
1497 arcPtr->numOutlinePoints = 22;
1498 }
1499 outlinePtr = arcPtr->outlinePtr;
1500
1501 /*
1502 * First compute the two points that lie at the centers of
1503 * the ends of the curved arc segment, which are marked with
1504 * X's in the figure below:
1505 *
1506 *
1507 * * * *
1508 * * *
1509 * * * * *
1510 * * * * *
1511 * * * * *
1512 * X * * X
1513 *
1514 * The code is tricky because the arc can be ovular in shape.
1515 * It computes the position for a unit circle, and then
1516 * scales to fit the shape of the arc's bounding box.
1517 *
1518 * Also, watch out because angles go counter-clockwise like you
1519 * might expect, but the y-coordinate system is inverted. To
1520 * handle this, just negate the angles in all the computations.
1521 */
1522
1523 boxWidth = arcPtr->bbox[2] - arcPtr->bbox[0];
1524 boxHeight = arcPtr->bbox[3] - arcPtr->bbox[1];
1525 angle = -arcPtr->start*PI/180.0;
1526 sin1 = sin(angle);
1527 cos1 = cos(angle);
1528 angle -= arcPtr->extent*PI/180.0;
1529 sin2 = sin(angle);
1530 cos2 = cos(angle);
1531 vertex[0] = (arcPtr->bbox[0] + arcPtr->bbox[2])/2.0;
1532 vertex[1] = (arcPtr->bbox[1] + arcPtr->bbox[3])/2.0;
1533 arcPtr->center1[0] = vertex[0] + cos1*boxWidth/2.0;
1534 arcPtr->center1[1] = vertex[1] + sin1*boxHeight/2.0;
1535 arcPtr->center2[0] = vertex[0] + cos2*boxWidth/2.0;
1536 arcPtr->center2[1] = vertex[1] + sin2*boxHeight/2.0;
1537
1538 /*
1539 * Next compute the "outermost corners" of the arc, which are
1540 * marked with X's in the figure below:
1541 *
1542 * * * *
1543 * * *
1544 * * * * *
1545 * * * * *
1546 * X * * X
1547 * * *
1548 *
1549 * The code below is tricky because it has to handle eccentricity
1550 * in the shape of the oval. The key in the code below is to
1551 * realize that the slope of the line from arcPtr->center1 to corner1
1552 * is (boxWidth*sin1)/(boxHeight*cos1), and similarly for arcPtr->center2
1553 * and corner2. These formulas can be computed from the formula for
1554 * the oval.
1555 */
1556
1557 width = arcPtr->outline.width;
1558 if (((TkCanvas *)canvas)->currentItemPtr == (Tk_Item *) arcPtr) {
1559 if (arcPtr->outline.activeWidth>arcPtr->outline.width) {
1560 width = arcPtr->outline.activeWidth;
1561 }
1562 } else if (state==TK_STATE_DISABLED) {
1563 if (arcPtr->outline.disabledWidth>arcPtr->outline.width) {
1564 width = arcPtr->outline.disabledWidth;
1565 }
1566 }
1567 halfWidth = width/2.0;
1568
1569 if (((boxWidth*sin1) == 0.0) && ((boxHeight*cos1) == 0.0)) {
1570 angle = 0.0;
1571 } else {
1572 angle = atan2(boxWidth*sin1, boxHeight*cos1);
1573 }
1574 corner1[0] = arcPtr->center1[0] + cos(angle)*halfWidth;
1575 corner1[1] = arcPtr->center1[1] + sin(angle)*halfWidth;
1576 if (((boxWidth*sin2) == 0.0) && ((boxHeight*cos2) == 0.0)) {
1577 angle = 0.0;
1578 } else {
1579 angle = atan2(boxWidth*sin2, boxHeight*cos2);
1580 }
1581 corner2[0] = arcPtr->center2[0] + cos(angle)*halfWidth;
1582 corner2[1] = arcPtr->center2[1] + sin(angle)*halfWidth;
1583
1584 /*
1585 * For a chord outline, generate a six-sided polygon with three
1586 * points for each end of the chord. The first and third points
1587 * for each end are butt points generated on either side of the
1588 * center point. The second point is the corner point.
1589 */
1590
1591 if (arcPtr->style == CHORD_STYLE) {
1592 outlinePtr[0] = outlinePtr[12] = corner1[0];
1593 outlinePtr[1] = outlinePtr[13] = corner1[1];
1594 TkGetButtPoints(arcPtr->center2, arcPtr->center1,
1595 width, 0, outlinePtr+10, outlinePtr+2);
1596 outlinePtr[4] = arcPtr->center2[0] + outlinePtr[2]
1597 - arcPtr->center1[0];
1598 outlinePtr[5] = arcPtr->center2[1] + outlinePtr[3]
1599 - arcPtr->center1[1];
1600 outlinePtr[6] = corner2[0];
1601 outlinePtr[7] = corner2[1];
1602 outlinePtr[8] = arcPtr->center2[0] + outlinePtr[10]
1603 - arcPtr->center1[0];
1604 outlinePtr[9] = arcPtr->center2[1] + outlinePtr[11]
1605 - arcPtr->center1[1];
1606 } else if (arcPtr->style == PIESLICE_STYLE) {
1607 /*
1608 * For pie slices, generate two polygons, one for each side
1609 * of the pie slice. The first arm has a shape like this,
1610 * where the center of the oval is X, arcPtr->center1 is at Y, and
1611 * corner1 is at Z:
1612 *
1613 * _____________________
1614 * | \
1615 * | \
1616 * X Y Z
1617 * | /
1618 * |_____________________/
1619 *
1620 */
1621
1622 TkGetButtPoints(arcPtr->center1, vertex, width, 0,
1623 outlinePtr, outlinePtr+2);
1624 outlinePtr[4] = arcPtr->center1[0] + outlinePtr[2] - vertex[0];
1625 outlinePtr[5] = arcPtr->center1[1] + outlinePtr[3] - vertex[1];
1626 outlinePtr[6] = corner1[0];
1627 outlinePtr[7] = corner1[1];
1628 outlinePtr[8] = arcPtr->center1[0] + outlinePtr[0] - vertex[0];
1629 outlinePtr[9] = arcPtr->center1[1] + outlinePtr[1] - vertex[1];
1630 outlinePtr[10] = outlinePtr[0];
1631 outlinePtr[11] = outlinePtr[1];
1632
1633 /*
1634 * The second arm has a shape like this:
1635 *
1636 *
1637 * ______________________
1638 * / \
1639 * / \
1640 * Z Y X /
1641 * \ /
1642 * \______________________/
1643 *
1644 * Similar to above X is the center of the oval/circle, Y is
1645 * arcPtr->center2, and Z is corner2. The extra jog out to the left
1646 * of X is needed in or to produce a butted joint with the
1647 * first arm; the corner to the right of X is one of the
1648 * first two points of the first arm, depending on extent.
1649 */
1650
1651 TkGetButtPoints(arcPtr->center2, vertex, width, 0,
1652 outlinePtr+12, outlinePtr+16);
1653 if ((arcPtr->extent > 180) ||
1654 ((arcPtr->extent < 0) && (arcPtr->extent > -180))) {
1655 outlinePtr[14] = outlinePtr[0];
1656 outlinePtr[15] = outlinePtr[1];
1657 } else {
1658 outlinePtr[14] = outlinePtr[2];
1659 outlinePtr[15] = outlinePtr[3];
1660 }
1661 outlinePtr[18] = arcPtr->center2[0] + outlinePtr[16] - vertex[0];
1662 outlinePtr[19] = arcPtr->center2[1] + outlinePtr[17] - vertex[1];
1663 outlinePtr[20] = corner2[0];
1664 outlinePtr[21] = corner2[1];
1665 outlinePtr[22] = arcPtr->center2[0] + outlinePtr[12] - vertex[0];
1666 outlinePtr[23] = arcPtr->center2[1] + outlinePtr[13] - vertex[1];
1667 outlinePtr[24] = outlinePtr[12];
1668 outlinePtr[25] = outlinePtr[13];
1669 }
1670 }
1671
1672 /*
1673 *--------------------------------------------------------------
1674 *
1675 * HorizLineToArc --
1676 *
1677 * Determines whether a horizontal line segment intersects
1678 * a given arc.
1679 *
1680 * Results:
1681 * The return value is 1 if the given line intersects the
1682 * infinitely-thin arc section defined by rx, ry, start,
1683 * and extent, and 0 otherwise. Only the perimeter of the
1684 * arc is checked: interior areas (e.g. pie-slice or chord)
1685 * are not checked.
1686 *
1687 * Side effects:
1688 * None.
1689 *
1690 *--------------------------------------------------------------
1691 */
1692
1693 static int
1694 HorizLineToArc(x1, x2, y, rx, ry, start, extent)
1695 double x1, x2; /* X-coords of endpoints of line segment.
1696 * X1 must be <= x2. */
1697 double y; /* Y-coordinate of line segment. */
1698 double rx, ry; /* These x- and y-radii define an oval
1699 * centered at the origin. */
1700 double start, extent; /* Angles that define extent of arc, in
1701 * the standard fashion for this module. */
1702 {
1703 double tmp;
1704 double tx, ty; /* Coordinates of intersection point in
1705 * transformed coordinate system. */
1706 double x;
1707
1708 /*
1709 * Compute the x-coordinate of one possible intersection point
1710 * between the arc and the line. Use a transformed coordinate
1711 * system where the oval is a unit circle centered at the origin.
1712 * Then scale back to get actual x-coordinate.
1713 */
1714
1715 ty = y/ry;
1716 tmp = 1 - ty*ty;
1717 if (tmp < 0) {
1718 return 0;
1719 }
1720 tx = sqrt(tmp);
1721 x = tx*rx;
1722
1723 /*
1724 * Test both intersection points.
1725 */
1726
1727 if ((x >= x1) && (x <= x2) && AngleInRange(tx, ty, start, extent)) {
1728 return 1;
1729 }
1730 if ((-x >= x1) && (-x <= x2) && AngleInRange(-tx, ty, start, extent)) {
1731 return 1;
1732 }
1733 return 0;
1734 }
1735
1736 /*
1737 *--------------------------------------------------------------
1738 *
1739 * VertLineToArc --
1740 *
1741 * Determines whether a vertical line segment intersects
1742 * a given arc.
1743 *
1744 * Results:
1745 * The return value is 1 if the given line intersects the
1746 * infinitely-thin arc section defined by rx, ry, start,
1747 * and extent, and 0 otherwise. Only the perimeter of the
1748 * arc is checked: interior areas (e.g. pie-slice or chord)
1749 * are not checked.
1750 *
1751 * Side effects:
1752 * None.
1753 *
1754 *--------------------------------------------------------------
1755 */
1756
1757 static int
1758 VertLineToArc(x, y1, y2, rx, ry, start, extent)
1759 double x; /* X-coordinate of line segment. */
1760 double y1, y2; /* Y-coords of endpoints of line segment.
1761 * Y1 must be <= y2. */
1762 double rx, ry; /* These x- and y-radii define an oval
1763 * centered at the origin. */
1764 double start, extent; /* Angles that define extent of arc, in
1765 * the standard fashion for this module. */
1766 {
1767 double tmp;
1768 double tx, ty; /* Coordinates of intersection point in
1769 * transformed coordinate system. */
1770 double y;
1771
1772 /*
1773 * Compute the y-coordinate of one possible intersection point
1774 * between the arc and the line. Use a transformed coordinate
1775 * system where the oval is a unit circle centered at the origin.
1776 * Then scale back to get actual y-coordinate.
1777 */
1778
1779 tx = x/rx;
1780 tmp = 1 - tx*tx;
1781 if (tmp < 0) {
1782 return 0;
1783 }
1784 ty = sqrt(tmp);
1785 y = ty*ry;
1786
1787 /*
1788 * Test both intersection points.
1789 */
1790
1791 if ((y > y1) && (y < y2) && AngleInRange(tx, ty, start, extent)) {
1792 return 1;
1793 }
1794 if ((-y > y1) && (-y < y2) && AngleInRange(tx, -ty, start, extent)) {
1795 return 1;
1796 }
1797 return 0;
1798 }
1799
1800 /*
1801 *--------------------------------------------------------------
1802 *
1803 * AngleInRange --
1804 *
1805 * Determine whether the angle from the origin to a given
1806 * point is within a given range.
1807 *
1808 * Results:
1809 * The return value is 1 if the angle from (0,0) to (x,y)
1810 * is in the range given by start and extent, where angles
1811 * are interpreted in the standard way for ovals (meaning
1812 * backwards from normal interpretation). Otherwise the
1813 * return value is 0.
1814 *
1815 * Side effects:
1816 * None.
1817 *
1818 *--------------------------------------------------------------
1819 */
1820
1821 static int
1822 AngleInRange(x, y, start, extent)
1823 double x, y; /* Coordinate of point; angle measured
1824 * from origin to here, relative to x-axis. */
1825 double start; /* First angle, degrees, >=0, <=360. */
1826 double extent; /* Size of arc in degrees >=-360, <=360. */
1827 {
1828 double diff;
1829
1830 if ((x == 0.0) && (y == 0.0)) {
1831 return 1;
1832 }
1833 diff = -atan2(y, x);
1834 diff = diff*(180.0/PI) - start;
1835 while (diff > 360.0) {
1836 diff -= 360.0;
1837 }
1838 while (diff < 0.0) {
1839 diff += 360.0;
1840 }
1841 if (extent >= 0) {
1842 return diff <= extent;
1843 }
1844 return (diff-360.0) >= extent;
1845 }
1846
1847 /*
1848 *--------------------------------------------------------------
1849 *
1850 * ArcToPostscript --
1851 *
1852 * This procedure is called to generate Postscript for
1853 * arc items.
1854 *
1855 * Results:
1856 * The return value is a standard Tcl result. If an error
1857 * occurs in generating Postscript then an error message is
1858 * left in the interp's result, replacing whatever used
1859 * to be there. If no error occurs, then Postscript for the
1860 * item is appended to the result.
1861 *
1862 * Side effects:
1863 * None.
1864 *
1865 *--------------------------------------------------------------
1866 */
1867
1868 static int
1869 ArcToPostscript(interp, canvas, itemPtr, prepass)
1870 Tcl_Interp *interp; /* Leave Postscript or error message
1871 * here. */
1872 Tk_Canvas canvas; /* Information about overall canvas. */
1873 Tk_Item *itemPtr; /* Item for which Postscript is
1874 * wanted. */
1875 int prepass; /* 1 means this is a prepass to
1876 * collect font information; 0 means
1877 * final Postscript is being created. */
1878 {
1879 ArcItem *arcPtr = (ArcItem *) itemPtr;
1880 char buffer[400];
1881 double y1, y2, ang1, ang2;
1882 XColor *color;
1883 Pixmap stipple;
1884 XColor *fillColor;
1885 Pixmap fillStipple;
1886 Tk_State state = Tk_GetItemState(canvas, itemPtr);
1887 Tk_Tile tile;
1888
1889 y1 = Tk_CanvasPsY(canvas, arcPtr->bbox[1]);
1890 y2 = Tk_CanvasPsY(canvas, arcPtr->bbox[3]);
1891 ang1 = arcPtr->start;
1892 ang2 = ang1 + arcPtr->extent;
1893 if (ang2 < ang1) {
1894 ang1 = ang2;
1895 ang2 = arcPtr->start;
1896 }
1897
1898 tile = arcPtr->outline.tile;
1899 color = arcPtr->outline.color;
1900 stipple = arcPtr->outline.stipple;
1901 fillColor = arcPtr->fillColor;
1902 fillStipple = arcPtr->fillStipple;
1903 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1904 if (arcPtr->outline.activeColor!=NULL) {
1905 color = arcPtr->outline.activeColor;
1906 }
1907 if (arcPtr->outline.activeStipple!=None) {
1908 stipple = arcPtr->outline.activeStipple;
1909 }
1910 if (arcPtr->activeFillColor!=NULL) {
1911 fillColor = arcPtr->activeFillColor;
1912 }
1913 if (arcPtr->activeFillStipple!=None) {
1914 fillStipple = arcPtr->activeFillStipple;
1915 }
1916 } else if (state==TK_STATE_DISABLED) {
1917 if (arcPtr->outline.disabledColor!=NULL) {
1918 color = arcPtr->outline.disabledColor;
1919 }
1920 if (arcPtr->outline.disabledStipple!=None) {
1921 stipple = arcPtr->outline.disabledStipple;
1922 }
1923 if (arcPtr->disabledFillColor!=NULL) {
1924 fillColor = arcPtr->disabledFillColor;
1925 }
1926 if (arcPtr->disabledFillStipple!=None) {
1927 fillStipple = arcPtr->disabledFillStipple;
1928 }
1929 }
1930
1931 /*
1932 * If the arc is filled, output Postscript for the interior region
1933 * of the arc.
1934 */
1935
1936 if (arcPtr->fillGC != None) {
1937 sprintf(buffer, "matrix currentmatrix\n%.15g %.15g translate %.15g %.15g scale\n",
1938 (arcPtr->bbox[0] + arcPtr->bbox[2])/2, (y1 + y2)/2,
1939 (arcPtr->bbox[2] - arcPtr->bbox[0])/2, (y1 - y2)/2);
1940 Tcl_AppendResult(interp, buffer, (char *) NULL);
1941 if (arcPtr->style == CHORD_STYLE) {
1942 sprintf(buffer, "0 0 1 %.15g %.15g arc closepath\nsetmatrix\n",
1943 ang1, ang2);
1944 } else {
1945 sprintf(buffer,
1946 "0 0 moveto 0 0 1 %.15g %.15g arc closepath\nsetmatrix\n",
1947 ang1, ang2);
1948 }
1949 Tcl_AppendResult(interp, buffer, (char *) NULL);
1950 if (Tk_CanvasPsColor(interp, canvas, fillColor) != TCL_OK) {
1951 return TCL_ERROR;
1952 };
1953 if (fillStipple != None) {
1954 Tcl_AppendResult(interp, "clip ", (char *) NULL);
1955 if (Tk_CanvasPsStipple(interp, canvas, fillStipple)
1956 != TCL_OK) {
1957 return TCL_ERROR;
1958 }
1959 if (arcPtr->outline.gc != None) {
1960 Tcl_AppendResult(interp, "grestore gsave\n", (char *) NULL);
1961 }
1962 } else {
1963 Tcl_AppendResult(interp, "fill\n", (char *) NULL);
1964 }
1965 }
1966
1967 /*
1968 * If there's an outline for the arc, draw it.
1969 */
1970
1971 if (arcPtr->outline.gc != None) {
1972 sprintf(buffer, "matrix currentmatrix\n%.15g %.15g translate %.15g %.15g scale\n",
1973 (arcPtr->bbox[0] + arcPtr->bbox[2])/2, (y1 + y2)/2,
1974 (arcPtr->bbox[2] - arcPtr->bbox[0])/2, (y1 - y2)/2);
1975 Tcl_AppendResult(interp, buffer, (char *) NULL);
1976 sprintf(buffer, "0 0 1 %.15g %.15g", ang1, ang2);
1977 Tcl_AppendResult(interp, buffer,
1978 " arc\nsetmatrix\n0 setlinecap\n", (char *) NULL);
1979 if (Tk_CanvasPsOutline(canvas, itemPtr,
1980 &(arcPtr->outline)) != TCL_OK) {
1981 return TCL_ERROR;
1982 }
1983 if (arcPtr->style != ARC_STYLE) {
1984 Tcl_AppendResult(interp, "grestore gsave\n", (char *) NULL);
1985 if (arcPtr->style == CHORD_STYLE) {
1986 Tk_CanvasPsPath(interp, canvas, arcPtr->outlinePtr,
1987 CHORD_OUTLINE_PTS);
1988 } else {
1989 Tk_CanvasPsPath(interp, canvas, arcPtr->outlinePtr,
1990 PIE_OUTLINE1_PTS);
1991 if (Tk_CanvasPsColor(interp, canvas, color)
1992 != TCL_OK) {
1993 return TCL_ERROR;
1994 }
1995 if (stipple != None) {
1996 Tcl_AppendResult(interp, "clip ", (char *) NULL);
1997 if (Tk_CanvasPsStipple(interp, canvas,
1998 stipple) != TCL_OK) {
1999 return TCL_ERROR;
2000 }
2001 } else {
2002 Tcl_AppendResult(interp, "fill\n", (char *) NULL);
2003 }
2004 Tcl_AppendResult(interp, "grestore gsave\n", (char *) NULL);
2005 Tk_CanvasPsPath(interp, canvas,
2006 arcPtr->outlinePtr + 2*PIE_OUTLINE1_PTS,
2007 PIE_OUTLINE2_PTS);
2008 }
2009 if (Tk_CanvasPsColor(interp, canvas, color)
2010 != TCL_OK) {
2011 return TCL_ERROR;
2012 }
2013 if (stipple != None) {
2014 Tcl_AppendResult(interp, "clip ", (char *) NULL);
2015 if (Tk_CanvasPsStipple(interp, canvas,
2016 stipple) != TCL_OK) {
2017 return TCL_ERROR;
2018 }
2019 } else {
2020 Tcl_AppendResult(interp, "fill\n", (char *) NULL);
2021 }
2022 }
2023 }
2024
2025 return TCL_OK;
2026 }
2027
2028 /*
2029 *--------------------------------------------------------------
2030 *
2031 * StyleParseProc --
2032 *
2033 * This procedure is invoked during option processing to handle
2034 * the "-style" option.
2035 *
2036 * Results:
2037 * A standard Tcl return value.
2038 *
2039 * Side effects:
2040 * The state for a given item gets replaced by the state
2041 * indicated in the value argument.
2042 *
2043 *--------------------------------------------------------------
2044 */
2045
2046 static int
2047 StyleParseProc(clientData, interp, tkwin, ovalue, widgRec, offset)
2048 ClientData clientData; /* some flags.*/
2049 Tcl_Interp *interp; /* Used for reporting errors. */
2050 Tk_Window tkwin; /* Window containing canvas widget. */
2051 Tcl_Obj * ovalue; /* Value of option. */
2052 char *widgRec; /* Pointer to record for item. */
2053 int offset; /* Offset into item. */
2054 {
2055 int c;
2056 size_t length;
2057 char *value = Tcl_GetString(ovalue);
2058
2059 register Style *stylePtr = (Style *) (widgRec + offset);
2060
2061 if(value == NULL || *value == 0) {
2062 *stylePtr = PIESLICE_STYLE;
2063 return TCL_OK;
2064 }
2065
2066 c = value[0];
2067 length = strlen(value);
2068
2069 if ((c == 'a') && (strncmp(value, "arc", length) == 0)) {
2070 *stylePtr = ARC_STYLE;
2071 return TCL_OK;
2072 }
2073 if ((c == 'c') && (strncmp(value, "chord", length) == 0)) {
2074 *stylePtr = CHORD_STYLE;
2075 return TCL_OK;
2076 }
2077 if ((c == 'p') && (strncmp(value, "pieslice", length) == 0)) {
2078 *stylePtr = PIESLICE_STYLE;
2079 return TCL_OK;
2080 }
2081
2082 Tcl_AppendResult(interp, "bad -style option \"",
2083 value, "\": must be arc, chord, or pieslice",
2084 (char *) NULL);
2085 *stylePtr = PIESLICE_STYLE;
2086 return TCL_ERROR;
2087 }
2088
2089 /*
2090 *--------------------------------------------------------------
2091 *
2092 * StylePrintProc --
2093 *
2094 * This procedure is invoked by the Tk configuration code
2095 * to produce a printable string for the "-style"
2096 * configuration option.
2097 *
2098 * Results:
2099 * The return value is a string describing the state for
2100 * the item referred to by "widgRec". In addition, *freeProcPtr
2101 * is filled in with the address of a procedure to call to free
2102 * the result string when it's no longer needed (or NULL to
2103 * indicate that the string doesn't need to be freed).
2104 *
2105 * Side effects:
2106 * None.
2107 *
2108 *--------------------------------------------------------------
2109 */
2110
2111 static Tcl_Obj *
2112 StylePrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
2113 ClientData clientData; /* Ignored. */
2114 Tk_Window tkwin; /* Ignored. */
2115 char *widgRec; /* Pointer to record for item. */
2116 int offset; /* Offset into item. */
2117 Tcl_FreeProc **freeProcPtr; /* Pointer to variable to fill in with
2118 * information about how to reclaim
2119 * storage for return string. */
2120 {
2121 Tcl_Obj * result = NULL;
2122 register Style *stylePtr = (Style *) (widgRec + offset);
2123
2124 if (*stylePtr==ARC_STYLE) {
2125 return LangStringArg("arc");
2126 } else if (*stylePtr==CHORD_STYLE) {
2127 return LangStringArg("chord");
2128 } else {
2129 return LangStringArg("pieslice");
2130 }
2131 }
2132