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

  1. /* 
  2.  * tkCursor.c --
  3.  *
  4.  *    This file maintains a database of read-only cursors for the Tk
  5.  *    toolkit.  This allows cursors to be shared between widgets and
  6.  *    also avoids round-trips to the X server.
  7.  *
  8.  * Copyright (c) 1990-1993 The Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * Permission is hereby granted, without written agreement and without
  12.  * license or royalty fees, to use, copy, modify, and distribute this
  13.  * software and its documentation for any purpose, provided that the
  14.  * above copyright notice and the following two paragraphs appear in
  15.  * all copies of this software.
  16.  * 
  17.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  18.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  19.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  20.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21.  *
  22.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  23.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  24.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  25.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  26.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  27.  */
  28.  
  29. #ifndef lint
  30. static char rcsid[] = "$Header: /user6/ouster/wish/RCS/tkCursor.c,v 1.16 93/06/16 17:16:20 ouster Exp $ SPRITE (Berkeley)";
  31. #endif /* not lint */
  32.  
  33. #include "tkConfig.h"
  34. #include "tkInt.h"
  35.  
  36. /*
  37.  * One of the following data structures exists for each cursor that is
  38.  * currently active.  Each structure is indexed with two hash tables
  39.  * defined below.  One of the tables is idTable, and the other is either
  40.  * nameTable or dataTable, also defined below.
  41.  * .
  42.  */
  43.  
  44. typedef struct {
  45.     Cursor cursor;        /* X identifier for cursor. */
  46.     Display *display;        /* Display for which cursor is valid. */
  47.     int refCount;        /* Number of active uses of cursor. */
  48.     Tcl_HashTable *otherTable;    /* Second table (other than idTable) used
  49.                  * to index this entry. */
  50.     Tcl_HashEntry *hashPtr;    /* Entry in otherTable for this structure
  51.                  * (needed when deleting). */
  52. } TkCursor;
  53.  
  54. /*
  55.  * Hash table to map from a textual description of a cursor to the
  56.  * TkCursor record for the cursor, and key structure used in that
  57.  * hash table:
  58.  */
  59.  
  60. static Tcl_HashTable nameTable;
  61. typedef struct {
  62.     Tk_Uid name;        /* Textual name for desired cursor. */
  63.     Display *display;        /* Display for which cursor will be used. */
  64. } NameKey;
  65.  
  66. /*
  67.  * Hash table to map from a collection of in-core data about a
  68.  * cursor (bitmap contents, etc.) to a TkCursor structure:
  69.  */
  70.  
  71. static Tcl_HashTable dataTable;
  72. typedef struct {
  73.     char *source;        /* Cursor bits. */
  74.     char *mask;            /* Mask bits. */
  75.     unsigned int width, height;    /* Dimensions of cursor (and data
  76.                  * and mask). */
  77.     int xHot, yHot;        /* Location of cursor hot-spot. */
  78.     Tk_Uid fg, bg;        /* Colors for cursor. */
  79.     Display *display;        /* Display on which cursor will be used. */
  80. } DataKey;
  81.  
  82. /*
  83.  * Hash table that maps from <display + cursor id> to the TkCursor structure
  84.  * for the cursor.  This table is used by Tk_FreeCursor.
  85.  */
  86.  
  87. static Tcl_HashTable idTable;
  88. typedef struct {
  89.     Display *display;        /* Display for which cursor was allocated. */
  90.     Cursor cursor;        /* Cursor identifier. */
  91. } IdKey;
  92.  
  93. static int initialized = 0;    /* 0 means static structures haven't been
  94.                  * initialized yet. */
  95.  
  96. /*
  97.  * The table below is used to map from the name of a cursor to its
  98.  * index in the official cursor font:
  99.  */
  100.  
  101. static struct CursorName {
  102.     char        *name;
  103.     unsigned int    shape;
  104. } cursorNames[] = {
  105.     {"X_cursor",        XC_X_cursor},
  106.     {"arrow",            XC_arrow},
  107.     {"based_arrow_down",    XC_based_arrow_down},
  108.     {"based_arrow_up",        XC_based_arrow_up},
  109.     {"boat",            XC_boat},
  110.     {"bogosity",        XC_bogosity},
  111.     {"bottom_left_corner",    XC_bottom_left_corner},
  112.     {"bottom_right_corner",    XC_bottom_right_corner},
  113.     {"bottom_side",        XC_bottom_side},
  114.     {"bottom_tee",        XC_bottom_tee},
  115.     {"box_spiral",        XC_box_spiral},
  116.     {"center_ptr",        XC_center_ptr},
  117.     {"circle",            XC_circle},
  118.     {"clock",            XC_clock},
  119.     {"coffee_mug",        XC_coffee_mug},
  120.     {"cross",            XC_cross},
  121.     {"cross_reverse",        XC_cross_reverse},
  122.     {"crosshair",        XC_crosshair},
  123.     {"diamond_cross",        XC_diamond_cross},
  124.     {"dot",            XC_dot},
  125.     {"dotbox",            XC_dotbox},
  126.     {"double_arrow",        XC_double_arrow},
  127.     {"draft_large",        XC_draft_large},
  128.     {"draft_small",        XC_draft_small},
  129.     {"draped_box",        XC_draped_box},
  130.     {"exchange",        XC_exchange},
  131.     {"fleur",            XC_fleur},
  132.     {"gobbler",            XC_gobbler},
  133.     {"gumby",            XC_gumby},
  134.     {"hand1",            XC_hand1},
  135.     {"hand2",            XC_hand2},
  136.     {"heart",            XC_heart},
  137.     {"icon",            XC_icon},
  138.     {"iron_cross",        XC_iron_cross},
  139.     {"left_ptr",        XC_left_ptr},
  140.     {"left_side",        XC_left_side},
  141.     {"left_tee",        XC_left_tee},
  142.     {"leftbutton",        XC_leftbutton},
  143.     {"ll_angle",        XC_ll_angle},
  144.     {"lr_angle",        XC_lr_angle},
  145.     {"man",            XC_man},
  146.     {"middlebutton",        XC_middlebutton},
  147.     {"mouse",            XC_mouse},
  148.     {"pencil",            XC_pencil},
  149.     {"pirate",            XC_pirate},
  150.     {"plus",            XC_plus},
  151.     {"question_arrow",        XC_question_arrow},
  152.     {"right_ptr",        XC_right_ptr},
  153.     {"right_side",        XC_right_side},
  154.     {"right_tee",        XC_right_tee},
  155.     {"rightbutton",        XC_rightbutton},
  156.     {"rtl_logo",        XC_rtl_logo},
  157.     {"sailboat",        XC_sailboat},
  158.     {"sb_down_arrow",        XC_sb_down_arrow},
  159.     {"sb_h_double_arrow",    XC_sb_h_double_arrow},
  160.     {"sb_left_arrow",        XC_sb_left_arrow},
  161.     {"sb_right_arrow",        XC_sb_right_arrow},
  162.     {"sb_up_arrow",        XC_sb_up_arrow},
  163.     {"sb_v_double_arrow",    XC_sb_v_double_arrow},
  164.     {"shuttle",            XC_shuttle},
  165.     {"sizing",            XC_sizing},
  166.     {"spider",            XC_spider},
  167.     {"spraycan",        XC_spraycan},
  168.     {"star",            XC_star},
  169.     {"target",            XC_target},
  170.     {"tcross",            XC_tcross},
  171.     {"top_left_arrow",        XC_top_left_arrow},
  172.     {"top_left_corner",        XC_top_left_corner},
  173.     {"top_right_corner",    XC_top_right_corner},
  174.     {"top_side",        XC_top_side},
  175.     {"top_tee",            XC_top_tee},
  176.     {"trek",            XC_trek},
  177.     {"ul_angle",        XC_ul_angle},
  178.     {"umbrella",        XC_umbrella},
  179.     {"ur_angle",        XC_ur_angle},
  180.     {"watch",            XC_watch},
  181.     {"xterm",            XC_xterm},
  182.     {NULL,            0}
  183. };
  184.  
  185. /*
  186.  * Font to use for cursors:
  187.  */
  188.  
  189. #ifndef CURSORFONT
  190. #define CURSORFONT "cursor"
  191. #endif
  192.  
  193. /*
  194.  * Forward declarations for procedures defined in this file:
  195.  */
  196.  
  197. static void        CursorInit _ANSI_ARGS_((void));
  198.  
  199. /*
  200.  *----------------------------------------------------------------------
  201.  *
  202.  * Tk_GetCursor --
  203.  *
  204.  *    Given a string describing a cursor, locate (or create if necessary)
  205.  *    a cursor that fits the description.
  206.  *
  207.  * Results:
  208.  *    The return value is the X identifer for the desired cursor,
  209.  *    unless string couldn't be parsed correctly.  In this case,
  210.  *    None is returned and an error message is left in interp->result.
  211.  *    The caller should never modify the cursor that is returned, and
  212.  *    should eventually call Tk_FreeCursor when the cursor is no longer
  213.  *    needed.
  214.  *
  215.  * Side effects:
  216.  *    The cursor is added to an internal database with a reference count.
  217.  *    For each call to this procedure, there should eventually be a call
  218.  *    to Tk_FreeCursor, so that the database can be cleaned up when cursors
  219.  *    aren't needed anymore.
  220.  *
  221.  *----------------------------------------------------------------------
  222.  */
  223.  
  224. Cursor
  225. Tk_GetCursor(interp, tkwin, string)
  226.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  227.     Tk_Window tkwin;        /* Window in which cursor will be used. */
  228.     Tk_Uid string;        /* Description of cursor.  See manual entry
  229.                  * for details on legal syntax. */
  230. {
  231.     NameKey nameKey;
  232.     IdKey idKey;
  233.     Tcl_HashEntry *nameHashPtr, *idHashPtr;
  234.     register TkCursor *cursorPtr;
  235.     int new;
  236.     Cursor cursor;
  237.     int argc;
  238.     char **argv = NULL;
  239.     Pixmap source = None;
  240.     Pixmap mask = None;
  241.  
  242.     if (!initialized) {
  243.     CursorInit();
  244.     }
  245.  
  246.     nameKey.name = string;
  247.     nameKey.display = Tk_Display(tkwin);
  248.     nameHashPtr = Tcl_CreateHashEntry(&nameTable, (char *) &nameKey, &new);
  249.     if (!new) {
  250.     cursorPtr = (TkCursor *) Tcl_GetHashValue(nameHashPtr);
  251.     cursorPtr->refCount++;
  252.     return cursorPtr->cursor;
  253.     }
  254.  
  255.     /*
  256.      * No suitable cursor exists.  Parse the cursor name into fields
  257.      * and create a cursor, either from the standard cursor font or
  258.      * from bitmap files.
  259.      */
  260.  
  261.     if (Tcl_SplitList(interp, string, &argc, &argv) != TCL_OK) {
  262.     goto error;
  263.     }
  264.     if (argc == 0) {
  265.     badString:
  266.     Tcl_AppendResult(interp, "bad cursor spec \"", string, "\"",
  267.         (char *) NULL);
  268.     goto error;
  269.     }
  270.     if (argv[0][0] != '@') {
  271.     XColor fg, bg;
  272.     int maskIndex;
  273.     register struct CursorName *namePtr;
  274.     TkDisplay *dispPtr;
  275.  
  276.     /*
  277.      * The cursor is to come from the standard cursor font.  If one
  278.      * arg, it is cursor name (use black and white for fg and bg).
  279.      * If two args, they are name and fg color (ignore mask).  If
  280.      * three args, they are name, fg, bg.  Some of the code below
  281.      * is stolen from the XCreateFontCursor Xlib procedure.
  282.      */
  283.  
  284.     if (argc > 3) {
  285.         goto badString;
  286.     }
  287.     for (namePtr = cursorNames; ; namePtr++) {
  288.         if (namePtr->name == NULL) {
  289.         goto badString;
  290.         }
  291.         if ((namePtr->name[0] == argv[0][0])
  292.             && (strcmp(namePtr->name, argv[0]) == 0)) {
  293.         break;
  294.         }
  295.     }
  296.     maskIndex = namePtr->shape + 1;
  297.     if (argc == 1) {
  298.         fg.red = fg.green = fg.blue = 0;
  299.         bg.red = bg.green = bg.blue = 65535;
  300.     } else {
  301.         if (XParseColor(nameKey.display, Tk_Colormap(tkwin), argv[1],
  302.             &fg) == 0) {
  303.         Tcl_AppendResult(interp, "invalid color name \"", argv[1],
  304.             "\"", (char *) NULL);
  305.         goto error;
  306.         }
  307.         if (argc == 2) {
  308.         bg.red = bg.green = bg.blue = 0;
  309.         maskIndex = namePtr->shape;
  310.         } else {
  311.         if (XParseColor(nameKey.display, Tk_Colormap(tkwin), argv[2],
  312.             &bg) == 0) {
  313.             Tcl_AppendResult(interp, "invalid color name \"", argv[2],
  314.                 "\"", (char *) NULL);
  315.             goto error;
  316.         }
  317.         }
  318.     }
  319.     dispPtr = ((TkWindow *) tkwin)->dispPtr;
  320.     if (dispPtr->cursorFont == None) {
  321.         dispPtr->cursorFont = XLoadFont(nameKey.display, CURSORFONT);
  322.         if (dispPtr->cursorFont == None) {
  323.         interp->result = "couldn't load cursor font";
  324.         goto error;
  325.         }
  326.     }
  327.     cursor = XCreateGlyphCursor(nameKey.display, dispPtr->cursorFont,
  328.         dispPtr->cursorFont, namePtr->shape, maskIndex,
  329.         &fg, &bg);
  330.     } else {
  331.     unsigned int width, height, maskWidth, maskHeight;
  332.     int xHot, yHot, dummy1, dummy2;
  333.     XColor fg, bg;
  334.  
  335.     /*
  336.      * The cursor is to be created by reading bitmap files.  There
  337.      * should be either two elements in the list (source, color) or
  338.      * four (source mask fg bg).
  339.      */
  340.  
  341.     if ((argc != 2) && (argc != 4)) {
  342.         goto badString;
  343.     }
  344.     if (XReadBitmapFile(nameKey.display,
  345.         RootWindowOfScreen(Tk_Screen(tkwin)), &argv[0][1],
  346.         &width, &height, &source, &xHot, &yHot)
  347.         != BitmapSuccess) {
  348.         Tcl_AppendResult(interp, "error reading bitmap file \"",
  349.             &argv[0][1], "\"", (char *) NULL);
  350.         goto error;
  351.     }
  352.     if ((xHot < 0) || (yHot < 0) || (xHot >= width) || (yHot >= height)) {
  353.         Tcl_AppendResult(interp, "bad hot spot in bitmap file \"",
  354.             &argv[0][1], "\"", (char *) NULL);
  355.         goto error;
  356.     }
  357.     if (argc == 2) {
  358.         if (XParseColor(nameKey.display, Tk_Colormap(tkwin), argv[1],
  359.             &fg) == 0) {
  360.         Tcl_AppendResult(interp, "invalid color name \"",
  361.             argv[1], "\"", (char *) NULL);
  362.         goto error;
  363.         }
  364.         cursor = XCreatePixmapCursor(nameKey.display, source, source,
  365.             &fg, &fg, xHot, yHot);
  366.     } else {
  367.         if (XReadBitmapFile(nameKey.display,
  368.             RootWindowOfScreen(Tk_Screen(tkwin)), argv[1],
  369.             &maskWidth, &maskHeight, &mask, &dummy1,
  370.             &dummy2) != BitmapSuccess) {
  371.         Tcl_AppendResult(interp, "error reading bitmap file \"",
  372.             argv[1], "\"", (char *) NULL);
  373.         goto error;
  374.         }
  375.         if ((maskWidth != width) && (maskHeight != height)) {
  376.         interp->result =
  377.             "source and mask bitmaps have different sizes";
  378.         goto error;
  379.         }
  380.         if (XParseColor(nameKey.display, Tk_Colormap(tkwin), argv[2],
  381.             &fg) == 0) {
  382.         Tcl_AppendResult(interp, "invalid color name \"", argv[2],
  383.             "\"", (char *) NULL);
  384.         goto error;
  385.         }
  386.         if (XParseColor(nameKey.display, Tk_Colormap(tkwin), argv[3],
  387.             &bg) == 0) {
  388.         Tcl_AppendResult(interp, "invalid color name \"", argv[3],
  389.             "\"", (char *) NULL);
  390.         goto error;
  391.         }
  392.         cursor = XCreatePixmapCursor(nameKey.display, source, mask,
  393.             &fg, &bg, xHot, yHot);
  394.     }
  395.     }
  396.     ckfree((char *) argv);
  397.  
  398.     /*
  399.      * Add information about this cursor to our database.
  400.      */
  401.  
  402.     cursorPtr = (TkCursor *) ckalloc(sizeof(TkCursor));
  403.     cursorPtr->cursor = cursor;
  404.     cursorPtr->display = nameKey.display;
  405.     cursorPtr->refCount = 1;
  406.     cursorPtr->otherTable = &nameTable;
  407.     cursorPtr->hashPtr = nameHashPtr;
  408.     idKey.display = nameKey.display;
  409.     idKey.cursor = cursor;
  410.     idHashPtr = Tcl_CreateHashEntry(&idTable, (char *) &idKey, &new);
  411.     if (!new) {
  412.     panic("cursor already registered in Tk_GetCursor");
  413.     }
  414.     Tcl_SetHashValue(nameHashPtr, cursorPtr);
  415.     Tcl_SetHashValue(idHashPtr, cursorPtr);
  416.     return cursorPtr->cursor;
  417.  
  418.     error:
  419.     Tcl_DeleteHashEntry(nameHashPtr);
  420.     if (argv != NULL) {
  421.     ckfree((char *) argv);
  422.     }
  423.     if (source != None) {
  424.     XFreePixmap(nameKey.display, source);
  425.     }
  426.     if (mask != None) {
  427.     XFreePixmap(nameKey.display, mask);
  428.     }
  429.     return None;
  430. }
  431.  
  432. /*
  433.  *----------------------------------------------------------------------
  434.  *
  435.  * Tk_GetCursorFromData --
  436.  *
  437.  *    Given a description of the bits and colors for a cursor,
  438.  *    make a cursor that has the given properties.
  439.  *
  440.  * Results:
  441.  *    The return value is the X identifer for the desired cursor,
  442.  *    unless it couldn't be created properly.  In this case, None is
  443.  *    returned and an error message is left in interp->result.  The
  444.  *    caller should never modify the cursor that is returned, and
  445.  *    should eventually call Tk_FreeCursor when the cursor is no
  446.  *    longer needed.
  447.  *
  448.  * Side effects:
  449.  *    The cursor is added to an internal database with a reference count.
  450.  *    For each call to this procedure, there should eventually be a call
  451.  *    to Tk_FreeCursor, so that the database can be cleaned up when cursors
  452.  *    aren't needed anymore.
  453.  *
  454.  *----------------------------------------------------------------------
  455.  */
  456.  
  457. Cursor
  458. Tk_GetCursorFromData(interp, tkwin, source, mask, width, height,
  459.     xHot, yHot, fg, bg)
  460.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  461.     Tk_Window tkwin;        /* Window in which cursor will be used. */
  462.     char *source;        /* Bitmap data for cursor shape. */
  463.     char *mask;            /* Bitmap data for cursor mask. */
  464.     unsigned int width, height;    /* Dimensions of cursor. */
  465.     int xHot, yHot;        /* Location of hot-spot in cursor. */
  466.     Tk_Uid fg;            /* Foreground color for cursor. */
  467.     Tk_Uid bg;            /* Background color for cursor. */
  468. {
  469.     DataKey dataKey;
  470.     IdKey idKey;
  471.     Tcl_HashEntry *dataHashPtr, *idHashPtr;
  472.     register TkCursor *cursorPtr;
  473.     int new;
  474.     XColor fgColor, bgColor;
  475.     Pixmap sourcePixmap, maskPixmap;
  476.  
  477.     if (!initialized) {
  478.     CursorInit();
  479.     }
  480.  
  481.     dataKey.source = source;
  482.     dataKey.mask = mask;
  483.     dataKey.width = width;
  484.     dataKey.height = height;
  485.     dataKey.xHot = xHot;
  486.     dataKey.yHot = yHot;
  487.     dataKey.fg = fg;
  488.     dataKey.bg = bg;
  489.     dataKey.display = Tk_Display(tkwin);
  490.     dataHashPtr = Tcl_CreateHashEntry(&dataTable, (char *) &dataKey, &new);
  491.     if (!new) {
  492.     cursorPtr = (TkCursor *) Tcl_GetHashValue(dataHashPtr);
  493.     cursorPtr->refCount++;
  494.     return cursorPtr->cursor;
  495.     }
  496.  
  497.     /*
  498.      * No suitable cursor exists yet.  Make one using the data
  499.      * available and add it to the database.
  500.      */
  501.  
  502.     if (XParseColor(dataKey.display, Tk_Colormap(tkwin), fg, &fgColor) == 0) {
  503.     Tcl_AppendResult(interp, "invalid color name \"", fg, "\"",
  504.         (char *) NULL);
  505.     goto error;
  506.     }
  507.     if (XParseColor(dataKey.display, Tk_Colormap(tkwin), bg, &bgColor) == 0) {
  508.     Tcl_AppendResult(interp, "invalid color name \"", bg, "\"",
  509.         (char *) NULL);
  510.     goto error;
  511.     }
  512.  
  513.     cursorPtr = (TkCursor *) ckalloc(sizeof(TkCursor));
  514.     sourcePixmap = XCreateBitmapFromData(dataKey.display,
  515.         RootWindowOfScreen(Tk_Screen(tkwin)), source, width, height);
  516.     maskPixmap = XCreateBitmapFromData(dataKey.display, 
  517.         RootWindowOfScreen(Tk_Screen(tkwin)), mask, width, height);
  518.     cursorPtr->cursor = XCreatePixmapCursor(dataKey.display, sourcePixmap,
  519.         maskPixmap, &fgColor, &bgColor, xHot, yHot);
  520.     XFreePixmap(dataKey.display, sourcePixmap);
  521.     XFreePixmap(dataKey.display, maskPixmap);
  522.     cursorPtr->display = dataKey.display;
  523.     cursorPtr->refCount = 1;
  524.     cursorPtr->otherTable = &dataTable;
  525.     cursorPtr->hashPtr = dataHashPtr;
  526.     idKey.display = dataKey.display;
  527.     idKey.cursor = cursorPtr->cursor;
  528.     idHashPtr = Tcl_CreateHashEntry(&idTable, (char *) &idKey, &new);
  529.     if (!new) {
  530.     panic("cursor already registered in Tk_GetCursorFromData");
  531.     }
  532.     Tcl_SetHashValue(dataHashPtr, cursorPtr);
  533.     Tcl_SetHashValue(idHashPtr, cursorPtr);
  534.     return cursorPtr->cursor;
  535.  
  536.     error:
  537.     Tcl_DeleteHashEntry(dataHashPtr);
  538.     return None;
  539. }
  540.  
  541. /*
  542.  *--------------------------------------------------------------
  543.  *
  544.  * Tk_NameOfCursor --
  545.  *
  546.  *    Given a cursor, return a textual string identifying it.
  547.  *
  548.  * Results:
  549.  *    If cursor was created by Tk_GetCursor, then the return
  550.  *    value is the "string" that was used to create it.
  551.  *    Otherwise the return value is a string giving the X
  552.  *    identifier for the cursor.  The storage for the returned
  553.  *    string is only guaranteed to persist up until the next
  554.  *    call to this procedure.
  555.  *
  556.  * Side effects:
  557.  *    None.
  558.  *
  559.  *--------------------------------------------------------------
  560.  */
  561.  
  562. char *
  563. Tk_NameOfCursor(display, cursor)
  564.     Display *display;        /* Display for which cursor was allocated. */
  565.     Cursor cursor;        /* Identifier for cursor whose name is
  566.                  * wanted. */
  567. {
  568.     IdKey idKey;
  569.     Tcl_HashEntry *idHashPtr;
  570.     TkCursor *cursorPtr;
  571.     static char string[20];
  572.  
  573.     if (!initialized) {
  574.     printid:
  575.     sprintf(string, "cursor id 0x%x", cursor);
  576.     return string;
  577.     }
  578.     idKey.display = display;
  579.     idKey.cursor = cursor;
  580.     idHashPtr = Tcl_FindHashEntry(&idTable, (char *) &idKey);
  581.     if (idHashPtr == NULL) {
  582.     goto printid;
  583.     }
  584.     cursorPtr = (TkCursor *) Tcl_GetHashValue(idHashPtr);
  585.     if (cursorPtr->otherTable != &nameTable) {
  586.     goto printid;
  587.     }
  588.     return ((NameKey *) cursorPtr->hashPtr->key.words)->name;
  589. }
  590.  
  591. /*
  592.  *----------------------------------------------------------------------
  593.  *
  594.  * Tk_FreeCursor --
  595.  *
  596.  *    This procedure is called to release a cursor allocated by
  597.  *    Tk_GetCursor or TkGetCursorFromData.
  598.  *
  599.  * Results:
  600.  *    None.
  601.  *
  602.  * Side effects:
  603.  *    The reference count associated with cursor is decremented, and
  604.  *    it is officially deallocated if no-one is using it anymore.
  605.  *
  606.  *----------------------------------------------------------------------
  607.  */
  608.  
  609. void
  610. Tk_FreeCursor(display, cursor)
  611.     Display *display;        /* Display for which cursor was allocated. */
  612.     Cursor cursor;        /* Identifier for cursor to be released. */
  613. {
  614.     IdKey idKey;
  615.     Tcl_HashEntry *idHashPtr;
  616.     register TkCursor *cursorPtr;
  617.  
  618.     if (!initialized) {
  619.     panic("Tk_FreeCursor called before Tk_GetCursor");
  620.     }
  621.  
  622.     idKey.display = display;
  623.     idKey.cursor = cursor;
  624.     idHashPtr = Tcl_FindHashEntry(&idTable, (char *) &idKey);
  625.     if (idHashPtr == NULL) {
  626.     panic("Tk_FreeCursor received unknown cursor argument");
  627.     }
  628.     cursorPtr = (TkCursor *) Tcl_GetHashValue(idHashPtr);
  629.     cursorPtr->refCount--;
  630.     if (cursorPtr->refCount == 0) {
  631.     XFreeCursor(cursorPtr->display, cursorPtr->cursor);
  632.     Tcl_DeleteHashEntry(cursorPtr->hashPtr);
  633.     Tcl_DeleteHashEntry(idHashPtr);
  634.     ckfree((char *) cursorPtr);
  635.     }
  636. }
  637.  
  638. /*
  639.  *----------------------------------------------------------------------
  640.  *
  641.  * CursorInit --
  642.  *
  643.  *    Initialize the structures used for cursor management.
  644.  *
  645.  * Results:
  646.  *    None.
  647.  *
  648.  * Side effects:
  649.  *    Read the code.
  650.  *
  651.  *----------------------------------------------------------------------
  652.  */
  653.  
  654. static void
  655. CursorInit()
  656. {
  657.     initialized = 1;
  658.     Tcl_InitHashTable(&nameTable, sizeof(NameKey)/sizeof(int));
  659.     Tcl_InitHashTable(&dataTable, sizeof(DataKey)/sizeof(int));
  660.  
  661.     /*
  662.      * The call below is tricky:  can't use sizeof(IdKey) because it
  663.      * gets padded with extra unpredictable bytes on some 64-bit
  664.      * machines.
  665.      */
  666.  
  667.     Tcl_InitHashTable(&idTable, (sizeof(Display *) + sizeof(Cursor))
  668.         /sizeof(int));
  669. }
  670.