Debian Codegraph @

pTk/ptkCanvGroup.c

raw symbols SCIP
1 /*
2 * ptkCanvGroup.c --
3 *
4 * This file implements grid items for canvas
5 * widgets.
6 *
7 * Copyright (c) 1991-1994 The Regents of the University of California.
8 * Copyright (c) 1994-1996 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: tkGroup.c,v 1.2 1998/09/14 18:23:16 stanton 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 GroupItem {
26 Tk_Item header; /* Generic stuff that's the same for all
27 * types. MUST BE FIRST IN STRUCTURE. */
28 double posn[2]; /* Nonminal position of the group */
29 Tcl_Interp *interp; /* For error reporting */
30 Tk_Canvas canvas; /* Needed to find items by id when configured */
31 int numMembers; /* Number of items in the group */
32 int numSlots; /* Space in the array */
33 Tk_Item **members; /* array of items */
34 } GroupItem;
35
36 /*
37 * Information used for parsing configuration specs:
38 */
39
40 static int MembersParseProc _ANSI_ARGS_((ClientData clientData,
41 Tcl_Interp *interp, Tk_Window tkwin,
42 Tcl_Obj * value, char *recordPtr, int offset));
43 static Tcl_Obj * MembersPrintProc _ANSI_ARGS_((ClientData clientData,
44 Tk_Window tkwin, char *recordPtr, int offset,
45 Tcl_FreeProc **freeProcPtr));
46
47
48 static Tk_CustomOption stateOption = {
49 TkStateParseProc,
50 TkStatePrintProc, (ClientData) 3
51 };
52 static Tk_CustomOption tagsOption = {
53 Tk_CanvasTagsParseProc,
54 Tk_CanvasTagsPrintProc, (ClientData) NULL
55 };
56
57 static Tk_CustomOption membersOption = {
58 MembersParseProc,
59 MembersPrintProc, (ClientData) NULL
60 };
61
62
63 static Tk_ConfigSpec configSpecs[] = {
64 {TK_CONFIG_CUSTOM, "-members", NULL, NULL,
65 "1.0", Tk_Offset(GroupItem, members),
66 TK_CONFIG_DONT_SET_DEFAULT, &membersOption},
67 {TK_CONFIG_CUSTOM, "-state", NULL, NULL,
68 NULL, Tk_Offset(Tk_Item, state),TK_CONFIG_NULL_OK,
69 &stateOption},
70 {TK_CONFIG_CUSTOM, "-tags", NULL, NULL,
71 NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
72 {TK_CONFIG_CALLBACK, "-updatecommand", NULL, NULL,
73 NULL, Tk_Offset(Tk_Item, updateCmd), TK_CONFIG_NULL_OK},
74 {TK_CONFIG_END, NULL, NULL, NULL,
75 NULL, 0, 0}
76 };
77
78 /*
79 * Prototypes for procedures defined in this file:
80 */
81
82 static void ComputeGroupBbox _ANSI_ARGS_((Tk_Canvas canvas,
83 GroupItem *groupPtr));
84 static int ConfigureGroup _ANSI_ARGS_((Tcl_Interp *interp,
85 Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
86 Tcl_Obj *CONST *args, int flags));
87 static int CreateGroup _ANSI_ARGS_((Tcl_Interp *interp,
88 Tk_Canvas canvas, struct Tk_Item *itemPtr,
89 int argc, Tcl_Obj *CONST *args));
90 static void DeleteGroup _ANSI_ARGS_((Tk_Canvas canvas,
91 Tk_Item *itemPtr, Display *display));
92 static void DisplayGroup _ANSI_ARGS_((Tk_Canvas canvas,
93 Tk_Item *itemPtr, Display *display, Drawable dst,
94 int x, int y, int width, int height));
95 static int GroupCoords _ANSI_ARGS_((Tcl_Interp *interp,
96 Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
97 Tcl_Obj *CONST *args));
98 static int GroupToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
99 Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
100 static int GroupToArea _ANSI_ARGS_((Tk_Canvas canvas,
101 Tk_Item *itemPtr, double *areaPtr));
102 static double GroupToPoint _ANSI_ARGS_((Tk_Canvas canvas,
103 Tk_Item *itemPtr, double *pointPtr));
104 static void ScaleGroup _ANSI_ARGS_((Tk_Canvas canvas,
105 Tk_Item *itemPtr, double originX, double originY,
106 double scaleX, double scaleY));
107 static void TranslateGroup _ANSI_ARGS_((Tk_Canvas canvas,
108 Tk_Item *itemPtr, double deltaX, double deltaY));
109 static int GroupIndex _ANSI_ARGS_((Tcl_Interp *interp,
110 Tk_Canvas canvas, Tk_Item *itemPtr, Tcl_Obj *indexString,
111 int *indexPtr));
112 static int GroupInsert _ANSI_ARGS_((Tk_Canvas canvas,
113 Tk_Item *itemPtr, int beforeThis, Tcl_Obj *string));
114 static void GroupInsertProc _ANSI_ARGS_((Tk_Canvas canvas,
115 Tk_Item *itemPtr, int beforeThis, Tcl_Obj *string));
116 static void GroupDChars _ANSI_ARGS_((Tk_Canvas canvas,
117 Tk_Item *itemPtr, int first, int last));
118
119 /*
120 * The structures below defines the rectangle and oval item types
121 * by means of procedures that can be invoked by generic item code.
122 */
123
124 Tk_ItemType ptkCanvGroupType = {
125 "group", /* name */
126 sizeof(GroupItem), /* itemSize */
127 CreateGroup, /* createProc */
128 configSpecs, /* configSpecs */
129 ConfigureGroup, /* configureProc */
130 GroupCoords, /* coordProc */
131 DeleteGroup, /* deleteProc */
132 DisplayGroup, /* displayProc */
133 TK_ITEM_ALWAYS_REDRAW|TK_CONFIG_OBJS,/* flags */
134 GroupToPoint, /* pointProc */
135 GroupToArea, /* areaProc */
136 GroupToPostscript, /* postscriptProc */
137 ScaleGroup, /* scaleProc */
138 TranslateGroup, /* translateProc */
139 GroupIndex, /* indexProc */
140 (Tk_ItemCursorProc *) NULL, /* icursorProc */ /* Abuse to set active? */
141 (Tk_ItemSelectionProc *) NULL, /* selectionProc */
142 GroupInsertProc, /* insertProc */
143 GroupDChars, /* dTextProc */
144 (Tk_ItemType *) NULL, /* nextPtr */
145 (Tk_ItemBboxProc *) ComputeGroupBbox,/* bboxProc */
146 Tk_Offset(Tk_VisitorType, visitGroup), /* acceptProc */
147 NULL, /* getCoordPtr */
148 NULL /* setCoordPtr */
149 };
150
151
152 static void
153 ShowMembers(char *f,GroupItem *groupPtr)
154 {
155 int i;
156 LangDebug("%s gid=%d %d [",f,groupPtr->header.id, groupPtr->numMembers);
157 if (groupPtr->numMembers > groupPtr->numSlots)
158 abort();
159 for (i=0; i < groupPtr->numMembers; i++)
160 {
161 if (groupPtr->members[i])
162 {
163 LangDebug(" %d",groupPtr->members[i]->id);
164 }
165 else
166 {
167 LangDebug(" NULL",groupPtr->members[i]->id);
168 }
169 }
170 LangDebug("]\n");
171 }
172
173
174
175 /*
176 *--------------------------------------------------------------
177 *
178 * CreateGroup --
179 *
180 * This procedure is invoked to create a new rectangle
181 * or oval item in a canvas.
182 *
183 * Results:
184 * A standard Tcl return value. If an error occurred in
185 * creating the item, then an error message is left in
186 * Tcl_GetResult(interp); in this case itemPtr is left uninitialized,
187 * so it can be safely freed by the caller.
188 *
189 * Side effects:
190 * A new rectangle or oval item is created.
191 *
192 *--------------------------------------------------------------
193 */
194
195 static int
196 CreateGroup(interp, canvas, itemPtr, argc, args)
197 Tcl_Interp *interp; /* For error reporting. */
198 Tk_Canvas canvas; /* Canvas to hold new item. */
199 Tk_Item *itemPtr; /* Record to hold new item; header
200 * has been initialized by caller. */
201 int argc; /* Number of arguments in args. */
202 Tcl_Obj *CONST *args; /* Arguments describing group. */
203 {
204 GroupItem *groupPtr = (GroupItem *) itemPtr;
205 int i;
206
207 if (argc==1) {
208 i = 1;
209 } else {
210 char *arg = Tcl_GetStringFromObj(args[1], NULL);
211 if ((argc>1) && (arg[0] == '-')
212 && (arg[1] >= 'a') && (arg[1] <= 'z')) {
213 i = 1;
214 } else {
215 i = 2;
216 }
217 }
218
219 if (argc < i) {
220 Tcl_AppendResult(interp, "wrong # args: should be \"",
221 Tk_PathName(Tk_CanvasTkwin(canvas)), " create ",
222 itemPtr->typePtr->name, " x1 y1 ?options?\"",
223 NULL);
224 return TCL_ERROR;
225 }
226
227 /*
228 * Carry out initialization that is needed in order to clean
229 * up after errors during the the remainder of this procedure.
230 */
231
232 groupPtr->canvas = canvas;
233 groupPtr->interp = interp;
234 groupPtr->members = NULL;
235 groupPtr->numSlots = 0;
236 groupPtr->numMembers = 0;
237
238 /*
239 * Process the arguments to fill in the item record.
240 */
241
242 if ((GroupCoords(interp, canvas, itemPtr, i, args) != TCL_OK)) {
243 goto error;
244 }
245 if (ConfigureGroup(interp, canvas, itemPtr, argc-i, args+i, 0)
246 == TCL_OK) {
247 return TCL_OK;
248 }
249
250 error:
251 DeleteGroup(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
252 return TCL_ERROR;
253 }
254
255 /*
256 *--------------------------------------------------------------
257 *
258 * GroupCoords --
259 *
260 * This procedure is invoked to process the "coords" widget
261 * command on rectangles and ovals. See the user documentation
262 * for details on what it does.
263 *
264 * Results:
265 * Returns TCL_OK or TCL_ERROR, and sets Tcl_GetResult(interp).
266 *
267 * Side effects:
268 * The coordinates for the given item may be changed.
269 *
270 *--------------------------------------------------------------
271 */
272
273 static int
274 GroupCoords(interp, canvas, itemPtr, argc, args)
275 Tcl_Interp *interp; /* Used for error reporting. */
276 Tk_Canvas canvas; /* Canvas containing item. */
277 Tk_Item *itemPtr; /* Item whose coordinates are to be
278 * read or modified. */
279 int argc; /* Number of coordinates supplied in
280 * args. */
281 Tcl_Obj *CONST *args; /* Array of coordinates: x1, y1,
282 * x2, y2, ... */
283 {
284 GroupItem *groupPtr = (GroupItem *) itemPtr;
285 char c0[TCL_DOUBLE_SPACE];
286
287 if (argc == 0) {
288 Tcl_Obj *obj = Tcl_NewObj();
289 Tcl_Obj *subobj = Tcl_NewDoubleObj(groupPtr->posn[0]);
290 Tcl_ListObjAppendElement(interp, obj, subobj);
291 subobj = Tcl_NewDoubleObj(groupPtr->posn[1]);
292 Tcl_ListObjAppendElement(interp, obj, subobj);
293 Tcl_SetObjResult(interp, obj);
294 } else if ((argc == 1)||(argc == 2)) {
295 double newX;
296 double newY;
297 if (argc==1) {
298 if (Tcl_ListObjGetElements(interp, args[0], &argc, (Tcl_Obj ***)&args) != TCL_OK) {
299 return TCL_ERROR;
300 } else if (argc != 2) {
301 sprintf(c0,"%d",argc);
302 Tcl_AppendResult(interp, "wrong # coordinates: expected 2, got ",
303 c0, NULL);
304 return TCL_ERROR;
305 }
306 }
307 if ((Tk_CanvasGetCoordFromObj(interp, canvas, args[0],
308 &newX) != TCL_OK)
309 || (Tk_CanvasGetCoordFromObj(interp, canvas, args[1],
310 &newY) != TCL_OK)) {
311 return TCL_ERROR;
312 }
313 TranslateGroup(canvas, itemPtr, newX - groupPtr->posn[0], newY - groupPtr->posn[1]);
314 } else {
315 sprintf(c0,"%d",argc);
316 Tcl_AppendResult(interp, "wrong # coordinates: expected 0 or 4, got ",
317 c0, NULL);
318 return TCL_ERROR;
319 }
320 return TCL_OK;
321 }
322
323 /*
324 *--------------------------------------------------------------
325 *
326 * ConfigureGroup --
327 *
328 * This procedure is invoked to configure various aspects
329 * of a rectangle or oval item, such as its border and
330 * background colors.
331 *
332 * Results:
333 * A standard Tcl result code. If an error occurs, then
334 * an error message is left in Tcl_GetResult(interp).
335 *
336 * Side effects:
337 * Configuration information, such as colors and stipple
338 * patterns, may be set for itemPtr.
339 *
340 *--------------------------------------------------------------
341 */
342
343 static int
344 ConfigureGroup(interp, canvas, itemPtr, argc, args, flags)
345 Tcl_Interp *interp; /* Used for error reporting. */
346 Tk_Canvas canvas; /* Canvas containing itemPtr. */
347 Tk_Item *itemPtr; /* Rectangle item to reconfigure. */
348 int argc; /* Number of elements in args. */
349 Tcl_Obj *CONST *args; /* Arguments describing things to configure. */
350 int flags; /* Flags to pass to Tk_ConfigureWidget. */
351 {
352 TkCanvas *canvasPtr = (TkCanvas *) canvas;
353 GroupItem *groupPtr = (GroupItem *) itemPtr;
354 Tk_Item *saveGroup = canvasPtr->activeGroup;
355 Tk_State state = Tk_GetItemState(canvas, itemPtr);
356 int i;
357 Tk_Window tkwin = Tk_CanvasTkwin(canvas);
358
359 if (Tk_ConfigureWidget(interp, tkwin, configSpecs, argc, args,
360 (char *) groupPtr, flags|TK_CONFIG_OBJS) != TCL_OK) {
361 return TCL_ERROR;
362 }
363
364 /*
365 * A few of the options require additional processing, such as
366 * graphics contexts.
367 */
368
369 itemPtr->redraw_flags &= ~TK_ITEM_STATE_DEPENDANT;
370
371 ComputeGroupBbox(canvas, groupPtr);
372
373 return TCL_OK;
374 }
375
376 /*
377 *--------------------------------------------------------------
378 *
379 * DeleteGroup --
380 *
381 * This procedure is called to clean up the data structure
382 * associated with a rectangle or oval item.
383 *
384 * Results:
385 * None.
386 *
387 * Side effects:
388 * Resources associated with itemPtr are released.
389 *
390 *--------------------------------------------------------------
391 */
392
393 static void
394 DeleteGroup(canvas, itemPtr, display)
395 Tk_Canvas canvas; /* Info about overall widget. */
396 Tk_Item *itemPtr; /* Item that is being deleted. */
397 Display *display; /* Display containing window for
398 * canvas. */
399 {
400 TkCanvas *canvasPtr = (TkCanvas *) canvas;
401 GroupItem *groupPtr = (GroupItem *) itemPtr;
402 Tk_Item *saveGroup = canvasPtr->activeGroup;
403 Tk_State state = Tk_GetItemState(canvas, itemPtr);
404 int i;
405
406 canvasPtr->activeGroup = itemPtr;
407 for (i=groupPtr->numMembers-1; i >= 0; i--) {
408 Tk_Item *subitemPtr = groupPtr->members[i];
409 TkGroupRemoveItem(subitemPtr);
410
411 #ifdef DELETE_GROUP_DELETES_MEMBERS
412 if (subitemPtr != NULL) {
413 (*subitemPtr->typePtr->deleteProc)(canvas, subitemPtr, display);
414 }
415 #endif
416 }
417 canvasPtr->activeGroup = saveGroup;
418 if (groupPtr->members) {
419 ckfree((char *) groupPtr->members);
420 }
421 }
422
423
424 void
425 TkGroupRemoveItem(itemPtr)
426 Tk_Item *itemPtr;
427 {
428 GroupItem *groupPtr = (GroupItem *) (itemPtr->group);
429 if (groupPtr != NULL) {
430 int i;
431 for (i=groupPtr->numMembers-1; i >= 0; i--) {
432 if (groupPtr->members[i] == itemPtr) {
433 int j;
434 for (j=i+1; j < groupPtr->numMembers; j++) {
435 groupPtr->members[j-1] = groupPtr->members[j];
436 }
437 itemPtr->redraw_flags |= FORCE_REDRAW;
438 groupPtr->numMembers--;
439 itemPtr->group = NULL;
440 return;
441 }
442 }
443 }
444 itemPtr->group = NULL;
445 LangDebug("Cannot find %d in %d\n",itemPtr->id, groupPtr->header.id);
446 }
447
448
449 /*
450 *--------------------------------------------------------------
451 *
452 * ComputeGroupBbox --
453 *
454 * This procedure is invoked to compute the bounding box of
455 * all the pixels that may be drawn as part of a rectangle
456 * or oval.
457 *
458 * Results:
459 * None.
460 *
461 * Side effects:
462 * The fields x1, y1, x2, and y2 are updated in the header
463 * for itemPtr.
464 *
465 *--------------------------------------------------------------
466 */
467
468 /* ARGSUSED */
469 static void
470 ComputeGroupBbox(canvas, groupPtr)
471 Tk_Canvas canvas; /* Canvas that contains item. */
472 GroupItem *groupPtr; /* Item whose bbox is to be
473 * recomputed. */
474 {
475 TkCanvas *canvasPtr = (TkCanvas *) canvas;
476 Tk_Item *saveGroup = canvasPtr->activeGroup;
477 Tk_State state = Tk_GetItemState(canvas, &groupPtr->header);
478 int seen = 0;
479 int i;
480
481 canvasPtr->activeGroup = &groupPtr->header;
482 for (i=0; i < groupPtr->numMembers; i++) {
483 Tk_Item *subitemPtr = groupPtr->members[i];
484 if (subitemPtr != NULL) {
485 if (Tk_GetItemState(canvas, subitemPtr) == TK_STATE_HIDDEN) {
486 continue;
487 }
488 if (seen++ == 0) {
489 groupPtr->header.x1 = subitemPtr->x1;
490 groupPtr->header.y1 = subitemPtr->y1;
491 groupPtr->header.x2 = subitemPtr->x2;
492 groupPtr->header.y2 = subitemPtr->y2;
493 } else {
494 if (subitemPtr->x1 < groupPtr->header.x1) {
495 groupPtr->header.x1 = subitemPtr->x1;
496 }
497 if (subitemPtr->y1 < groupPtr->header.y1) {
498 groupPtr->header.y1 = subitemPtr->y1;
499 }
500 if (subitemPtr->x2 > groupPtr->header.x2) {
501 groupPtr->header.x2 = subitemPtr->x2;
502 }
503 if (subitemPtr->y2 > groupPtr->header.y2) {
504 groupPtr->header.y2 = subitemPtr->y2;
505 }
506 }
507 }
508 }
509 canvasPtr->activeGroup = saveGroup;
510
511 /* If all items were hidden then have a "null" bbox */
512
513 if (seen == 0) {
514 groupPtr->header.x1 = groupPtr->posn[0];
515 groupPtr->header.y1 = groupPtr->posn[1];
516 groupPtr->header.x2 = groupPtr->header.x1;
517 groupPtr->header.y2 = groupPtr->header.y1;
518 }
519 }
520
521 /*
522 *--------------------------------------------------------------
523 *
524 * DisplayGroup --
525 *
526 * This procedure is invoked to draw a rectangle or oval
527 * item in a given drawable.
528 *
529 * Results:
530 * None.
531 *
532 * Side effects:
533 * ItemPtr is drawn in drawable using the transformation
534 * information in canvas.
535 *
536 *--------------------------------------------------------------
537 */
538
539 static void
540 DisplayGroup(canvas, itemPtr, display, drawable, x, y, width, height)
541 Tk_Canvas canvas; /* Canvas that contains item. */
542 Tk_Item *itemPtr; /* Item to be displayed. */
543 Display *display; /* Display on which to draw item. */
544 Drawable drawable; /* Pixmap or window in which to draw
545 * item. */
546 int x, y, width, height; /* Describes region of canvas that
547 * must be redisplayed (not used). */
548 {
549 TkCanvas *canvasPtr = (TkCanvas *) canvas;
550 GroupItem *groupPtr = (GroupItem *) itemPtr;
551 Tk_Item *saveGroup = canvasPtr->activeGroup;
552 Tk_State state = Tk_GetItemState(canvas, itemPtr);
553 int i;
554 if (state == TK_STATE_HIDDEN) {
555 return;
556 }
557 canvasPtr->activeGroup = itemPtr;
558 for (i=0; i < groupPtr->numMembers; i++) {
559 Tk_Item *subitemPtr = groupPtr->members[i];
560 if (subitemPtr != NULL) {
561 if (Tk_GetItemState(canvas, subitemPtr) == TK_STATE_HIDDEN) {
562 continue;
563 }
564 if (drawable != None ||
565 (subitemPtr->typePtr->alwaysRedraw & 1)) {
566 if (subitemPtr->updateCmd) {
567 if (canvasPtr->updateCmds == NULL) {
568 canvasPtr->updateCmds = Tcl_NewListObj(0,NULL);
569 }
570 Tcl_IncrRefCount(subitemPtr->updateCmd);
571 Tcl_ListObjAppendElement(canvasPtr->interp,canvasPtr->updateCmds,
572 subitemPtr->updateCmd);
573 }
574 (*subitemPtr->typePtr->displayProc)(canvas, subitemPtr, display,
575 drawable, x, y, width, height);
576 }
577 }
578 }
579 canvasPtr->activeGroup = saveGroup;
580 }
581
582 /*
583 *--------------------------------------------------------------
584 *
585 * GroupToPoint --
586 *
587 * Computes the distance from a given point to a given
588 * group, in canvas units.
589 *
590 * Results:
591 * The return value is 0 if the point whose x and y coordinates
592 * are coordPtr[0] and coordPtr[1] is inside the group. If the
593 * point isn't inside the rectangle then the return value is the
594 * distance from the point to the group.
595 *
596 * Side effects:
597 * None.
598 *
599 *--------------------------------------------------------------
600 */
601
602 /* ARGSUSED */
603 static double
604 GroupToPoint(canvas, itemPtr, pointPtr)
605 Tk_Canvas canvas; /* Canvas containing item. */
606 Tk_Item *itemPtr; /* Item to check against point. */
607 double *pointPtr; /* Pointer to x and y coordinates. */
608 {
609 TkCanvas *canvasPtr = (TkCanvas *) canvas;
610 GroupItem *groupPtr = (GroupItem *) itemPtr;
611 Tk_Item *saveGroup = canvasPtr->activeGroup;
612 Tk_State state = Tk_GetItemState(canvas, itemPtr);
613 int i;
614
615 double best = 1.0e36;
616
617 if (state == TK_STATE_HIDDEN) {
618 return best;
619 }
620
621 /* If the group is active it is invisible to picking */
622 if (state == TK_STATE_ACTIVE) {
623 return best;
624 }
625
626 canvasPtr->activeGroup = itemPtr;
627 for (i=0; i < groupPtr->numMembers; i++) {
628 Tk_Item *subitemPtr = groupPtr->members[i];
629 if (subitemPtr != NULL) {
630 double try = (*subitemPtr->typePtr->pointProc)(canvas, subitemPtr, pointPtr);
631 if (try < best) {
632 best = try;
633 if (best == 0.0) {
634 break;
635 }
636 }
637 }
638 }
639 canvasPtr->activeGroup = saveGroup;
640 return best;
641 }
642
643
644 /*
645 *--------------------------------------------------------------
646 *
647 * GroupToArea --
648 *
649 * This procedure is called to determine whether an item
650 * lies entirely inside, entirely outside, or overlapping
651 * a given rectangle.
652 *
653 * Results:
654 * -1 is returned if the item is entirely outside the area
655 * given by rectPtr, 0 if it overlaps, and 1 if it is entirely
656 * inside the given area.
657 *
658 * Side effects:
659 * None.
660 *
661 *--------------------------------------------------------------
662 */
663
664 /* ARGSUSED */
665 static int
666 GroupToArea(canvas, itemPtr, areaPtr)
667 Tk_Canvas canvas; /* Canvas containing item. */
668 Tk_Item *itemPtr; /* Item to check against rectangle. */
669 double *areaPtr; /* Pointer to array of four coordinates
670 * (x1, y1, x2, y2) describing rectangular
671 * area. */
672 {
673 TkCanvas *canvasPtr = (TkCanvas *) canvas;
674 GroupItem *groupPtr = (GroupItem *) itemPtr;
675 Tk_Item *saveGroup = canvasPtr->activeGroup;
676 Tk_State state = Tk_GetItemState(canvas, itemPtr);
677 int i;
678 #define ALL_OUTSIDE 1
679 #define ALL_INSIDE 2
680 int seen = ALL_INSIDE|ALL_OUTSIDE;
681
682 if (state == TK_STATE_HIDDEN) {
683 return -1;
684 }
685
686 /* If the group is active it is invisible to picking */
687 if (state == TK_STATE_ACTIVE) {
688 return -1;
689 }
690
691 canvasPtr->activeGroup = itemPtr;
692 for (i=0; i < groupPtr->numMembers; i++) {
693 Tk_Item *subitemPtr = groupPtr->members[i];
694 if (subitemPtr != NULL) {
695 int inner = (*subitemPtr->typePtr->areaProc)(canvas, subitemPtr, areaPtr);
696 if (inner < 0) /* outside */
697 seen &= ~ALL_INSIDE; /* clear the inside option */
698 if (inner == 0) /* overlap */
699 seen = 0;
700 if (inner > 0) /* inside */
701 seen &= ~ALL_OUTSIDE; /* clear the outside option */
702 if (seen == 0)
703 break;
704 }
705 }
706 canvasPtr->activeGroup = saveGroup;
707
708 switch (seen) {
709 case 0 :
710 return 0;
711 case ALL_INSIDE :
712 return 1;
713 default:
714 case ALL_OUTSIDE :
715 return -1;
716 }
717 }
718
719
720 /*
721 *--------------------------------------------------------------
722 *
723 * ScaleGroup --
724 *
725 * This procedure is invoked to rescale a rectangle or oval
726 * item.
727 *
728 * Results:
729 * None.
730 *
731 * Side effects:
732 * The rectangle or oval referred to by itemPtr is rescaled
733 * so that the following transformation is applied to all
734 * point coordinates:
735 * x' = originX + scaleX*(x-originX)
736 * y' = originY + scaleY*(y-originY)
737 *
738 *--------------------------------------------------------------
739 */
740
741 static void
742 ScaleGroup(canvas, itemPtr, originX, originY, scaleX, scaleY)
743 Tk_Canvas canvas; /* Canvas containing rectangle. */
744 Tk_Item *itemPtr; /* Rectangle to be scaled. */
745 double originX, originY; /* Origin about which to scale rect. */
746 double scaleX; /* Amount to scale in X direction. */
747 double scaleY; /* Amount to scale in Y direction. */
748 {
749 TkCanvas *canvasPtr = (TkCanvas *) canvas;
750 GroupItem *groupPtr = (GroupItem *) itemPtr;
751 Tk_Item *saveGroup = canvasPtr->activeGroup;
752 Tk_State state = Tk_GetItemState(canvas, itemPtr);
753 int i;
754
755 groupPtr->posn[0] = originX + scaleX*(groupPtr->posn[0] - originX);
756 groupPtr->posn[1] = originY + scaleY*(groupPtr->posn[1] - originY);
757
758 canvasPtr->activeGroup = itemPtr;
759 for (i=0; i < groupPtr->numMembers; i++) {
760 Tk_Item *subitemPtr = groupPtr->members[i];
761 if (subitemPtr != NULL) {
762 (*subitemPtr->typePtr->scaleProc)(canvas, subitemPtr, originX, originY, scaleX, scaleY);
763 }
764 }
765 canvasPtr->activeGroup = saveGroup;
766
767 ComputeGroupBbox(canvas, groupPtr);
768 }
769
770 /*
771 *--------------------------------------------------------------
772 *
773 * TranslateGroup --
774 *
775 * This procedure is called to move a rectangle or oval by a
776 * given amount.
777 *
778 * Results:
779 * None.
780 *
781 * Side effects:
782 * The position of the rectangle or oval is offset by
783 * (xDelta, yDelta), and the bounding box is updated in the
784 * generic part of the item structure.
785 *
786 *--------------------------------------------------------------
787 */
788
789 static void
790 TranslateGroup(canvas, itemPtr, deltaX, deltaY)
791 Tk_Canvas canvas; /* Canvas containing item. */
792 Tk_Item *itemPtr; /* Item that is being moved. */
793 double deltaX, deltaY; /* Amount by which item is to be
794 * moved. */
795 {
796 TkCanvas *canvasPtr = (TkCanvas *) canvas;
797 GroupItem *groupPtr = (GroupItem *) itemPtr;
798 Tk_Item *saveGroup = canvasPtr->activeGroup;
799 int i;
800 groupPtr->posn[0] += deltaX;
801 groupPtr->posn[1] += deltaY;
802 canvasPtr->activeGroup = itemPtr;
803 for (i=0; i < groupPtr->numMembers; i++) {
804 Tk_Item *subitemPtr = groupPtr->members[i];
805 if (subitemPtr != NULL) {
806 (*subitemPtr->typePtr->translateProc)(canvas, subitemPtr, deltaX, deltaY);
807 }
808 }
809 canvasPtr->activeGroup = saveGroup;
810 ComputeGroupBbox(canvas, groupPtr);
811 }
812
813 /*
814 *--------------------------------------------------------------
815 *
816 * GroupToPostscript --
817 *
818 * This procedure is called to generate Postscript for
819 * rectangle and oval items.
820 *
821 * Results:
822 * The return value is a standard Tcl result. If an error
823 * occurs in generating Postscript then an error message is
824 * left in Tcl_GetResult(interp), replacing whatever used to be there.
825 * If no error occurs, then Postscript for the rectangle is
826 * appended to the result.
827 *
828 * Side effects:
829 * None.
830 *
831 *--------------------------------------------------------------
832 */
833
834 static int
835 GroupToPostscript(interp, canvas, itemPtr, prepass)
836 Tcl_Interp *interp; /* Interpreter for error reporting. */
837 Tk_Canvas canvas; /* Information about overall canvas. */
838 Tk_Item *itemPtr; /* Item for which Postscript is
839 * wanted. */
840 int prepass; /* 1 means this is a prepass to
841 * collect font information; 0 means
842 * final Postscript is being created. */
843 {
844 TkCanvas *canvasPtr = (TkCanvas *) canvas;
845 GroupItem *groupPtr = (GroupItem *) itemPtr;
846 Tk_Item *saveGroup = canvasPtr->activeGroup;
847 Tk_State state = Tk_GetItemState(canvas, itemPtr);
848 int code = TCL_OK;
849 int i;
850
851 if (state == TK_STATE_HIDDEN) {
852 return code;
853 }
854
855 canvasPtr->activeGroup = itemPtr;
856 for (i=0; i < groupPtr->numMembers; i++) {
857 Tk_Item *subitemPtr = groupPtr->members[i];
858 if (subitemPtr != NULL) {
859 if (Tk_GetItemState(canvas, subitemPtr) == TK_STATE_HIDDEN) {
860 continue;
861 }
862 code = (*subitemPtr->typePtr->postscriptProc)(interp, canvas,
863 subitemPtr, prepass);
864 if (code != TCL_OK) {
865 break;
866 }
867 }
868 }
869 canvasPtr->activeGroup = saveGroup;
870 return code;
871 }
872
873 static int
874 GroupIndex(interp, canvas, itemPtr, obj, indexPtr)
875 Tcl_Interp *interp;
876 Tk_Canvas canvas;
877 Tk_Item *itemPtr;
878 Tcl_Obj *obj;
879 int *indexPtr;
880 {
881 TkCanvas *canvasPtr = (TkCanvas *) canvas;
882 GroupItem *groupPtr = (GroupItem *) itemPtr;
883 Tk_Item *saveGroup = canvasPtr->activeGroup;
884 Tk_State state = Tk_GetItemState(canvas, itemPtr);
885 int i;
886 int length;
887 int id;
888 char *string;
889 double point[2];
890 double bestDist;
891 char *end, *p;
892 Tcl_Obj **objv;
893
894 *indexPtr = -1;
895
896 if (Tcl_ListObjGetElements(interp, obj, &i, &objv) == TCL_OK && i == 2
897 && Tk_CanvasGetCoordFromObj(interp, canvas, objv[0], &point[0]) == TCL_OK
898 && Tk_CanvasGetCoordFromObj(interp, canvas, objv[1], &point[1]) == TCL_OK) {
899 goto doxy;
900 }
901
902 string = Tcl_GetStringFromObj(obj, &length);
903 if (string[0] == 'e') {
904 if (strncmp(string, "end", length) == 0) {
905 *indexPtr = groupPtr->numMembers;
906 } else {
907 badIndex:
908
909 /*
910 * Some of the paths here leave messages in interp->result,
911 * so we have to clear it out before storing our own message.
912 */
913
914 Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
915 Tcl_AppendResult(interp, "bad index \"", string, "\"",
916 (char *) NULL);
917 return TCL_ERROR;
918 }
919 } else if (string[0] == '@') {
920 p = string+1;
921 point[0] = strtod(p, &end);
922 if ((end == p) || (*end != ',')) {
923 goto badIndex;
924 }
925 p = end+1;
926 point[1] = strtod(p, &end);
927 if ((end == p) || (*end != 0)) {
928 goto badIndex;
929 }
930 doxy:
931 bestDist = 1.0e36;
932 *indexPtr = 0;
933 canvasPtr->activeGroup = itemPtr;
934 for(i=0; i < groupPtr->numMembers; i++) {
935 Tk_Item *subitemPtr = groupPtr->members[i];
936 double dist = (*subitemPtr->typePtr->pointProc)(canvas, subitemPtr, point);
937 if (dist < bestDist) {
938 bestDist = dist;
939 *indexPtr = i;
940 }
941 }
942 canvasPtr->activeGroup = saveGroup;
943 } else {
944 if (Tcl_GetIntFromObj(interp, obj, &id) == TCL_OK) {
945 for(i=0; i < groupPtr->numMembers; i++) {
946 Tk_Item *subitemPtr = groupPtr->members[i];
947 if (subitemPtr != NULL && subitemPtr->id == id) {
948 *indexPtr = i;
949 return TCL_OK;
950 }
951 }
952 goto badIndex;
953 } else {
954 return TCL_ERROR;
955 }
956 }
957 return TCL_OK;
958 }
959
960 static int
961 GroupInsert(canvas, itemPtr, beforeThis, string)
962 Tk_Canvas canvas;
963 Tk_Item *itemPtr;
964 int beforeThis;
965 Tcl_Obj *string;
966 {
967 TkCanvas *canvasPtr = (TkCanvas *) canvas;
968 GroupItem *groupPtr = (GroupItem *) itemPtr;
969 Tk_Item *saveGroup = canvasPtr->activeGroup;
970 Tk_State state = Tk_GetItemState(canvas, itemPtr);
971 Tcl_Obj **objv;
972 int argc;
973 int i;
974 int id;
975 if (Tcl_ListObjGetElements(groupPtr->interp,string,&argc,&objv) == TCL_OK) {
976 int count = 0;
977 for (i=0; i < argc; i++) {
978 if (Tcl_GetIntFromObj(groupPtr->interp,objv[i],&id) == TCL_OK) {
979 Tcl_HashEntry *entryPtr = Tcl_FindHashEntry(&canvasPtr->idTable, (char *) id);
980 if (entryPtr != NULL) {
981 Tk_Item *subitemPtr = (Tk_Item *) Tcl_GetHashValue(entryPtr);
982 if (subitemPtr == NULL
983 || subitemPtr == itemPtr
984 || subitemPtr->group == itemPtr) {
985 continue;
986 }
987 if (subitemPtr->group != NULL) {
988 TkGroupRemoveItem(subitemPtr);
989 }
990 count++;
991 }
992 } else {
993 return TCL_ERROR;
994 }
995 }
996 i = count + groupPtr->numMembers;
997 if (i > groupPtr->numSlots) {
998 if (groupPtr->members == NULL) {
999 groupPtr->members = (Tk_Item **)ckalloc(i*sizeof(Tk_Item *));
1000 } else {
1001 groupPtr->members = (Tk_Item **)ckrealloc((char *)groupPtr->members,
1002 i*sizeof(Tk_Item *));
1003 }
1004 if (groupPtr->members != NULL) {
1005 groupPtr->numSlots = i;
1006 } else {
1007 groupPtr->numMembers = 0;
1008 groupPtr->numSlots = 0;
1009 Tcl_SetResult(groupPtr->interp,"Out of memory",TCL_STATIC);
1010 return TCL_ERROR;
1011 }
1012 }
1013 /* Move tail up */
1014 for (i=groupPtr->numMembers-1; i >= beforeThis; i--) {
1015 groupPtr->members[i+count] = groupPtr->members[i];
1016 }
1017 /* Fill in slots */
1018 groupPtr->numMembers += count;
1019 for (i=0; i < argc; i++) {
1020 groupPtr->members[beforeThis] = NULL;
1021 if (Tcl_GetIntFromObj(groupPtr->interp,objv[i],&id) == TCL_OK) {
1022 Tcl_HashEntry *entryPtr = Tcl_FindHashEntry(&canvasPtr->idTable, (char *) id);
1023 if (entryPtr != NULL) {
1024 Tk_Item *subitemPtr = (Tk_Item *) Tcl_GetHashValue(entryPtr);
1025 if (subitemPtr == NULL
1026 || subitemPtr == itemPtr
1027 || subitemPtr->group == itemPtr) {
1028 continue;
1029 }
1030 subitemPtr->group = itemPtr;
1031 subitemPtr->redraw_flags |= FORCE_REDRAW;
1032 groupPtr->members[beforeThis] = subitemPtr;
1033 beforeThis++;
1034 count--;
1035 }
1036 }
1037 }
1038 if (count != 0) {
1039 abort();
1040 }
1041 ComputeGroupBbox(groupPtr->canvas, groupPtr);
1042 return TCL_OK;
1043 } else {
1044 return TCL_ERROR;
1045 }
1046 }
1047
1048 /* Just like above but with void return to go in the function table */
1049 static void
1050 GroupInsertProc(canvas, itemPtr, beforeThis, string)
1051 Tk_Canvas canvas;
1052 Tk_Item *itemPtr;
1053 int beforeThis;
1054 Tcl_Obj *string;
1055 {
1056 GroupInsert(canvas, itemPtr, beforeThis, string);
1057 }
1058
1059
1060 static void
1061 GroupDChars(canvas, itemPtr, first, last)
1062 Tk_Canvas canvas;
1063 Tk_Item *itemPtr;
1064 int first;
1065 int last;
1066 {
1067 TkCanvas *canvasPtr = (TkCanvas *) canvas;
1068 GroupItem *groupPtr = (GroupItem *) itemPtr;
1069 Tk_Item *saveGroup = canvasPtr->activeGroup;
1070 Tk_State state = Tk_GetItemState(canvas, itemPtr);
1071 int i;
1072
1073 if (first < 0) {
1074 first = 0;
1075 }
1076 if (last >= groupPtr->numMembers) {
1077 last = groupPtr->numMembers-1;
1078 }
1079 if (first > last) {
1080 return;
1081 }
1082 for (i=last; i >= first; i--) {
1083 TkGroupRemoveItem(groupPtr->members[i]);
1084 }
1085 ComputeGroupBbox(groupPtr->canvas, groupPtr);
1086 }
1087
1088 static int
1089 MembersParseProc(clientData,interp,tkwin,value,recordPtr,offset)
1090 ClientData clientData;
1091 Tcl_Interp *interp;
1092 Tk_Window tkwin;
1093 Tcl_Obj * value;
1094 char *recordPtr;
1095 int offset;
1096 {
1097 Tk_Item *itemPtr = (Tk_Item *) recordPtr;
1098 GroupItem *groupPtr = (GroupItem *) itemPtr;
1099 int code = TCL_OK;
1100 Tk_CanvasEventuallyRedraw(groupPtr->canvas, itemPtr->x1, itemPtr->y1, itemPtr->x2, itemPtr->y2);
1101 GroupDChars(groupPtr->canvas, itemPtr, 0, groupPtr->numMembers-1);
1102 code = GroupInsert(groupPtr->canvas, itemPtr, 0, value);
1103 Tk_CanvasEventuallyRedraw(groupPtr->canvas, itemPtr->x1, itemPtr->y1, itemPtr->x2, itemPtr->y2);
1104 return code;
1105 }
1106
1107 static Tcl_Obj *
1108 MembersPrintProc(clientData,tkwin,recordPtr,offset,freeProcPtr)
1109 ClientData clientData;
1110 Tk_Window tkwin;
1111 char *recordPtr;
1112 int offset;
1113 Tcl_FreeProc **freeProcPtr;
1114 {
1115 GroupItem *groupPtr = (GroupItem *) recordPtr;
1116 Tcl_Obj *result = Tcl_NewListObj(0,NULL);
1117 int i;
1118 for (i=0; i < groupPtr->numMembers; i++) {
1119 Tk_Item *subitemPtr = groupPtr->members[i];
1120 if (subitemPtr != NULL) {
1121 Tcl_ListObjAppendElement(groupPtr->interp,result,
1122 Tcl_NewIntObj(subitemPtr->id));
1123 }
1124 }
1125 return result;
1126 }
1127
1128