Debian Codegraph @

pTk/mTk/generic/tkRectOval.c

raw symbols SCIP
1 /*
2 * tkRectOval.c --
3 *
4 * This file implements rectangle and oval items for canvas
5 * widgets.
6 *
7 * Copyright (c) 1991-1994 The Regents of the University of California.
8 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
9 *
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 *
13 * RCS: @(#) $Id: tkRectOval.c,v 1.10 2003/02/09 07:48:22 hobbs Exp $
14 */
15
16 #include "tkPort.h"
17 #include "tk.h"
18 #include "tkInt.h"
19 #include "tkCanvases.h"
20
21 /*
22 * The structure below defines the record for each rectangle/oval item.
23 */
24
25 typedef struct RectOvalItem {
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 double bbox[4]; /* Coordinates of bounding box for rectangle
30 * or oval (x1, y1, x2, y2). Item includes
31 * x1 and x2 but not y1 and y2. */
32 Tk_TSOffset tsoffset;
33 XColor *fillColor; /* Color for filling rectangle/oval. */
34 XColor *activeFillColor; /* Color for filling rectangle/oval if state is active. */
35 XColor *disabledFillColor; /* Color for filling rectangle/oval if state is disabled. */
36 Pixmap fillStipple; /* Stipple bitmap for filling item. */
37 Pixmap activeFillStipple; /* Stipple bitmap for filling item if state is active. */
38 Pixmap disabledFillStipple; /* Stipple bitmap for filling item if state is disabled. */
39 GC fillGC; /* Graphics context for filling item. */
40 Tk_Tile fillTile;
41 } RectOvalItem;
42
43 /*
44 * Information used for parsing configuration specs:
45 */
46
47 static Tk_CustomOption stateOption = {
48 TkStateParseProc,
49 TkStatePrintProc, (ClientData) 2
50 };
51 static Tk_CustomOption tagsOption = {
52 Tk_CanvasTagsParseProc,
53 Tk_CanvasTagsPrintProc, (ClientData) NULL
54 };
55 static Tk_CustomOption dashOption = {
56 TkCanvasDashParseProc,
57 TkCanvasDashPrintProc, (ClientData) NULL
58 };
59 static Tk_CustomOption tileOption = {
60 Tk_TileParseProc,
61 Tk_TilePrintProc, (ClientData) NULL
62 };
63 static Tk_CustomOption offsetOption = {
64 Tk_OffsetParseProc,
65 Tk_OffsetPrintProc, (ClientData) TK_OFFSET_RELATIVE
66 };
67 static Tk_CustomOption pixelOption = {
68 Tk_PixelParseProc,
69 Tk_PixelPrintProc, (ClientData) NULL
70 };
71
72 static Tk_ConfigSpec configSpecs[] = {
73 {TK_CONFIG_CUSTOM, "-activedash", (char *) NULL, (char *) NULL,
74 (char *) NULL, Tk_Offset(RectOvalItem, outline.activeDash),
75 TK_CONFIG_NULL_OK, &dashOption},
76 {TK_CONFIG_COLOR, "-activefill", (char *) NULL, (char *) NULL,
77 (char *) NULL, Tk_Offset(RectOvalItem, activeFillColor),
78 TK_CONFIG_NULL_OK},
79 {TK_CONFIG_COLOR, "-activeoutline", (char *) NULL, (char *) NULL,
80 (char *) NULL, Tk_Offset(RectOvalItem, outline.activeColor),
81 TK_CONFIG_NULL_OK},
82 {TK_CONFIG_BITMAP, "-activeoutlinestipple", (char *) NULL, (char *) NULL,
83 (char *) NULL, Tk_Offset(RectOvalItem, outline.activeStipple),
84 TK_CONFIG_NULL_OK},
85 {TK_CONFIG_BITMAP, "-activestipple", (char *) NULL, (char *) NULL,
86 (char *) NULL, Tk_Offset(RectOvalItem, activeFillStipple),
87 TK_CONFIG_NULL_OK},
88 {TK_CONFIG_CUSTOM, "-activewidth", (char *) NULL, (char *) NULL,
89 "0.0", Tk_Offset(RectOvalItem, outline.activeWidth),
90 TK_CONFIG_DONT_SET_DEFAULT, &pixelOption},
91 {TK_CONFIG_CUSTOM, "-dash", (char *) NULL, (char *) NULL,
92 (char *) NULL, Tk_Offset(RectOvalItem, outline.dash),
93 TK_CONFIG_NULL_OK, &dashOption},
94 {TK_CONFIG_PIXELS, "-dashoffset", (char *) NULL, (char *) NULL,
95 "0", Tk_Offset(RectOvalItem, outline.offset),
96 TK_CONFIG_DONT_SET_DEFAULT},
97 {TK_CONFIG_CUSTOM, "-disableddash", (char *) NULL, (char *) NULL,
98 (char *) NULL, Tk_Offset(RectOvalItem, outline.disabledDash),
99 TK_CONFIG_NULL_OK, &dashOption},
100 {TK_CONFIG_COLOR, "-disabledfill", (char *) NULL, (char *) NULL,
101 (char *) NULL, Tk_Offset(RectOvalItem, disabledFillColor),
102 TK_CONFIG_NULL_OK},
103 {TK_CONFIG_COLOR, "-disabledoutline", (char *) NULL, (char *) NULL,
104 (char *) NULL, Tk_Offset(RectOvalItem, outline.disabledColor),
105 TK_CONFIG_NULL_OK},
106 {TK_CONFIG_BITMAP, "-disabledoutlinestipple", (char *) NULL, (char *) NULL,
107 (char *) NULL, Tk_Offset(RectOvalItem, outline.disabledStipple),
108 TK_CONFIG_NULL_OK},
109 {TK_CONFIG_BITMAP, "-disabledstipple", (char *) NULL, (char *) NULL,
110 (char *) NULL, Tk_Offset(RectOvalItem, disabledFillStipple),
111 TK_CONFIG_NULL_OK},
112 {TK_CONFIG_PIXELS, "-disabledwidth", (char *) NULL, (char *) NULL,
113 "0.0", Tk_Offset(RectOvalItem, outline.disabledWidth),
114 TK_CONFIG_DONT_SET_DEFAULT, &pixelOption},
115 {TK_CONFIG_COLOR, "-fill", (char *) NULL, (char *) NULL,
116 (char *) NULL, Tk_Offset(RectOvalItem, fillColor), TK_CONFIG_NULL_OK},
117 {TK_CONFIG_CUSTOM, "-offset", (char *) NULL, (char *) NULL,
118 "0 0", Tk_Offset(RectOvalItem, tsoffset),
119 TK_CONFIG_DONT_SET_DEFAULT, &offsetOption},
120 {TK_CONFIG_COLOR, "-outline", (char *) NULL, (char *) NULL,
121 "black", Tk_Offset(RectOvalItem, outline.color), TK_CONFIG_NULL_OK},
122 {TK_CONFIG_CUSTOM, "-outlineoffset", (char *) NULL, (char *) NULL,
123 "0 0", Tk_Offset(RectOvalItem, outline.tsoffset),
124 TK_CONFIG_DONT_SET_DEFAULT, &offsetOption},
125 {TK_CONFIG_BITMAP, "-outlinestipple", (char *) NULL, (char *) NULL,
126 (char *) NULL, Tk_Offset(RectOvalItem, outline.stipple),
127 TK_CONFIG_NULL_OK},
128 {TK_CONFIG_CUSTOM, "-state", (char *) NULL, (char *) NULL,
129 (char *) NULL, Tk_Offset(Tk_Item, state),TK_CONFIG_NULL_OK,
130 &stateOption},
131 {TK_CONFIG_BITMAP, "-stipple", (char *) NULL, (char *) NULL,
132 (char *) NULL, Tk_Offset(RectOvalItem, fillStipple), TK_CONFIG_NULL_OK},
133 {TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
134 (char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
135 {TK_CONFIG_CUSTOM, "-tile", (char *) NULL, (char *) NULL,
136 (char *) NULL, Tk_Offset(RectOvalItem, fillTile),
137 TK_CONFIG_NULL_OK, &tileOption},
138 {TK_CONFIG_CALLBACK, "-updatecommand", (char *) NULL, (char *) NULL,
139 (char *) NULL, Tk_Offset(Tk_Item, updateCmd), TK_CONFIG_NULL_OK},
140 {TK_CONFIG_CUSTOM, "-width", (char *) NULL, (char *) NULL,
141 "1.0", Tk_Offset(RectOvalItem, outline.width),
142 TK_CONFIG_DONT_SET_DEFAULT, &pixelOption},
143 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
144 (char *) NULL, 0, 0}
145 };
146
147 /*
148 * Prototypes for procedures defined in this file:
149 */
150
151 static void ComputeRectOvalBbox _ANSI_ARGS_((Tk_Canvas canvas,
152 RectOvalItem *rectOvalPtr));
153 static int ConfigureRectOval _ANSI_ARGS_((Tcl_Interp *interp,
154 Tk_Canvas canvas, Tk_Item *itemPtr, int objc,
155 Tcl_Obj *CONST objv[], int flags));
156 static int CreateRectOval _ANSI_ARGS_((Tcl_Interp *interp,
157 Tk_Canvas canvas, struct Tk_Item *itemPtr,
158 int objc, Tcl_Obj *CONST objv[]));
159 static void DeleteRectOval _ANSI_ARGS_((Tk_Canvas canvas,
160 Tk_Item *itemPtr, Display *display));
161 static void DisplayRectOval _ANSI_ARGS_((Tk_Canvas canvas,
162 Tk_Item *itemPtr, Display *display, Drawable dst,
163 int x, int y, int width, int height));
164 static int OvalToArea _ANSI_ARGS_((Tk_Canvas canvas,
165 Tk_Item *itemPtr, double *areaPtr));
166 static double OvalToPoint _ANSI_ARGS_((Tk_Canvas canvas,
167 Tk_Item *itemPtr, double *pointPtr));
168 static int RectOvalCoords _ANSI_ARGS_((Tcl_Interp *interp,
169 Tk_Canvas canvas, Tk_Item *itemPtr, int objc,
170 Tcl_Obj *CONST objv[]));
171 static int RectOvalToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
172 Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
173 static int RectToArea _ANSI_ARGS_((Tk_Canvas canvas,
174 Tk_Item *itemPtr, double *areaPtr));
175 static double RectToPoint _ANSI_ARGS_((Tk_Canvas canvas,
176 Tk_Item *itemPtr, double *pointPtr));
177 static void ScaleRectOval _ANSI_ARGS_((Tk_Canvas canvas,
178 Tk_Item *itemPtr, double originX, double originY,
179 double scaleX, double scaleY));
180 static void TranslateRectOval _ANSI_ARGS_((Tk_Canvas canvas,
181 Tk_Item *itemPtr, double deltaX, double deltaY));
182
183 /*
184 * The structures below defines the rectangle and oval item types
185 * by means of procedures that can be invoked by generic item code.
186 */
187
188 Tk_ItemType tkRectangleType = {
189 "rectangle", /* name */
190 sizeof(RectOvalItem), /* itemSize */
191 CreateRectOval, /* createProc */
192 configSpecs, /* configSpecs */
193 ConfigureRectOval, /* configureProc */
194 RectOvalCoords, /* coordProc */
195 DeleteRectOval, /* deleteProc */
196 DisplayRectOval, /* displayProc */
197 TK_CONFIG_OBJS, /* flags */
198 RectToPoint, /* pointProc */
199 RectToArea, /* areaProc */
200 RectOvalToPostscript, /* postscriptProc */
201 ScaleRectOval, /* scaleProc */
202 TranslateRectOval, /* translateProc */
203 (Tk_ItemIndexProc *) NULL, /* indexProc */
204 (Tk_ItemCursorProc *) NULL, /* icursorProc */
205 (Tk_ItemSelectionProc *) NULL, /* selectionProc */
206 (Tk_ItemInsertProc *) NULL, /* insertProc */
207 (Tk_ItemDCharsProc *) NULL, /* dTextProc */
208 (Tk_ItemType *) NULL, /* nextPtr */
209 };
210
211 Tk_ItemType tkOvalType = {
212 "oval", /* name */
213 sizeof(RectOvalItem), /* itemSize */
214 CreateRectOval, /* createProc */
215 configSpecs, /* configSpecs */
216 ConfigureRectOval, /* configureProc */
217 RectOvalCoords, /* coordProc */
218 DeleteRectOval, /* deleteProc */
219 DisplayRectOval, /* displayProc */
220 TK_CONFIG_OBJS, /* flags */
221 OvalToPoint, /* pointProc */
222 OvalToArea, /* areaProc */
223 RectOvalToPostscript, /* postscriptProc */
224 ScaleRectOval, /* scaleProc */
225 TranslateRectOval, /* translateProc */
226 (Tk_ItemIndexProc *) NULL, /* indexProc */
227 (Tk_ItemCursorProc *) NULL, /* cursorProc */
228 (Tk_ItemSelectionProc *) NULL, /* selectionProc */
229 (Tk_ItemInsertProc *) NULL, /* insertProc */
230 (Tk_ItemDCharsProc *) NULL, /* dTextProc */
231 (Tk_ItemType *) NULL, /* nextPtr */
232 };
233
234 /*
235 *--------------------------------------------------------------
236 *
237 * CreateRectOval --
238 *
239 * This procedure is invoked to create a new rectangle
240 * or oval item in a canvas.
241 *
242 * Results:
243 * A standard Tcl return value. If an error occurred in
244 * creating the item, then an error message is left in
245 * the interp's result; in this case itemPtr is left uninitialized,
246 * so it can be safely freed by the caller.
247 *
248 * Side effects:
249 * A new rectangle or oval item is created.
250 *
251 *--------------------------------------------------------------
252 */
253
254 static int
255 CreateRectOval(interp, canvas, itemPtr, objc, objv)
256 Tcl_Interp *interp; /* For error reporting. */
257 Tk_Canvas canvas; /* Canvas to hold new item. */
258 Tk_Item *itemPtr; /* Record to hold new item; header
259 * has been initialized by caller. */
260 int objc; /* Number of arguments in objv. */
261 Tcl_Obj *CONST objv[]; /* Arguments describing rectangle. */
262 {
263 RectOvalItem *rectOvalPtr = (RectOvalItem *) itemPtr;
264 int i;
265
266 if (objc == 0) {
267 panic("canvas did not pass any coords\n");
268 }
269
270 /*
271 * Carry out initialization that is needed in order to clean
272 * up after errors during the the remainder of this procedure.
273 */
274
275 Tk_CreateOutline(&(rectOvalPtr->outline));
276 rectOvalPtr->tsoffset.flags = 0;
277 rectOvalPtr->tsoffset.xoffset = 0;
278 rectOvalPtr->tsoffset.yoffset = 0;
279 rectOvalPtr->fillColor = NULL;
280 rectOvalPtr->activeFillColor = NULL;
281 rectOvalPtr->disabledFillColor = NULL;
282 rectOvalPtr->fillStipple = None;
283 rectOvalPtr->activeFillStipple = None;
284 rectOvalPtr->disabledFillStipple = None;
285 rectOvalPtr->fillGC = None;
286
287 /*
288 * Process the arguments to fill in the item record.
289 */
290
291 for (i = 1; i < objc; i++) {
292 char *arg = Tcl_GetString(objv[i]);
293 if ((arg[0] == '-') && (arg[1] >= 'a') && (arg[1] <= 'z')) {
294 break;
295 }
296 }
297 if ((RectOvalCoords(interp, canvas, itemPtr, i, objv) != TCL_OK)) {
298 goto error;
299 }
300 if (ConfigureRectOval(interp, canvas, itemPtr, objc-i, objv+i, 0)
301 == TCL_OK) {
302 return TCL_OK;
303 }
304
305 error:
306 DeleteRectOval(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
307 return TCL_ERROR;
308 }
309
310 /*
311 *--------------------------------------------------------------
312 *
313 * RectOvalCoords --
314 *
315 * This procedure is invoked to process the "coords" widget
316 * command on rectangles and ovals. See the user documentation
317 * for details on what it does.
318 *
319 * Results:
320 * Returns TCL_OK or TCL_ERROR, and sets the interp's result.
321 *
322 * Side effects:
323 * The coordinates for the given item may be changed.
324 *
325 *--------------------------------------------------------------
326 */
327
328 static int
329 RectOvalCoords(interp, canvas, itemPtr, objc, objv)
330 Tcl_Interp *interp; /* Used for error reporting. */
331 Tk_Canvas canvas; /* Canvas containing item. */
332 Tk_Item *itemPtr; /* Item whose coordinates are to be
333 * read or modified. */
334 int objc; /* Number of coordinates supplied in
335 * objv. */
336 Tcl_Obj *CONST objv[]; /* Array of coordinates: x1, y1,
337 * x2, y2, ... */
338 {
339 RectOvalItem *rectOvalPtr = (RectOvalItem *) itemPtr;
340
341 if (objc == 0) {
342 Tcl_Obj *obj = Tcl_NewObj();
343 Tcl_Obj *subobj = Tcl_NewDoubleObj(rectOvalPtr->bbox[0]);
344 Tcl_ListObjAppendElement(interp, obj, subobj);
345 subobj = Tcl_NewDoubleObj(rectOvalPtr->bbox[1]);
346 Tcl_ListObjAppendElement(interp, obj, subobj);
347 subobj = Tcl_NewDoubleObj(rectOvalPtr->bbox[2]);
348 Tcl_ListObjAppendElement(interp, obj, subobj);
349 subobj = Tcl_NewDoubleObj(rectOvalPtr->bbox[3]);
350 Tcl_ListObjAppendElement(interp, obj, subobj);
351 Tcl_SetObjResult(interp, obj);
352 } else if ((objc == 1)||(objc == 4)) {
353 if (objc==1) {
354 if (Tcl_ListObjGetElements(interp, objv[0], &objc,
355 (Tcl_Obj ***) &objv) != TCL_OK) {
356 return TCL_ERROR;
357 } else if (objc != 4) {
358 char buf[64 + TCL_INTEGER_SPACE];
359
360 sprintf(buf, "wrong # coordinates: expected 0 or 4, got %d", objc);
361 Tcl_SetResult(interp, buf, TCL_VOLATILE);
362 return TCL_ERROR;
363 }
364 }
365 if ((Tk_CanvasGetCoordFromObj(interp, canvas, objv[0],
366 &rectOvalPtr->bbox[0]) != TCL_OK)
367 || (Tk_CanvasGetCoordFromObj(interp, canvas, objv[1],
368 &rectOvalPtr->bbox[1]) != TCL_OK)
369 || (Tk_CanvasGetCoordFromObj(interp, canvas, objv[2],
370 &rectOvalPtr->bbox[2]) != TCL_OK)
371 || (Tk_CanvasGetCoordFromObj(interp, canvas, objv[3],
372 &rectOvalPtr->bbox[3]) != TCL_OK)) {
373 return TCL_ERROR;
374 }
375 ComputeRectOvalBbox(canvas, rectOvalPtr);
376 } else {
377 char buf[64 + TCL_INTEGER_SPACE];
378
379 sprintf(buf, "wrong # coordinates: expected 0 or 4, got %d", objc);
380 Tcl_SetResult(interp, buf, TCL_VOLATILE);
381 return TCL_ERROR;
382 }
383 return TCL_OK;
384 }
385
386 /*
387 *--------------------------------------------------------------
388 *
389 * ConfigureRectOval --
390 *
391 * This procedure is invoked to configure various aspects
392 * of a rectangle or oval item, such as its border and
393 * background colors.
394 *
395 * Results:
396 * A standard Tcl result code. If an error occurs, then
397 * an error message is left in the interp's result.
398 *
399 * Side effects:
400 * Configuration information, such as colors and stipple
401 * patterns, may be set for itemPtr.
402 *
403 *--------------------------------------------------------------
404 */
405
406 static int
407 ConfigureRectOval(interp, canvas, itemPtr, objc, objv, flags)
408 Tcl_Interp *interp; /* Used for error reporting. */
409 Tk_Canvas canvas; /* Canvas containing itemPtr. */
410 Tk_Item *itemPtr; /* Rectangle item to reconfigure. */
411 int objc; /* Number of elements in objv. */
412 Tcl_Obj *CONST objv[]; /* Arguments describing things to configure. */
413 int flags; /* Flags to pass to Tk_ConfigureWidget. */
414 {
415 RectOvalItem *rectOvalPtr = (RectOvalItem *) itemPtr;
416 XGCValues gcValues;
417 GC newGC;
418 unsigned long mask;
419 Tk_Window tkwin;
420 Tk_TSOffset *tsoffset;
421 XColor *color;
422 Pixmap stipple;
423 Tk_State state;
424
425 tkwin = Tk_CanvasTkwin(canvas);
426
427 if (TCL_OK != Tk_ConfigureWidget(interp, tkwin, configSpecs, objc,
428 objv, (char *) rectOvalPtr, flags|TK_CONFIG_OBJS)) {
429 return TCL_ERROR;
430 }
431
432 state = Tk_GetItemState(canvas, itemPtr);
433
434 /*
435 * A few of the options require additional processing, such as
436 * graphics contexts.
437 */
438
439 if (rectOvalPtr->outline.activeWidth > rectOvalPtr->outline.width ||
440 rectOvalPtr->outline.activeDash.number != 0 ||
441 rectOvalPtr->outline.activeColor != NULL ||
442 rectOvalPtr->outline.activeStipple != None ||
443 rectOvalPtr->activeFillColor != NULL ||
444 rectOvalPtr->activeFillStipple != None) {
445 itemPtr->redraw_flags |= TK_ITEM_STATE_DEPENDANT;
446 } else {
447 itemPtr->redraw_flags &= ~TK_ITEM_STATE_DEPENDANT;
448 }
449
450 tsoffset = &rectOvalPtr->outline.tsoffset;
451 flags = tsoffset->flags;
452 if (flags & TK_OFFSET_LEFT) {
453 tsoffset->xoffset = (int) (rectOvalPtr->bbox[0] + 0.5);
454 } else if (flags & TK_OFFSET_CENTER) {
455 tsoffset->xoffset = (int) ((rectOvalPtr->bbox[0]+rectOvalPtr->bbox[2]+1)/2);
456 } else if (flags & TK_OFFSET_RIGHT) {
457 tsoffset->xoffset = (int) (rectOvalPtr->bbox[2] + 0.5);
458 }
459 if (flags & TK_OFFSET_TOP) {
460 tsoffset->yoffset = (int) (rectOvalPtr->bbox[1] + 0.5);
461 } else if (flags & TK_OFFSET_MIDDLE) {
462 tsoffset->yoffset = (int) ((rectOvalPtr->bbox[1]+rectOvalPtr->bbox[3]+1)/2);
463 } else if (flags & TK_OFFSET_BOTTOM) {
464 tsoffset->yoffset = (int) (rectOvalPtr->bbox[2] + 0.5);
465 }
466
467 /*
468 * Configure the outline graphics context. If mask is non-zero,
469 * the gc has changed and must be reallocated, provided that the
470 * new settings specify a valid outline (non-zero width and non-NULL
471 * color)
472 */
473
474 mask = Tk_ConfigOutlineGC(&gcValues, canvas, itemPtr,
475 &(rectOvalPtr->outline));
476 if (mask && \
477 rectOvalPtr->outline.width != 0 && \
478 rectOvalPtr->outline.color != NULL) {
479 newGC = Tk_GetGC(tkwin, mask, &gcValues);
480 } else {
481 newGC = None;
482 }
483 if (rectOvalPtr->outline.gc != None) {
484 Tk_FreeGC(Tk_Display(tkwin), rectOvalPtr->outline.gc);
485 }
486 rectOvalPtr->outline.gc = newGC;
487
488 if (state==TK_STATE_HIDDEN) {
489 ComputeRectOvalBbox(canvas, rectOvalPtr);
490 return TCL_OK;
491 }
492
493 color = rectOvalPtr->fillColor;
494 stipple = rectOvalPtr->fillStipple;
495 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
496 if (rectOvalPtr->activeFillColor!=NULL) {
497 color = rectOvalPtr->activeFillColor;
498 }
499 if (rectOvalPtr->activeFillStipple!=None) {
500 stipple = rectOvalPtr->activeFillStipple;
501 }
502 } else if (state==TK_STATE_DISABLED) {
503 if (rectOvalPtr->disabledFillColor!=NULL) {
504 color = rectOvalPtr->disabledFillColor;
505 }
506 if (rectOvalPtr->disabledFillStipple!=None) {
507 stipple = rectOvalPtr->disabledFillStipple;
508 }
509 }
510
511 if (color == NULL) {
512 newGC = None;
513 } else {
514 gcValues.foreground = color->pixel;
515 if (stipple != None) {
516 gcValues.stipple = stipple;
517 gcValues.fill_style = FillStippled;
518 mask = GCForeground|GCStipple|GCFillStyle;
519 } else {
520 mask = GCForeground;
521 }
522 newGC = Tk_GetGC(tkwin, mask, &gcValues);
523 }
524 if (rectOvalPtr->fillGC != None) {
525 Tk_FreeGC(Tk_Display(tkwin), rectOvalPtr->fillGC);
526 }
527 rectOvalPtr->fillGC = newGC;
528
529 tsoffset = &rectOvalPtr->tsoffset;
530 flags = tsoffset->flags;
531 if (flags & TK_OFFSET_LEFT) {
532 tsoffset->xoffset = (int) (rectOvalPtr->bbox[0] + 0.5);
533 } else if (flags & TK_OFFSET_CENTER) {
534 tsoffset->xoffset = (int) ((rectOvalPtr->bbox[0]+rectOvalPtr->bbox[2]+1)/2);
535 } else if (flags & TK_OFFSET_RIGHT) {
536 tsoffset->xoffset = (int) (rectOvalPtr->bbox[2] + 0.5);
537 }
538 if (flags & TK_OFFSET_TOP) {
539 tsoffset->yoffset = (int) (rectOvalPtr->bbox[1] + 0.5);
540 } else if (flags & TK_OFFSET_MIDDLE) {
541 tsoffset->yoffset = (int) ((rectOvalPtr->bbox[1]+rectOvalPtr->bbox[3]+1)/2);
542 } else if (flags & TK_OFFSET_BOTTOM) {
543 tsoffset->yoffset = (int) (rectOvalPtr->bbox[3] + 0.5);
544 }
545
546 ComputeRectOvalBbox(canvas, rectOvalPtr);
547
548 return TCL_OK;
549 }
550
551 /*
552 *--------------------------------------------------------------
553 *
554 * DeleteRectOval --
555 *
556 * This procedure is called to clean up the data structure
557 * associated with a rectangle or oval item.
558 *
559 * Results:
560 * None.
561 *
562 * Side effects:
563 * Resources associated with itemPtr are released.
564 *
565 *--------------------------------------------------------------
566 */
567
568 static void
569 DeleteRectOval(canvas, itemPtr, display)
570 Tk_Canvas canvas; /* Info about overall widget. */
571 Tk_Item *itemPtr; /* Item that is being deleted. */
572 Display *display; /* Display containing window for
573 * canvas. */
574 {
575 RectOvalItem *rectOvalPtr = (RectOvalItem *) itemPtr;
576
577 Tk_DeleteOutline(display, &(rectOvalPtr->outline));
578 if (rectOvalPtr->fillColor != NULL) {
579 Tk_FreeColor(rectOvalPtr->fillColor);
580 }
581 if (rectOvalPtr->activeFillColor != NULL) {
582 Tk_FreeColor(rectOvalPtr->activeFillColor);
583 }
584 if (rectOvalPtr->disabledFillColor != NULL) {
585 Tk_FreeColor(rectOvalPtr->disabledFillColor);
586 }
587 if (rectOvalPtr->fillStipple != None) {
588 Tk_FreeBitmap(display, rectOvalPtr->fillStipple);
589 }
590 if (rectOvalPtr->activeFillStipple != None) {
591 Tk_FreeBitmap(display, rectOvalPtr->activeFillStipple);
592 }
593 if (rectOvalPtr->disabledFillStipple != None) {
594 Tk_FreeBitmap(display, rectOvalPtr->disabledFillStipple);
595 }
596 if (rectOvalPtr->fillGC != None) {
597 Tk_FreeGC(display, rectOvalPtr->fillGC);
598 }
599 }
600
601 /*
602 *--------------------------------------------------------------
603 *
604 * ComputeRectOvalBbox --
605 *
606 * This procedure is invoked to compute the bounding box of
607 * all the pixels that may be drawn as part of a rectangle
608 * or oval.
609 *
610 * Results:
611 * None.
612 *
613 * Side effects:
614 * The fields x1, y1, x2, and y2 are updated in the header
615 * for itemPtr.
616 *
617 *--------------------------------------------------------------
618 */
619
620 /* ARGSUSED */
621 static void
622 ComputeRectOvalBbox(canvas, rectOvalPtr)
623 Tk_Canvas canvas; /* Canvas that contains item. */
624 RectOvalItem *rectOvalPtr; /* Item whose bbox is to be
625 * recomputed. */
626 {
627 int bloat, tmp;
628 double dtmp, width;
629 Tk_State state = Tk_GetItemState(canvas, &rectOvalPtr->header);
630
631 width = rectOvalPtr->outline.width;
632 if (state==TK_STATE_HIDDEN) {
633 rectOvalPtr->header.x1 = rectOvalPtr->header.y1 =
634 rectOvalPtr->header.x2 = rectOvalPtr->header.y2 = -1;
635 return;
636 }
637 if (((TkCanvas *)canvas)->currentItemPtr == (Tk_Item *)rectOvalPtr) {
638 if (rectOvalPtr->outline.activeWidth>width) {
639 width = rectOvalPtr->outline.activeWidth;
640 }
641 } else if (state==TK_STATE_DISABLED) {
642 if (rectOvalPtr->outline.disabledWidth>0) {
643 width = rectOvalPtr->outline.disabledWidth;
644 }
645 }
646
647 /*
648 * Make sure that the first coordinates are the lowest ones.
649 */
650
651 if (rectOvalPtr->bbox[1] > rectOvalPtr->bbox[3]) {
652 double tmp;
653 tmp = rectOvalPtr->bbox[3];
654 rectOvalPtr->bbox[3] = rectOvalPtr->bbox[1];
655 rectOvalPtr->bbox[1] = tmp;
656 }
657 if (rectOvalPtr->bbox[0] > rectOvalPtr->bbox[2]) {
658 double tmp;
659 tmp = rectOvalPtr->bbox[2];
660 rectOvalPtr->bbox[2] = rectOvalPtr->bbox[0];
661 rectOvalPtr->bbox[0] = tmp;
662 }
663
664 if (rectOvalPtr->outline.gc == None) {
665 /*
666 * The Win32 switch was added for 8.3 to solve a problem
667 * with ovals leaving traces on bottom and right of 1 pixel.
668 * This may not be the correct place to solve it, but it works.
669 */
670 #ifdef __WIN32__
671 bloat = 1;
672 #else
673 bloat = 0;
674 #endif
675 } else {
676 bloat = (int) (width+1)/2;
677 }
678
679 /*
680 * Special note: the rectangle is always drawn at least 1x1 in
681 * size, so round up the upper coordinates to be at least 1 unit
682 * greater than the lower ones.
683 */
684
685 tmp = (int) ((rectOvalPtr->bbox[0] >= 0) ? rectOvalPtr->bbox[0] + .5
686 : rectOvalPtr->bbox[0] - .5);
687 rectOvalPtr->header.x1 = tmp - bloat;
688 tmp = (int) ((rectOvalPtr->bbox[1] >= 0) ? rectOvalPtr->bbox[1] + .5
689 : rectOvalPtr->bbox[1] - .5);
690 rectOvalPtr->header.y1 = tmp - bloat;
691 dtmp = rectOvalPtr->bbox[2];
692 if (dtmp < (rectOvalPtr->bbox[0] + 1)) {
693 dtmp = rectOvalPtr->bbox[0] + 1;
694 }
695 tmp = (int) ((dtmp >= 0) ? dtmp + .5 : dtmp - .5);
696 rectOvalPtr->header.x2 = tmp + bloat;
697 dtmp = rectOvalPtr->bbox[3];
698 if (dtmp < (rectOvalPtr->bbox[1] + 1)) {
699 dtmp = rectOvalPtr->bbox[1] + 1;
700 }
701 tmp = (int) ((dtmp >= 0) ? dtmp + .5 : dtmp - .5);
702 rectOvalPtr->header.y2 = tmp + bloat;
703 }
704
705 /*
706 *--------------------------------------------------------------
707 *
708 * DisplayRectOval --
709 *
710 * This procedure is invoked to draw a rectangle or oval
711 * item in a given drawable.
712 *
713 * Results:
714 * None.
715 *
716 * Side effects:
717 * ItemPtr is drawn in drawable using the transformation
718 * information in canvas.
719 *
720 *--------------------------------------------------------------
721 */
722
723 static void
724 DisplayRectOval(canvas, itemPtr, display, drawable, x, y, width, height)
725 Tk_Canvas canvas; /* Canvas that contains item. */
726 Tk_Item *itemPtr; /* Item to be displayed. */
727 Display *display; /* Display on which to draw item. */
728 Drawable drawable; /* Pixmap or window in which to draw
729 * item. */
730 int x, y, width, height; /* Describes region of canvas that
731 * must be redisplayed (not used). */
732 {
733 RectOvalItem *rectOvalPtr = (RectOvalItem *) itemPtr;
734 short x1, y1, x2, y2;
735 Pixmap fillStipple;
736 Tk_State state = Tk_GetItemState(canvas, itemPtr);
737 Tk_Tile fillTile;
738
739 /*
740 * Compute the screen coordinates of the bounding box for the item.
741 * Make sure that the bbox is at least one pixel large, since some
742 * X servers will die if it isn't.
743 */
744
745 Tk_CanvasDrawableCoords(canvas, rectOvalPtr->bbox[0], rectOvalPtr->bbox[1],
746 &x1, &y1);
747 Tk_CanvasDrawableCoords(canvas, rectOvalPtr->bbox[2], rectOvalPtr->bbox[3],
748 &x2, &y2);
749 if (x2 <= x1) {
750 x2 = x1+1;
751 }
752 if (y2 <= y1) {
753 y2 = y1+1;
754 }
755
756 /*
757 * Display filled part first (if wanted), then outline. If we're
758 * stippling, then modify the stipple offset in the GC. Be sure to
759 * reset the offset when done, since the GC is supposed to be
760 * read-only.
761 */
762
763 fillTile = rectOvalPtr->fillTile;
764 fillStipple = rectOvalPtr->fillStipple;
765 if (((TkCanvas *)canvas)->currentItemPtr == (Tk_Item *)rectOvalPtr) {
766 if (rectOvalPtr->activeFillStipple!=None) {
767 fillStipple = rectOvalPtr->activeFillStipple;
768 }
769 } else if (state==TK_STATE_DISABLED) {
770 if (rectOvalPtr->disabledFillStipple!=None) {
771 fillStipple = rectOvalPtr->disabledFillStipple;
772 }
773 }
774
775 if (rectOvalPtr->fillGC != None) {
776 if (fillStipple != None) {
777 Tk_TSOffset *tsoffset;
778 int w=0; int h=0;
779 tsoffset = &rectOvalPtr->tsoffset;
780 if (tsoffset) {
781 int flags = tsoffset->flags;
782 if (flags & (TK_OFFSET_CENTER|TK_OFFSET_MIDDLE)) {
783 Tk_SizeOfBitmap(display, fillStipple, &w, &h);
784 if (flags & TK_OFFSET_CENTER) {
785 w /= 2;
786 } else {
787 w = 0;
788 }
789 if (flags & TK_OFFSET_MIDDLE) {
790 h /= 2;
791 } else {
792 h = 0;
793 }
794 }
795 tsoffset->xoffset -= w;
796 tsoffset->yoffset -= h;
797 }
798 Tk_CanvasSetOffset(canvas, rectOvalPtr->fillGC, tsoffset);
799 if (tsoffset) {
800 tsoffset->xoffset += w;
801 tsoffset->yoffset += h;
802 }
803 }
804 if (rectOvalPtr->header.typePtr == &tkRectangleType) {
805 XFillRectangle(display, drawable, rectOvalPtr->fillGC,
806 x1, y1, (unsigned int) (x2-x1), (unsigned int) (y2-y1));
807 } else {
808 XFillArc(display, drawable, rectOvalPtr->fillGC,
809 x1, y1, (unsigned) (x2-x1), (unsigned) (y2-y1),
810 0, 360*64);
811 }
812 if (fillStipple != None) {
813 XSetTSOrigin(display, rectOvalPtr->fillGC, 0, 0);
814 }
815 }
816 if (rectOvalPtr->outline.gc != None) {
817 Tk_ChangeOutlineGC(canvas, itemPtr, &(rectOvalPtr->outline));
818 if (rectOvalPtr->header.typePtr == &tkRectangleType) {
819 XDrawRectangle(display, drawable, rectOvalPtr->outline.gc,
820 x1, y1, (unsigned) (x2-x1), (unsigned) (y2-y1));
821 } else {
822 XDrawArc(display, drawable, rectOvalPtr->outline.gc,
823 x1, y1, (unsigned) (x2-x1), (unsigned) (y2-y1), 0, 360*64);
824 }
825 Tk_ResetOutlineGC(canvas, itemPtr, &(rectOvalPtr->outline));
826 }
827 }
828
829 /*
830 *--------------------------------------------------------------
831 *
832 * RectToPoint --
833 *
834 * Computes the distance from a given point to a given
835 * rectangle, in canvas units.
836 *
837 * Results:
838 * The return value is 0 if the point whose x and y coordinates
839 * are coordPtr[0] and coordPtr[1] is inside the rectangle. If the
840 * point isn't inside the rectangle then the return value is the
841 * distance from the point to the rectangle. If itemPtr is filled,
842 * then anywhere in the interior is considered "inside"; if
843 * itemPtr isn't filled, then "inside" means only the area
844 * occupied by the outline.
845 *
846 * Side effects:
847 * None.
848 *
849 *--------------------------------------------------------------
850 */
851
852 /* ARGSUSED */
853 static double
854 RectToPoint(canvas, itemPtr, pointPtr)
855 Tk_Canvas canvas; /* Canvas containing item. */
856 Tk_Item *itemPtr; /* Item to check against point. */
857 double *pointPtr; /* Pointer to x and y coordinates. */
858 {
859 RectOvalItem *rectPtr = (RectOvalItem *) itemPtr;
860 double xDiff, yDiff, x1, y1, x2, y2, inc, tmp;
861 double width;
862 Tk_State state = Tk_GetItemState(canvas, itemPtr);
863
864 width = rectPtr->outline.width;
865 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
866 if (rectPtr->outline.activeWidth>width) {
867 width = rectPtr->outline.activeWidth;
868 }
869 } else if (state==TK_STATE_DISABLED) {
870 if (rectPtr->outline.disabledWidth>0) {
871 width = rectPtr->outline.disabledWidth;
872 }
873 }
874
875 /*
876 * Generate a new larger rectangle that includes the border
877 * width, if there is one.
878 */
879
880 x1 = rectPtr->bbox[0];
881 y1 = rectPtr->bbox[1];
882 x2 = rectPtr->bbox[2];
883 y2 = rectPtr->bbox[3];
884 if (rectPtr->outline.gc != None) {
885 inc = width/2.0;
886 x1 -= inc;
887 y1 -= inc;
888 x2 += inc;
889 y2 += inc;
890 }
891
892 /*
893 * If the point is inside the rectangle, handle specially:
894 * distance is 0 if rectangle is filled, otherwise compute
895 * distance to nearest edge of rectangle and subtract width
896 * of edge.
897 */
898
899 if ((pointPtr[0] >= x1) && (pointPtr[0] < x2)
900 && (pointPtr[1] >= y1) && (pointPtr[1] < y2)) {
901 if ((rectPtr->fillGC != None) || (rectPtr->outline.gc == None)) {
902 return 0.0;
903 }
904 xDiff = pointPtr[0] - x1;
905 tmp = x2 - pointPtr[0];
906 if (tmp < xDiff) {
907 xDiff = tmp;
908 }
909 yDiff = pointPtr[1] - y1;
910 tmp = y2 - pointPtr[1];
911 if (tmp < yDiff) {
912 yDiff = tmp;
913 }
914 if (yDiff < xDiff) {
915 xDiff = yDiff;
916 }
917 xDiff -= width;
918 if (xDiff < 0.0) {
919 return 0.0;
920 }
921 return xDiff;
922 }
923
924 /*
925 * Point is outside rectangle.
926 */
927
928 if (pointPtr[0] < x1) {
929 xDiff = x1 - pointPtr[0];
930 } else if (pointPtr[0] > x2) {
931 xDiff = pointPtr[0] - x2;
932 } else {
933 xDiff = 0;
934 }
935
936 if (pointPtr[1] < y1) {
937 yDiff = y1 - pointPtr[1];
938 } else if (pointPtr[1] > y2) {
939 yDiff = pointPtr[1] - y2;
940 } else {
941 yDiff = 0;
942 }
943
944 return hypot(xDiff, yDiff);
945 }
946
947 /*
948 *--------------------------------------------------------------
949 *
950 * OvalToPoint --
951 *
952 * Computes the distance from a given point to a given
953 * oval, in canvas units.
954 *
955 * Results:
956 * The return value is 0 if the point whose x and y coordinates
957 * are coordPtr[0] and coordPtr[1] is inside the oval. If the
958 * point isn't inside the oval then the return value is the
959 * distance from the point to the oval. If itemPtr is filled,
960 * then anywhere in the interior is considered "inside"; if
961 * itemPtr isn't filled, then "inside" means only the area
962 * occupied by the outline.
963 *
964 * Side effects:
965 * None.
966 *
967 *--------------------------------------------------------------
968 */
969
970 /* ARGSUSED */
971 static double
972 OvalToPoint(canvas, itemPtr, pointPtr)
973 Tk_Canvas canvas; /* Canvas containing item. */
974 Tk_Item *itemPtr; /* Item to check against point. */
975 double *pointPtr; /* Pointer to x and y coordinates. */
976 {
977 RectOvalItem *ovalPtr = (RectOvalItem *) itemPtr;
978 double width;
979 int filled;
980 Tk_State state = Tk_GetItemState(canvas, itemPtr);
981
982 width = (double) ovalPtr->outline.width;
983 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
984 if (ovalPtr->outline.activeWidth>width) {
985 width = (double) ovalPtr->outline.activeWidth;
986 }
987 } else if (state==TK_STATE_DISABLED) {
988 if (ovalPtr->outline.disabledWidth>0) {
989 width = (double) ovalPtr->outline.disabledWidth;
990 }
991 }
992
993
994 filled = ovalPtr->fillGC != None;
995 if (ovalPtr->outline.gc == None) {
996 width = 0.0;
997 filled = 1;
998 }
999 return TkOvalToPoint(ovalPtr->bbox, width, filled, pointPtr);
1000 }
1001
1002 /*
1003 *--------------------------------------------------------------
1004 *
1005 * RectToArea --
1006 *
1007 * This procedure is called to determine whether an item
1008 * lies entirely inside, entirely outside, or overlapping
1009 * a given rectangle.
1010 *
1011 * Results:
1012 * -1 is returned if the item is entirely outside the area
1013 * given by rectPtr, 0 if it overlaps, and 1 if it is entirely
1014 * inside the given area.
1015 *
1016 * Side effects:
1017 * None.
1018 *
1019 *--------------------------------------------------------------
1020 */
1021
1022 /* ARGSUSED */
1023 static int
1024 RectToArea(canvas, itemPtr, areaPtr)
1025 Tk_Canvas canvas; /* Canvas containing item. */
1026 Tk_Item *itemPtr; /* Item to check against rectangle. */
1027 double *areaPtr; /* Pointer to array of four coordinates
1028 * (x1, y1, x2, y2) describing rectangular
1029 * area. */
1030 {
1031 RectOvalItem *rectPtr = (RectOvalItem *) itemPtr;
1032 double halfWidth;
1033 double width;
1034 Tk_State state = Tk_GetItemState(canvas, itemPtr);
1035
1036 width = rectPtr->outline.width;
1037 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1038 if (rectPtr->outline.activeWidth>width) {
1039 width = rectPtr->outline.activeWidth;
1040 }
1041 } else if (state==TK_STATE_DISABLED) {
1042 if (rectPtr->outline.disabledWidth>0) {
1043 width = rectPtr->outline.disabledWidth;
1044 }
1045 }
1046
1047 halfWidth = width/2.0;
1048 if (rectPtr->outline.gc == None) {
1049 halfWidth = 0.0;
1050 }
1051
1052 if ((areaPtr[2] <= (rectPtr->bbox[0] - halfWidth))
1053 || (areaPtr[0] >= (rectPtr->bbox[2] + halfWidth))
1054 || (areaPtr[3] <= (rectPtr->bbox[1] - halfWidth))
1055 || (areaPtr[1] >= (rectPtr->bbox[3] + halfWidth))) {
1056 return -1;
1057 }
1058 if ((rectPtr->fillGC == None) && (rectPtr->outline.gc != None)
1059 && (areaPtr[0] >= (rectPtr->bbox[0] + halfWidth))
1060 && (areaPtr[1] >= (rectPtr->bbox[1] + halfWidth))
1061 && (areaPtr[2] <= (rectPtr->bbox[2] - halfWidth))
1062 && (areaPtr[3] <= (rectPtr->bbox[3] - halfWidth))) {
1063 return -1;
1064 }
1065 if ((areaPtr[0] <= (rectPtr->bbox[0] - halfWidth))
1066 && (areaPtr[1] <= (rectPtr->bbox[1] - halfWidth))
1067 && (areaPtr[2] >= (rectPtr->bbox[2] + halfWidth))
1068 && (areaPtr[3] >= (rectPtr->bbox[3] + halfWidth))) {
1069 return 1;
1070 }
1071 return 0;
1072 }
1073
1074 /*
1075 *--------------------------------------------------------------
1076 *
1077 * OvalToArea --
1078 *
1079 * This procedure is called to determine whether an item
1080 * lies entirely inside, entirely outside, or overlapping
1081 * a given rectangular area.
1082 *
1083 * Results:
1084 * -1 is returned if the item is entirely outside the area
1085 * given by rectPtr, 0 if it overlaps, and 1 if it is entirely
1086 * inside the given area.
1087 *
1088 * Side effects:
1089 * None.
1090 *
1091 *--------------------------------------------------------------
1092 */
1093
1094 /* ARGSUSED */
1095 static int
1096 OvalToArea(canvas, itemPtr, areaPtr)
1097 Tk_Canvas canvas; /* Canvas containing item. */
1098 Tk_Item *itemPtr; /* Item to check against oval. */
1099 double *areaPtr; /* Pointer to array of four coordinates
1100 * (x1, y1, x2, y2) describing rectangular
1101 * area. */
1102 {
1103 RectOvalItem *ovalPtr = (RectOvalItem *) itemPtr;
1104 double oval[4], halfWidth;
1105 int result;
1106 double width;
1107 Tk_State state = Tk_GetItemState(canvas, itemPtr);
1108
1109 width = ovalPtr->outline.width;
1110 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1111 if (ovalPtr->outline.activeWidth>width) {
1112 width = ovalPtr->outline.activeWidth;
1113 }
1114 } else if (state==TK_STATE_DISABLED) {
1115 if (ovalPtr->outline.disabledWidth>0) {
1116 width = ovalPtr->outline.disabledWidth;
1117 }
1118 }
1119
1120 /*
1121 * Expand the oval to include the width of the outline, if any.
1122 */
1123
1124 halfWidth = width/2.0;
1125 if (ovalPtr->outline.gc == None) {
1126 halfWidth = 0.0;
1127 }
1128 oval[0] = ovalPtr->bbox[0] - halfWidth;
1129 oval[1] = ovalPtr->bbox[1] - halfWidth;
1130 oval[2] = ovalPtr->bbox[2] + halfWidth;
1131 oval[3] = ovalPtr->bbox[3] + halfWidth;
1132
1133 result = TkOvalToArea(oval, areaPtr);
1134
1135 /*
1136 * If the rectangle appears to overlap the oval and the oval
1137 * isn't filled, do one more check to see if perhaps all four
1138 * of the rectangle's corners are totally inside the oval's
1139 * unfilled center, in which case we should return "outside".
1140 */
1141
1142 if ((result == 0) && (ovalPtr->outline.gc != None)
1143 && (ovalPtr->fillGC == None)) {
1144 double centerX, centerY, height;
1145 double xDelta1, yDelta1, xDelta2, yDelta2;
1146
1147 centerX = (ovalPtr->bbox[0] + ovalPtr->bbox[2])/2.0;
1148 centerY = (ovalPtr->bbox[1] + ovalPtr->bbox[3])/2.0;
1149 width = (ovalPtr->bbox[2] - ovalPtr->bbox[0])/2.0 - halfWidth;
1150 height = (ovalPtr->bbox[3] - ovalPtr->bbox[1])/2.0 - halfWidth;
1151 xDelta1 = (areaPtr[0] - centerX)/width;
1152 xDelta1 *= xDelta1;
1153 yDelta1 = (areaPtr[1] - centerY)/height;
1154 yDelta1 *= yDelta1;
1155 xDelta2 = (areaPtr[2] - centerX)/width;
1156 xDelta2 *= xDelta2;
1157 yDelta2 = (areaPtr[3] - centerY)/height;
1158 yDelta2 *= yDelta2;
1159 if (((xDelta1 + yDelta1) < 1.0)
1160 && ((xDelta1 + yDelta2) < 1.0)
1161 && ((xDelta2 + yDelta1) < 1.0)
1162 && ((xDelta2 + yDelta2) < 1.0)) {
1163 return -1;
1164 }
1165 }
1166 return result;
1167 }
1168
1169 /*
1170 *--------------------------------------------------------------
1171 *
1172 * ScaleRectOval --
1173 *
1174 * This procedure is invoked to rescale a rectangle or oval
1175 * item.
1176 *
1177 * Results:
1178 * None.
1179 *
1180 * Side effects:
1181 * The rectangle or oval referred to by itemPtr is rescaled
1182 * so that the following transformation is applied to all
1183 * point coordinates:
1184 * x' = originX + scaleX*(x-originX)
1185 * y' = originY + scaleY*(y-originY)
1186 *
1187 *--------------------------------------------------------------
1188 */
1189
1190 static void
1191 ScaleRectOval(canvas, itemPtr, originX, originY, scaleX, scaleY)
1192 Tk_Canvas canvas; /* Canvas containing rectangle. */
1193 Tk_Item *itemPtr; /* Rectangle to be scaled. */
1194 double originX, originY; /* Origin about which to scale rect. */
1195 double scaleX; /* Amount to scale in X direction. */
1196 double scaleY; /* Amount to scale in Y direction. */
1197 {
1198 RectOvalItem *rectOvalPtr = (RectOvalItem *) itemPtr;
1199
1200 rectOvalPtr->bbox[0] = originX + scaleX*(rectOvalPtr->bbox[0] - originX);
1201 rectOvalPtr->bbox[1] = originY + scaleY*(rectOvalPtr->bbox[1] - originY);
1202 rectOvalPtr->bbox[2] = originX + scaleX*(rectOvalPtr->bbox[2] - originX);
1203 rectOvalPtr->bbox[3] = originY + scaleY*(rectOvalPtr->bbox[3] - originY);
1204 ComputeRectOvalBbox(canvas, rectOvalPtr);
1205 }
1206
1207 /*
1208 *--------------------------------------------------------------
1209 *
1210 * TranslateRectOval --
1211 *
1212 * This procedure is called to move a rectangle or oval by a
1213 * given amount.
1214 *
1215 * Results:
1216 * None.
1217 *
1218 * Side effects:
1219 * The position of the rectangle or oval is offset by
1220 * (xDelta, yDelta), and the bounding box is updated in the
1221 * generic part of the item structure.
1222 *
1223 *--------------------------------------------------------------
1224 */
1225
1226 static void
1227 TranslateRectOval(canvas, itemPtr, deltaX, deltaY)
1228 Tk_Canvas canvas; /* Canvas containing item. */
1229 Tk_Item *itemPtr; /* Item that is being moved. */
1230 double deltaX, deltaY; /* Amount by which item is to be
1231 * moved. */
1232 {
1233 RectOvalItem *rectOvalPtr = (RectOvalItem *) itemPtr;
1234
1235 rectOvalPtr->bbox[0] += deltaX;
1236 rectOvalPtr->bbox[1] += deltaY;
1237 rectOvalPtr->bbox[2] += deltaX;
1238 rectOvalPtr->bbox[3] += deltaY;
1239 ComputeRectOvalBbox(canvas, rectOvalPtr);
1240 }
1241
1242 /*
1243 *--------------------------------------------------------------
1244 *
1245 * RectOvalToPostscript --
1246 *
1247 * This procedure is called to generate Postscript for
1248 * rectangle and oval items.
1249 *
1250 * Results:
1251 * The return value is a standard Tcl result. If an error
1252 * occurs in generating Postscript then an error message is
1253 * left in the interp's result, replacing whatever used to be there.
1254 * If no error occurs, then Postscript for the rectangle is
1255 * appended to the result.
1256 *
1257 * Side effects:
1258 * None.
1259 *
1260 *--------------------------------------------------------------
1261 */
1262
1263 static int
1264 RectOvalToPostscript(interp, canvas, itemPtr, prepass)
1265 Tcl_Interp *interp; /* Interpreter for error reporting. */
1266 Tk_Canvas canvas; /* Information about overall canvas. */
1267 Tk_Item *itemPtr; /* Item for which Postscript is
1268 * wanted. */
1269 int prepass; /* 1 means this is a prepass to
1270 * collect font information; 0 means
1271 * final Postscript is being created. */
1272 {
1273 char pathCmd[500];
1274 RectOvalItem *rectOvalPtr = (RectOvalItem *) itemPtr;
1275 double y1, y2;
1276 XColor *color;
1277 XColor *fillColor;
1278 Pixmap fillStipple;
1279 Tk_State state = Tk_GetItemState(canvas, itemPtr);
1280 Tk_Tile tile;
1281
1282 y1 = Tk_CanvasPsY(canvas, rectOvalPtr->bbox[1]);
1283 y2 = Tk_CanvasPsY(canvas, rectOvalPtr->bbox[3]);
1284
1285 /*
1286 * Generate a string that creates a path for the rectangle or oval.
1287 * This is the only part of the procedure's code that is type-
1288 * specific.
1289 */
1290
1291
1292 if (rectOvalPtr->header.typePtr == &tkRectangleType) {
1293 sprintf(pathCmd, "%.15g %.15g moveto %.15g 0 rlineto 0 %.15g rlineto %.15g 0 rlineto closepath\n",
1294 rectOvalPtr->bbox[0], y1,
1295 rectOvalPtr->bbox[2]-rectOvalPtr->bbox[0], y2-y1,
1296 rectOvalPtr->bbox[0]-rectOvalPtr->bbox[2]);
1297 } else {
1298 sprintf(pathCmd, "matrix currentmatrix\n%.15g %.15g translate %.15g %.15g scale 1 0 moveto 0 0 1 0 360 arc\nsetmatrix\n",
1299 (rectOvalPtr->bbox[0] + rectOvalPtr->bbox[2])/2, (y1 + y2)/2,
1300 (rectOvalPtr->bbox[2] - rectOvalPtr->bbox[0])/2, (y1 - y2)/2);
1301 }
1302
1303 tile = rectOvalPtr->outline.tile;
1304 color = rectOvalPtr->outline.color;
1305 fillColor = rectOvalPtr->fillColor;
1306 fillStipple = rectOvalPtr->fillStipple;
1307 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1308 if (rectOvalPtr->outline.activeColor!=NULL) {
1309 color = rectOvalPtr->outline.activeColor;
1310 }
1311 if (rectOvalPtr->activeFillColor!=NULL) {
1312 fillColor = rectOvalPtr->activeFillColor;
1313 }
1314 if (rectOvalPtr->activeFillStipple!=None) {
1315 fillStipple = rectOvalPtr->activeFillStipple;
1316 }
1317 } else if (state==TK_STATE_DISABLED) {
1318 if (rectOvalPtr->outline.disabledColor!=NULL) {
1319 color = rectOvalPtr->outline.disabledColor;
1320 }
1321 if (rectOvalPtr->disabledFillColor!=NULL) {
1322 fillColor = rectOvalPtr->disabledFillColor;
1323 }
1324 if (rectOvalPtr->disabledFillStipple!=None) {
1325 fillStipple = rectOvalPtr->disabledFillStipple;
1326 }
1327 }
1328
1329 /*
1330 * First draw the filled area of the rectangle.
1331 */
1332
1333 if (fillColor != NULL) {
1334 Tcl_AppendResult(interp, pathCmd, (char *) NULL);
1335 if (Tk_CanvasPsColor(interp, canvas, fillColor)
1336 != TCL_OK) {
1337 return TCL_ERROR;
1338 }
1339 if (fillStipple != None) {
1340 Tcl_AppendResult(interp, "clip ", (char *) NULL);
1341 if (Tk_CanvasPsStipple(interp, canvas, fillStipple)
1342 != TCL_OK) {
1343 return TCL_ERROR;
1344 }
1345 if (color != NULL) {
1346 Tcl_AppendResult(interp, "grestore gsave\n", (char *) NULL);
1347 }
1348 } else {
1349 Tcl_AppendResult(interp, "fill\n", (char *) NULL);
1350 }
1351 }
1352
1353 /*
1354 * Now draw the outline, if there is one.
1355 */
1356
1357 if (color != NULL) {
1358 Tcl_AppendResult(interp, pathCmd, "0 setlinejoin 2 setlinecap\n",
1359 (char *) NULL);
1360 if (Tk_CanvasPsOutline(canvas, itemPtr,
1361 &(rectOvalPtr->outline))!= TCL_OK) {
1362 return TCL_ERROR;
1363 }
1364 }
1365 return TCL_OK;
1366 }
1367