4 * This file contains generic Tk code for geometry management (stuff
5 * that's used by all geometry managers).
7 * Copyright (c) 1990-1994 The Regents of the University of California.
8 * Copyright (c) 1994-1995 Sun Microsystems, Inc.
10 * See the file "license.terms" for information on usage and redistribution of
11 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
17 * Data structures of the following type are used by Tk_MaintainGeometry. For
18 * each content managed by Tk_MaintainGeometry, there is one of these structures
19 * associated with its container.
22 typedef struct MaintainContent {
23 Tk_Window content; /* The content window being positioned. */
24 Tk_Window container; /* The container that determines content's
25 * position; it must be a descendant of
26 * content's parent. */
27 int x, y; /* Desired position of content relative to
29 int width, height; /* Desired dimensions of content. */
30 struct MaintainContent *nextPtr;
31 /* Next in list of Maintains associated with
36 * For each window that has been specified as a content to Tk_MaintainGeometry,
37 * there is a structure of the following type:
40 typedef struct MaintainContainer {
41 Tk_Window ancestor; /* The lowest ancestor of this window for
42 * which we have *not* created a
43 * StructureNotify handler. May be the same as
44 * the window itself. */
45 int checkScheduled; /* Non-zero means that there is already a call
46 * to MaintainCheckProc scheduled as an idle
48 MaintainContent *contentPtr; /* First in list of all content associated with
53 * Prototypes for static procedures in this file:
56 static void MaintainCheckProc(ClientData clientData);
57 static void MaintainContainerProc(ClientData clientData,
59 static void MaintainContentProc(ClientData clientData,
63 *--------------------------------------------------------------
65 * Tk_ManageGeometry --
67 * Arrange for a particular procedure to manage the geometry of a given
74 * Proc becomes the new geometry manager for tkwin, replacing any
75 * previous geometry manager. The geometry manager will be notified (by
76 * calling procedures in *mgrPtr) when interesting things happen in the
77 * future. If there was an existing geometry manager for tkwin different
78 * from the new one, it is notified by calling its lostSlaveProc.
80 *--------------------------------------------------------------
85 Tk_Window tkwin, /* Window whose geometry is to be managed by
87 const Tk_GeomMgr *mgrPtr, /* Static structure describing the geometry
88 * manager. This structure must never go
90 ClientData clientData) /* Arbitrary one-word argument to pass to
91 * geometry manager procedures. */
93 TkWindow *winPtr = (TkWindow *) tkwin;
95 if ((winPtr->geomMgrPtr != NULL) && (mgrPtr != NULL)
96 && ((winPtr->geomMgrPtr != mgrPtr)
97 || (winPtr->geomData != clientData))
98 && (winPtr->geomMgrPtr->lostSlaveProc != NULL)) {
99 winPtr->geomMgrPtr->lostSlaveProc(winPtr->geomData, tkwin);
102 winPtr->geomMgrPtr = mgrPtr;
103 winPtr->geomData = clientData;
107 *--------------------------------------------------------------
109 * Tk_GeometryRequest --
111 * This procedure is invoked by widget code to indicate its preferences
112 * about the size of a window it manages. In general, widget code should
113 * call this procedure rather than Tk_ResizeWindow.
119 * The geometry manager for tkwin (if any) is invoked to handle the
120 * request. If possible, it will reconfigure tkwin and/or other windows
121 * to satisfy the request. The caller gets no indication of success or
122 * failure, but it will get X events if the window size was actually
125 *--------------------------------------------------------------
130 Tk_Window tkwin, /* Window that geometry information pertains
132 int reqWidth, int reqHeight)/* Minimum desired dimensions for window, in
135 TkWindow *winPtr = (TkWindow *) tkwin;
138 * X gets very upset if a window requests a width or height of zero, so
139 * rounds requested sizes up to at least 1.
145 if (reqHeight <= 0) {
148 if ((reqWidth == winPtr->reqWidth) && (reqHeight == winPtr->reqHeight)) {
151 winPtr->reqWidth = reqWidth;
152 winPtr->reqHeight = reqHeight;
153 if ((winPtr->geomMgrPtr != NULL)
154 && (winPtr->geomMgrPtr->requestProc != NULL)) {
155 winPtr->geomMgrPtr->requestProc(winPtr->geomData, tkwin);
160 *----------------------------------------------------------------------
162 * Tk_SetInternalBorderEx --
164 * Notify relevant geometry managers that a window has an internal border
165 * of a given width and that child windows should not be placed on that
172 * The border widths are recorded for the window, and all geometry
173 * managers of all children are notified so that can re-layout, if
176 *----------------------------------------------------------------------
180 Tk_SetInternalBorderEx(
181 Tk_Window tkwin, /* Window that will have internal border. */
182 int left, int right, /* Width of internal border, in pixels. */
185 TkWindow *winPtr = (TkWindow *) tkwin;
191 if (left != winPtr->internalBorderLeft) {
192 winPtr->internalBorderLeft = left;
199 if (right != winPtr->internalBorderRight) {
200 winPtr->internalBorderRight = right;
207 if (top != winPtr->internalBorderTop) {
208 winPtr->internalBorderTop = top;
215 if (bottom != winPtr->internalBorderBottom) {
216 winPtr->internalBorderBottom = bottom;
221 * All the content for which this is the container window must now be
222 * repositioned to take account of the new internal border width. To
223 * signal all the geometry managers to do this, trigger a ConfigureNotify
224 * event. This will cause geometry managers to recompute everything.
228 TkDoConfigureNotify(winPtr);
233 *----------------------------------------------------------------------
235 * Tk_SetInternalBorder --
237 * Notify relevant geometry managers that a window has an internal border
238 * of a given width and that child windows should not be placed on that
245 * The border width is recorded for the window, and all geometry managers
246 * of all children are notified so that can re-layout, if necessary.
248 *----------------------------------------------------------------------
252 Tk_SetInternalBorder(
253 Tk_Window tkwin, /* Window that will have internal border. */
254 int width) /* Width of internal border, in pixels. */
256 Tk_SetInternalBorderEx(tkwin, width, width, width, width);
260 *----------------------------------------------------------------------
262 * Tk_SetMinimumRequestSize --
264 * Notify relevant geometry managers that a window has a minimum request
271 * The minimum request size is recorded for the window, and a new size is
272 * requested for the window, if necessary.
274 *----------------------------------------------------------------------
278 Tk_SetMinimumRequestSize(
279 Tk_Window tkwin, /* Window that will have internal border. */
280 int minWidth, int minHeight)/* Minimum requested size, in pixels. */
282 TkWindow *winPtr = (TkWindow *) tkwin;
284 if ((winPtr->minReqWidth == minWidth) &&
285 (winPtr->minReqHeight == minHeight)) {
289 winPtr->minReqWidth = minWidth;
290 winPtr->minReqHeight = minHeight;
293 * The changed min size may cause geometry managers to get a different
294 * result, so make them recompute. To signal all the geometry managers to
295 * do this, just resize the window to its current size. The
296 * ConfigureNotify event will cause geometry managers to recompute
300 Tk_ResizeWindow(tkwin, Tk_Width(tkwin), Tk_Height(tkwin));
304 *----------------------------------------------------------------------
306 * TkSetGeometryContainer --
308 * Set a geometry container for this window. Only one container may own
309 * a window at any time.
312 * A standard Tcl result.
315 * The geometry container is recorded for the window.
317 *----------------------------------------------------------------------
321 TkSetGeometryContainer(
322 Tcl_Interp *interp, /* Current interpreter, for error. */
323 Tk_Window tkwin, /* Window that will have geometry container
325 const char *name) /* The name of the geometry manager. */
327 TkWindow *winPtr = (TkWindow *) tkwin;
329 if (winPtr->geomMgrName != NULL &&
330 strcmp(winPtr->geomMgrName, name) == 0) {
333 if (winPtr->geomMgrName != NULL) {
334 if (interp != NULL) {
335 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
336 "cannot use geometry manager %s inside %s which already"
337 " has slaves managed by %s",
338 name, Tk_PathName(tkwin), winPtr->geomMgrName));
339 Tcl_SetErrorCode(interp, "TK", "GEOMETRY", "FIGHT", NULL);
344 winPtr->geomMgrName = (char *)ckalloc(strlen(name) + 1);
345 strcpy(winPtr->geomMgrName, name);
350 *----------------------------------------------------------------------
352 * TkFreeGeometryContainer --
354 * Remove a geometry container for this window. Only one container may own
355 * a window at any time.
361 * The geometry container is cleared for the window.
363 *----------------------------------------------------------------------
367 TkFreeGeometryContainer(
368 Tk_Window tkwin, /* Window that will have geometry container
370 const char *name) /* The name of the geometry manager. */
372 TkWindow *winPtr = (TkWindow *) tkwin;
374 if (winPtr->geomMgrName != NULL &&
375 strcmp(winPtr->geomMgrName, name) != 0) {
376 Tcl_Panic("Trying to free %s from geometry manager %s",
377 winPtr->geomMgrName, name);
379 if (winPtr->geomMgrName != NULL) {
380 ckfree(winPtr->geomMgrName);
381 winPtr->geomMgrName = NULL;
386 *----------------------------------------------------------------------
388 * Tk_MaintainGeometry --
390 * This procedure is invoked by geometry managers to handle content whose
391 * container's are not their parents. It translates the desired geometry for
392 * the content into the coordinate system of the parent and respositions
393 * the content if it isn't already at the right place. Furthermore, it sets
394 * up event handlers so that if the container (or any of its ancestors up to
395 * the content's parent) is mapped, unmapped, or moved, then the content will
396 * be adjusted to match.
402 * Event handlers are created and state is allocated to keep track of
403 * content. Note: if content was already managed for container by
404 * Tk_MaintainGeometry, then the previous information is replaced with
405 * the new information. The caller must eventually call
406 * Tk_UnmaintainGeometry to eliminate the correspondence (or, the state
407 * is automatically freed when either window is destroyed).
409 *----------------------------------------------------------------------
414 Tk_Window window, /* Window for geometry management. */
415 Tk_Window container, /* Container for window; must be a descendant of
416 * window's parent. */
417 int x, int y, /* Desired position of window within container. */
418 int width, int height) /* Desired dimensions for window. */
421 MaintainContainer *containerPtr;
422 MaintainContent *contentPtr;
424 Tk_Window ancestor, parent;
425 TkDisplay *dispPtr = ((TkWindow *) container)->dispPtr;
427 ((TkWindow *)window)->maintainerPtr = (TkWindow *)container;
429 if (container == Tk_Parent(window)) {
431 * If the window is a direct descendant of the container, don't bother
432 * setting up the extra infrastructure for management, just make a
433 * call to Tk_MoveResizeWindow; the parent/child relationship will
434 * take care of the rest.
437 Tk_MoveResizeWindow(window, x, y, width, height);
440 * Map the window if the container is already mapped; otherwise, wait
441 * until the container is mapped later (in which case mapping the window
442 * is taken care of elsewhere).
445 if (Tk_IsMapped(container)) {
446 Tk_MapWindow(window);
451 if (!dispPtr->geomInit) {
452 dispPtr->geomInit = 1;
453 Tcl_InitHashTable(&dispPtr->maintainHashTable, TCL_ONE_WORD_KEYS);
457 * See if there is already a MaintainContainer structure for the container; if
458 * not, then create one.
461 parent = Tk_Parent(window);
462 hPtr = Tcl_CreateHashEntry(&dispPtr->maintainHashTable,
463 (char *) container, &isNew);
465 containerPtr = (MaintainContainer *)Tcl_GetHashValue(hPtr);
467 containerPtr = (MaintainContainer *)ckalloc(sizeof(MaintainContainer));
468 containerPtr->ancestor = container;
469 containerPtr->checkScheduled = 0;
470 containerPtr->contentPtr = NULL;
471 Tcl_SetHashValue(hPtr, containerPtr);
475 * Create a MaintainContent structure for the window if there isn't already
479 for (contentPtr = containerPtr->contentPtr; contentPtr != NULL;
480 contentPtr = contentPtr->nextPtr) {
481 if (contentPtr->content == window) {
485 contentPtr = (MaintainContent *)ckalloc(sizeof(MaintainContent));
486 contentPtr->content = window;
487 contentPtr->container = container;
488 contentPtr->nextPtr = containerPtr->contentPtr;
489 containerPtr->contentPtr = contentPtr;
490 Tk_CreateEventHandler(window, StructureNotifyMask, MaintainContentProc,
494 * Make sure that there are event handlers registered for all the windows
495 * between container and windows's parent (including container but not window's
496 * parent). There may already be handlers for container and some of its
497 * ancestors (containerPtr->ancestor tells how many).
500 for (ancestor = container; ancestor != parent;
501 ancestor = Tk_Parent(ancestor)) {
502 if (ancestor == containerPtr->ancestor) {
503 Tk_CreateEventHandler(ancestor, StructureNotifyMask,
504 MaintainContainerProc, containerPtr);
505 containerPtr->ancestor = Tk_Parent(ancestor);
510 * Fill in up-to-date information in the structure, then update the window
511 * if it's not currently in the right place or state.
517 contentPtr->width = width;
518 contentPtr->height = height;
520 for (ancestor = contentPtr->container; ; ancestor = Tk_Parent(ancestor)) {
521 if (!Tk_IsMapped(ancestor) && (ancestor != parent)) {
524 if (ancestor == parent) {
525 if ((x != Tk_X(contentPtr->content))
526 || (y != Tk_Y(contentPtr->content))
527 || (width != Tk_Width(contentPtr->content))
528 || (height != Tk_Height(contentPtr->content))) {
529 Tk_MoveResizeWindow(contentPtr->content, x, y, width, height);
532 Tk_MapWindow(contentPtr->content);
534 Tk_UnmapWindow(contentPtr->content);
538 x += Tk_X(ancestor) + Tk_Changes(ancestor)->border_width;
539 y += Tk_Y(ancestor) + Tk_Changes(ancestor)->border_width;
544 *----------------------------------------------------------------------
546 * Tk_UnmaintainGeometry --
548 * This procedure cancels a previous Tk_MaintainGeometry call, so that
549 * the relationship between window and container is no longer maintained.
555 * The window is unmapped and state is released, so that window won't track
556 * container any more. If we weren't previously managing window relative to
557 * container, then this procedure has no effect.
559 *----------------------------------------------------------------------
563 Tk_UnmaintainGeometry(
564 Tk_Window window, /* WIndow for geometry management. */
565 Tk_Window container) /* Container for window; must be a descendant of
566 * window's parent. */
569 MaintainContainer *containerPtr;
570 MaintainContent *contentPtr, *prevPtr;
572 TkDisplay *dispPtr = ((TkWindow *) window)->dispPtr;
574 ((TkWindow *)window)->maintainerPtr = NULL;
576 if (container == Tk_Parent(window)) {
578 * If the window is a direct descendant of the container,
579 * Tk_MaintainGeometry will not have set up any of the extra
580 * infrastructure. Don't even bother to look for it, just return.
585 if (!dispPtr->geomInit) {
586 dispPtr->geomInit = 1;
587 Tcl_InitHashTable(&dispPtr->maintainHashTable, TCL_ONE_WORD_KEYS);
590 if (!(((TkWindow *) window)->flags & TK_ALREADY_DEAD)) {
591 Tk_UnmapWindow(window);
593 hPtr = Tcl_FindHashEntry(&dispPtr->maintainHashTable, container);
597 containerPtr = (MaintainContainer *)Tcl_GetHashValue(hPtr);
598 contentPtr = containerPtr->contentPtr;
599 if (contentPtr->content == window) {
600 containerPtr->contentPtr = contentPtr->nextPtr;
602 for (prevPtr = contentPtr, contentPtr = contentPtr->nextPtr; ;
603 prevPtr = contentPtr, contentPtr = contentPtr->nextPtr) {
604 if (contentPtr == NULL) {
607 if (contentPtr->content == window) {
608 prevPtr->nextPtr = contentPtr->nextPtr;
613 Tk_DeleteEventHandler(contentPtr->content, StructureNotifyMask,
614 MaintainContentProc, contentPtr);
616 if (containerPtr->contentPtr == NULL) {
617 if (containerPtr->ancestor != NULL) {
618 for (ancestor = container; ; ancestor = Tk_Parent(ancestor)) {
619 Tk_DeleteEventHandler(ancestor, StructureNotifyMask,
620 MaintainContainerProc, containerPtr);
621 if (ancestor == containerPtr->ancestor) {
626 if (containerPtr->checkScheduled) {
627 Tcl_CancelIdleCall(MaintainCheckProc, containerPtr);
629 Tcl_DeleteHashEntry(hPtr);
630 ckfree(containerPtr);
635 *----------------------------------------------------------------------
637 * MaintainContainerProc --
639 * This procedure is invoked by the Tk event dispatcher in response to
640 * StructureNotify events on the container or one of its ancestors, on
641 * behalf of Tk_MaintainGeometry.
647 * It schedules a call to MaintainCheckProc, which will eventually caused
648 * the postions and mapped states to be recalculated for all the
649 * maintained windows of the container. Or, if the container window is being
650 * deleted then state is cleaned up.
652 *----------------------------------------------------------------------
656 MaintainContainerProc(
657 ClientData clientData, /* Pointer to MaintainContainer structure for the
658 * container window. */
659 XEvent *eventPtr) /* Describes what just happened. */
661 MaintainContainer *containerPtr = (MaintainContainer *)clientData;
662 MaintainContent *contentPtr;
665 if ((eventPtr->type == ConfigureNotify)
666 || (eventPtr->type == MapNotify)
667 || (eventPtr->type == UnmapNotify)) {
668 if (!containerPtr->checkScheduled) {
669 containerPtr->checkScheduled = 1;
670 Tcl_DoWhenIdle(MaintainCheckProc, containerPtr);
672 } else if (eventPtr->type == DestroyNotify) {
674 * Delete all of the state associated with this container, but be careful
675 * not to use containerPtr after the last window is deleted, since its
676 * memory will have been freed.
681 contentPtr = containerPtr->contentPtr;
682 if (contentPtr->nextPtr == NULL) {
685 Tk_UnmaintainGeometry(contentPtr->content, contentPtr->container);
691 *----------------------------------------------------------------------
693 * MaintainContentProc --
695 * This procedure is invoked by the Tk event dispatcher in response to
696 * StructureNotify events on a window being managed by
697 * Tk_MaintainGeometry.
703 * If the event is a DestroyNotify event then the Maintain state and
704 * event handlers for this window are deleted.
706 *----------------------------------------------------------------------
711 ClientData clientData, /* Pointer to MaintainContent structure for
712 * container-window pair. */
713 XEvent *eventPtr) /* Describes what just happened. */
715 MaintainContent *contentPtr = (MaintainContent *)clientData;
717 if (eventPtr->type == DestroyNotify) {
718 Tk_UnmaintainGeometry(contentPtr->content, contentPtr->container);
723 *----------------------------------------------------------------------
725 * MaintainCheckProc --
727 * This procedure is invoked by the Tk event dispatcher as an idle
728 * handler, when a container or one of its ancestors has been reconfigured,
729 * mapped, or unmapped. Its job is to scan all of the windows for the
730 * container and reposition them, map them, or unmap them as needed to
731 * maintain their geometry relative to the container.
737 * Windows can get repositioned, mapped, or unmapped.
739 *----------------------------------------------------------------------
744 ClientData clientData) /* Pointer to MaintainContainer structure for the
745 * container window. */
747 MaintainContainer *containerPtr = (MaintainContainer *)clientData;
748 MaintainContent *contentPtr;
749 Tk_Window ancestor, parent;
752 containerPtr->checkScheduled = 0;
753 for (contentPtr = containerPtr->contentPtr; contentPtr != NULL;
754 contentPtr = contentPtr->nextPtr) {
755 parent = Tk_Parent(contentPtr->content);
759 for (ancestor = contentPtr->container; ; ancestor = Tk_Parent(ancestor)) {
760 if (!Tk_IsMapped(ancestor) && (ancestor != parent)) {
763 if (ancestor == parent) {
764 if ((x != Tk_X(contentPtr->content))
765 || (y != Tk_Y(contentPtr->content))) {
766 Tk_MoveWindow(contentPtr->content, x, y);
769 Tk_MapWindow(contentPtr->content);
771 Tk_UnmapWindow(contentPtr->content);
775 x += Tk_X(ancestor) + Tk_Changes(ancestor)->border_width;
776 y += Tk_Y(ancestor) + Tk_Changes(ancestor)->border_width;