/*1* tkGeometry.c --2*3* This file contains generic Tk code for geometry management4* (stuff that's used by all geometry managers).5*6* Copyright (c) 1990-1994 The Regents of the University of California.7* Copyright (c) 1994-1995 Sun Microsystems, Inc.8*9* See the file "license.terms" for information on usage and redistribution10* of this file, and for a DISCLAIMER OF ALL WARRANTIES.11*12* SCCS: @(#) tkGeometry.c 1.31 96/02/15 18:53:3213*/1415#include "tkInt.h"1617/*18* Data structures of the following type are used by Tk_MaintainGeometry.19* For each slave managed by Tk_MaintainGeometry, there is one of these20* structures associated with its master.21*/2223typedef struct MaintainSlave {24Tk_Window slave; /* The slave window being positioned. */25Tk_Window master; /* The master that determines slave's26* position; it must be a descendant of27* slave's parent. */28int x, y; /* Desired position of slave relative to29* master. */30int width, height; /* Desired dimensions of slave. */31struct MaintainSlave *nextPtr;32/* Next in list of Maintains associated33* with master. */34} MaintainSlave;3536/*37* For each window that has been specified as a master to38* Tk_MaintainGeometry, there is a structure of the following type:39*/4041typedef struct MaintainMaster {42Tk_Window ancestor; /* The lowest ancestor of this window43* for which we have *not* created a44* StructureNotify handler. May be the45* same as the window itself. */46int checkScheduled; /* Non-zero means that there is already a47* call to MaintainCheckProc scheduled as48* an idle handler. */49MaintainSlave *slavePtr; /* First in list of all slaves associated50* with this master. */51} MaintainMaster;5253/*54* Hash table that maps from a master's Tk_Window token to a list of55* Maintains for that master:56*/5758static Tcl_HashTable maintainHashTable;5960/*61* Has maintainHashTable been initialized yet?62*/6364static int initialized = 0;6566/*67* Prototypes for static procedures in this file:68*/6970static void MaintainCheckProc _ANSI_ARGS_((ClientData clientData));71static void MaintainMasterProc _ANSI_ARGS_((ClientData clientData,72XEvent *eventPtr));73static void MaintainSlaveProc _ANSI_ARGS_((ClientData clientData,74XEvent *eventPtr));7576/*77*--------------------------------------------------------------78*79* Tk_ManageGeometry --80*81* Arrange for a particular procedure to manage the geometry82* of a given slave window.83*84* Results:85* None.86*87* Side effects:88* Proc becomes the new geometry manager for tkwin, replacing89* any previous geometry manager. The geometry manager will90* be notified (by calling procedures in *mgrPtr) when interesting91* things happen in the future. If there was an existing geometry92* manager for tkwin different from the new one, it is notified93* by calling its lostSlaveProc.94*95*--------------------------------------------------------------96*/9798void99Tk_ManageGeometry(tkwin, mgrPtr, clientData)100Tk_Window tkwin; /* Window whose geometry is to101* be managed by proc. */102Tk_GeomMgr *mgrPtr; /* Static structure describing the103* geometry manager. This structure104* must never go away. */105ClientData clientData; /* Arbitrary one-word argument to106* pass to geometry manager procedures. */107{108register TkWindow *winPtr = (TkWindow *) tkwin;109110if ((winPtr->geomMgrPtr != NULL) && (mgrPtr != NULL)111&& ((winPtr->geomMgrPtr != mgrPtr)112|| (winPtr->geomData != clientData))113&& (winPtr->geomMgrPtr->lostSlaveProc != NULL)) {114(*winPtr->geomMgrPtr->lostSlaveProc)(winPtr->geomData, tkwin);115}116117winPtr->geomMgrPtr = mgrPtr;118winPtr->geomData = clientData;119}120121/*122*--------------------------------------------------------------123*124* Tk_GeometryRequest --125*126* This procedure is invoked by widget code to indicate127* its preferences about the size of a window it manages.128* In general, widget code should call this procedure129* rather than Tk_ResizeWindow.130*131* Results:132* None.133*134* Side effects:135* The geometry manager for tkwin (if any) is invoked to136* handle the request. If possible, it will reconfigure137* tkwin and/or other windows to satisfy the request. The138* caller gets no indication of success or failure, but it139* will get X events if the window size was actually140* changed.141*142*--------------------------------------------------------------143*/144145void146Tk_GeometryRequest(tkwin, reqWidth, reqHeight)147Tk_Window tkwin; /* Window that geometry information148* pertains to. */149int reqWidth, reqHeight; /* Minimum desired dimensions for150* window, in pixels. */151{152register TkWindow *winPtr = (TkWindow *) tkwin;153154/*155* X gets very upset if a window requests a width or height of156* zero, so rounds requested sizes up to at least 1.157*/158159if (reqWidth <= 0) {160reqWidth = 1;161}162if (reqHeight <= 0) {163reqHeight = 1;164}165if ((reqWidth == winPtr->reqWidth) && (reqHeight == winPtr->reqHeight)) {166return;167}168winPtr->reqWidth = reqWidth;169winPtr->reqHeight = reqHeight;170if ((winPtr->geomMgrPtr != NULL)171&& (winPtr->geomMgrPtr->requestProc != NULL)) {172(*winPtr->geomMgrPtr->requestProc)(winPtr->geomData, tkwin);173}174}175176/*177*----------------------------------------------------------------------178*179* Tk_SetInternalBorder --180*181* Notify relevant geometry managers that a window has an internal182* border of a given width and that child windows should not be183* placed on that border.184*185* Results:186* None.187*188* Side effects:189* The border width is recorded for the window, and all geometry190* managers of all children are notified so that can re-layout, if191* necessary.192*193*----------------------------------------------------------------------194*/195196void197Tk_SetInternalBorder(tkwin, width)198Tk_Window tkwin; /* Window that will have internal border. */199int width; /* Width of internal border, in pixels. */200{201register TkWindow *winPtr = (TkWindow *) tkwin;202203if (width == winPtr->internalBorderWidth) {204return;205}206if (width < 0) {207width = 0;208}209winPtr->internalBorderWidth = width;210211/*212* All the slaves for which this is the master window must now be213* repositioned to take account of the new internal border width.214* To signal all the geometry managers to do this, just resize the215* window to its current size. The ConfigureNotify event will216* cause geometry managers to recompute everything.217*/218219Tk_ResizeWindow(tkwin, Tk_Width(tkwin), Tk_Height(tkwin));220}221222/*223*----------------------------------------------------------------------224*225* Tk_MaintainGeometry --226*227* This procedure is invoked by geometry managers to handle slaves228* whose master's are not their parents. It translates the desired229* geometry for the slave into the coordinate system of the parent230* and respositions the slave if it isn't already at the right place.231* Furthermore, it sets up event handlers so that if the master (or232* any of its ancestors up to the slave's parent) is mapped, unmapped,233* or moved, then the slave will be adjusted to match.234*235* Results:236* None.237*238* Side effects:239* Event handlers are created and state is allocated to keep track240* of slave. Note: if slave was already managed for master by241* Tk_MaintainGeometry, then the previous information is replaced242* with the new information. The caller must eventually call243* Tk_UnmaintainGeometry to eliminate the correspondence (or, the244* state is automatically freed when either window is destroyed).245*246*----------------------------------------------------------------------247*/248249void250Tk_MaintainGeometry(slave, master, x, y, width, height)251Tk_Window slave; /* Slave for geometry management. */252Tk_Window master; /* Master for slave; must be a descendant253* of slave's parent. */254int x, y; /* Desired position of slave within master. */255int width, height; /* Desired dimensions for slave. */256{257Tcl_HashEntry *hPtr;258MaintainMaster *masterPtr;259register MaintainSlave *slavePtr;260int new, map;261Tk_Window ancestor, parent;262263if (!initialized) {264initialized = 1;265Tcl_InitHashTable(&maintainHashTable, TCL_ONE_WORD_KEYS);266}267268/*269* See if there is already a MaintainMaster structure for the master;270* if not, then create one.271*/272273parent = Tk_Parent(slave);274hPtr = Tcl_CreateHashEntry(&maintainHashTable, (char *) master, &new);275if (!new) {276masterPtr = (MaintainMaster *) Tcl_GetHashValue(hPtr);277} else {278masterPtr = (MaintainMaster *) ckalloc(sizeof(MaintainMaster));279masterPtr->ancestor = master;280masterPtr->checkScheduled = 0;281masterPtr->slavePtr = NULL;282Tcl_SetHashValue(hPtr, masterPtr);283}284285/*286* Create a MaintainSlave structure for the slave if there isn't287* already one.288*/289290for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;291slavePtr = slavePtr->nextPtr) {292if (slavePtr->slave == slave) {293goto gotSlave;294}295}296slavePtr = (MaintainSlave *) ckalloc(sizeof(MaintainSlave));297slavePtr->slave = slave;298slavePtr->master = master;299slavePtr->nextPtr = masterPtr->slavePtr;300masterPtr->slavePtr = slavePtr;301Tk_CreateEventHandler(slave, StructureNotifyMask, MaintainSlaveProc,302(ClientData) slavePtr);303304/*305* Make sure that there are event handlers registered for all306* the windows between master and slave's parent (including master307* but not slave's parent). There may already be handlers for master308* and some of its ancestors (masterPtr->ancestor tells how many).309*/310311for (ancestor = master; ancestor != parent;312ancestor = Tk_Parent(ancestor)) {313if (ancestor == masterPtr->ancestor) {314Tk_CreateEventHandler(ancestor, StructureNotifyMask,315MaintainMasterProc, (ClientData) masterPtr);316masterPtr->ancestor = Tk_Parent(ancestor);317}318}319320/*321* Fill in up-to-date information in the structure, then update the322* window if it's not currently in the right place or state.323*/324325gotSlave:326slavePtr->x = x;327slavePtr->y = y;328slavePtr->width = width;329slavePtr->height = height;330map = 1;331for (ancestor = slavePtr->master; ; ancestor = Tk_Parent(ancestor)) {332if (!Tk_IsMapped(ancestor) && (ancestor != parent)) {333map = 0;334}335if (ancestor == parent) {336if ((x != Tk_X(slavePtr->slave))337|| (y != Tk_Y(slavePtr->slave))338|| (width != Tk_Width(slavePtr->slave))339|| (height != Tk_Height(slavePtr->slave))) {340Tk_MoveResizeWindow(slavePtr->slave, x, y, width, height);341}342if (map) {343Tk_MapWindow(slavePtr->slave);344} else {345Tk_UnmapWindow(slavePtr->slave);346}347break;348}349x += Tk_X(ancestor) + Tk_Changes(ancestor)->border_width;350y += Tk_Y(ancestor) + Tk_Changes(ancestor)->border_width;351}352}353354/*355*----------------------------------------------------------------------356*357* Tk_UnmaintainGeometry --358*359* This procedure cancels a previous Tk_MaintainGeometry call,360* so that the relationship between slave and master is no longer361* maintained.362*363* Results:364* None.365*366* Side effects:367* The slave is unmapped and state is released, so that slave won't368* track master any more. If we weren't previously managing slave369* relative to master, then this procedure has no effect.370*371*----------------------------------------------------------------------372*/373374void375Tk_UnmaintainGeometry(slave, master)376Tk_Window slave; /* Slave for geometry management. */377Tk_Window master; /* Master for slave; must be a descendant378* of slave's parent. */379{380Tcl_HashEntry *hPtr;381MaintainMaster *masterPtr;382register MaintainSlave *slavePtr, *prevPtr;383Tk_Window ancestor;384385if (!initialized) {386initialized = 1;387Tcl_InitHashTable(&maintainHashTable, TCL_ONE_WORD_KEYS);388}389390if (!(((TkWindow *) slave)->flags & TK_ALREADY_DEAD)) {391Tk_UnmapWindow(slave);392}393hPtr = Tcl_FindHashEntry(&maintainHashTable, (char *) master);394if (hPtr == NULL) {395return;396}397masterPtr = (MaintainMaster *) Tcl_GetHashValue(hPtr);398slavePtr = masterPtr->slavePtr;399if (slavePtr->slave == slave) {400masterPtr->slavePtr = slavePtr->nextPtr;401} else {402for (prevPtr = slavePtr, slavePtr = slavePtr->nextPtr; ;403prevPtr = slavePtr, slavePtr = slavePtr->nextPtr) {404if (slavePtr == NULL) {405return;406}407if (slavePtr->slave == slave) {408prevPtr->nextPtr = slavePtr->nextPtr;409break;410}411}412}413Tk_DeleteEventHandler(slavePtr->slave, StructureNotifyMask,414MaintainSlaveProc, (ClientData) slavePtr);415ckfree((char *) slavePtr);416if (masterPtr->slavePtr == NULL) {417if (masterPtr->ancestor != NULL) {418for (ancestor = master; ; ancestor = Tk_Parent(ancestor)) {419Tk_DeleteEventHandler(ancestor, StructureNotifyMask,420MaintainMasterProc, (ClientData) masterPtr);421if (ancestor == masterPtr->ancestor) {422break;423}424}425}426if (masterPtr->checkScheduled) {427Tcl_CancelIdleCall(MaintainCheckProc, (ClientData) masterPtr);428}429Tcl_DeleteHashEntry(hPtr);430ckfree((char *) masterPtr);431}432}433434/*435*----------------------------------------------------------------------436*437* MaintainMasterProc --438*439* This procedure is invoked by the Tk event dispatcher in440* response to StructureNotify events on the master or one441* of its ancestors, on behalf of Tk_MaintainGeometry.442*443* Results:444* None.445*446* Side effects:447* It schedules a call to MaintainCheckProc, which will eventually448* caused the postions and mapped states to be recalculated for all449* the maintained slaves of the master. Or, if the master window is450* being deleted then state is cleaned up.451*452*----------------------------------------------------------------------453*/454455static void456MaintainMasterProc(clientData, eventPtr)457ClientData clientData; /* Pointer to MaintainMaster structure458* for the master window. */459XEvent *eventPtr; /* Describes what just happened. */460{461MaintainMaster *masterPtr = (MaintainMaster *) clientData;462MaintainSlave *slavePtr;463int done;464465if ((eventPtr->type == ConfigureNotify)466|| (eventPtr->type == MapNotify)467|| (eventPtr->type == UnmapNotify)) {468if (!masterPtr->checkScheduled) {469masterPtr->checkScheduled = 1;470Tcl_DoWhenIdle(MaintainCheckProc, (ClientData) masterPtr);471}472} else if (eventPtr->type == DestroyNotify) {473/*474* Delete all of the state associated with this master, but475* be careful not to use masterPtr after the last slave is476* deleted, since its memory will have been freed.477*/478479done = 0;480do {481slavePtr = masterPtr->slavePtr;482if (slavePtr->nextPtr == NULL) {483done = 1;484}485Tk_UnmaintainGeometry(slavePtr->slave, slavePtr->master);486} while (!done);487}488}489490/*491*----------------------------------------------------------------------492*493* MaintainSlaveProc --494*495* This procedure is invoked by the Tk event dispatcher in496* response to StructureNotify events on a slave being managed497* by Tk_MaintainGeometry.498*499* Results:500* None.501*502* Side effects:503* If the event is a DestroyNotify event then the Maintain state504* and event handlers for this slave are deleted.505*506*----------------------------------------------------------------------507*/508509static void510MaintainSlaveProc(clientData, eventPtr)511ClientData clientData; /* Pointer to MaintainSlave structure512* for master-slave pair. */513XEvent *eventPtr; /* Describes what just happened. */514{515MaintainSlave *slavePtr = (MaintainSlave *) clientData;516517if (eventPtr->type == DestroyNotify) {518Tk_UnmaintainGeometry(slavePtr->slave, slavePtr->master);519}520}521522/*523*----------------------------------------------------------------------524*525* MaintainCheckProc --526*527* This procedure is invoked by the Tk event dispatcher as an528* idle handler, when a master or one of its ancestors has been529* reconfigured, mapped, or unmapped. Its job is to scan all of530* the slaves for the master and reposition them, map them, or531* unmap them as needed to maintain their geometry relative to532* the master.533*534* Results:535* None.536*537* Side effects:538* Slaves can get repositioned, mapped, or unmapped.539*540*----------------------------------------------------------------------541*/542543static void544MaintainCheckProc(clientData)545ClientData clientData; /* Pointer to MaintainMaster structure546* for the master window. */547{548MaintainMaster *masterPtr = (MaintainMaster *) clientData;549MaintainSlave *slavePtr;550Tk_Window ancestor, parent;551int x, y, map;552553masterPtr->checkScheduled = 0;554for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;555slavePtr = slavePtr->nextPtr) {556parent = Tk_Parent(slavePtr->slave);557x = slavePtr->x;558y = slavePtr->y;559map = 1;560for (ancestor = slavePtr->master; ; ancestor = Tk_Parent(ancestor)) {561if (!Tk_IsMapped(ancestor) && (ancestor != parent)) {562map = 0;563}564if (ancestor == parent) {565if ((x != Tk_X(slavePtr->slave))566|| (y != Tk_Y(slavePtr->slave))) {567Tk_MoveWindow(slavePtr->slave, x, y);568}569if (map) {570Tk_MapWindow(slavePtr->slave);571} else {572Tk_UnmapWindow(slavePtr->slave);573}574break;575}576x += Tk_X(ancestor) + Tk_Changes(ancestor)->border_width;577y += Tk_Y(ancestor) + Tk_Changes(ancestor)->border_width;578}579}580}581582583