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