home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tk8.0 / generic / tkWindow.c < prev   
Encoding:
C/C++ Source or Header  |  1997-08-15  |  78.7 KB  |  2,766 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tkWindow.c --
  3.  *
  4.  *    This file provides basic window-manipulation procedures,
  5.  *    which are equivalent to procedures in Xlib (and even
  6.  *    invoke them) but also maintain the local Tk_Window
  7.  *    structure.
  8.  *
  9.  * Copyright (c) 1989-1994 The Regents of the University of California.
  10.  * Copyright (c) 1994-1997 Sun Microsystems, Inc.
  11.  *
  12.  * See the file "license.terms" for information on usage and redistribution
  13.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14.  *
  15.  * SCCS: @(#) tkWindow.c 1.232 97/08/06 21:42:15
  16.  */
  17.  
  18. #include "tkPort.h"
  19. #include "tkInt.h"
  20.  
  21. /*
  22.  * Count of number of main windows currently open in this process.
  23.  */
  24.  
  25. static int numMainWindows;
  26.  
  27. /*
  28.  * First in list of all main windows managed by this process.
  29.  */
  30.  
  31. TkMainInfo *tkMainWindowList = NULL;
  32.  
  33. /*
  34.  * List of all displays currently in use.
  35.  */
  36.  
  37. TkDisplay *tkDisplayList = NULL;
  38.  
  39. /*
  40.  * Have statics in this module been initialized?
  41.  */
  42.  
  43. static int initialized = 0;
  44.  
  45. /*
  46.  * The variables below hold several uid's that are used in many places
  47.  * in the toolkit.
  48.  */
  49.  
  50. Tk_Uid tkDisabledUid = NULL;
  51. Tk_Uid tkActiveUid = NULL;
  52. Tk_Uid tkNormalUid = NULL;
  53.  
  54. /*
  55.  * Default values for "changes" and "atts" fields of TkWindows.  Note
  56.  * that Tk always requests all events for all windows, except StructureNotify
  57.  * events on internal windows:  these events are generated internally.
  58.  */
  59.  
  60. static XWindowChanges defChanges = {
  61.     0, 0, 1, 1, 0, 0, Above
  62. };
  63. #define ALL_EVENTS_MASK \
  64.     KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask| \
  65.     EnterWindowMask|LeaveWindowMask|PointerMotionMask|ExposureMask| \
  66.     VisibilityChangeMask|PropertyChangeMask|ColormapChangeMask
  67. static XSetWindowAttributes defAtts= {
  68.     None,            /* background_pixmap */
  69.     0,                /* background_pixel */
  70.     CopyFromParent,        /* border_pixmap */
  71.     0,                /* border_pixel */
  72.     NorthWestGravity,        /* bit_gravity */
  73.     NorthWestGravity,        /* win_gravity */
  74.     NotUseful,            /* backing_store */
  75.     (unsigned) ~0,        /* backing_planes */
  76.     0,                /* backing_pixel */
  77.     False,            /* save_under */
  78.     ALL_EVENTS_MASK,        /* event_mask */
  79.     0,                /* do_not_propagate_mask */
  80.     False,            /* override_redirect */
  81.     CopyFromParent,        /* colormap */
  82.     None            /* cursor */
  83. };
  84.  
  85. /*
  86.  * The following structure defines all of the commands supported by
  87.  * Tk, and the C procedures that execute them.
  88.  */
  89.  
  90. typedef struct {
  91.     char *name;            /* Name of command. */
  92.     Tcl_CmdProc *cmdProc;    /* Command's string-based procedure. */
  93.     Tcl_ObjCmdProc *objProc;    /* Command's object-based procedure. */
  94.     int isSafe;            /* If !0, this command will be exposed in
  95.                                  * a safe interpreter. Otherwise it will be
  96.                                  * hidden in a safe interpreter. */
  97. } TkCmd;
  98.  
  99. static TkCmd commands[] = {
  100.     /*
  101.      * Commands that are part of the intrinsics:
  102.      */
  103.  
  104.     {"bell",        Tk_BellCmd,        NULL,            0},
  105.     {"bind",        Tk_BindCmd,        NULL,            1},
  106.     {"bindtags",    Tk_BindtagsCmd,        NULL,            1},
  107.     {"clipboard",    Tk_ClipboardCmd,    NULL,            0},
  108.     {"destroy",        Tk_DestroyCmd,        NULL,            1},
  109.     {"event",        Tk_EventCmd,        NULL,            1},
  110.     {"focus",        Tk_FocusCmd,        NULL,            1},
  111.     {"font",        NULL,            Tk_FontObjCmd,        1},
  112.     {"grab",        Tk_GrabCmd,        NULL,            0},
  113.     {"grid",        Tk_GridCmd,        NULL,            1},
  114.     {"image",        Tk_ImageCmd,        NULL,            1},
  115.     {"lower",        Tk_LowerCmd,        NULL,            1},
  116.     {"option",        Tk_OptionCmd,        NULL,            1},
  117.     {"pack",        Tk_PackCmd,        NULL,            1},
  118.     {"place",        Tk_PlaceCmd,        NULL,            1},
  119.     {"raise",        Tk_RaiseCmd,        NULL,            1},
  120.     {"selection",    Tk_SelectionCmd,    NULL,            0},
  121.     {"tk",        NULL,            Tk_TkObjCmd,        0},
  122.     {"tkwait",        Tk_TkwaitCmd,        NULL,            1},
  123.     {"tk_chooseColor",  Tk_ChooseColorCmd,    NULL,            0},
  124.     {"tk_getOpenFile",  Tk_GetOpenFileCmd,    NULL,            0},
  125.     {"tk_getSaveFile",  Tk_GetSaveFileCmd,    NULL,            0},
  126.     {"tk_messageBox",   Tk_MessageBoxCmd,    NULL,            0},
  127.     {"update",        Tk_UpdateCmd,        NULL,            1},
  128.     {"winfo",        NULL,            Tk_WinfoObjCmd,        1},
  129.     {"wm",        Tk_WmCmd,        NULL,            0},
  130.  
  131.     /*
  132.      * Widget class commands.
  133.      */
  134.     {"button",        Tk_ButtonCmd,        NULL,            1},
  135.     {"canvas",        Tk_CanvasCmd,        NULL,            1},
  136.     {"checkbutton",    Tk_CheckbuttonCmd,    NULL,            1},
  137.     {"entry",        Tk_EntryCmd,        NULL,            1},
  138.     {"frame",        Tk_FrameCmd,        NULL,            1},
  139.     {"label",        Tk_LabelCmd,        NULL,            1},
  140.     {"listbox",        Tk_ListboxCmd,        NULL,            1},
  141.     {"menu",        Tk_MenuCmd,        NULL,            0},
  142.     {"menubutton",    Tk_MenubuttonCmd,    NULL,            1},
  143.     {"message",        Tk_MessageCmd,        NULL,            1},
  144.     {"radiobutton",    Tk_RadiobuttonCmd,    NULL,            1},
  145.     {"scale",        Tk_ScaleCmd,        NULL,            1},
  146.     {"scrollbar",    Tk_ScrollbarCmd,    NULL,            1},
  147.     {"text",        Tk_TextCmd,        NULL,            1},
  148.     {"toplevel",    Tk_ToplevelCmd,        NULL,            0},
  149.  
  150.     /*
  151.      * Misc.
  152.      */
  153.  
  154. #ifdef MAC_TCL
  155.     {"unsupported1",    TkUnsupported1Cmd,    NULL,            1},
  156. #endif
  157.     {(char *) NULL,    (int (*) _ANSI_ARGS_((ClientData, Tcl_Interp *, int, char **))) NULL, NULL, 0}
  158. };
  159.     
  160. /*
  161.  * The variables and table below are used to parse arguments from
  162.  * the "argv" variable in Tk_Init.
  163.  */
  164.  
  165. static int synchronize = 0;
  166. static char *name = NULL;
  167. static char *display = NULL;
  168. static char *geometry = NULL;
  169. static char *colormap = NULL;
  170. static char *use = NULL;
  171. static char *visual = NULL;
  172. static int rest = 0;
  173.  
  174. static Tk_ArgvInfo argTable[] = {
  175.     {"-colormap", TK_ARGV_STRING, (char *) NULL, (char *) &colormap,
  176.     "Colormap for main window"},
  177.     {"-display", TK_ARGV_STRING, (char *) NULL, (char *) &display,
  178.     "Display to use"},
  179.     {"-geometry", TK_ARGV_STRING, (char *) NULL, (char *) &geometry,
  180.     "Initial geometry for window"},
  181.     {"-name", TK_ARGV_STRING, (char *) NULL, (char *) &name,
  182.     "Name to use for application"},
  183.     {"-sync", TK_ARGV_CONSTANT, (char *) 1, (char *) &synchronize,
  184.     "Use synchronous mode for display server"},
  185.     {"-visual", TK_ARGV_STRING, (char *) NULL, (char *) &visual,
  186.     "Visual for main window"},
  187.     {"-use", TK_ARGV_STRING, (char *) NULL, (char *) &use,
  188.     "Id of window in which to embed application"},
  189.     {"--", TK_ARGV_REST, (char *) 1, (char *) &rest,
  190.     "Pass all remaining arguments through to script"},
  191.     {(char *) NULL, TK_ARGV_END, (char *) NULL, (char *) NULL,
  192.     (char *) NULL}
  193. };
  194.  
  195. /*
  196.  * Forward declarations to procedures defined later in this file:
  197.  */
  198.  
  199. static Tk_Window    CreateTopLevelWindow _ANSI_ARGS_((Tcl_Interp *interp,
  200.                 Tk_Window parent, char *name, char *screenName));
  201. static void        DeleteWindowsExitProc _ANSI_ARGS_((
  202.                 ClientData clientData));
  203. static TkDisplay *    GetScreen _ANSI_ARGS_((Tcl_Interp *interp,
  204.                 char *screenName, int *screenPtr));
  205. static int        Initialize _ANSI_ARGS_((Tcl_Interp *interp));
  206. static int        NameWindow _ANSI_ARGS_((Tcl_Interp *interp,
  207.                 TkWindow *winPtr, TkWindow *parentPtr,
  208.                 char *name));
  209. static void        OpenIM _ANSI_ARGS_((TkDisplay *dispPtr));
  210. static void        UnlinkWindow _ANSI_ARGS_((TkWindow *winPtr));
  211.  
  212. /*
  213.  *----------------------------------------------------------------------
  214.  *
  215.  * CreateTopLevelWindow --
  216.  *
  217.  *    Make a new window that will be at top-level (its parent will
  218.  *    be the root window of a screen).
  219.  *
  220.  * Results:
  221.  *    The return value is a token for the new window, or NULL if
  222.  *    an error prevented the new window from being created.  If
  223.  *    NULL is returned, an error message will be left in
  224.  *    interp->result.
  225.  *
  226.  * Side effects:
  227.  *    A new window structure is allocated locally.  An X
  228.  *    window is NOT initially created, but will be created
  229.  *    the first time the window is mapped.
  230.  *
  231.  *----------------------------------------------------------------------
  232.  */
  233.  
  234. static Tk_Window
  235. CreateTopLevelWindow(interp, parent, name, screenName)
  236.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  237.     Tk_Window parent;        /* Token for logical parent of new window
  238.                  * (used for naming, options, etc.).  May
  239.                  * be NULL. */
  240.     char *name;            /* Name for new window;  if parent is
  241.                  * non-NULL, must be unique among parent's
  242.                  * children. */
  243.     char *screenName;        /* Name of screen on which to create
  244.                  * window.  NULL means use DISPLAY environment
  245.                  * variable to determine.  Empty string means
  246.                  * use parent's screen, or DISPLAY if no
  247.                  * parent. */
  248. {
  249.     register TkWindow *winPtr;
  250.     register TkDisplay *dispPtr;
  251.     int screenId;
  252.  
  253.     if (!initialized) {
  254.     initialized = 1;
  255.     tkActiveUid = Tk_GetUid("active");
  256.     tkDisabledUid = Tk_GetUid("disabled");
  257.     tkNormalUid = Tk_GetUid("normal");
  258.  
  259.     /*
  260.      * Create built-in image types.
  261.      */
  262.     
  263.     Tk_CreateImageType(&tkBitmapImageType);
  264.     Tk_CreateImageType(&tkPhotoImageType);
  265.     
  266.     /*
  267.      * Create built-in photo image formats.
  268.      */
  269.     
  270.     Tk_CreatePhotoImageFormat(&tkImgFmtGIF);
  271.     Tk_CreatePhotoImageFormat(&tkImgFmtPPM);
  272.  
  273.     /*
  274.      * Create exit handler to delete all windows when the application
  275.      * exits.
  276.      */
  277.  
  278.     Tcl_CreateExitHandler(DeleteWindowsExitProc, (ClientData) NULL);
  279.     }
  280.  
  281.     if ((parent != NULL) && (screenName != NULL) && (screenName[0] == '\0')) {
  282.     dispPtr = ((TkWindow *) parent)->dispPtr;
  283.     screenId = Tk_ScreenNumber(parent);
  284.     } else {
  285.     dispPtr = GetScreen(interp, screenName, &screenId);
  286.     if (dispPtr == NULL) {
  287.         return (Tk_Window) NULL;
  288.     }
  289.     }
  290.  
  291.     winPtr = TkAllocWindow(dispPtr, screenId, (TkWindow *) parent);
  292.  
  293.     /*
  294.      * Force the window to use a border pixel instead of border pixmap. 
  295.      * This is needed for the case where the window doesn't use the
  296.      * default visual.  In this case, the default border is a pixmap
  297.      * inherited from the root window, which won't work because it will
  298.      * have the wrong visual.
  299.      */
  300.  
  301.     winPtr->dirtyAtts |= CWBorderPixel;
  302.  
  303.     /*
  304.      * (Need to set the TK_TOP_LEVEL flag immediately here;  otherwise
  305.      * Tk_DestroyWindow will core dump if it is called before the flag
  306.      * has been set.)
  307.      */
  308.  
  309.     winPtr->flags |= TK_TOP_LEVEL;
  310.  
  311.     if (parent != NULL) {
  312.         if (NameWindow(interp, winPtr, (TkWindow *) parent, name) != TCL_OK) {
  313.         Tk_DestroyWindow((Tk_Window) winPtr);
  314.         return (Tk_Window) NULL;
  315.     }
  316.     }
  317.     TkWmNewWindow(winPtr);
  318.  
  319.     return (Tk_Window) winPtr;
  320. }
  321.  
  322. /*
  323.  *----------------------------------------------------------------------
  324.  *
  325.  * GetScreen --
  326.  *
  327.  *    Given a string name for a display-plus-screen, find the
  328.  *    TkDisplay structure for the display and return the screen
  329.  *    number too.
  330.  *
  331.  * Results:
  332.  *    The return value is a pointer to information about the display,
  333.  *    or NULL if the display couldn't be opened.  In this case, an
  334.  *    error message is left in interp->result.  The location at
  335.  *    *screenPtr is overwritten with the screen number parsed from
  336.  *    screenName.
  337.  *
  338.  * Side effects:
  339.  *    A new connection is opened to the display if there is no
  340.  *    connection already.  A new TkDisplay data structure is also
  341.  *    setup, if necessary.
  342.  *
  343.  *----------------------------------------------------------------------
  344.  */
  345.  
  346. static TkDisplay *
  347. GetScreen(interp, screenName, screenPtr)
  348.     Tcl_Interp *interp;        /* Place to leave error message. */
  349.     char *screenName;        /* Name for screen.  NULL or empty means
  350.                  * use DISPLAY envariable. */
  351.     int *screenPtr;        /* Where to store screen number. */
  352. {
  353.     register TkDisplay *dispPtr;
  354.     char *p;
  355.     int screenId;
  356.     size_t length;
  357.  
  358.     /*
  359.      * Separate the screen number from the rest of the display
  360.      * name.  ScreenName is assumed to have the syntax
  361.      * <display>.<screen> with the dot and the screen being
  362.      * optional.
  363.      */
  364.  
  365.     screenName = TkGetDefaultScreenName(interp, screenName);
  366.     if (screenName == NULL) {
  367.     interp->result =
  368.         "no display name and no $DISPLAY environment variable";
  369.     return (TkDisplay *) NULL;
  370.     }
  371.     length = strlen(screenName);
  372.     screenId = 0;
  373.     p = screenName+length-1;
  374.     while (isdigit(UCHAR(*p)) && (p != screenName)) {
  375.     p--;
  376.     }
  377.     if ((*p == '.') && (p[1] != '\0')) {
  378.     length = p - screenName;
  379.     screenId = strtoul(p+1, (char **) NULL, 10);
  380.     }
  381.  
  382.     /*
  383.      * See if we already have a connection to this display.  If not,
  384.      * then open a new connection.
  385.      */
  386.  
  387.     for (dispPtr = tkDisplayList; ; dispPtr = dispPtr->nextPtr) {
  388.     if (dispPtr == NULL) {
  389.         dispPtr = TkpOpenDisplay(screenName);
  390.         if (dispPtr == NULL) {
  391.         Tcl_AppendResult(interp, "couldn't connect to display \"",
  392.             screenName, "\"", (char *) NULL);
  393.         return (TkDisplay *) NULL;
  394.         }
  395.         dispPtr->nextPtr = tkDisplayList;
  396.         dispPtr->name = (char *) ckalloc((unsigned) (length+1));
  397.         dispPtr->lastEventTime = CurrentTime;
  398.         strncpy(dispPtr->name, screenName, length);
  399.         dispPtr->name[length] = '\0';
  400.         dispPtr->bindInfoStale = 1;
  401.         dispPtr->modeModMask = 0;
  402.         dispPtr->metaModMask = 0;
  403.         dispPtr->altModMask = 0;
  404.         dispPtr->numModKeyCodes = 0;
  405.         dispPtr->modKeyCodes = NULL;
  406.         OpenIM(dispPtr);
  407.         dispPtr->errorPtr = NULL;
  408.         dispPtr->deleteCount = 0;
  409.         dispPtr->commTkwin = NULL;
  410.         dispPtr->selectionInfoPtr = NULL;
  411.         dispPtr->multipleAtom = None;
  412.         dispPtr->clipWindow = NULL;
  413.         dispPtr->clipboardActive = 0;
  414.         dispPtr->clipboardAppPtr = NULL;
  415.         dispPtr->clipTargetPtr = NULL;
  416.         dispPtr->atomInit = 0;
  417.         dispPtr->cursorFont = None;
  418.         dispPtr->grabWinPtr = NULL;
  419.         dispPtr->eventualGrabWinPtr = NULL;
  420.         dispPtr->buttonWinPtr = NULL;
  421.         dispPtr->serverWinPtr = NULL;
  422.         dispPtr->firstGrabEventPtr = NULL;
  423.         dispPtr->lastGrabEventPtr = NULL;
  424.         dispPtr->grabFlags = 0;
  425.         TkInitXId(dispPtr);
  426.         dispPtr->destroyCount = 0;
  427.         dispPtr->lastDestroyRequest = 0;
  428.         dispPtr->cmapPtr = NULL;
  429.         dispPtr->implicitWinPtr = NULL;
  430.         dispPtr->stressPtr = NULL;
  431.         dispPtr->delayedMotionPtr = NULL;
  432.         Tcl_InitHashTable(&dispPtr->winTable, TCL_ONE_WORD_KEYS);
  433.             dispPtr->refCount = 0;
  434.             
  435.         tkDisplayList = dispPtr;
  436.         break;
  437.     }
  438.     if ((strncmp(dispPtr->name, screenName, length) == 0)
  439.         && (dispPtr->name[length] == '\0')) {
  440.         break;
  441.     }
  442.     }
  443.     if (screenId >= ScreenCount(dispPtr->display)) {
  444.     sprintf(interp->result, "bad screen number \"%d\"", screenId);
  445.     return (TkDisplay *) NULL;
  446.     }
  447.     *screenPtr = screenId;
  448.     return dispPtr;
  449. }
  450.  
  451. /*
  452.  *----------------------------------------------------------------------
  453.  *
  454.  * TkGetDisplay --
  455.  *
  456.  *    Given an X display, TkGetDisplay returns the TkDisplay 
  457.  *      structure for the display.
  458.  *
  459.  * Results:
  460.  *    The return value is a pointer to information about the display,
  461.  *    or NULL if the display did not have a TkDisplay structure.
  462.  *
  463.  * Side effects:
  464.  *      None.
  465.  *
  466.  *----------------------------------------------------------------------
  467.  */
  468.  
  469. TkDisplay *
  470. TkGetDisplay(display)
  471.      Display *display;          /* X's display pointer */
  472. {
  473.     TkDisplay *dispPtr;
  474.  
  475.     for (dispPtr = tkDisplayList; dispPtr != NULL;
  476.         dispPtr = dispPtr->nextPtr) {
  477.     if (dispPtr->display == display) {
  478.         break;
  479.     }
  480.     }
  481.     return dispPtr;
  482. }
  483.  
  484. /*
  485.  *--------------------------------------------------------------
  486.  *
  487.  * TkAllocWindow --
  488.  *
  489.  *    This procedure creates and initializes a TkWindow structure.
  490.  *
  491.  * Results:
  492.  *    The return value is a pointer to the new window.
  493.  *
  494.  * Side effects:
  495.  *    A new window structure is allocated and all its fields are
  496.  *    initialized.
  497.  *
  498.  *--------------------------------------------------------------
  499.  */
  500.  
  501. TkWindow *
  502. TkAllocWindow(dispPtr, screenNum, parentPtr)
  503.     TkDisplay *dispPtr;        /* Display associated with new window. */
  504.     int screenNum;        /* Index of screen for new window. */
  505.     TkWindow *parentPtr;    /* Parent from which this window should
  506.                  * inherit visual information.  NULL means
  507.                  * use screen defaults instead of
  508.                  * inheriting. */
  509. {
  510.     register TkWindow *winPtr;
  511.  
  512.     winPtr = (TkWindow *) ckalloc(sizeof(TkWindow));
  513.     winPtr->display = dispPtr->display;
  514.     winPtr->dispPtr = dispPtr;
  515.     winPtr->screenNum = screenNum;
  516.     if ((parentPtr != NULL) && (parentPtr->display == winPtr->display)
  517.         && (parentPtr->screenNum == winPtr->screenNum)) {
  518.     winPtr->visual = parentPtr->visual;
  519.     winPtr->depth = parentPtr->depth;
  520.     } else {
  521.     winPtr->visual = DefaultVisual(dispPtr->display, screenNum);
  522.     winPtr->depth = DefaultDepth(dispPtr->display, screenNum);
  523.     }
  524.     winPtr->window = None;
  525.     winPtr->childList = NULL;
  526.     winPtr->lastChildPtr = NULL;
  527.     winPtr->parentPtr = NULL;
  528.     winPtr->nextPtr = NULL;
  529.     winPtr->mainPtr = NULL;
  530.     winPtr->pathName = NULL;
  531.     winPtr->nameUid = NULL;
  532.     winPtr->classUid = NULL;
  533.     winPtr->changes = defChanges;
  534.     winPtr->dirtyChanges = CWX|CWY|CWWidth|CWHeight|CWBorderWidth;
  535.     winPtr->atts = defAtts;
  536.     if ((parentPtr != NULL) && (parentPtr->display == winPtr->display)
  537.         && (parentPtr->screenNum == winPtr->screenNum)) {
  538.     winPtr->atts.colormap = parentPtr->atts.colormap;
  539.     } else {
  540.     winPtr->atts.colormap = DefaultColormap(dispPtr->display, screenNum);
  541.     }
  542.     winPtr->dirtyAtts = CWEventMask|CWColormap|CWBitGravity;
  543.     winPtr->flags = 0;
  544.     winPtr->handlerList = NULL;
  545. #ifdef TK_USE_INPUT_METHODS
  546.     winPtr->inputContext = NULL;
  547. #endif /* TK_USE_INPUT_METHODS */
  548.     winPtr->tagPtr = NULL;
  549.     winPtr->numTags = 0;
  550.     winPtr->optionLevel = -1;
  551.     winPtr->selHandlerList = NULL;
  552.     winPtr->geomMgrPtr = NULL;
  553.     winPtr->geomData = NULL;
  554.     winPtr->reqWidth = winPtr->reqHeight = 1;
  555.     winPtr->internalBorderWidth = 0;
  556.     winPtr->wmInfoPtr = NULL;
  557.     winPtr->classProcsPtr = NULL;
  558.     winPtr->instanceData = NULL;
  559.     winPtr->privatePtr = NULL;
  560.  
  561.     return winPtr;
  562. }
  563.  
  564. /*
  565.  *----------------------------------------------------------------------
  566.  *
  567.  * NameWindow --
  568.  *
  569.  *    This procedure is invoked to give a window a name and insert
  570.  *    the window into the hierarchy associated with a particular
  571.  *    application.
  572.  *
  573.  * Results:
  574.  *    A standard Tcl return value.
  575.  *
  576.  * Side effects:
  577.  *      See above.
  578.  *
  579.  *----------------------------------------------------------------------
  580.  */
  581.  
  582. static int
  583. NameWindow(interp, winPtr, parentPtr, name)
  584.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  585.     register TkWindow *winPtr;    /* Window that is to be named and inserted. */
  586.     TkWindow *parentPtr;    /* Pointer to logical parent for winPtr
  587.                  * (used for naming, options, etc.). */
  588.     char *name;            /* Name for winPtr;   must be unique among
  589.                  * parentPtr's children. */
  590. {
  591. #define FIXED_SIZE 200
  592.     char staticSpace[FIXED_SIZE];
  593.     char *pathName;
  594.     int new;
  595.     Tcl_HashEntry *hPtr;
  596.     int length1, length2;
  597.  
  598.     /*
  599.      * Setup all the stuff except name right away, then do the name stuff
  600.      * last.  This is so that if the name stuff fails, everything else
  601.      * will be properly initialized (needed to destroy the window cleanly
  602.      * after the naming failure).
  603.      */
  604.     winPtr->parentPtr = parentPtr;
  605.     winPtr->nextPtr = NULL;
  606.     if (parentPtr->childList == NULL) {
  607.     parentPtr->childList = winPtr;
  608.     } else {
  609.     parentPtr->lastChildPtr->nextPtr = winPtr;
  610.     }
  611.     parentPtr->lastChildPtr = winPtr;
  612.     winPtr->mainPtr = parentPtr->mainPtr;
  613.     winPtr->mainPtr->refCount++;
  614.     winPtr->nameUid = Tk_GetUid(name);
  615.  
  616.     /*
  617.      * Don't permit names that start with an upper-case letter:  this
  618.      * will just cause confusion with class names in the option database.
  619.      */
  620.  
  621.     if (isupper(UCHAR(name[0]))) {
  622.     Tcl_AppendResult(interp,
  623.         "window name starts with an upper-case letter: \"",
  624.         name, "\"", (char *) NULL);
  625.     return TCL_ERROR;
  626.     }
  627.  
  628.     /*
  629.      * To permit names of arbitrary length, must be prepared to malloc
  630.      * a buffer to hold the new path name.  To run fast in the common
  631.      * case where names are short, use a fixed-size buffer on the
  632.      * stack.
  633.      */
  634.  
  635.     length1 = strlen(parentPtr->pathName);
  636.     length2 = strlen(name);
  637.     if ((length1+length2+2) <= FIXED_SIZE) {
  638.     pathName = staticSpace;
  639.     } else {
  640.     pathName = (char *) ckalloc((unsigned) (length1+length2+2));
  641.     }
  642.     if (length1 == 1) {
  643.     pathName[0] = '.';
  644.     strcpy(pathName+1, name);
  645.     } else {
  646.     strcpy(pathName, parentPtr->pathName);
  647.     pathName[length1] = '.';
  648.     strcpy(pathName+length1+1, name);
  649.     }
  650.     hPtr = Tcl_CreateHashEntry(&parentPtr->mainPtr->nameTable, pathName, &new);
  651.     if (pathName != staticSpace) {
  652.     ckfree(pathName);
  653.     }
  654.     if (!new) {
  655.     Tcl_AppendResult(interp, "window name \"", name,
  656.         "\" already exists in parent", (char *) NULL);
  657.     return TCL_ERROR;
  658.     }
  659.     Tcl_SetHashValue(hPtr, winPtr);
  660.     winPtr->pathName = Tcl_GetHashKey(&parentPtr->mainPtr->nameTable, hPtr);
  661.     return TCL_OK;
  662. }
  663.  
  664. /*
  665.  *----------------------------------------------------------------------
  666.  *
  667.  * TkCreateMainWindow --
  668.  *
  669.  *    Make a new main window.  A main window is a special kind of
  670.  *    top-level window used as the outermost window in an
  671.  *    application.
  672.  *
  673.  * Results:
  674.  *    The return value is a token for the new window, or NULL if
  675.  *    an error prevented the new window from being created.  If
  676.  *    NULL is returned, an error message will be left in
  677.  *    interp->result.
  678.  *
  679.  * Side effects:
  680.  *    A new window structure is allocated locally;  "interp" is
  681.  *    associated with the window and registered for "send" commands
  682.  *    under "baseName".  BaseName may be extended with an instance
  683.  *    number in the form "#2" if necessary to make it globally
  684.  *    unique.  Tk-related commands are bound into interp.
  685.  *
  686.  *----------------------------------------------------------------------
  687.  */
  688.  
  689. Tk_Window
  690. TkCreateMainWindow(interp, screenName, baseName)
  691.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  692.     char *screenName;        /* Name of screen on which to create
  693.                  * window.  Empty or NULL string means
  694.                  * use DISPLAY environment variable. */
  695.     char *baseName;        /* Base name for application;  usually of the
  696.                  * form "prog instance". */
  697. {
  698.     Tk_Window tkwin;
  699.     int dummy;
  700.     int isSafe;
  701.     Tcl_HashEntry *hPtr;
  702.     register TkMainInfo *mainPtr;
  703.     register TkWindow *winPtr;
  704.     register TkCmd *cmdPtr;
  705.     
  706.     /*
  707.      * Panic if someone updated the TkWindow structure without
  708.      * also updating the Tk_FakeWin structure (or vice versa).
  709.      */
  710.  
  711.     if (sizeof(TkWindow) != sizeof(Tk_FakeWin)) {
  712.     panic("TkWindow and Tk_FakeWin are not the same size");
  713.     }
  714.  
  715.     /*
  716.      * Create the basic TkWindow structure.
  717.      */
  718.  
  719.     tkwin = CreateTopLevelWindow(interp, (Tk_Window) NULL, baseName,
  720.         screenName);
  721.     if (tkwin == NULL) {
  722.     return NULL;
  723.     }
  724.     
  725.     /*
  726.      * Create the TkMainInfo structure for this application, and set
  727.      * up name-related information for the new window.
  728.      */
  729.  
  730.     winPtr = (TkWindow *) tkwin;
  731.     mainPtr = (TkMainInfo *) ckalloc(sizeof(TkMainInfo));
  732.     mainPtr->winPtr = winPtr;
  733.     mainPtr->refCount = 1;
  734.     mainPtr->interp = interp;
  735.     Tcl_InitHashTable(&mainPtr->nameTable, TCL_STRING_KEYS);
  736.     TkBindInit(mainPtr);
  737.     TkFontPkgInit(mainPtr);
  738.     mainPtr->focusPtr = NULL;
  739.     mainPtr->focusWinPtr = NULL;
  740.     mainPtr->focusSerial = 0;
  741.     mainPtr->focusOnMapPtr = NULL;
  742.     mainPtr->forceFocus = 0;
  743.     mainPtr->optionRootPtr = NULL;
  744.     Tcl_InitHashTable(&mainPtr->imageTable, TCL_STRING_KEYS);
  745.     mainPtr->strictMotif = 0;
  746.     if (Tcl_LinkVar(interp, "tk_strictMotif", (char *) &mainPtr->strictMotif,
  747.         TCL_LINK_BOOLEAN) != TCL_OK) {
  748.     Tcl_ResetResult(interp);
  749.     }
  750.     mainPtr->nextPtr = tkMainWindowList;
  751.     tkMainWindowList = mainPtr;
  752.     winPtr->mainPtr = mainPtr;
  753.     hPtr = Tcl_CreateHashEntry(&mainPtr->nameTable, ".", &dummy);
  754.     Tcl_SetHashValue(hPtr, winPtr);
  755.     winPtr->pathName = Tcl_GetHashKey(&mainPtr->nameTable, hPtr);
  756.  
  757.     /*
  758.      * We have just created another Tk application; increment the refcount
  759.      * on the display pointer.
  760.      */
  761.  
  762.     winPtr->dispPtr->refCount++;
  763.  
  764.     /*
  765.      * Register the interpreter for "send" purposes.
  766.      */
  767.  
  768.     winPtr->nameUid = Tk_GetUid(Tk_SetAppName(tkwin, baseName));
  769.  
  770.     /*
  771.      * Bind in Tk's commands.
  772.      */
  773.  
  774.     isSafe = Tcl_IsSafe(interp);
  775.     for (cmdPtr = commands; cmdPtr->name != NULL; cmdPtr++) {
  776.     if ((cmdPtr->cmdProc == NULL) && (cmdPtr->objProc == NULL)) {
  777.         panic("TkCreateMainWindow: builtin command with NULL string and object procs");
  778.     }
  779.     if (cmdPtr->cmdProc != NULL) {
  780.         Tcl_CreateCommand(interp, cmdPtr->name, cmdPtr->cmdProc,
  781.             (ClientData) tkwin, (void (*) _ANSI_ARGS_((ClientData))) NULL);
  782.     } else {
  783.         Tcl_CreateObjCommand(interp, cmdPtr->name, cmdPtr->objProc,
  784.             (ClientData) tkwin, NULL);
  785.     }
  786.         if (isSafe) {
  787.             if (!(cmdPtr->isSafe)) {
  788.                 Tcl_HideCommand(interp, cmdPtr->name, cmdPtr->name);
  789.             }
  790.         }
  791.     }
  792.  
  793.     /*
  794.      * Set variables for the intepreter.
  795.      */
  796.  
  797.     Tcl_SetVar(interp, "tk_patchLevel", TK_PATCH_LEVEL, TCL_GLOBAL_ONLY);
  798.     Tcl_SetVar(interp, "tk_version", TK_VERSION, TCL_GLOBAL_ONLY);
  799.  
  800.     numMainWindows++;
  801.     return tkwin;
  802. }
  803.  
  804. /*
  805.  *--------------------------------------------------------------
  806.  *
  807.  * Tk_CreateWindow --
  808.  *
  809.  *    Create a new internal or top-level window as a child of an
  810.  *    existing window.
  811.  *
  812.  * Results:
  813.  *    The return value is a token for the new window.  This
  814.  *    is not the same as X's token for the window.  If an error
  815.  *    occurred in creating the window (e.g. no such display or
  816.  *    screen), then an error message is left in interp->result and
  817.  *    NULL is returned.
  818.  *
  819.  * Side effects:
  820.  *    A new window structure is allocated locally.  An X
  821.  *    window is not initially created, but will be created
  822.  *    the first time the window is mapped.
  823.  *
  824.  *--------------------------------------------------------------
  825.  */
  826.  
  827. Tk_Window
  828. Tk_CreateWindow(interp, parent, name, screenName)
  829.     Tcl_Interp *interp;        /* Interpreter to use for error reporting.
  830.                  * Interp->result is assumed to be
  831.                  * initialized by the caller. */
  832.     Tk_Window parent;        /* Token for parent of new window. */
  833.     char *name;            /* Name for new window.  Must be unique
  834.                  * among parent's children. */
  835.     char *screenName;        /* If NULL, new window will be internal on
  836.                  * same screen as its parent.  If non-NULL,
  837.                  * gives name of screen on which to create
  838.                  * new window;  window will be a top-level
  839.                  * window. */
  840. {
  841.     TkWindow *parentPtr = (TkWindow *) parent;
  842.     TkWindow *winPtr;
  843.  
  844.     if ((parentPtr != NULL) && (parentPtr->flags & TK_ALREADY_DEAD)) {
  845.     Tcl_AppendResult(interp,
  846.         "can't create window: parent has been destroyed",
  847.         (char *) NULL);
  848.     return NULL;
  849.     } else if ((parentPtr != NULL) &&
  850.         (parentPtr->flags & TK_CONTAINER)) {
  851.     Tcl_AppendResult(interp,
  852.         "can't create window: its parent has -container = yes",
  853.         (char *) NULL);
  854.     return NULL;
  855.     }
  856.     if (screenName == NULL) {
  857.     winPtr = TkAllocWindow(parentPtr->dispPtr, parentPtr->screenNum,
  858.         parentPtr);
  859.     if (NameWindow(interp, winPtr, parentPtr, name) != TCL_OK) {
  860.         Tk_DestroyWindow((Tk_Window) winPtr);
  861.         return NULL;
  862.     } else {
  863.             return (Tk_Window) winPtr;
  864.     }
  865.     } else {
  866.     return CreateTopLevelWindow(interp, parent, name, screenName);
  867.     }
  868. }
  869.  
  870. /*
  871.  *----------------------------------------------------------------------
  872.  *
  873.  * Tk_CreateWindowFromPath --
  874.  *
  875.  *    This procedure is similar to Tk_CreateWindow except that
  876.  *    it uses a path name to create the window, rather than a
  877.  *    parent and a child name.
  878.  *
  879.  * Results:
  880.  *    The return value is a token for the new window.  This
  881.  *    is not the same as X's token for the window.  If an error
  882.  *    occurred in creating the window (e.g. no such display or
  883.  *    screen), then an error message is left in interp->result and
  884.  *    NULL is returned.
  885.  *
  886.  * Side effects:
  887.  *    A new window structure is allocated locally.  An X
  888.  *    window is not initially created, but will be created
  889.  *    the first time the window is mapped.
  890.  *
  891.  *----------------------------------------------------------------------
  892.  */
  893.  
  894. Tk_Window
  895. Tk_CreateWindowFromPath(interp, tkwin, pathName, screenName)
  896.     Tcl_Interp *interp;        /* Interpreter to use for error reporting.
  897.                  * Interp->result is assumed to be
  898.                  * initialized by the caller. */
  899.     Tk_Window tkwin;        /* Token for any window in application
  900.                  * that is to contain new window. */
  901.     char *pathName;        /* Path name for new window within the
  902.                  * application of tkwin.  The parent of
  903.                  * this window must already exist, but
  904.                  * the window itself must not exist. */
  905.     char *screenName;        /* If NULL, new window will be on same
  906.                  * screen as its parent.  If non-NULL,
  907.                  * gives name of screen on which to create
  908.                  * new window;  window will be a top-level
  909.                  * window. */
  910. {
  911. #define FIXED_SPACE 5
  912.     char fixedSpace[FIXED_SPACE+1];
  913.     char *p;
  914.     Tk_Window parent;
  915.     int numChars;
  916.  
  917.     /*
  918.      * Strip the parent's name out of pathName (it's everything up
  919.      * to the last dot).  There are two tricky parts: (a) must
  920.      * copy the parent's name somewhere else to avoid modifying
  921.      * the pathName string (for large names, space for the copy
  922.      * will have to be malloc'ed);  (b) must special-case the
  923.      * situation where the parent is ".".
  924.      */
  925.  
  926.     p = strrchr(pathName, '.');
  927.     if (p == NULL) {
  928.     Tcl_AppendResult(interp, "bad window path name \"", pathName,
  929.         "\"", (char *) NULL);
  930.     return NULL;
  931.     }
  932.     numChars = p-pathName;
  933.     if (numChars > FIXED_SPACE) {
  934.     p = (char *) ckalloc((unsigned) (numChars+1));
  935.     } else {
  936.     p = fixedSpace;
  937.     }
  938.     if (numChars == 0) {
  939.     *p = '.';
  940.     p[1] = '\0';
  941.     } else {
  942.     strncpy(p, pathName, (size_t) numChars);
  943.     p[numChars] = '\0';
  944.     }
  945.  
  946.     /*
  947.      * Find the parent window.
  948.      */
  949.  
  950.     parent = Tk_NameToWindow(interp, p, tkwin);
  951.     if (p != fixedSpace) {
  952.         ckfree(p);
  953.     }
  954.     if (parent == NULL) {
  955.     return NULL;
  956.     }
  957.     if (((TkWindow *) parent)->flags & TK_ALREADY_DEAD) {
  958.     Tcl_AppendResult(interp, 
  959.         "can't create window: parent has been destroyed", (char *) NULL);
  960.     return NULL;
  961.     } else if (((TkWindow *) parent)->flags & TK_CONTAINER) {
  962.     Tcl_AppendResult(interp, 
  963.         "can't create window: its parent has -container = yes",
  964.         (char *) NULL);
  965.     return NULL;
  966.     }
  967.  
  968.     /*
  969.      * Create the window.
  970.      */
  971.  
  972.     if (screenName == NULL) {
  973.     TkWindow *parentPtr = (TkWindow *) parent;
  974.     TkWindow *winPtr;
  975.  
  976.     winPtr = TkAllocWindow(parentPtr->dispPtr, parentPtr->screenNum,
  977.         parentPtr);
  978.     if (NameWindow(interp, winPtr, parentPtr, pathName+numChars+1)
  979.         != TCL_OK) {
  980.         Tk_DestroyWindow((Tk_Window) winPtr);
  981.         return NULL;
  982.     } else {
  983.         return (Tk_Window) winPtr;
  984.     }
  985.     } else {
  986.     return CreateTopLevelWindow(interp, parent, pathName+numChars+1,
  987.         screenName);
  988.     }
  989. }
  990.  
  991. /*
  992.  *--------------------------------------------------------------
  993.  *
  994.  * Tk_DestroyWindow --
  995.  *
  996.  *    Destroy an existing window.  After this call, the caller
  997.  *    should never again use the token.
  998.  *
  999.  * Results:
  1000.  *    None.
  1001.  *
  1002.  * Side effects:
  1003.  *    The window is deleted, along with all of its children.
  1004.  *    Relevant callback procedures are invoked.
  1005.  *
  1006.  *--------------------------------------------------------------
  1007.  */
  1008.  
  1009. void
  1010. Tk_DestroyWindow(tkwin)
  1011.     Tk_Window tkwin;        /* Window to destroy. */
  1012. {
  1013.     TkWindow *winPtr = (TkWindow *) tkwin;
  1014.     TkDisplay *dispPtr = winPtr->dispPtr;
  1015.     XEvent event;
  1016.  
  1017.     if (winPtr->flags & TK_ALREADY_DEAD) {
  1018.     /*
  1019.      * A destroy event binding caused the window to be destroyed
  1020.      * again.  Ignore the request.
  1021.      */
  1022.  
  1023.     return;
  1024.     }
  1025.     winPtr->flags |= TK_ALREADY_DEAD;
  1026.  
  1027.     /*
  1028.      * Some cleanup needs to be done immediately, rather than later,
  1029.      * because it needs information that will be destoyed before we
  1030.      * get to the main cleanup point.  For example, TkFocusDeadWindow
  1031.      * needs to access the parentPtr field from a window, but if
  1032.      * a Destroy event handler deletes the window's parent this
  1033.      * field will be NULL before the main cleanup point is reached.
  1034.      */
  1035.  
  1036.     TkFocusDeadWindow(winPtr);
  1037.  
  1038.     /*
  1039.      * If this is a main window, remove it from the list of main
  1040.      * windows.  This needs to be done now (rather than later with
  1041.      * all the other main window cleanup) to handle situations where
  1042.      * a destroy binding for a window calls "exit".  In this case
  1043.      * the child window cleanup isn't complete when exit is called,
  1044.      * so the reference count of its application doesn't go to zero
  1045.      * when exit calls Tk_DestroyWindow on ".", so the main window
  1046.      * doesn't get removed from the list and exit loops infinitely.
  1047.      * Even worse, if "destroy ." is called by the destroy binding
  1048.      * before calling "exit", "exit" will attempt to destroy
  1049.      * mainPtr->winPtr, which no longer exists, and there may be a
  1050.      * core dump.
  1051.      *
  1052.      * Also decrement the display refcount so that if this is the
  1053.      * last Tk application in this process on this display, the display
  1054.      * can be closed and its data structures deleted.
  1055.      */
  1056.  
  1057.     if (winPtr->mainPtr->winPtr == winPtr) {
  1058.         dispPtr->refCount--;
  1059.     if (tkMainWindowList == winPtr->mainPtr) {
  1060.         tkMainWindowList = winPtr->mainPtr->nextPtr;
  1061.     } else {
  1062.         TkMainInfo *prevPtr;
  1063.  
  1064.         for (prevPtr = tkMainWindowList;
  1065.             prevPtr->nextPtr != winPtr->mainPtr;
  1066.             prevPtr = prevPtr->nextPtr) {
  1067.         /* Empty loop body. */
  1068.         }
  1069.         prevPtr->nextPtr = winPtr->mainPtr->nextPtr;
  1070.     }
  1071.     numMainWindows--;
  1072.     }
  1073.  
  1074.     /*
  1075.      * Recursively destroy children.
  1076.      */
  1077.  
  1078.     dispPtr->destroyCount++;
  1079.     while (winPtr->childList != NULL) {
  1080.     TkWindow *childPtr;
  1081.     childPtr = winPtr->childList;
  1082.     childPtr->flags |= TK_DONT_DESTROY_WINDOW;
  1083.     Tk_DestroyWindow((Tk_Window) childPtr);
  1084.     if (winPtr->childList == childPtr) {
  1085.         /*
  1086.          * The child didn't remove itself from the child list, so
  1087.          * let's remove it here.  This can happen in some strange
  1088.          * conditions, such as when a Delete event handler for a
  1089.          * window deletes the window's parent.
  1090.          */
  1091.  
  1092.         winPtr->childList = childPtr->nextPtr;
  1093.         childPtr->parentPtr = NULL;
  1094.     }
  1095.     }
  1096.     if ((winPtr->flags & (TK_CONTAINER|TK_BOTH_HALVES))
  1097.         == (TK_CONTAINER|TK_BOTH_HALVES)) {
  1098.     /*
  1099.      * This is the container for an embedded application, and
  1100.      * the embedded application is also in this process.  Delete
  1101.      * the embedded window in-line here, for the same reasons we
  1102.      * delete children in-line (otherwise, for example, the Tk
  1103.      * window may appear to exist even though its X window is
  1104.      * gone; this could cause errors).  Special note: it's possible
  1105.      * that the embedded window has already been deleted, in which
  1106.      * case TkpGetOtherWindow will return NULL.
  1107.      */
  1108.  
  1109.     TkWindow *childPtr;
  1110.     childPtr = TkpGetOtherWindow(winPtr);
  1111.     if (childPtr != NULL) {
  1112.         childPtr->flags |= TK_DONT_DESTROY_WINDOW;
  1113.         Tk_DestroyWindow((Tk_Window) childPtr);
  1114.     }
  1115.     }
  1116.  
  1117.     /*
  1118.      * Generate a DestroyNotify event.  In order for the DestroyNotify
  1119.      * event to be processed correctly, need to make sure the window
  1120.      * exists.  This is a bit of a kludge, and may be unnecessarily
  1121.      * expensive, but without it no event handlers will get called for
  1122.      * windows that don't exist yet.
  1123.      *
  1124.      * Note: if the window's pathName is NULL it means that the window
  1125.      * was not successfully initialized in the first place, so we should
  1126.      * not make the window exist or generate the event.
  1127.      */
  1128.  
  1129.     if (winPtr->pathName != NULL) {
  1130.     if (winPtr->window == None) {
  1131.         Tk_MakeWindowExist(tkwin);
  1132.     }
  1133.     event.type = DestroyNotify;
  1134.     event.xdestroywindow.serial =
  1135.         LastKnownRequestProcessed(winPtr->display);
  1136.     event.xdestroywindow.send_event = False;
  1137.     event.xdestroywindow.display = winPtr->display;
  1138.     event.xdestroywindow.event = winPtr->window;
  1139.     event.xdestroywindow.window = winPtr->window;
  1140.     Tk_HandleEvent(&event);
  1141.     }
  1142.  
  1143.     /*
  1144.      * Cleanup the data structures associated with this window.
  1145.      */
  1146.  
  1147.     if (winPtr->flags & TK_TOP_LEVEL) {
  1148.     TkWmDeadWindow(winPtr);
  1149.     } else if (winPtr->flags & TK_WM_COLORMAP_WINDOW) {
  1150.     TkWmRemoveFromColormapWindows(winPtr);
  1151.     }
  1152.     if (winPtr->window != None) {
  1153. #if defined(MAC_TCL) || defined(__WIN32__)
  1154.     XDestroyWindow(winPtr->display, winPtr->window);
  1155. #else
  1156.     if ((winPtr->flags & TK_TOP_LEVEL)
  1157.         || !(winPtr->flags & TK_DONT_DESTROY_WINDOW)) {
  1158.         /*
  1159.          * The parent has already been destroyed and this isn't
  1160.          * a top-level window, so this window will be destroyed
  1161.          * implicitly when the parent's X window is destroyed;
  1162.          * it's much faster not to do an explicit destroy of this
  1163.          * X window.
  1164.          */
  1165.  
  1166.         dispPtr->lastDestroyRequest = NextRequest(winPtr->display);
  1167.         XDestroyWindow(winPtr->display, winPtr->window);
  1168.     }
  1169. #endif
  1170.     TkFreeWindowId(dispPtr, winPtr->window);
  1171.     Tcl_DeleteHashEntry(Tcl_FindHashEntry(&dispPtr->winTable,
  1172.         (char *) winPtr->window));
  1173.     winPtr->window = None;
  1174.     }
  1175.     dispPtr->destroyCount--;
  1176.     UnlinkWindow(winPtr);
  1177.     TkEventDeadWindow(winPtr);
  1178.     TkBindDeadWindow(winPtr);
  1179. #ifdef TK_USE_INPUT_METHODS
  1180.     if (winPtr->inputContext != NULL) {
  1181.     XDestroyIC(winPtr->inputContext);
  1182.     }
  1183. #endif /* TK_USE_INPUT_METHODS */
  1184.     if (winPtr->tagPtr != NULL) {
  1185.     TkFreeBindingTags(winPtr);
  1186.     }
  1187.     TkOptionDeadWindow(winPtr);
  1188.     TkSelDeadWindow(winPtr);
  1189.     TkGrabDeadWindow(winPtr);
  1190.     if (winPtr->mainPtr != NULL) {
  1191.     if (winPtr->pathName != NULL) {
  1192.         Tk_DeleteAllBindings(winPtr->mainPtr->bindingTable,
  1193.             (ClientData) winPtr->pathName);
  1194.         Tcl_DeleteHashEntry(Tcl_FindHashEntry(&winPtr->mainPtr->nameTable,
  1195.             winPtr->pathName));
  1196.     }
  1197.     winPtr->mainPtr->refCount--;
  1198.     if (winPtr->mainPtr->refCount == 0) {
  1199.         register TkCmd *cmdPtr;
  1200.  
  1201.         /*
  1202.          * We just deleted the last window in the application.  Delete
  1203.          * the TkMainInfo structure too and replace all of Tk's commands
  1204.          * with dummy commands that return errors.  Also delete the
  1205.          * "send" command to unregister the interpreter.
  1206.              *
  1207.              * NOTE: Only replace the commands it if the interpreter is
  1208.              * not being deleted. If it *is*, the interpreter cleanup will
  1209.              * do all the needed work.
  1210.          */
  1211.  
  1212.             if ((winPtr->mainPtr->interp != NULL) &&
  1213.                     (!Tcl_InterpDeleted(winPtr->mainPtr->interp))) {
  1214.                 for (cmdPtr = commands; cmdPtr->name != NULL; cmdPtr++) {
  1215.                     Tcl_CreateCommand(winPtr->mainPtr->interp, cmdPtr->name,
  1216.                             TkDeadAppCmd, (ClientData) NULL,
  1217.                             (void (*) _ANSI_ARGS_((ClientData))) NULL);
  1218.                 }
  1219.                 Tcl_CreateCommand(winPtr->mainPtr->interp, "send",
  1220.                         TkDeadAppCmd, (ClientData) NULL, 
  1221.                         (void (*) _ANSI_ARGS_((ClientData))) NULL);
  1222.                 Tcl_UnlinkVar(winPtr->mainPtr->interp, "tk_strictMotif");
  1223.             }
  1224.                 
  1225.         Tcl_DeleteHashTable(&winPtr->mainPtr->nameTable);
  1226.         TkBindFree(winPtr->mainPtr);
  1227.         TkFontPkgFree(winPtr->mainPtr);
  1228.         TkDeleteAllImages(winPtr->mainPtr);
  1229.  
  1230.             /*
  1231.              * When embedding Tk into other applications, make sure 
  1232.              * that all destroy events reach the server. Otherwise
  1233.              * the embedding application may also attempt to destroy
  1234.              * the windows, resulting in an X error
  1235.              */
  1236.  
  1237.             if (winPtr->flags & TK_EMBEDDED) {
  1238.                 XSync(winPtr->display,False) ; 
  1239.             }
  1240.         ckfree((char *) winPtr->mainPtr);
  1241.  
  1242.             /*
  1243.              * If no other applications are using the display, close the
  1244.              * display now and relinquish its data structures.
  1245.              */
  1246.             
  1247.             if (dispPtr->refCount <= 0) {
  1248. #ifdef    NOT_YET
  1249.                 /*
  1250.                  * I have disabled this code because on Windows there are
  1251.                  * still order dependencies in close-down. All displays
  1252.                  * and resources will get closed down properly anyway at
  1253.                  * exit, through the exit handler.
  1254.                  */
  1255.                 
  1256.                 TkDisplay *theDispPtr, *backDispPtr;
  1257.                 
  1258.                 /*
  1259.                  * Splice this display out of the list of displays.
  1260.                  */
  1261.                 
  1262.                 for (theDispPtr = tkDisplayList, backDispPtr = NULL;
  1263.                          (theDispPtr != winPtr->dispPtr) &&
  1264.                              (theDispPtr != NULL);
  1265.                          theDispPtr = theDispPtr->nextPtr) {
  1266.                     backDispPtr = theDispPtr;
  1267.                 }
  1268.                 if (theDispPtr == NULL) {
  1269.                     panic("could not find display to close!");
  1270.                 }
  1271.                 if (backDispPtr == NULL) {
  1272.                     tkDisplayList = theDispPtr->nextPtr;
  1273.                 } else {
  1274.                     backDispPtr->nextPtr = theDispPtr->nextPtr;
  1275.                 }
  1276.                 
  1277.                 /*
  1278.                  * Found and spliced it out, now actually do the cleanup.
  1279.                  */
  1280.                 
  1281.                 if (dispPtr->name != NULL) {
  1282.                     ckfree(dispPtr->name);
  1283.                 }
  1284.                 
  1285.                 Tcl_DeleteHashTable(&(dispPtr->winTable));
  1286.  
  1287.         /*
  1288.                  * Cannot yet close the display because we still have
  1289.                  * order of deletion problems. Defer until exit handling
  1290.                  * instead. At that time, the display will cleanly shut
  1291.                  * down (hopefully..). (JYL)
  1292.                  */
  1293.  
  1294.                 TkpCloseDisplay(dispPtr);
  1295.  
  1296.                 /*
  1297.                  * There is lots more to clean up, we leave it at this for
  1298.                  * the time being.
  1299.                  */
  1300. #endif
  1301.             }
  1302.     }
  1303.     }
  1304.     ckfree((char *) winPtr);
  1305. }
  1306.  
  1307. /*
  1308.  *--------------------------------------------------------------
  1309.  *
  1310.  * Tk_MapWindow --
  1311.  *
  1312.  *    Map a window within its parent.  This may require the
  1313.  *    window and/or its parents to actually be created.
  1314.  *
  1315.  * Results:
  1316.  *    None.
  1317.  *
  1318.  * Side effects:
  1319.  *    The given window will be mapped.  Windows may also
  1320.  *    be created.
  1321.  *
  1322.  *--------------------------------------------------------------
  1323.  */
  1324.  
  1325. void
  1326. Tk_MapWindow(tkwin)
  1327.     Tk_Window tkwin;        /* Token for window to map. */
  1328. {
  1329.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1330.     XEvent event;
  1331.  
  1332.     if (winPtr->flags & TK_MAPPED) {
  1333.     return;
  1334.     }
  1335.     if (winPtr->window == None) {
  1336.     Tk_MakeWindowExist(tkwin);
  1337.     }
  1338.     if (winPtr->flags & TK_TOP_LEVEL) {
  1339.     /*
  1340.      * Lots of special processing has to be done for top-level
  1341.      * windows.  Let tkWm.c handle everything itself.
  1342.      */
  1343.  
  1344.     TkWmMapWindow(winPtr);
  1345.     return;
  1346.     }
  1347.     winPtr->flags |= TK_MAPPED;
  1348.     XMapWindow(winPtr->display, winPtr->window);
  1349.     event.type = MapNotify;
  1350.     event.xmap.serial = LastKnownRequestProcessed(winPtr->display);
  1351.     event.xmap.send_event = False;
  1352.     event.xmap.display = winPtr->display;
  1353.     event.xmap.event = winPtr->window;
  1354.     event.xmap.window = winPtr->window;
  1355.     event.xmap.override_redirect = winPtr->atts.override_redirect;
  1356.     Tk_HandleEvent(&event);
  1357. }
  1358.  
  1359. /*
  1360.  *--------------------------------------------------------------
  1361.  *
  1362.  * Tk_MakeWindowExist --
  1363.  *
  1364.  *    Ensure that a particular window actually exists.  This
  1365.  *    procedure shouldn't normally need to be invoked from
  1366.  *    outside the Tk package, but may be needed if someone
  1367.  *    wants to manipulate a window before mapping it.
  1368.  *
  1369.  * Results:
  1370.  *    None.
  1371.  *
  1372.  * Side effects:
  1373.  *    When the procedure returns, the X window associated with
  1374.  *    tkwin is guaranteed to exist.  This may require the
  1375.  *    window's ancestors to be created also.
  1376.  *
  1377.  *--------------------------------------------------------------
  1378.  */
  1379.  
  1380. void
  1381. Tk_MakeWindowExist(tkwin)
  1382.     Tk_Window tkwin;        /* Token for window. */
  1383. {
  1384.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1385.     TkWindow *winPtr2;
  1386.     Window parent;
  1387.     Tcl_HashEntry *hPtr;
  1388.     int new;
  1389.  
  1390.     if (winPtr->window != None) {
  1391.     return;
  1392.     }
  1393.  
  1394.     if ((winPtr->parentPtr == NULL) || (winPtr->flags & TK_TOP_LEVEL)) {
  1395.     parent = XRootWindow(winPtr->display, winPtr->screenNum);
  1396.     } else {
  1397.     if (winPtr->parentPtr->window == None) {
  1398.         Tk_MakeWindowExist((Tk_Window) winPtr->parentPtr);
  1399.     }
  1400.     parent = winPtr->parentPtr->window;
  1401.     }
  1402.  
  1403.     if (winPtr->classProcsPtr != NULL
  1404.         && winPtr->classProcsPtr->createProc != NULL) {
  1405.     winPtr->window = (*winPtr->classProcsPtr->createProc)(tkwin, parent,
  1406.         winPtr->instanceData);
  1407.     } else {
  1408.     winPtr->window = TkpMakeWindow(winPtr, parent);
  1409.     }
  1410.  
  1411.     hPtr = Tcl_CreateHashEntry(&winPtr->dispPtr->winTable,
  1412.         (char *) winPtr->window, &new);
  1413.     Tcl_SetHashValue(hPtr, winPtr);
  1414.     winPtr->dirtyAtts = 0;
  1415.     winPtr->dirtyChanges = 0;
  1416. #ifdef TK_USE_INPUT_METHODS
  1417.     winPtr->inputContext = NULL;
  1418. #endif /* TK_USE_INPUT_METHODS */
  1419.  
  1420.     if (!(winPtr->flags & TK_TOP_LEVEL)) {
  1421.     /*
  1422.      * If any siblings higher up in the stacking order have already
  1423.      * been created then move this window to its rightful position
  1424.      * in the stacking order.
  1425.      *
  1426.      * NOTE: this code ignores any changes anyone might have made
  1427.      * to the sibling and stack_mode field of the window's attributes,
  1428.      * so it really isn't safe for these to be manipulated except
  1429.      * by calling Tk_RestackWindow.
  1430.      */
  1431.  
  1432.     for (winPtr2 = winPtr->nextPtr; winPtr2 != NULL;
  1433.         winPtr2 = winPtr2->nextPtr) {
  1434.         if ((winPtr2->window != None)
  1435.             && !(winPtr2->flags & (TK_TOP_LEVEL|TK_REPARENTED))) {
  1436.         XWindowChanges changes;
  1437.         changes.sibling = winPtr2->window;
  1438.         changes.stack_mode = Below;
  1439.         XConfigureWindow(winPtr->display, winPtr->window,
  1440.             CWSibling|CWStackMode, &changes);
  1441.         break;
  1442.         }
  1443.     }
  1444.  
  1445.     /*
  1446.      * If this window has a different colormap than its parent, add
  1447.      * the window to the WM_COLORMAP_WINDOWS property for its top-level.
  1448.      */
  1449.  
  1450.     if ((winPtr->parentPtr != NULL) &&
  1451.         (winPtr->atts.colormap != winPtr->parentPtr->atts.colormap)) {
  1452.         TkWmAddToColormapWindows(winPtr);
  1453.         winPtr->flags |= TK_WM_COLORMAP_WINDOW;
  1454.     }
  1455.     }
  1456.  
  1457.     /*
  1458.      * Issue a ConfigureNotify event if there were deferred configuration
  1459.      * changes (but skip it if the window is being deleted;  the
  1460.      * ConfigureNotify event could cause problems if we're being called
  1461.      * from Tk_DestroyWindow under some conditions).
  1462.      */
  1463.  
  1464.     if ((winPtr->flags & TK_NEED_CONFIG_NOTIFY)
  1465.         && !(winPtr->flags & TK_ALREADY_DEAD)){
  1466.     winPtr->flags &= ~TK_NEED_CONFIG_NOTIFY;
  1467.     TkDoConfigureNotify(winPtr);
  1468.     }
  1469. }
  1470.  
  1471. /*
  1472.  *--------------------------------------------------------------
  1473.  *
  1474.  * Tk_UnmapWindow, etc. --
  1475.  *
  1476.  *    There are several procedures under here, each of which
  1477.  *    mirrors an existing X procedure.  In addition to performing
  1478.  *    the functions of the corresponding procedure, each
  1479.  *    procedure also updates the local window structure and
  1480.  *    synthesizes an X event (if the window's structure is being
  1481.  *    managed internally).
  1482.  *
  1483.  * Results:
  1484.  *    See the manual entries.
  1485.  *
  1486.  * Side effects:
  1487.  *    See the manual entries.
  1488.  *
  1489.  *--------------------------------------------------------------
  1490.  */
  1491.  
  1492. void
  1493. Tk_UnmapWindow(tkwin)
  1494.     Tk_Window tkwin;        /* Token for window to unmap. */
  1495. {
  1496.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1497.  
  1498.     if (!(winPtr->flags & TK_MAPPED) || (winPtr->flags & TK_ALREADY_DEAD)) {
  1499.     return;
  1500.     }
  1501.     if (winPtr->flags & TK_TOP_LEVEL) {
  1502.     /*
  1503.      * Special processing has to be done for top-level windows.  Let
  1504.      * tkWm.c handle everything itself.
  1505.      */
  1506.  
  1507.     TkWmUnmapWindow(winPtr);
  1508.     return;
  1509.     }
  1510.     winPtr->flags &= ~TK_MAPPED;
  1511.     XUnmapWindow(winPtr->display, winPtr->window);
  1512.     if (!(winPtr->flags & TK_TOP_LEVEL)) {
  1513.     XEvent event;
  1514.  
  1515.     event.type = UnmapNotify;
  1516.     event.xunmap.serial = LastKnownRequestProcessed(winPtr->display);
  1517.     event.xunmap.send_event = False;
  1518.     event.xunmap.display = winPtr->display;
  1519.     event.xunmap.event = winPtr->window;
  1520.     event.xunmap.window = winPtr->window;
  1521.     event.xunmap.from_configure = False;
  1522.     Tk_HandleEvent(&event);
  1523.     }
  1524. }
  1525.  
  1526. void
  1527. Tk_ConfigureWindow(tkwin, valueMask, valuePtr)
  1528.     Tk_Window tkwin;        /* Window to re-configure. */
  1529.     unsigned int valueMask;    /* Mask indicating which parts of
  1530.                  * *valuePtr are to be used. */
  1531.     XWindowChanges *valuePtr;    /* New values. */
  1532. {
  1533.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1534.  
  1535.     if (valueMask & CWX) {
  1536.     winPtr->changes.x = valuePtr->x;
  1537.     }
  1538.     if (valueMask & CWY) {
  1539.     winPtr->changes.y = valuePtr->y;
  1540.     }
  1541.     if (valueMask & CWWidth) {
  1542.     winPtr->changes.width = valuePtr->width;
  1543.     }
  1544.     if (valueMask & CWHeight) {
  1545.     winPtr->changes.height = valuePtr->height;
  1546.     }
  1547.     if (valueMask & CWBorderWidth) {
  1548.     winPtr->changes.border_width = valuePtr->border_width;
  1549.     }
  1550.     if (valueMask & (CWSibling|CWStackMode)) {
  1551.     panic("Can't set sibling or stack mode from Tk_ConfigureWindow.");
  1552.     }
  1553.  
  1554.     if (winPtr->window != None) {
  1555.     XConfigureWindow(winPtr->display, winPtr->window,
  1556.         valueMask, valuePtr);
  1557.         TkDoConfigureNotify(winPtr);
  1558.     } else {
  1559.     winPtr->dirtyChanges |= valueMask;
  1560.     winPtr->flags |= TK_NEED_CONFIG_NOTIFY;
  1561.     }
  1562. }
  1563.  
  1564. void
  1565. Tk_MoveWindow(tkwin, x, y)
  1566.     Tk_Window tkwin;        /* Window to move. */
  1567.     int x, y;            /* New location for window (within
  1568.                  * parent). */
  1569. {
  1570.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1571.  
  1572.     winPtr->changes.x = x;
  1573.     winPtr->changes.y = y;
  1574.     if (winPtr->window != None) {
  1575.     XMoveWindow(winPtr->display, winPtr->window, x, y);
  1576.         TkDoConfigureNotify(winPtr);
  1577.     } else {
  1578.     winPtr->dirtyChanges |= CWX|CWY;
  1579.     winPtr->flags |= TK_NEED_CONFIG_NOTIFY;
  1580.     }
  1581. }
  1582.  
  1583. void
  1584. Tk_ResizeWindow(tkwin, width, height)
  1585.     Tk_Window tkwin;        /* Window to resize. */
  1586.     int width, height;        /* New dimensions for window. */
  1587. {
  1588.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1589.  
  1590.     winPtr->changes.width = (unsigned) width;
  1591.     winPtr->changes.height = (unsigned) height;
  1592.     if (winPtr->window != None) {
  1593.     XResizeWindow(winPtr->display, winPtr->window, (unsigned) width,
  1594.         (unsigned) height);
  1595.         TkDoConfigureNotify(winPtr);
  1596.     } else {
  1597.     winPtr->dirtyChanges |= CWWidth|CWHeight;
  1598.     winPtr->flags |= TK_NEED_CONFIG_NOTIFY;
  1599.     }
  1600. }
  1601.  
  1602. void
  1603. Tk_MoveResizeWindow(tkwin, x, y, width, height)
  1604.     Tk_Window tkwin;        /* Window to move and resize. */
  1605.     int x, y;            /* New location for window (within
  1606.                  * parent). */
  1607.     int width, height;        /* New dimensions for window. */
  1608. {
  1609.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1610.  
  1611.     winPtr->changes.x = x;
  1612.     winPtr->changes.y = y;
  1613.     winPtr->changes.width = (unsigned) width;
  1614.     winPtr->changes.height = (unsigned) height;
  1615.     if (winPtr->window != None) {
  1616.     XMoveResizeWindow(winPtr->display, winPtr->window, x, y,
  1617.         (unsigned) width, (unsigned) height);
  1618.         TkDoConfigureNotify(winPtr);
  1619.     } else {
  1620.     winPtr->dirtyChanges |= CWX|CWY|CWWidth|CWHeight;
  1621.     winPtr->flags |= TK_NEED_CONFIG_NOTIFY;
  1622.     }
  1623. }
  1624.  
  1625. void
  1626. Tk_SetWindowBorderWidth(tkwin, width)
  1627.     Tk_Window tkwin;        /* Window to modify. */
  1628.     int width;            /* New border width for window. */
  1629. {
  1630.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1631.  
  1632.     winPtr->changes.border_width = width;
  1633.     if (winPtr->window != None) {
  1634.     XSetWindowBorderWidth(winPtr->display, winPtr->window,
  1635.         (unsigned) width);
  1636.         TkDoConfigureNotify(winPtr);
  1637.     } else {
  1638.     winPtr->dirtyChanges |= CWBorderWidth;
  1639.     winPtr->flags |= TK_NEED_CONFIG_NOTIFY;
  1640.     }
  1641. }
  1642.  
  1643. void
  1644. Tk_ChangeWindowAttributes(tkwin, valueMask, attsPtr)
  1645.     Tk_Window tkwin;        /* Window to manipulate. */
  1646.     unsigned long valueMask;    /* OR'ed combination of bits,
  1647.                  * indicating which fields of
  1648.                  * *attsPtr are to be used. */
  1649.     register XSetWindowAttributes *attsPtr;
  1650.                 /* New values for some attributes. */
  1651. {
  1652.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1653.  
  1654.     if (valueMask & CWBackPixmap) {
  1655.     winPtr->atts.background_pixmap = attsPtr->background_pixmap;
  1656.     }
  1657.     if (valueMask & CWBackPixel) {
  1658.     winPtr->atts.background_pixel = attsPtr->background_pixel;
  1659.     }
  1660.     if (valueMask & CWBorderPixmap) {
  1661.     winPtr->atts.border_pixmap = attsPtr->border_pixmap;
  1662.     }
  1663.     if (valueMask & CWBorderPixel) {
  1664.     winPtr->atts.border_pixel = attsPtr->border_pixel;
  1665.     }
  1666.     if (valueMask & CWBitGravity) {
  1667.     winPtr->atts.bit_gravity = attsPtr->bit_gravity;
  1668.     }
  1669.     if (valueMask & CWWinGravity) {
  1670.     winPtr->atts.win_gravity = attsPtr->win_gravity;
  1671.     }
  1672.     if (valueMask & CWBackingStore) {
  1673.     winPtr->atts.backing_store = attsPtr->backing_store;
  1674.     }
  1675.     if (valueMask & CWBackingPlanes) {
  1676.     winPtr->atts.backing_planes = attsPtr->backing_planes;
  1677.     }
  1678.     if (valueMask & CWBackingPixel) {
  1679.     winPtr->atts.backing_pixel = attsPtr->backing_pixel;
  1680.     }
  1681.     if (valueMask & CWOverrideRedirect) {
  1682.     winPtr->atts.override_redirect = attsPtr->override_redirect;
  1683.     }
  1684.     if (valueMask & CWSaveUnder) {
  1685.     winPtr->atts.save_under = attsPtr->save_under;
  1686.     }
  1687.     if (valueMask & CWEventMask) {
  1688.     winPtr->atts.event_mask = attsPtr->event_mask;
  1689.     }
  1690.     if (valueMask & CWDontPropagate) {
  1691.     winPtr->atts.do_not_propagate_mask
  1692.         = attsPtr->do_not_propagate_mask;
  1693.     }
  1694.     if (valueMask & CWColormap) {
  1695.     winPtr->atts.colormap = attsPtr->colormap;
  1696.     }
  1697.     if (valueMask & CWCursor) {
  1698.     winPtr->atts.cursor = attsPtr->cursor;
  1699.     }
  1700.  
  1701.     if (winPtr->window != None) {
  1702.     XChangeWindowAttributes(winPtr->display, winPtr->window,
  1703.         valueMask, attsPtr);
  1704.     } else {
  1705.     winPtr->dirtyAtts |= valueMask;
  1706.     }
  1707. }
  1708.  
  1709. void
  1710. Tk_SetWindowBackground(tkwin, pixel)
  1711.     Tk_Window tkwin;        /* Window to manipulate. */
  1712.     unsigned long pixel;    /* Pixel value to use for
  1713.                  * window's background. */
  1714. {
  1715.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1716.  
  1717.     winPtr->atts.background_pixel = pixel;
  1718.  
  1719.     if (winPtr->window != None) {
  1720.     XSetWindowBackground(winPtr->display, winPtr->window, pixel);
  1721.     } else {
  1722.     winPtr->dirtyAtts = (winPtr->dirtyAtts & (unsigned) ~CWBackPixmap)
  1723.         | CWBackPixel;
  1724.     }
  1725. }
  1726.  
  1727. void
  1728. Tk_SetWindowBackgroundPixmap(tkwin, pixmap)
  1729.     Tk_Window tkwin;        /* Window to manipulate. */
  1730.     Pixmap pixmap;        /* Pixmap to use for window's
  1731.                  * background. */
  1732. {
  1733.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1734.  
  1735.     winPtr->atts.background_pixmap = pixmap;
  1736.  
  1737.     if (winPtr->window != None) {
  1738.     XSetWindowBackgroundPixmap(winPtr->display,
  1739.         winPtr->window, pixmap);
  1740.     } else {
  1741.     winPtr->dirtyAtts = (winPtr->dirtyAtts & (unsigned) ~CWBackPixel)
  1742.         | CWBackPixmap;
  1743.     }
  1744. }
  1745.  
  1746. void
  1747. Tk_SetWindowBorder(tkwin, pixel)
  1748.     Tk_Window tkwin;        /* Window to manipulate. */
  1749.     unsigned long pixel;    /* Pixel value to use for
  1750.                  * window's border. */
  1751. {
  1752.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1753.  
  1754.     winPtr->atts.border_pixel = pixel;
  1755.  
  1756.     if (winPtr->window != None) {
  1757.     XSetWindowBorder(winPtr->display, winPtr->window, pixel);
  1758.     } else {
  1759.     winPtr->dirtyAtts = (winPtr->dirtyAtts & (unsigned) ~CWBorderPixmap)
  1760.         | CWBorderPixel;
  1761.     }
  1762. }
  1763.  
  1764. void
  1765. Tk_SetWindowBorderPixmap(tkwin, pixmap)
  1766.     Tk_Window tkwin;        /* Window to manipulate. */
  1767.     Pixmap pixmap;        /* Pixmap to use for window's
  1768.                  * border. */
  1769. {
  1770.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1771.  
  1772.     winPtr->atts.border_pixmap = pixmap;
  1773.  
  1774.     if (winPtr->window != None) {
  1775.     XSetWindowBorderPixmap(winPtr->display,
  1776.         winPtr->window, pixmap);
  1777.     } else {
  1778.     winPtr->dirtyAtts = (winPtr->dirtyAtts & (unsigned) ~CWBorderPixel)
  1779.         | CWBorderPixmap;
  1780.     }
  1781. }
  1782.  
  1783. void
  1784. Tk_DefineCursor(tkwin, cursor)
  1785.     Tk_Window tkwin;        /* Window to manipulate. */
  1786.     Tk_Cursor cursor;        /* Cursor to use for window (may be None). */
  1787. {
  1788.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1789.  
  1790. #ifdef MAC_TCL
  1791.     winPtr->atts.cursor = (XCursor) cursor;
  1792. #else
  1793.     winPtr->atts.cursor = (Cursor) cursor;
  1794. #endif
  1795.     
  1796.     if (winPtr->window != None) {
  1797.     XDefineCursor(winPtr->display, winPtr->window, winPtr->atts.cursor);
  1798.     } else {
  1799.     winPtr->dirtyAtts = winPtr->dirtyAtts | CWCursor;
  1800.     }
  1801. }
  1802.  
  1803. void
  1804. Tk_UndefineCursor(tkwin)
  1805.     Tk_Window tkwin;        /* Window to manipulate. */
  1806. {
  1807.     Tk_DefineCursor(tkwin, None);
  1808. }
  1809.  
  1810. void
  1811. Tk_SetWindowColormap(tkwin, colormap)
  1812.     Tk_Window tkwin;        /* Window to manipulate. */
  1813.     Colormap colormap;        /* Colormap to use for window. */
  1814. {
  1815.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1816.  
  1817.     winPtr->atts.colormap = colormap;
  1818.  
  1819.     if (winPtr->window != None) {
  1820.     XSetWindowColormap(winPtr->display, winPtr->window, colormap);
  1821.     if (!(winPtr->flags & TK_TOP_LEVEL)) {
  1822.         TkWmAddToColormapWindows(winPtr);
  1823.         winPtr->flags |= TK_WM_COLORMAP_WINDOW;
  1824.     }
  1825.     } else {
  1826.     winPtr->dirtyAtts |= CWColormap;
  1827.     }
  1828. }
  1829.  
  1830. /*
  1831.  *----------------------------------------------------------------------
  1832.  *
  1833.  * Tk_SetWindowVisual --
  1834.  *
  1835.  *    This procedure is called to specify a visual to be used
  1836.  *    for a Tk window when it is created.  This procedure, if
  1837.  *    called at all, must be called before the X window is created
  1838.  *    (i.e. before Tk_MakeWindowExist is called).
  1839.  *
  1840.  * Results:
  1841.  *    The return value is 1 if successful, or 0 if the X window has
  1842.  *    been already created.
  1843.  *
  1844.  * Side effects:
  1845.  *    The information given is stored for when the window is created.
  1846.  *
  1847.  *----------------------------------------------------------------------
  1848.  */
  1849.  
  1850. int
  1851. Tk_SetWindowVisual(tkwin, visual, depth, colormap)
  1852.     Tk_Window tkwin;        /* Window to manipulate. */
  1853.     Visual *visual;        /* New visual for window. */
  1854.     int depth;            /* New depth for window. */
  1855.     Colormap colormap;        /* An appropriate colormap for the visual. */
  1856. {
  1857.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1858.  
  1859.     if( winPtr->window != None ){
  1860.     /* Too late! */
  1861.     return 0;
  1862.     }
  1863.  
  1864.     winPtr->visual = visual;
  1865.     winPtr->depth = depth;
  1866.     winPtr->atts.colormap = colormap;
  1867.     winPtr->dirtyAtts |= CWColormap;
  1868.  
  1869.     /*
  1870.      * The following code is needed to make sure that the window doesn't
  1871.      * inherit the parent's border pixmap, which would result in a BadMatch
  1872.      * error.
  1873.      */
  1874.  
  1875.     if (!(winPtr->dirtyAtts & CWBorderPixmap)) {
  1876.     winPtr->dirtyAtts |= CWBorderPixel;
  1877.     }
  1878.     return 1;
  1879. }
  1880.  
  1881. /*
  1882.  *----------------------------------------------------------------------
  1883.  *
  1884.  * TkDoConfigureNotify --
  1885.  *
  1886.  *    Generate a ConfigureNotify event describing the current
  1887.  *    configuration of a window.
  1888.  *
  1889.  * Results:
  1890.  *    None.
  1891.  *
  1892.  * Side effects:
  1893.  *    An event is generated and processed by Tk_HandleEvent.
  1894.  *
  1895.  *----------------------------------------------------------------------
  1896.  */
  1897.  
  1898. void
  1899. TkDoConfigureNotify(winPtr)
  1900.     register TkWindow *winPtr;        /* Window whose configuration
  1901.                      * was just changed. */
  1902. {
  1903.     XEvent event;
  1904.  
  1905.     event.type = ConfigureNotify;
  1906.     event.xconfigure.serial = LastKnownRequestProcessed(winPtr->display);
  1907.     event.xconfigure.send_event = False;
  1908.     event.xconfigure.display = winPtr->display;
  1909.     event.xconfigure.event = winPtr->window;
  1910.     event.xconfigure.window = winPtr->window;
  1911.     event.xconfigure.x = winPtr->changes.x;
  1912.     event.xconfigure.y = winPtr->changes.y;
  1913.     event.xconfigure.width = winPtr->changes.width;
  1914.     event.xconfigure.height = winPtr->changes.height;
  1915.     event.xconfigure.border_width = winPtr->changes.border_width;
  1916.     if (winPtr->changes.stack_mode == Above) {
  1917.     event.xconfigure.above = winPtr->changes.sibling;
  1918.     } else {
  1919.     event.xconfigure.above = None;
  1920.     }
  1921.     event.xconfigure.override_redirect = winPtr->atts.override_redirect;
  1922.     Tk_HandleEvent(&event);
  1923. }
  1924.  
  1925. /*
  1926.  *----------------------------------------------------------------------
  1927.  *
  1928.  * Tk_SetClass --
  1929.  *
  1930.  *    This procedure is used to give a window a class.
  1931.  *
  1932.  * Results:
  1933.  *    None.
  1934.  *
  1935.  * Side effects:
  1936.  *    A new class is stored for tkwin, replacing any existing
  1937.  *    class for it.
  1938.  *
  1939.  *----------------------------------------------------------------------
  1940.  */
  1941.  
  1942. void
  1943. Tk_SetClass(tkwin, className)
  1944.     Tk_Window tkwin;        /* Token for window to assign class. */
  1945.     char *className;        /* New class for tkwin. */
  1946. {
  1947.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1948.  
  1949.     winPtr->classUid = Tk_GetUid(className);
  1950.     if (winPtr->flags & TK_TOP_LEVEL) {
  1951.     TkWmSetClass(winPtr);
  1952.     }
  1953.     TkOptionClassChanged(winPtr);
  1954. }
  1955.  
  1956. /*
  1957.  *----------------------------------------------------------------------
  1958.  *
  1959.  * TkSetClassProcs --
  1960.  *
  1961.  *    This procedure is used to set the class procedures and
  1962.  *    instance data for a window.
  1963.  *
  1964.  * Results:
  1965.  *    None.
  1966.  *
  1967.  * Side effects:
  1968.  *    A new set of class procedures and instance data is stored
  1969.  *    for tkwin, replacing any existing values.
  1970.  *
  1971.  *----------------------------------------------------------------------
  1972.  */
  1973.  
  1974. void
  1975. TkSetClassProcs(tkwin, procs, instanceData)
  1976.     Tk_Window tkwin;        /* Token for window to modify. */
  1977.     TkClassProcs *procs;    /* Class procs structure. */
  1978.     ClientData instanceData;    /* Data to be passed to class procedures. */
  1979. {
  1980.     register TkWindow *winPtr = (TkWindow *) tkwin;
  1981.  
  1982.     winPtr->classProcsPtr = procs;
  1983.     winPtr->instanceData = instanceData;
  1984. }
  1985.  
  1986. /*
  1987.  *----------------------------------------------------------------------
  1988.  *
  1989.  * Tk_NameToWindow --
  1990.  *
  1991.  *    Given a string name for a window, this procedure
  1992.  *    returns the token for the window, if there exists a
  1993.  *    window corresponding to the given name.
  1994.  *
  1995.  * Results:
  1996.  *    The return result is either a token for the window corresponding
  1997.  *    to "name", or else NULL to indicate that there is no such
  1998.  *    window.  In this case, an error message is left in interp->result.
  1999.  *
  2000.  * Side effects:
  2001.  *    None.
  2002.  *
  2003.  *----------------------------------------------------------------------
  2004.  */
  2005.  
  2006. Tk_Window
  2007. Tk_NameToWindow(interp, pathName, tkwin)
  2008.     Tcl_Interp *interp;        /* Where to report errors. */
  2009.     char *pathName;        /* Path name of window. */
  2010.     Tk_Window tkwin;        /* Token for window:  name is assumed to
  2011.                  * belong to the same main window as tkwin. */
  2012. {
  2013.     Tcl_HashEntry *hPtr;
  2014.  
  2015.     hPtr = Tcl_FindHashEntry(&((TkWindow *) tkwin)->mainPtr->nameTable,
  2016.         pathName);
  2017.     if (hPtr == NULL) {
  2018.     Tcl_AppendResult(interp, "bad window path name \"",
  2019.         pathName, "\"", (char *) NULL);
  2020.     return NULL;
  2021.     }
  2022.     return (Tk_Window) Tcl_GetHashValue(hPtr);
  2023. }
  2024.  
  2025. /*
  2026.  *----------------------------------------------------------------------
  2027.  *
  2028.  * Tk_IdToWindow --
  2029.  *
  2030.  *    Given an X display and window ID, this procedure returns the
  2031.  *    Tk token for the window, if there exists a Tk window corresponding
  2032.  *    to the given ID.
  2033.  *
  2034.  * Results:
  2035.  *    The return result is either a token for the window corresponding
  2036.  *    to the given X id, or else NULL to indicate that there is no such
  2037.  *    window.
  2038.  *
  2039.  * Side effects:
  2040.  *    None.
  2041.  *
  2042.  *----------------------------------------------------------------------
  2043.  */
  2044.  
  2045. Tk_Window
  2046. Tk_IdToWindow(display, window)
  2047.     Display *display;        /* X display containing the window. */
  2048.     Window window;        /* X window window id. */
  2049. {
  2050.     TkDisplay *dispPtr;
  2051.     Tcl_HashEntry *hPtr;
  2052.  
  2053.     for (dispPtr = tkDisplayList; ; dispPtr = dispPtr->nextPtr) {
  2054.     if (dispPtr == NULL) {
  2055.         return NULL;
  2056.     }
  2057.     if (dispPtr->display == display) {
  2058.         break;
  2059.     }
  2060.     }
  2061.  
  2062.     hPtr = Tcl_FindHashEntry(&dispPtr->winTable, (char *) window);
  2063.     if (hPtr == NULL) {
  2064.     return NULL;
  2065.     }
  2066.     return (Tk_Window) Tcl_GetHashValue(hPtr);
  2067. }
  2068.  
  2069. /*
  2070.  *----------------------------------------------------------------------
  2071.  *
  2072.  * Tk_DisplayName --
  2073.  *
  2074.  *    Return the textual name of a window's display.
  2075.  *
  2076.  * Results:
  2077.  *    The return value is the string name of the display associated
  2078.  *    with tkwin.
  2079.  *
  2080.  * Side effects:
  2081.  *    None.
  2082.  *
  2083.  *----------------------------------------------------------------------
  2084.  */
  2085.  
  2086. char *
  2087. Tk_DisplayName(tkwin)
  2088.     Tk_Window tkwin;        /* Window whose display name is desired. */
  2089. {
  2090.     return ((TkWindow *) tkwin)->dispPtr->name;
  2091. }
  2092.  
  2093. /*
  2094.  *----------------------------------------------------------------------
  2095.  *
  2096.  * UnlinkWindow --
  2097.  *
  2098.  *    This procedure removes a window from the childList of its
  2099.  *    parent.
  2100.  *
  2101.  * Results:
  2102.  *    None.
  2103.  *
  2104.  * Side effects:
  2105.  *    The window is unlinked from its childList.
  2106.  *
  2107.  *----------------------------------------------------------------------
  2108.  */
  2109.  
  2110. static void
  2111. UnlinkWindow(winPtr)
  2112.     TkWindow *winPtr;            /* Child window to be unlinked. */
  2113. {
  2114.     TkWindow *prevPtr;
  2115.  
  2116.     if (winPtr->parentPtr == NULL) {
  2117.     return;
  2118.     }
  2119.     prevPtr = winPtr->parentPtr->childList;
  2120.     if (prevPtr == winPtr) {
  2121.     winPtr->parentPtr->childList = winPtr->nextPtr;
  2122.     if (winPtr->nextPtr == NULL) {
  2123.         winPtr->parentPtr->lastChildPtr = NULL;
  2124.     }
  2125.     } else {
  2126.     while (prevPtr->nextPtr != winPtr) {
  2127.         prevPtr = prevPtr->nextPtr;
  2128.         if (prevPtr == NULL) {
  2129.         panic("UnlinkWindow couldn't find child in parent");
  2130.         }
  2131.     }
  2132.     prevPtr->nextPtr = winPtr->nextPtr;
  2133.     if (winPtr->nextPtr == NULL) {
  2134.         winPtr->parentPtr->lastChildPtr = prevPtr;
  2135.     }
  2136.     }
  2137. }
  2138.  
  2139. /*
  2140.  *----------------------------------------------------------------------
  2141.  *
  2142.  * Tk_RestackWindow --
  2143.  *
  2144.  *    Change a window's position in the stacking order.
  2145.  *
  2146.  * Results:
  2147.  *    TCL_OK is normally returned.  If other is not a descendant
  2148.  *    of tkwin's parent then TCL_ERROR is returned and tkwin is
  2149.  *    not repositioned.
  2150.  *
  2151.  * Side effects:
  2152.  *    Tkwin is repositioned in the stacking order.
  2153.  *
  2154.  *----------------------------------------------------------------------
  2155.  */
  2156.  
  2157. int
  2158. Tk_RestackWindow(tkwin, aboveBelow, other)
  2159.     Tk_Window tkwin;        /* Token for window whose position in
  2160.                  * the stacking order is to change. */
  2161.     int aboveBelow;        /* Indicates new position of tkwin relative
  2162.                  * to other;  must be Above or Below. */
  2163.     Tk_Window other;        /* Tkwin will be moved to a position that
  2164.                  * puts it just above or below this window.
  2165.                  * If NULL then tkwin goes above or below
  2166.                  * all windows in the same parent. */
  2167. {
  2168.     TkWindow *winPtr = (TkWindow *) tkwin;
  2169.     TkWindow *otherPtr = (TkWindow *) other;
  2170.     XWindowChanges changes;
  2171.     unsigned int mask;
  2172.  
  2173.  
  2174.     /*
  2175.      * Special case:  if winPtr is a top-level window then just find
  2176.      * the top-level ancestor of otherPtr and restack winPtr above
  2177.      * otherPtr without changing any of Tk's childLists.
  2178.      */
  2179.  
  2180.     changes.stack_mode = aboveBelow;
  2181.     mask = CWStackMode;
  2182.     if (winPtr->flags & TK_TOP_LEVEL) {
  2183.     while ((otherPtr != NULL) && !(otherPtr->flags & TK_TOP_LEVEL)) {
  2184.         otherPtr = otherPtr->parentPtr;
  2185.     }
  2186.     TkWmRestackToplevel(winPtr, aboveBelow, otherPtr);
  2187.     return TCL_OK;
  2188.     }
  2189.  
  2190.     /*
  2191.      * Find an ancestor of otherPtr that is a sibling of winPtr.
  2192.      */
  2193.  
  2194.     if (winPtr->parentPtr == NULL) {
  2195.     /*
  2196.      * Window is going to be deleted shortly;  don't do anything.
  2197.      */
  2198.  
  2199.     return TCL_OK;
  2200.     }
  2201.     if (otherPtr == NULL) {
  2202.     if (aboveBelow == Above) {
  2203.         otherPtr = winPtr->parentPtr->lastChildPtr;
  2204.     } else {
  2205.         otherPtr = winPtr->parentPtr->childList;
  2206.     }
  2207.     } else {
  2208.     while (winPtr->parentPtr != otherPtr->parentPtr) {
  2209.         if ((otherPtr == NULL) || (otherPtr->flags & TK_TOP_LEVEL)) {
  2210.         return TCL_ERROR;
  2211.         }
  2212.         otherPtr = otherPtr->parentPtr;
  2213.     }
  2214.     }
  2215.     if (otherPtr == winPtr) {
  2216.     return TCL_OK;
  2217.     }
  2218.  
  2219.     /*
  2220.      * Reposition winPtr in the stacking order.
  2221.      */
  2222.  
  2223.     UnlinkWindow(winPtr);
  2224.     if (aboveBelow == Above) {
  2225.     winPtr->nextPtr = otherPtr->nextPtr;
  2226.     if (winPtr->nextPtr == NULL) {
  2227.         winPtr->parentPtr->lastChildPtr = winPtr;
  2228.     }
  2229.     otherPtr->nextPtr = winPtr;
  2230.     } else {
  2231.     TkWindow *prevPtr;
  2232.  
  2233.     prevPtr = winPtr->parentPtr->childList;
  2234.     if (prevPtr == otherPtr) {
  2235.         winPtr->parentPtr->childList = winPtr;
  2236.     } else {
  2237.         while (prevPtr->nextPtr != otherPtr) {
  2238.         prevPtr = prevPtr->nextPtr;
  2239.         }
  2240.         prevPtr->nextPtr = winPtr;
  2241.     }
  2242.     winPtr->nextPtr = otherPtr;
  2243.     }
  2244.  
  2245.     /*
  2246.      * Notify the X server of the change.  If winPtr hasn't yet been
  2247.      * created then there's no need to tell the X server now, since
  2248.      * the stacking order will be handled properly when the window
  2249.      * is finally created.
  2250.      */
  2251.  
  2252.     if (winPtr->window != None) {
  2253.     changes.stack_mode = Above;
  2254.     for (otherPtr = winPtr->nextPtr; otherPtr != NULL;
  2255.         otherPtr = otherPtr->nextPtr) {
  2256.         if ((otherPtr->window != None)
  2257.             && !(otherPtr->flags & (TK_TOP_LEVEL|TK_REPARENTED))){
  2258.         changes.sibling = otherPtr->window;
  2259.         changes.stack_mode = Below;
  2260.         mask = CWStackMode|CWSibling;
  2261.         break;
  2262.         }
  2263.     }
  2264.     XConfigureWindow(winPtr->display, winPtr->window, mask, &changes);
  2265.     }
  2266.     return TCL_OK;
  2267. }
  2268.  
  2269. /*
  2270.  *----------------------------------------------------------------------
  2271.  *
  2272.  * Tk_MainWindow --
  2273.  *
  2274.  *    Returns the main window for an application.
  2275.  *
  2276.  * Results:
  2277.  *    If interp has a Tk application associated with it, the main
  2278.  *    window for the application is returned.  Otherwise NULL is
  2279.  *    returned and an error message is left in interp->result.
  2280.  *
  2281.  * Side effects:
  2282.  *    None.
  2283.  *
  2284.  *----------------------------------------------------------------------
  2285.  */
  2286.  
  2287. Tk_Window
  2288. Tk_MainWindow(interp)
  2289.     Tcl_Interp *interp;            /* Interpreter that embodies the
  2290.                      * application.  Used for error
  2291.                      * reporting also. */
  2292. {
  2293.     TkMainInfo *mainPtr;
  2294.  
  2295.     for (mainPtr = tkMainWindowList; mainPtr != NULL;
  2296.         mainPtr = mainPtr->nextPtr) {
  2297.     if (mainPtr->interp == interp) {
  2298.         return (Tk_Window) mainPtr->winPtr;
  2299.     }
  2300.     }
  2301.     interp->result = "this isn't a Tk application";
  2302.     return NULL;
  2303. }
  2304.  
  2305. /*
  2306.  *----------------------------------------------------------------------
  2307.  *
  2308.  * Tk_StrictMotif --
  2309.  *
  2310.  *    Indicates whether strict Motif compliance has been specified
  2311.  *    for the given window.
  2312.  *
  2313.  * Results:
  2314.  *    The return value is 1 if strict Motif compliance has been
  2315.  *    requested for tkwin's application by setting the tk_strictMotif
  2316.  *    variable in its interpreter to a true value.  0 is returned
  2317.  *    if tk_strictMotif has a false value.
  2318.  *
  2319.  * Side effects:
  2320.  *    None.
  2321.  *
  2322.  *----------------------------------------------------------------------
  2323.  */
  2324.  
  2325. int
  2326. Tk_StrictMotif(tkwin)
  2327.     Tk_Window tkwin;            /* Window whose application is
  2328.                      * to be checked. */
  2329. {
  2330.     return ((TkWindow *) tkwin)->mainPtr->strictMotif;
  2331. }
  2332.  
  2333. /* 
  2334.  *--------------------------------------------------------------
  2335.  *
  2336.  * OpenIM --
  2337.  *
  2338.  *    Tries to open an X input method, associated with the
  2339.  *    given display.  Right now we can only deal with a bare-bones
  2340.  *    input style:  no preedit, and no status.
  2341.  *
  2342.  * Results:
  2343.  *    Stores the input method in dispPtr->inputMethod;  if there isn't
  2344.  *    a suitable input method, then NULL is stored in dispPtr->inputMethod.
  2345.  *
  2346.  * Side effects:
  2347.  *    An input method gets opened.
  2348.  *
  2349.  *--------------------------------------------------------------
  2350.  */
  2351.  
  2352. static void
  2353. OpenIM(dispPtr)
  2354.     TkDisplay *dispPtr;        /* Tk's structure for the display. */
  2355. {
  2356. #ifndef TK_USE_INPUT_METHODS
  2357.     return;
  2358. #else
  2359.     unsigned short i;
  2360.     XIMStyles *stylePtr;
  2361.  
  2362.     dispPtr->inputMethod = XOpenIM(dispPtr->display, NULL, NULL, NULL);
  2363.     if (dispPtr->inputMethod == NULL) {
  2364.     return;
  2365.     }
  2366.  
  2367.     if ((XGetIMValues(dispPtr->inputMethod, XNQueryInputStyle, &stylePtr,
  2368.         NULL) != NULL) || (stylePtr == NULL)) {
  2369.     goto error;
  2370.     }
  2371.     for (i = 0; i < stylePtr->count_styles; i++) {
  2372.     if (stylePtr->supported_styles[i]
  2373.         == (XIMPreeditNothing|XIMStatusNothing)) {
  2374.         XFree(stylePtr);
  2375.         return;
  2376.     }
  2377.     }
  2378.     XFree(stylePtr);
  2379.  
  2380.     error:
  2381.  
  2382.     /*
  2383.      * Should close the input method, but this causes core dumps on some
  2384.      * systems (e.g. Solaris 2.3 as of 1/6/95).
  2385.      * XCloseIM(dispPtr->inputMethod);
  2386.      */
  2387.     dispPtr->inputMethod = NULL;
  2388.     return;
  2389. #endif /* TK_USE_INPUT_METHODS */
  2390. }
  2391.  
  2392. /*
  2393.  *----------------------------------------------------------------------
  2394.  *
  2395.  * Tk_GetNumMainWindows --
  2396.  *
  2397.  *    This procedure returns the number of main windows currently
  2398.  *    open in this process.
  2399.  *
  2400.  * Results:
  2401.  *    The number of main windows open in this process.
  2402.  *
  2403.  * Side effects:
  2404.  *    None.
  2405.  *
  2406.  *----------------------------------------------------------------------
  2407.  */
  2408.  
  2409. int
  2410. Tk_GetNumMainWindows()
  2411. {
  2412.     return numMainWindows;
  2413. }
  2414.  
  2415. /*
  2416.  *----------------------------------------------------------------------
  2417.  *
  2418.  * DeleteWindowsExitProc --
  2419.  *
  2420.  *    This procedure is invoked as an exit handler.  It deletes all
  2421.  *    of the main windows in the process.
  2422.  *
  2423.  * Results:
  2424.  *    None.
  2425.  *
  2426.  * Side effects:
  2427.  *    None.
  2428.  *
  2429.  *----------------------------------------------------------------------
  2430.  */
  2431.  
  2432. static void
  2433. DeleteWindowsExitProc(clientData)
  2434.     ClientData clientData;        /* Not used. */
  2435. {
  2436.     TkDisplay *displayPtr, *nextPtr;
  2437.     Tcl_Interp *interp;
  2438.     
  2439.     while (tkMainWindowList != NULL) {
  2440.         /*
  2441.          * We must protect the interpreter while deleting the window,
  2442.          * because of <Destroy> bindings which could destroy the interpreter
  2443.          * while the window is being deleted. This would leave frames on
  2444.          * the call stack pointing at deleted memory, causing core dumps.
  2445.          */
  2446.         
  2447.         interp = tkMainWindowList->winPtr->mainPtr->interp;
  2448.         Tcl_Preserve((ClientData) interp);
  2449.     Tk_DestroyWindow((Tk_Window) tkMainWindowList->winPtr);
  2450.         Tcl_Release((ClientData) interp);
  2451.     }
  2452.     
  2453.     displayPtr = tkDisplayList;
  2454.     tkDisplayList = NULL;
  2455.     
  2456.     /*
  2457.      * Iterate destroying the displays until no more displays remain.
  2458.      * It is possible for displays to get recreated during exit by any
  2459.      * code that calls GetScreen, so we must destroy these new displays
  2460.      * as well as the old ones.
  2461.      */
  2462.     
  2463.     for (displayPtr = tkDisplayList;
  2464.          displayPtr != NULL;
  2465.          displayPtr = tkDisplayList) {
  2466.  
  2467.         /*
  2468.          * Now iterate over the current list of open displays, and first
  2469.          * set the global pointer to NULL so we will be able to notice if
  2470.          * any new displays got created during deletion of the current set.
  2471.          * We must also do this to ensure that Tk_IdToWindow does not find
  2472.          * the old display as it is being destroyed, when it wants to see
  2473.          * if it needs to dispatch a message.
  2474.          */
  2475.         
  2476.         for (tkDisplayList = NULL; displayPtr != NULL; displayPtr = nextPtr) {
  2477.             nextPtr = displayPtr->nextPtr;
  2478.             if (displayPtr->name != (char *) NULL) {
  2479.                 ckfree(displayPtr->name);
  2480.             }
  2481.             Tcl_DeleteHashTable(&(displayPtr->winTable));
  2482.             TkpCloseDisplay(displayPtr);
  2483.         }
  2484.     }
  2485.     
  2486.     numMainWindows = 0;
  2487.     tkMainWindowList = NULL;
  2488.     initialized = 0;
  2489.     tkDisabledUid = NULL;
  2490.     tkActiveUid = NULL;
  2491.     tkNormalUid = NULL;
  2492. }
  2493.  
  2494. /*
  2495.  *----------------------------------------------------------------------
  2496.  *
  2497.  * Tk_Init --
  2498.  *
  2499.  *    This procedure is invoked to add Tk to an interpreter.  It
  2500.  *    incorporates all of Tk's commands into the interpreter and
  2501.  *    creates the main window for a new Tk application.  If the
  2502.  *    interpreter contains a variable "argv", this procedure
  2503.  *    extracts several arguments from that variable, uses them
  2504.  *    to configure the main window, and modifies argv to exclude
  2505.  *    the arguments (see the "wish" documentation for a list of
  2506.  *    the arguments that are extracted).
  2507.  *
  2508.  * Results:
  2509.  *    Returns a standard Tcl completion code and sets interp->result
  2510.  *    if there is an error.
  2511.  *
  2512.  * Side effects:
  2513.  *    Depends on various initialization scripts that get invoked.
  2514.  *
  2515.  *----------------------------------------------------------------------
  2516.  */
  2517.  
  2518. int
  2519. Tk_Init(interp)
  2520.     Tcl_Interp *interp;        /* Interpreter to initialize. */
  2521. {
  2522.     return Initialize(interp);
  2523. }
  2524.  
  2525. /*
  2526.  *----------------------------------------------------------------------
  2527.  *
  2528.  * Tk_SafeInit --
  2529.  *
  2530.  *    This procedure is invoked to add Tk to a safe interpreter. It
  2531.  *    invokes the internal procedure that does the real work.
  2532.  *
  2533.  * Results:
  2534.  *    Returns a standard Tcl completion code and sets interp->result
  2535.  *    if there is an error.
  2536.  *
  2537.  * Side effects:
  2538.  *    Depends on various initialization scripts that are invoked.
  2539.  *
  2540.  *----------------------------------------------------------------------
  2541.  */
  2542.  
  2543. int
  2544. Tk_SafeInit(interp)
  2545.     Tcl_Interp *interp;        /* Interpreter to initialize. */
  2546. {
  2547.     /*
  2548.      * Initialize the interpreter with Tk, safely. This removes
  2549.      * all the Tk commands that are unsafe.
  2550.      *
  2551.      * Rationale:
  2552.      *
  2553.      * - Toplevel and menu are unsafe because they can be used to cover
  2554.      *   the entire screen and to steal input from the user.
  2555.      * - Continuous ringing of the bell is a nuisance.
  2556.      * - Cannot allow access to the clipboard because a malicious script
  2557.      *   can replace the contents with the string "rm -r *" and lead to
  2558.      *   surprises when the contents of the clipboard are pasted. We do
  2559.      *   not currently hide the selection command.. Should we?
  2560.      * - Cannot allow send because it can be used to cause unsafe
  2561.      *   interpreters to execute commands. The tk command recreates the
  2562.      *   send command, so that too must be hidden.
  2563.      * - Focus can be used to grab the focus away from another window,
  2564.      *   in effect stealing user input. Cannot allow that.
  2565.      *   NOTE: We currently do *not* hide focus as it would make it
  2566.      *   impossible to provide keyboard input to Tk in a safe interpreter.
  2567.      * - Grab can be used to block the user from using any other apps
  2568.      *   on the screen.
  2569.      * - Tkwait can block the containing process forever. Use bindings,
  2570.      *   fileevents and split the protocol into before-the-wait and
  2571.      *   after-the-wait parts. More work but necessary.
  2572.      * - Wm is unsafe because (if toplevels are allowed, in the future)
  2573.      *   it can be used to remove decorations, move windows around, cover
  2574.      *   the entire screen etc etc.
  2575.      *
  2576.      * Current risks:
  2577.      *
  2578.      * - No CPU time limit, no memory allocation limits, no color limits.
  2579.      *
  2580.      *  The actual code called is the same as Tk_Init but Tcl_IsSafe()
  2581.      *  is checked at several places to differentiate the two initialisations.
  2582.      */
  2583.  
  2584.     return Initialize(interp);
  2585. }
  2586.  
  2587. /*
  2588.  *----------------------------------------------------------------------
  2589.  *
  2590.  * Initialize --
  2591.  *
  2592.  *
  2593.  * Results:
  2594.  *    A standard Tcl result. Also leaves an error message in interp->result
  2595.  *    if there was an error.
  2596.  *
  2597.  * Side effects:
  2598.  *    Depends on the initialization scripts that are invoked.
  2599.  *
  2600.  *----------------------------------------------------------------------
  2601.  */
  2602.  
  2603. static int
  2604. Initialize(interp)
  2605.     Tcl_Interp *interp;        /* Interpreter to initialize. */
  2606. {
  2607.     char *p;
  2608.     int argc, code;
  2609.     char **argv, *args[20];
  2610.     Tcl_DString class;
  2611.     char buffer[30];
  2612.  
  2613.     /*
  2614.      * Start by initializing all the static variables to default acceptable
  2615.      * values so that no information is leaked from a previous run of this
  2616.      * code.
  2617.      */
  2618.  
  2619.     synchronize = 0;
  2620.     name = NULL;
  2621.     display = NULL;
  2622.     geometry = NULL;
  2623.     colormap = NULL;
  2624.     use = NULL;
  2625.     visual = NULL;
  2626.     rest = 0;
  2627.  
  2628.     /*
  2629.      * If there is an "argv" variable, get its value, extract out
  2630.      * relevant arguments from it, and rewrite the variable without
  2631.      * the arguments that we used.
  2632.      */
  2633.  
  2634.     p = Tcl_GetVar2(interp, "argv", (char *) NULL, TCL_GLOBAL_ONLY);
  2635.     argv = NULL;
  2636.     if (p != NULL) {
  2637.     if (Tcl_SplitList(interp, p, &argc, &argv) != TCL_OK) {
  2638.         argError:
  2639.         Tcl_AddErrorInfo(interp,
  2640.             "\n    (processing arguments in argv variable)");
  2641.         return TCL_ERROR;
  2642.     }
  2643.     if (Tk_ParseArgv(interp, (Tk_Window) NULL, &argc, argv,
  2644.         argTable, TK_ARGV_DONT_SKIP_FIRST_ARG|TK_ARGV_NO_DEFAULTS)
  2645.         != TCL_OK) {
  2646.         ckfree((char *) argv);
  2647.         goto argError;
  2648.     }
  2649.     p = Tcl_Merge(argc, argv);
  2650.     Tcl_SetVar2(interp, "argv", (char *) NULL, p, TCL_GLOBAL_ONLY);
  2651.     sprintf(buffer, "%d", argc);
  2652.     Tcl_SetVar2(interp, "argc", (char *) NULL, buffer, TCL_GLOBAL_ONLY);
  2653.     ckfree(p);
  2654.     }
  2655.  
  2656.     /*
  2657.      * Figure out the application's name and class.
  2658.      */
  2659.  
  2660.     Tcl_DStringInit(&class);
  2661.     if (name == NULL) {
  2662.     int offset;
  2663.     TkpGetAppName(interp, &class);
  2664.     offset = Tcl_DStringLength(&class)+1;
  2665.     Tcl_DStringSetLength(&class, offset);
  2666.     Tcl_DStringAppend(&class, Tcl_DStringValue(&class), offset-1);
  2667.     name = Tcl_DStringValue(&class) + offset;
  2668.     } else {
  2669.     Tcl_DStringAppend(&class, name, -1);
  2670.     }
  2671.  
  2672.     p = Tcl_DStringValue(&class);
  2673.     if (islower(UCHAR(*p))) {
  2674.     *p = toupper(UCHAR(*p));
  2675.     }
  2676.  
  2677.     /*
  2678.      * Create an argument list for creating the top-level window,
  2679.      * using the information parsed from argv, if any.
  2680.      */
  2681.  
  2682.     args[0] = "toplevel";
  2683.     args[1] = ".";
  2684.     args[2] = "-class";
  2685.     args[3] = Tcl_DStringValue(&class);
  2686.     argc = 4;
  2687.     if (display != NULL) {
  2688.     args[argc] = "-screen";
  2689.     args[argc+1] = display;
  2690.     argc += 2;
  2691.  
  2692.     /*
  2693.      * If this is the first application for this process, save
  2694.      * the display name in the DISPLAY environment variable so
  2695.      * that it will be available to subprocesses created by us.
  2696.      */
  2697.  
  2698.     if (numMainWindows == 0) {
  2699.         Tcl_SetVar2(interp, "env", "DISPLAY", display, TCL_GLOBAL_ONLY);
  2700.     }
  2701.     }
  2702.     if (colormap != NULL) {
  2703.     args[argc] = "-colormap";
  2704.     args[argc+1] = colormap;
  2705.     argc += 2;
  2706.         colormap = NULL;
  2707.     }
  2708.     if (use != NULL) {
  2709.     args[argc] = "-use";
  2710.     args[argc+1] = use;
  2711.     argc += 2;
  2712.         use = NULL;
  2713.     }
  2714.     if (visual != NULL) {
  2715.     args[argc] = "-visual";
  2716.     args[argc+1] = visual;
  2717.     argc += 2;
  2718.         visual = NULL;
  2719.     }
  2720.     args[argc] = NULL;
  2721.     code = TkCreateFrame((ClientData) NULL, interp, argc, args, 1, name);
  2722.  
  2723.     Tcl_DStringFree(&class);
  2724.     if (code != TCL_OK) {
  2725.     goto done;
  2726.     }
  2727.     Tcl_ResetResult(interp);
  2728.     if (synchronize) {
  2729.     XSynchronize(Tk_Display(Tk_MainWindow(interp)), True);
  2730.     }
  2731.  
  2732.     /*
  2733.      * Set the geometry of the main window, if requested.  Put the
  2734.      * requested geometry into the "geometry" variable.
  2735.      */
  2736.  
  2737.     if (geometry != NULL) {
  2738.     Tcl_SetVar(interp, "geometry", geometry, TCL_GLOBAL_ONLY);
  2739.     code = Tcl_VarEval(interp, "wm geometry . ", geometry, (char *) NULL);
  2740.     if (code != TCL_OK) {
  2741.         goto done;
  2742.     }
  2743.         geometry = NULL;
  2744.     }
  2745.     if (Tcl_PkgRequire(interp, "Tcl", TCL_VERSION, 1) == NULL) {
  2746.     code = TCL_ERROR;
  2747.     goto done;
  2748.     }
  2749.     code = Tcl_PkgProvide(interp, "Tk", TK_VERSION);
  2750.     if (code != TCL_OK) {
  2751.     goto done;
  2752.     }
  2753.  
  2754.     /*
  2755.      * Invoke platform-specific initialization.
  2756.      */
  2757.  
  2758.     code = TkpInit(interp);
  2759.  
  2760.     done:
  2761.     if (argv != NULL) {
  2762.     ckfree((char *) argv);
  2763.     }
  2764.     return code;
  2765. }
  2766.