Debian Codegraph @

pTk/mTk/generic/tkCanvWind.c

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