home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / tk3.3b1 / tkAtom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-16  |  5.2 KB  |  182 lines

  1. /* 
  2.  * tkAtom.c --
  3.  *
  4.  *    This file manages a cache of X Atoms in order to avoid
  5.  *    interactions with the X server.  It's much like the Xmu
  6.  *    routines, except it has a cleaner interface (caller
  7.  *    doesn't have to provide permanent storage for atom names,
  8.  *    for example).
  9.  *
  10.  * Copyright (c) 1990-1993 The Regents of the University of California.
  11.  * All rights reserved.
  12.  *
  13.  * Permission is hereby granted, without written agreement and without
  14.  * license or royalty fees, to use, copy, modify, and distribute this
  15.  * software and its documentation for any purpose, provided that the
  16.  * above copyright notice and the following two paragraphs appear in
  17.  * all copies of this software.
  18.  * 
  19.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  20.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  21.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  22.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23.  *
  24.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  25.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  26.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  27.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  28.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  29.  */
  30.  
  31. #ifndef lint
  32. static char rcsid[] = "$Header: /user6/ouster/wish/RCS/tkAtom.c,v 1.8 93/06/16 17:15:18 ouster Exp $ SPRITE (Berkeley)";
  33. #endif
  34.  
  35. #include "tkConfig.h"
  36. #include "tkInt.h"
  37.  
  38. /*
  39.  * Forward references to procedures defined in this file:
  40.  */
  41.  
  42. static void    AtomInit _ANSI_ARGS_((TkDisplay *dispPtr));
  43.  
  44. /*
  45.  *--------------------------------------------------------------
  46.  *
  47.  * Tk_InternAtom --
  48.  *
  49.  *    Given a string, produce the equivalent X atom.  This
  50.  *    procedure is equivalent to XInternAtom, except that it
  51.  *    keeps a local cache of atoms.  Once a name is known,
  52.  *    the server need not be contacted again for that name.
  53.  *
  54.  * Results:
  55.  *    The return value is the Atom corresponding to name.
  56.  *
  57.  * Side effects:
  58.  *    A new entry may be added to the local atom cache.
  59.  *
  60.  *--------------------------------------------------------------
  61.  */
  62.  
  63. Atom
  64. Tk_InternAtom(tkwin, name)
  65.     Tk_Window tkwin;        /* Window token;  map name to atom
  66.                  * for this window's display. */
  67.     char *name;            /* Name to turn into atom. */
  68. {
  69.     register TkDisplay *dispPtr;
  70.     register Tcl_HashEntry *hPtr;
  71.     int new;
  72.  
  73.     dispPtr = ((TkWindow *) tkwin)->dispPtr;
  74.     if (!dispPtr->atomInit) {
  75.     AtomInit(dispPtr);
  76.     }
  77.  
  78.     hPtr = Tcl_CreateHashEntry(&dispPtr->nameTable, name, &new);
  79.     if (new) {
  80.     Tcl_HashEntry *hPtr2;
  81.     Atom atom;
  82.  
  83.     atom = XInternAtom(dispPtr->display, name, False);
  84.     Tcl_SetHashValue(hPtr, atom);
  85.     hPtr2 = Tcl_CreateHashEntry(&dispPtr->atomTable, (char *) atom,
  86.         &new);
  87.     Tcl_SetHashValue(hPtr2, Tcl_GetHashKey(&dispPtr->nameTable, hPtr));
  88.     }
  89.     return (Atom) Tcl_GetHashValue(hPtr);
  90. }
  91.  
  92. /*
  93.  *--------------------------------------------------------------
  94.  *
  95.  * Tk_GetAtomName --
  96.  *
  97.  *    This procedure is equivalent to XGetAtomName except that
  98.  *    it uses the local atom cache to avoid contacting the
  99.  *    server.
  100.  *
  101.  * Results:
  102.  *    The return value is a character string corresponding to
  103.  *    the atom given by "atom".  This string's storage space
  104.  *    is static:  it need not be freed by the caller, and should
  105.  *    not be modified by the caller.  If "atom" doesn't exist
  106.  *    on tkwin's display, then the string "?bad atom?" is returned.
  107.  *
  108.  * Side effects:
  109.  *    None.
  110.  *
  111.  *--------------------------------------------------------------
  112.  */
  113.  
  114. char *
  115. Tk_GetAtomName(tkwin, atom)
  116.     Tk_Window tkwin;        /* Window token;  map atom to name
  117.                  * relative to this window's
  118.                  * display. */
  119.     Atom atom;            /* Atom whose name is wanted. */
  120. {
  121.     register TkDisplay *dispPtr;
  122.     register Tcl_HashEntry *hPtr;
  123.  
  124.     dispPtr = ((TkWindow *) tkwin)->dispPtr;
  125.     if (!dispPtr->atomInit) {
  126.     AtomInit(dispPtr);
  127.     }
  128.  
  129.     hPtr = Tcl_FindHashEntry(&dispPtr->atomTable, (char *) atom);
  130.     if (hPtr == NULL) {
  131.     char *name;
  132.     Tk_ErrorHandler handler;
  133.     int new, mustFree;
  134.  
  135.     handler= Tk_CreateErrorHandler(dispPtr->display, BadAtom,
  136.         -1, -1, (int (*)()) NULL, (ClientData) NULL);
  137.     name = XGetAtomName(dispPtr->display, atom);
  138.     mustFree = 1;
  139.     if (name == NULL) {
  140.         name = "?bad atom?";
  141.         mustFree = 0;
  142.     }
  143.     Tk_DeleteErrorHandler(handler);
  144.     hPtr = Tcl_CreateHashEntry(&dispPtr->nameTable, (char *) name,
  145.         &new);
  146.     Tcl_SetHashValue(hPtr, atom);
  147.     if (mustFree) {
  148.         XFree(name);
  149.     }
  150.     name = Tcl_GetHashKey(&dispPtr->nameTable, hPtr);
  151.     hPtr = Tcl_CreateHashEntry(&dispPtr->atomTable, (char *) atom,
  152.         &new);
  153.     Tcl_SetHashValue(hPtr, name);
  154.     }
  155.     return (char *) Tcl_GetHashValue(hPtr);
  156. }
  157.  
  158. /*
  159.  *--------------------------------------------------------------
  160.  *
  161.  * AtomInit --
  162.  *
  163.  *    Initialize atom-related information for a display.
  164.  *
  165.  * Results:
  166.  *    None.
  167.  *
  168.  * Side effects:
  169.  *    Tables get initialized, etc. etc..
  170.  *
  171.  *--------------------------------------------------------------
  172.  */
  173.  
  174. static void
  175. AtomInit(dispPtr)
  176.     register TkDisplay *dispPtr;    /* Display to initialize. */
  177. {
  178.     dispPtr->atomInit = 1;
  179.     Tcl_InitHashTable(&dispPtr->nameTable, TCL_STRING_KEYS);
  180.     Tcl_InitHashTable(&dispPtr->atomTable, TCL_ONE_WORD_KEYS);
  181. }
  182.