OSDN Git Service

Merge branch 'master' of git://github.com/monaka/binutils
[pf3gnuchains/pf3gnuchains3x.git] / tk / generic / tkCanvWind.c
1 /* 
2  * tkCanvWind.c --
3  *
4  *      This file implements window 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$
13  */
14
15 #include <stdio.h>
16 #include "tkInt.h"
17 #include "tkPort.h"
18 #include "tkCanvas.h"
19
20 /*
21  * The structure below defines the record for each window item.
22  */
23
24 typedef struct WindowItem  {
25     Tk_Item header;             /* Generic stuff that's the same for all
26                                  * types.  MUST BE FIRST IN STRUCTURE. */
27     double x, y;                /* Coordinates of positioning point for
28                                  * window. */
29     Tk_Window tkwin;            /* Window associated with item.  NULL means
30                                  * window has been destroyed. */
31     int width;                  /* Width to use for window (<= 0 means use
32                                  * window's requested width). */
33     int height;                 /* Width to use for window (<= 0 means use
34                                  * window's requested width). */
35     Tk_Anchor anchor;           /* Where to anchor window relative to
36                                  * (x,y). */
37     Tk_Canvas canvas;           /* Canvas containing this item. */
38 } WindowItem;
39
40 /*
41  * Information used for parsing configuration specs:
42  */
43
44 static Tk_CustomOption stateOption = {
45     (Tk_OptionParseProc *) TkStateParseProc,
46     TkStatePrintProc, (ClientData) 2
47 };
48 static Tk_CustomOption tagsOption = {
49     (Tk_OptionParseProc *) Tk_CanvasTagsParseProc,
50     Tk_CanvasTagsPrintProc, (ClientData) NULL
51 };
52
53 static Tk_ConfigSpec configSpecs[] = {
54     {TK_CONFIG_ANCHOR, "-anchor", (char *) NULL, (char *) NULL,
55         "center", Tk_Offset(WindowItem, anchor), TK_CONFIG_DONT_SET_DEFAULT},
56     {TK_CONFIG_PIXELS, "-height", (char *) NULL, (char *) NULL,
57         "0", Tk_Offset(WindowItem, height), TK_CONFIG_DONT_SET_DEFAULT},
58     {TK_CONFIG_CUSTOM, "-state", (char *) NULL, (char *) NULL,
59         (char *) NULL, Tk_Offset(Tk_Item, state), TK_CONFIG_NULL_OK,
60         &stateOption},
61     {TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
62         (char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
63     {TK_CONFIG_PIXELS, "-width", (char *) NULL, (char *) NULL,
64         "0", Tk_Offset(WindowItem, width), TK_CONFIG_DONT_SET_DEFAULT},
65     {TK_CONFIG_WINDOW, "-window", (char *) NULL, (char *) NULL,
66         (char *) NULL, Tk_Offset(WindowItem, tkwin), TK_CONFIG_NULL_OK},
67     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
68         (char *) NULL, 0, 0}
69 };
70
71 /*
72  * Prototypes for procedures defined in this file:
73  */
74
75 static void             ComputeWindowBbox _ANSI_ARGS_((Tk_Canvas canvas,
76                             WindowItem *winItemPtr));
77 static int              ConfigureWinItem _ANSI_ARGS_((Tcl_Interp *interp,
78                             Tk_Canvas canvas, Tk_Item *itemPtr, int objc,
79                             Tcl_Obj *CONST objv[], int flags));
80 static int              CreateWinItem _ANSI_ARGS_((Tcl_Interp *interp,
81                             Tk_Canvas canvas, struct Tk_Item *itemPtr,
82                             int objc, Tcl_Obj *CONST objv[]));
83 static void             DeleteWinItem _ANSI_ARGS_((Tk_Canvas canvas,
84                             Tk_Item *itemPtr, Display *display));
85 static void             DisplayWinItem _ANSI_ARGS_((Tk_Canvas canvas,
86                             Tk_Item *itemPtr, Display *display, Drawable dst,
87                             int x, int y, int width, int height));
88 static void             ScaleWinItem _ANSI_ARGS_((Tk_Canvas canvas,
89                             Tk_Item *itemPtr, double originX, double originY,
90                             double scaleX, double scaleY));
91 static void             TranslateWinItem _ANSI_ARGS_((Tk_Canvas canvas,
92                             Tk_Item *itemPtr, double deltaX, double deltaY));
93 static int              WinItemCoords _ANSI_ARGS_((Tcl_Interp *interp,
94                             Tk_Canvas canvas, Tk_Item *itemPtr, int objc,
95                             Tcl_Obj *CONST objv[]));
96 static void             WinItemLostSlaveProc _ANSI_ARGS_((
97                             ClientData clientData, Tk_Window tkwin));
98 static void             WinItemRequestProc _ANSI_ARGS_((ClientData clientData,
99                             Tk_Window tkwin));
100 static void             WinItemStructureProc _ANSI_ARGS_((
101                             ClientData clientData, XEvent *eventPtr));
102 static int              WinItemToArea _ANSI_ARGS_((Tk_Canvas canvas,
103                             Tk_Item *itemPtr, double *rectPtr));
104 static int              WinItemToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
105                             Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
106 static double           WinItemToPoint _ANSI_ARGS_((Tk_Canvas canvas,
107                             Tk_Item *itemPtr, double *pointPtr));
108 #ifdef X_GetImage
109 static int              xerrorhandler _ANSI_ARGS_((ClientData clientData,
110                             XErrorEvent *e));
111 #endif
112 static int              CanvasPsWindow _ANSI_ARGS_((Tcl_Interp *interp,
113                             Tk_Window tkwin, Tk_Canvas canvas, double x,
114                             double y, int width, int height));
115
116 /*
117  * The structure below defines the window item type by means of procedures
118  * that can be invoked by generic item code.
119  */
120
121 Tk_ItemType tkWindowType = {
122     "window",                           /* name */
123     sizeof(WindowItem),                 /* itemSize */
124     CreateWinItem,                      /* createProc */
125     configSpecs,                        /* configSpecs */
126     ConfigureWinItem,                   /* configureProc */
127     WinItemCoords,                      /* coordProc */
128     DeleteWinItem,                      /* deleteProc */
129     DisplayWinItem,                     /* displayProc */
130     1|TK_CONFIG_OBJS,                   /* flags */
131     WinItemToPoint,                     /* pointProc */
132     WinItemToArea,                      /* areaProc */
133     WinItemToPostscript,                /* postscriptProc */
134     ScaleWinItem,                       /* scaleProc */
135     TranslateWinItem,                   /* translateProc */
136     (Tk_ItemIndexProc *) NULL,          /* indexProc */
137     (Tk_ItemCursorProc *) NULL,         /* cursorProc */
138     (Tk_ItemSelectionProc *) NULL,      /* selectionProc */
139     (Tk_ItemInsertProc *) NULL,         /* insertProc */
140     (Tk_ItemDCharsProc *) NULL,         /* dTextProc */
141     (Tk_ItemType *) NULL,               /* nextPtr */
142 };
143
144
145 /*
146  * The structure below defines the official type record for the
147  * placer:
148  */
149
150 static Tk_GeomMgr canvasGeomType = {
151     "canvas",                           /* name */
152     WinItemRequestProc,                 /* requestProc */
153     WinItemLostSlaveProc,               /* lostSlaveProc */
154 };
155 \f
156 /*
157  *--------------------------------------------------------------
158  *
159  * CreateWinItem --
160  *
161  *      This procedure is invoked to create a new window
162  *      item in a canvas.
163  *
164  * Results:
165  *      A standard Tcl return value.  If an error occurred in
166  *      creating the item, then an error message is left in
167  *      the interp's result;  in this case itemPtr is
168  *      left uninitialized, so it can be safely freed by the
169  *      caller.
170  *
171  * Side effects:
172  *      A new window item is created.
173  *
174  *--------------------------------------------------------------
175  */
176
177 static int
178 CreateWinItem(interp, canvas, itemPtr, objc, objv)
179     Tcl_Interp *interp;                 /* Interpreter for error reporting. */
180     Tk_Canvas canvas;                   /* Canvas to hold new item. */
181     Tk_Item *itemPtr;                   /* Record to hold new item;  header
182                                          * has been initialized by caller. */
183     int objc;                           /* Number of arguments in objv. */
184     Tcl_Obj *CONST objv[];              /* Arguments describing window. */
185 {
186     WindowItem *winItemPtr = (WindowItem *) itemPtr;
187     int i = 2;
188
189     if (objc == 1) {
190         i = 1;
191     } else if (objc > 1) {
192         char *arg = Tcl_GetString(objv[1]);
193         if ((arg[0] == '-') && (arg[1] >= 'a') && (arg[1] <= 'z')) {
194             i = 1;
195         }
196     }
197
198     if (objc < i) {
199         Tcl_AppendResult(interp, "wrong # args: should be \"",
200                 Tk_PathName(Tk_CanvasTkwin(canvas)), " create ",
201                 itemPtr->typePtr->name, " x y ?options?\"",
202                 (char *) NULL);
203         return TCL_ERROR;
204     }
205
206     /*
207      * Initialize item's record.
208      */
209
210     winItemPtr->tkwin = NULL;
211     winItemPtr->width = 0;
212     winItemPtr->height = 0;
213     winItemPtr->anchor = TK_ANCHOR_CENTER;
214     winItemPtr->canvas = canvas;
215
216     /*
217      * Process the arguments to fill in the item record.
218      */
219
220     if ((WinItemCoords(interp, canvas, itemPtr, i, objv) != TCL_OK)) {
221         goto error;
222     }
223     if (ConfigureWinItem(interp, canvas, itemPtr, objc-i, objv+i, 0) == TCL_OK) {
224         return TCL_OK;
225     }
226
227     error:
228     DeleteWinItem(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
229     return TCL_ERROR;
230 }
231 \f
232 /*
233  *--------------------------------------------------------------
234  *
235  * WinItemCoords --
236  *
237  *      This procedure is invoked to process the "coords" widget
238  *      command on window items.  See the user documentation for
239  *      details on what it does.
240  *
241  * Results:
242  *      Returns TCL_OK or TCL_ERROR, and sets the interp's result.
243  *
244  * Side effects:
245  *      The coordinates for the given item may be changed.
246  *
247  *--------------------------------------------------------------
248  */
249
250 static int
251 WinItemCoords(interp, canvas, itemPtr, objc, objv)
252     Tcl_Interp *interp;                 /* Used for error reporting. */
253     Tk_Canvas canvas;                   /* Canvas containing item. */
254     Tk_Item *itemPtr;                   /* Item whose coordinates are to be
255                                          * read or modified. */
256     int objc;                           /* Number of coordinates supplied in
257                                          * objv. */
258     Tcl_Obj *CONST objv[];              /* Array of coordinates: x1, y1,
259                                          * x2, y2, ... */
260 {
261     WindowItem *winItemPtr = (WindowItem *) itemPtr;
262
263     if (objc == 0) {
264         Tcl_Obj *obj = Tcl_NewObj();
265         Tcl_Obj *subobj = Tcl_NewDoubleObj(winItemPtr->x);
266         Tcl_ListObjAppendElement(interp, obj, subobj);
267         subobj = Tcl_NewDoubleObj(winItemPtr->y);
268         Tcl_ListObjAppendElement(interp, obj, subobj);
269         Tcl_SetObjResult(interp, obj);
270     } else if (objc < 3) {
271         if (objc==1) {
272             if (Tcl_ListObjGetElements(interp, objv[0], &objc,
273                     (Tcl_Obj ***) &objv) != TCL_OK) {
274                 return TCL_ERROR;
275             } else if (objc != 2) {
276                 char buf[64 + TCL_INTEGER_SPACE];
277
278                 sprintf(buf, "wrong # coordinates: expected 2, got %d", objc);
279                 Tcl_SetResult(interp, buf, TCL_VOLATILE);
280                 return TCL_ERROR;
281             }
282         }
283         if ((Tk_CanvasGetCoordFromObj(interp, canvas, objv[0], &winItemPtr->x)
284                 != TCL_OK) || (Tk_CanvasGetCoordFromObj(interp, canvas, objv[1],
285                 &winItemPtr->y) != TCL_OK)) {
286             return TCL_ERROR;
287         }
288         ComputeWindowBbox(canvas, winItemPtr);
289     } else {
290         char buf[64 + TCL_INTEGER_SPACE];
291
292         sprintf(buf, "wrong # coordinates: expected 0 or 2, got %d", objc);
293         Tcl_SetResult(interp, buf, TCL_VOLATILE);
294         return TCL_ERROR;
295     }
296     return TCL_OK;
297 }
298 \f
299 /*
300  *--------------------------------------------------------------
301  *
302  * ConfigureWinItem --
303  *
304  *      This procedure is invoked to configure various aspects
305  *      of a window item, such as its anchor position.
306  *
307  * Results:
308  *      A standard Tcl result code.  If an error occurs, then
309  *      an error message is left in the interp's result.
310  *
311  * Side effects:
312  *      Configuration information may be set for itemPtr.
313  *
314  *--------------------------------------------------------------
315  */
316
317 static int
318 ConfigureWinItem(interp, canvas, itemPtr, objc, objv, flags)
319     Tcl_Interp *interp;         /* Used for error reporting. */
320     Tk_Canvas canvas;           /* Canvas containing itemPtr. */
321     Tk_Item *itemPtr;           /* Window item to reconfigure. */
322     int objc;                   /* Number of elements in objv.  */
323     Tcl_Obj *CONST objv[];      /* Arguments describing things to configure. */
324     int flags;                  /* Flags to pass to Tk_ConfigureWidget. */
325 {
326     WindowItem *winItemPtr = (WindowItem *) itemPtr;
327     Tk_Window oldWindow;
328     Tk_Window canvasTkwin;
329
330     oldWindow = winItemPtr->tkwin;
331     canvasTkwin = Tk_CanvasTkwin(canvas);
332     if (TCL_OK != Tk_ConfigureWidget(interp, canvasTkwin, configSpecs, objc,
333             (CONST char **) objv, (char *) winItemPtr, flags|TK_CONFIG_OBJS)) {
334         return TCL_ERROR;
335     }
336
337     /*
338      * A few of the options require additional processing.
339      */
340
341     if (oldWindow != winItemPtr->tkwin) {
342         if (oldWindow != NULL) {
343             Tk_DeleteEventHandler(oldWindow, StructureNotifyMask,
344                     WinItemStructureProc, (ClientData) winItemPtr);
345             Tk_ManageGeometry(oldWindow, (Tk_GeomMgr *) NULL,
346                     (ClientData) NULL);
347             Tk_UnmaintainGeometry(oldWindow, canvasTkwin);
348             Tk_UnmapWindow(oldWindow);
349         }
350         if (winItemPtr->tkwin != NULL) {
351             Tk_Window ancestor, parent;
352
353             /*
354              * Make sure that the canvas is either the parent of the
355              * window associated with the item or a descendant of that
356              * parent.  Also, don't allow a top-of-hierarchy window to be
357              * managed inside a canvas.
358              */
359
360             parent = Tk_Parent(winItemPtr->tkwin);
361             for (ancestor = canvasTkwin; ;
362                     ancestor = Tk_Parent(ancestor)) {
363                 if (ancestor == parent) {
364                     break;
365                 }
366                 if (((Tk_FakeWin *) (ancestor))->flags & TK_TOP_HIERARCHY) {
367                     badWindow:
368                     Tcl_AppendResult(interp, "can't use ",
369                             Tk_PathName(winItemPtr->tkwin),
370                             " in a window item of this canvas", (char *) NULL);
371                     winItemPtr->tkwin = NULL;
372                     return TCL_ERROR;
373                 }
374             }
375             if (((Tk_FakeWin *) (winItemPtr->tkwin))->flags & TK_TOP_HIERARCHY) {
376                 goto badWindow;
377             }
378             if (winItemPtr->tkwin == canvasTkwin) {
379                 goto badWindow;
380             }
381             Tk_CreateEventHandler(winItemPtr->tkwin, StructureNotifyMask,
382                     WinItemStructureProc, (ClientData) winItemPtr);
383             Tk_ManageGeometry(winItemPtr->tkwin, &canvasGeomType,
384                     (ClientData) winItemPtr);
385         }
386     }
387
388     ComputeWindowBbox(canvas, winItemPtr);
389
390     return TCL_OK;
391 }
392 \f
393 /*
394  *--------------------------------------------------------------
395  *
396  * DeleteWinItem --
397  *
398  *      This procedure is called to clean up the data structure
399  *      associated with a window item.
400  *
401  * Results:
402  *      None.
403  *
404  * Side effects:
405  *      Resources associated with itemPtr are released.
406  *
407  *--------------------------------------------------------------
408  */
409
410 static void
411 DeleteWinItem(canvas, itemPtr, display)
412     Tk_Canvas canvas;                   /* Overall info about widget. */
413     Tk_Item *itemPtr;                   /* Item that is being deleted. */
414     Display *display;                   /* Display containing window for
415                                          * canvas. */
416 {
417     WindowItem *winItemPtr = (WindowItem *) itemPtr;
418     Tk_Window canvasTkwin = Tk_CanvasTkwin(canvas);
419
420     if (winItemPtr->tkwin != NULL) {
421         Tk_DeleteEventHandler(winItemPtr->tkwin, StructureNotifyMask,
422                 WinItemStructureProc, (ClientData) winItemPtr);
423         Tk_ManageGeometry(winItemPtr->tkwin, (Tk_GeomMgr *) NULL,
424                 (ClientData) NULL);
425         if (canvasTkwin != Tk_Parent(winItemPtr->tkwin)) {
426             Tk_UnmaintainGeometry(winItemPtr->tkwin, canvasTkwin);
427         }
428         Tk_UnmapWindow(winItemPtr->tkwin);
429     }
430 }
431 \f
432 /*
433  *--------------------------------------------------------------
434  *
435  * ComputeWindowBbox --
436  *
437  *      This procedure is invoked to compute the bounding box of
438  *      all the pixels that may be drawn as part of a window item.
439  *      This procedure is where the child window's placement is
440  *      computed.
441  *
442  * Results:
443  *      None.
444  *
445  * Side effects:
446  *      The fields x1, y1, x2, and y2 are updated in the header
447  *      for itemPtr.
448  *
449  *--------------------------------------------------------------
450  */
451
452 static void
453 ComputeWindowBbox(canvas, winItemPtr)
454     Tk_Canvas canvas;                   /* Canvas that contains item. */
455     WindowItem *winItemPtr;             /* Item whose bbox is to be
456                                          * recomputed. */
457 {
458     int width, height, x, y;
459     Tk_State state = winItemPtr->header.state;
460
461     x = (int) (winItemPtr->x + ((winItemPtr->x >= 0) ? 0.5 : - 0.5));
462     y = (int) (winItemPtr->y + ((winItemPtr->y >= 0) ? 0.5 : - 0.5));
463
464     if (state == TK_STATE_NULL) {
465         state = ((TkCanvas *)canvas)->canvas_state;
466     }
467     if ((winItemPtr->tkwin == NULL) || (state == TK_STATE_HIDDEN)) {
468         /*
469          * There is no window for this item yet.  Just give it a 1x1
470          * bounding box.  Don't give it a 0x0 bounding box; there are
471          * strange cases where this bounding box might be used as the
472          * dimensions of the window, and 0x0 causes problems under X.
473          */
474
475         winItemPtr->header.x1 = x;
476         winItemPtr->header.x2 = winItemPtr->header.x1 + 1;
477         winItemPtr->header.y1 = y;
478         winItemPtr->header.y2 = winItemPtr->header.y1 + 1;
479         return;
480     }
481
482     /*
483      * Compute dimensions of window.
484      */
485
486     width = winItemPtr->width;
487     if (width <= 0) {
488         width = Tk_ReqWidth(winItemPtr->tkwin);
489         if (width <= 0) {
490             width = 1;
491         }
492     }
493     height = winItemPtr->height;
494     if (height <= 0) {
495         height = Tk_ReqHeight(winItemPtr->tkwin);
496         if (height <= 0) {
497             height = 1;
498         }
499     }
500
501     /*
502      * Compute location of window, using anchor information.
503      */
504
505     switch (winItemPtr->anchor) {
506         case TK_ANCHOR_N:
507             x -= width/2;
508             break;
509         case TK_ANCHOR_NE:
510             x -= width;
511             break;
512         case TK_ANCHOR_E:
513             x -= width;
514             y -= height/2;
515             break;
516         case TK_ANCHOR_SE:
517             x -= width;
518             y -= height;
519             break;
520         case TK_ANCHOR_S:
521             x -= width/2;
522             y -= height;
523             break;
524         case TK_ANCHOR_SW:
525             y -= height;
526             break;
527         case TK_ANCHOR_W:
528             y -= height/2;
529             break;
530         case TK_ANCHOR_NW:
531             break;
532         case TK_ANCHOR_CENTER:
533             x -= width/2;
534             y -= height/2;
535             break;
536     }
537
538     /*
539      * Store the information in the item header.
540      */
541
542     winItemPtr->header.x1 = x;
543     winItemPtr->header.y1 = y;
544     winItemPtr->header.x2 = x + width;
545     winItemPtr->header.y2 = y + height;
546 }
547 \f
548 /*
549  *--------------------------------------------------------------
550  *
551  * DisplayWinItem --
552  *
553  *      This procedure is invoked to "draw" a window item in a given
554  *      drawable.  Since the window draws itself, we needn't do any
555  *      actual redisplay here.  However, this procedure takes care
556  *      of actually repositioning the child window so that it occupies
557  *      the correct screen position.
558  *
559  * Results:
560  *      None.
561  *
562  * Side effects:
563  *      The child window's position may get changed.  Note: this
564  *      procedure gets called both when a window needs to be displayed
565  *      and when it ceases to be visible on the screen (e.g. it was
566  *      scrolled or moved off-screen or the enclosing canvas is
567  *      unmapped).
568  *
569  *--------------------------------------------------------------
570  */
571
572 static void
573 DisplayWinItem(canvas, itemPtr, display, drawable, regionX, regionY,
574         regionWidth, regionHeight)
575     Tk_Canvas canvas;                   /* Canvas that contains item. */
576     Tk_Item *itemPtr;                   /* Item to be displayed. */
577     Display *display;                   /* Display on which to draw item. */
578     Drawable drawable;                  /* Pixmap or window in which to draw
579                                          * item. */
580     int regionX, regionY, regionWidth, regionHeight;
581                                         /* Describes region of canvas that
582                                          * must be redisplayed (not used). */
583 {
584     WindowItem *winItemPtr = (WindowItem *) itemPtr;
585     int width, height;
586     short x, y;
587     Tk_Window canvasTkwin = Tk_CanvasTkwin(canvas);
588     Tk_State state = itemPtr->state;
589
590     if (winItemPtr->tkwin == NULL) {
591         return;
592     }
593     if(state == TK_STATE_NULL) {
594         state = ((TkCanvas *)canvas)->canvas_state;
595     }
596     if (state == TK_STATE_HIDDEN) {
597         Tk_UnmapWindow(winItemPtr->tkwin);
598         return;
599     }
600     Tk_CanvasWindowCoords(canvas, (double) winItemPtr->header.x1,
601             (double) winItemPtr->header.y1, &x, &y);
602     width = winItemPtr->header.x2 - winItemPtr->header.x1;
603     height = winItemPtr->header.y2 - winItemPtr->header.y1;
604
605     /*
606      * If the window is completely out of the visible area of the canvas
607      * then unmap it.  This code used not to be present (why unmap the
608      * window if it isn't visible anyway?) but this could cause the
609      * window to suddenly reappear if the canvas window got resized.
610      */
611
612     if (((x + width) <= 0) || ((y + height) <= 0)
613             || (x >= Tk_Width(canvasTkwin)) || (y >= Tk_Height(canvasTkwin))) {
614         if (canvasTkwin == Tk_Parent(winItemPtr->tkwin)) {
615             Tk_UnmapWindow(winItemPtr->tkwin); 
616         } else {
617             Tk_UnmaintainGeometry(winItemPtr->tkwin, canvasTkwin);
618         }
619         return;
620     }
621
622     /*
623      * Reposition and map the window (but in different ways depending
624      * on whether the canvas is the window's parent).
625      */
626
627     if (canvasTkwin == Tk_Parent(winItemPtr->tkwin)) {
628         if ((x != Tk_X(winItemPtr->tkwin)) || (y != Tk_Y(winItemPtr->tkwin))
629                 || (width != Tk_Width(winItemPtr->tkwin))
630                 || (height != Tk_Height(winItemPtr->tkwin))) {
631             Tk_MoveResizeWindow(winItemPtr->tkwin, x, y, width, height);
632         }
633         Tk_MapWindow(winItemPtr->tkwin);
634     } else {
635         Tk_MaintainGeometry(winItemPtr->tkwin, canvasTkwin, x, y,
636                 width, height);
637     }
638 }
639 \f
640 /*
641  *--------------------------------------------------------------
642  *
643  * WinItemToPoint --
644  *
645  *      Computes the distance from a given point to a given
646  *      window, in canvas units.
647  *
648  * Results:
649  *      The return value is 0 if the point whose x and y coordinates
650  *      are coordPtr[0] and coordPtr[1] is inside the window.  If the
651  *      point isn't inside the window then the return value is the
652  *      distance from the point to the window.
653  *
654  * Side effects:
655  *      None.
656  *
657  *--------------------------------------------------------------
658  */
659
660 static double
661 WinItemToPoint(canvas, itemPtr, pointPtr)
662     Tk_Canvas canvas;           /* Canvas containing item. */
663     Tk_Item *itemPtr;           /* Item to check against point. */
664     double *pointPtr;           /* Pointer to x and y coordinates. */
665 {
666     WindowItem *winItemPtr = (WindowItem *) itemPtr;
667     double x1, x2, y1, y2, xDiff, yDiff;
668
669     x1 = winItemPtr->header.x1;
670     y1 = winItemPtr->header.y1;
671     x2 = winItemPtr->header.x2;
672     y2 = winItemPtr->header.y2;
673
674     /*
675      * Point is outside window.
676      */
677
678     if (pointPtr[0] < x1) {
679         xDiff = x1 - pointPtr[0];
680     } else if (pointPtr[0] >= x2)  {
681         xDiff = pointPtr[0] + 1 - x2;
682     } else {
683         xDiff = 0;
684     }
685
686     if (pointPtr[1] < y1) {
687         yDiff = y1 - pointPtr[1];
688     } else if (pointPtr[1] >= y2)  {
689         yDiff = pointPtr[1] + 1 - y2;
690     } else {
691         yDiff = 0;
692     }
693
694     return hypot(xDiff, yDiff);
695 }
696 \f
697 /*
698  *--------------------------------------------------------------
699  *
700  * WinItemToArea --
701  *
702  *      This procedure is called to determine whether an item
703  *      lies entirely inside, entirely outside, or overlapping
704  *      a given rectangle.
705  *
706  * Results:
707  *      -1 is returned if the item is entirely outside the area
708  *      given by rectPtr, 0 if it overlaps, and 1 if it is entirely
709  *      inside the given area.
710  *
711  * Side effects:
712  *      None.
713  *
714  *--------------------------------------------------------------
715  */
716
717 static int
718 WinItemToArea(canvas, itemPtr, rectPtr)
719     Tk_Canvas canvas;           /* Canvas containing item. */
720     Tk_Item *itemPtr;           /* Item to check against rectangle. */
721     double *rectPtr;            /* Pointer to array of four coordinates
722                                  * (x1, y1, x2, y2) describing rectangular
723                                  * area.  */
724 {
725     WindowItem *winItemPtr = (WindowItem *) itemPtr;
726
727     if ((rectPtr[2] <= winItemPtr->header.x1)
728             || (rectPtr[0] >= winItemPtr->header.x2)
729             || (rectPtr[3] <= winItemPtr->header.y1)
730             || (rectPtr[1] >= winItemPtr->header.y2)) {
731         return -1;
732     }
733     if ((rectPtr[0] <= winItemPtr->header.x1)
734             && (rectPtr[1] <= winItemPtr->header.y1)
735             && (rectPtr[2] >= winItemPtr->header.x2)
736             && (rectPtr[3] >= winItemPtr->header.y2)) {
737         return 1;
738     }
739     return 0;
740 }
741 \f
742 /*
743  *--------------------------------------------------------------
744  *
745  * xerrorhandler --
746  *
747  *      This is a dummy function to catch X11 errors during an
748  *      attempt to print a canvas window.
749  *
750  * Results:
751  *      None.
752  *
753  * Side effects:
754  *      None.
755  *
756  *--------------------------------------------------------------
757  */
758
759 #ifdef X_GetImage
760 static int
761 xerrorhandler(clientData, e)
762     ClientData clientData;
763     XErrorEvent *e;
764 {
765         return 0;
766 }
767 #endif
768
769 \f
770 /*
771  *--------------------------------------------------------------
772  *
773  * WinItemToPostscript --
774  *
775  *      This procedure is called to generate Postscript for
776  *      window items.
777  *
778  * Results:
779  *      The return value is a standard Tcl result.  If an error
780  *      occurs in generating Postscript then an error message is
781  *      left in interp->result, replacing whatever used to be there.
782  *      If no error occurs, then Postscript for the item is appended
783  *      to the result.
784  *
785  * Side effects:
786  *      None.
787  *
788  *--------------------------------------------------------------
789  */
790
791 static int
792 WinItemToPostscript(interp, canvas, itemPtr, prepass)
793     Tcl_Interp *interp;                 /* Leave Postscript or error message
794                                          * here. */
795     Tk_Canvas canvas;                   /* Information about overall canvas. */
796     Tk_Item *itemPtr;                   /* Item for which Postscript is
797                                          * wanted. */
798     int prepass;                        /* 1 means this is a prepass to
799                                          * collect font information;  0 means
800                                          * final Postscript is being created.*/
801 {
802     WindowItem *winItemPtr = (WindowItem *)itemPtr;
803
804     double x, y;
805     int width, height;
806     Tk_Window tkwin = winItemPtr->tkwin;
807
808     if (prepass || winItemPtr->tkwin == NULL) {
809         return TCL_OK;
810     }
811     
812     width = Tk_Width(tkwin);
813     height = Tk_Height(tkwin);
814
815     /*
816      * Compute the coordinates of the lower-left corner of the window,
817      * taking into account the anchor position for the window.
818      */
819
820     x = winItemPtr->x;
821     y = Tk_CanvasPsY(canvas, winItemPtr->y);
822     
823     switch (winItemPtr->anchor) {
824         case TK_ANCHOR_NW:                      y -= height;            break;
825         case TK_ANCHOR_N:       x -= width/2.0; y -= height;            break;
826         case TK_ANCHOR_NE:      x -= width;     y -= height;            break;
827         case TK_ANCHOR_E:       x -= width;     y -= height/2.0;        break;
828         case TK_ANCHOR_SE:      x -= width;                             break;
829         case TK_ANCHOR_S:       x -= width/2.0;                         break;
830         case TK_ANCHOR_SW:                                              break;
831         case TK_ANCHOR_W:                       y -= height/2.0;        break;
832         case TK_ANCHOR_CENTER:  x -= width/2.0; y -= height/2.0;        break;
833     }
834
835     return CanvasPsWindow(interp, tkwin, canvas, x, y, width, height);
836 }
837
838 static int
839 CanvasPsWindow(interp, tkwin, canvas, x, y, width, height)
840     Tcl_Interp *interp;                 /* Leave Postscript or error message
841                                          * here. */
842     Tk_Window tkwin;                    /* window to be printed */
843     Tk_Canvas canvas;                   /* Information about overall canvas. */
844     double x, y;                        /* origin of window. */
845     int width, height;                  /* width/height of window. */
846 {
847     char buffer[256];
848     XImage *ximage;
849     int result;
850     Tcl_DString buffer1, buffer2;
851 #ifdef X_GetImage
852     Tk_ErrorHandler     handle;
853 #endif
854
855     sprintf(buffer, "\n%%%% %s item (%s, %d x %d)\n%.15g %.15g translate\n",
856             Tk_Class(tkwin), Tk_PathName(tkwin), width, height, x, y);
857     Tcl_AppendResult(interp, buffer, (char *) NULL);
858
859     /* first try if the widget has its own "postscript" command. If it
860      * exists, this will produce much better postscript than
861      * when a pixmap is used.
862      */
863
864     Tcl_DStringInit(&buffer1);
865     Tcl_DStringInit(&buffer2);
866     Tcl_DStringGetResult(interp, &buffer2);
867     sprintf (buffer, "%s postscript -prolog 0\n", Tk_PathName(tkwin));
868     result = Tcl_Eval(interp, buffer);
869     Tcl_DStringGetResult(interp, &buffer1);
870     Tcl_DStringResult(interp, &buffer2);
871     Tcl_DStringFree(&buffer2);
872
873     if (result == TCL_OK) {
874         Tcl_AppendResult(interp,
875                 "50 dict begin\nsave\ngsave\n",
876                 (char *) NULL);
877         sprintf (buffer,
878                 "0 %d moveto %d 0 rlineto 0 -%d rlineto -%d",
879                 height, width, height, width);
880         Tcl_AppendResult(interp, buffer, (char *) NULL);
881         Tcl_AppendResult(interp, " 0 rlineto closepath\n",
882                 "1.000 1.000 1.000 setrgbcolor AdjustColor\nfill\ngrestore\n",
883                 Tcl_DStringValue(&buffer1), "\nrestore\nend\n\n\n",
884                 (char *) NULL);
885         Tcl_DStringFree(&buffer1);
886
887         return result;
888     }
889     Tcl_DStringFree(&buffer1);
890
891     /*
892      * If the window is off the screen it will generate an BadMatch/XError
893      * We catch any BadMatch errors here
894      */
895 #ifdef X_GetImage
896     handle = Tk_CreateErrorHandler(Tk_Display(tkwin), BadMatch,
897             X_GetImage, -1, xerrorhandler, (ClientData) tkwin);
898 #endif
899
900     /*
901      * Generate an XImage from the window.  We can then read pixel 
902      * values out of the XImage.
903      */
904
905     ximage = XGetImage(Tk_Display(tkwin), Tk_WindowId(tkwin), 0, 0,
906             (unsigned int)width, (unsigned int)height, AllPlanes, ZPixmap);
907
908 #ifdef X_GetImage
909     Tk_DeleteErrorHandler(handle);
910 #endif
911
912     if (ximage == (XImage*) NULL) { 
913         return TCL_OK;
914     }
915
916     result = TkPostscriptImage(interp, tkwin,
917             ((TkCanvas *)canvas)->psInfo, ximage, 0, 0, width, height);
918
919     XDestroyImage(ximage);
920     return result;
921 }
922 \f
923 /*
924  *--------------------------------------------------------------
925  *
926  * ScaleWinItem --
927  *
928  *      This procedure is invoked to rescale a window item.
929  *
930  * Results:
931  *      None.
932  *
933  * Side effects:
934  *      The window referred to by itemPtr is rescaled
935  *      so that the following transformation is applied to all
936  *      point coordinates:
937  *              x' = originX + scaleX*(x-originX)
938  *              y' = originY + scaleY*(y-originY)
939  *
940  *--------------------------------------------------------------
941  */
942
943 static void
944 ScaleWinItem(canvas, itemPtr, originX, originY, scaleX, scaleY)
945     Tk_Canvas canvas;                   /* Canvas containing window. */
946     Tk_Item *itemPtr;                   /* Window to be scaled. */
947     double originX, originY;            /* Origin about which to scale window. */
948     double scaleX;                      /* Amount to scale in X direction. */
949     double scaleY;                      /* Amount to scale in Y direction. */
950 {
951     WindowItem *winItemPtr = (WindowItem *) itemPtr;
952
953     winItemPtr->x = originX + scaleX*(winItemPtr->x - originX);
954     winItemPtr->y = originY + scaleY*(winItemPtr->y - originY);
955     if (winItemPtr->width > 0) {
956         winItemPtr->width = (int) (scaleX*winItemPtr->width);
957     }
958     if (winItemPtr->height > 0) {
959         winItemPtr->height = (int) (scaleY*winItemPtr->height);
960     }
961     ComputeWindowBbox(canvas, winItemPtr);
962 }
963 \f
964 /*
965  *--------------------------------------------------------------
966  *
967  * TranslateWinItem --
968  *
969  *      This procedure is called to move a window by a given amount.
970  *
971  * Results:
972  *      None.
973  *
974  * Side effects:
975  *      The position of the window is offset by (xDelta, yDelta),
976  *      and the bounding box is updated in the generic part of the
977  *      item structure.
978  *
979  *--------------------------------------------------------------
980  */
981
982 static void
983 TranslateWinItem(canvas, itemPtr, deltaX, deltaY)
984     Tk_Canvas canvas;                   /* Canvas containing item. */
985     Tk_Item *itemPtr;                   /* Item that is being moved. */
986     double deltaX, deltaY;              /* Amount by which item is to be
987                                          * moved. */
988 {
989     WindowItem *winItemPtr = (WindowItem *) itemPtr;
990
991     winItemPtr->x += deltaX;
992     winItemPtr->y += deltaY;
993     ComputeWindowBbox(canvas, winItemPtr);
994 }
995 \f
996 /*
997  *--------------------------------------------------------------
998  *
999  * WinItemStructureProc --
1000  *
1001  *      This procedure is invoked whenever StructureNotify events
1002  *      occur for a window that's managed as part of a canvas window
1003  *      item.  This procudure's only purpose is to clean up when
1004  *      windows are deleted.
1005  *
1006  * Results:
1007  *      None.
1008  *
1009  * Side effects:
1010  *      The window is disassociated from the window item when it is
1011  *      deleted.
1012  *
1013  *--------------------------------------------------------------
1014  */
1015
1016 static void
1017 WinItemStructureProc(clientData, eventPtr)
1018     ClientData clientData;      /* Pointer to record describing window item. */
1019     XEvent *eventPtr;           /* Describes what just happened. */
1020 {
1021     WindowItem *winItemPtr = (WindowItem *) clientData;
1022
1023     if (eventPtr->type == DestroyNotify) {
1024         winItemPtr->tkwin = NULL;
1025     }
1026 }
1027 \f
1028 /*
1029  *--------------------------------------------------------------
1030  *
1031  * WinItemRequestProc --
1032  *
1033  *      This procedure is invoked whenever a window that's associated
1034  *      with a window canvas item changes its requested dimensions.
1035  *
1036  * Results:
1037  *      None.
1038  *
1039  * Side effects:
1040  *      The size and location on the screen of the window may change,
1041  *      depending on the options specified for the window item.
1042  *
1043  *--------------------------------------------------------------
1044  */
1045
1046 static void
1047 WinItemRequestProc(clientData, tkwin)
1048     ClientData clientData;              /* Pointer to record for window item. */
1049     Tk_Window tkwin;                    /* Window that changed its desired
1050                                          * size. */
1051 {
1052     WindowItem *winItemPtr = (WindowItem *) clientData;
1053
1054     ComputeWindowBbox(winItemPtr->canvas, winItemPtr);
1055     DisplayWinItem(winItemPtr->canvas, (Tk_Item *) winItemPtr,
1056             (Display *) NULL, (Drawable) None, 0, 0, 0, 0);
1057 }
1058 \f
1059 /*
1060  *--------------------------------------------------------------
1061  *
1062  * WinItemLostSlaveProc --
1063  *
1064  *      This procedure is invoked by Tk whenever some other geometry
1065  *      claims control over a slave that used to be managed by us.
1066  *
1067  * Results:
1068  *      None.
1069  *
1070  * Side effects:
1071  *      Forgets all canvas-related information about the slave.
1072  *
1073  *--------------------------------------------------------------
1074  */
1075
1076         /* ARGSUSED */
1077 static void
1078 WinItemLostSlaveProc(clientData, tkwin)
1079     ClientData clientData;      /* WindowItem structure for slave window that
1080                                  * was stolen away. */
1081     Tk_Window tkwin;            /* Tk's handle for the slave window. */
1082 {
1083     WindowItem *winItemPtr = (WindowItem *) clientData;
1084     Tk_Window canvasTkwin = Tk_CanvasTkwin(winItemPtr->canvas);
1085
1086     Tk_DeleteEventHandler(winItemPtr->tkwin, StructureNotifyMask,
1087             WinItemStructureProc, (ClientData) winItemPtr);
1088     if (canvasTkwin != Tk_Parent(winItemPtr->tkwin)) {
1089         Tk_UnmaintainGeometry(winItemPtr->tkwin, canvasTkwin);
1090     }
1091     Tk_UnmapWindow(winItemPtr->tkwin);
1092     winItemPtr->tkwin = NULL;
1093 }