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 / mac / tkMacCursor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  6.8 KB  |  295 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tkMacCursor.c --
  3.  *
  4.  *    This file contains Macintosh specific cursor related routines.
  5.  *
  6.  * Copyright (c) 1995-1996 Sun Microsystems, Inc.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * SCCS: @(#) tkMacCursor.c 1.17 96/12/17 15:23:07
  12.  */
  13.  
  14. #include "tkPort.h"
  15. #include "tkInt.h"
  16. #include "tkMacInt.h"
  17.  
  18. #include <Resources.h>
  19. #include <ToolUtils.h>
  20.  
  21. /*
  22.  * There are three different ways to set the cursor on the Mac.
  23.  */
  24. #define ARROW    0    /* The arrow cursor. */
  25. #define COLOR    1    /* Cursors of type crsr. */
  26. #define NORMAL    2    /* Cursors of type CURS. */
  27.  
  28. /*
  29.  * The following data structure contains the system specific data
  30.  * necessary to control Windows cursors.
  31.  */
  32.  
  33. typedef struct {
  34.     TkCursor info;        /* Generic cursor info used by tkCursor.c */
  35.     Handle macCursor;        /* Resource containing Macintosh cursor. */
  36.     int type;            /* Type of Mac cursor: arrow, crsr, CURS */
  37. } TkMacCursor;
  38.  
  39. /*
  40.  * The table below is used to map from the name of a predefined cursor
  41.  * to its resource identifier.
  42.  */
  43.  
  44. static struct CursorName {
  45.     char *name;
  46.     int id;
  47. } cursorNames[] = {
  48.     {"ibeam",        1},
  49.     {"text",        1},
  50.     {"xterm",        1},
  51.     {"cross",        2},
  52.     {"crosshair",    2},
  53.     {"cross-hair",    2},
  54.     {"plus",        3},
  55.     {"watch",        4},
  56.     {"arrow",        5},
  57.     {NULL,        0}
  58. };
  59.  
  60. /*
  61.  * Declarations of static variables used in this file.
  62.  */
  63.  
  64. static TkMacCursor *    gCurrentCursor = NULL;     /* A pointer to the current
  65.                           * cursor. */
  66. static int        gResizeOverride = false; /* A boolean indicating wether
  67.                           * we should use the resize
  68.                           * cursor during installations. */
  69.  
  70. /*
  71.  *----------------------------------------------------------------------
  72.  *
  73.  * TkGetCursorByName --
  74.  *
  75.  *    Retrieve a system cursor by name.  
  76.  *
  77.  * Results:
  78.  *    Returns a new cursor, or NULL on errors.  
  79.  *
  80.  * Side effects:
  81.  *    Allocates a new cursor.
  82.  *
  83.  *----------------------------------------------------------------------
  84.  */
  85.  
  86. TkCursor *
  87. TkGetCursorByName(
  88.     Tcl_Interp *interp,        /* Interpreter to use for error reporting. */
  89.     Tk_Window tkwin,        /* Window in which cursor will be used. */
  90.     Tk_Uid string)        /* Description of cursor.  See manual entry
  91.                  * for details on legal syntax. */
  92. {
  93.     struct CursorName *namePtr;
  94.     TkMacCursor *macCursorPtr;
  95.  
  96.     /*
  97.      * Check for the cursor in the system cursor set.
  98.      */
  99.  
  100.     for (namePtr = cursorNames; namePtr->name != NULL; namePtr++) {
  101.     if (strcmp(namePtr->name, string) == 0) {
  102.         break;
  103.     }
  104.     }
  105.  
  106.     macCursorPtr = (TkMacCursor *) ckalloc(sizeof(TkMacCursor));
  107.     macCursorPtr->info.cursor = (Tk_Cursor) macCursorPtr;
  108.     if (namePtr->name != NULL) {
  109.         if (namePtr->id == 5) {
  110.         macCursorPtr->macCursor = (Handle) -1;
  111.         macCursorPtr->type = ARROW;
  112.         } else {
  113.         macCursorPtr->macCursor = (Handle) GetCursor(namePtr->id);
  114.         macCursorPtr->type = NORMAL;
  115.     }
  116.     } else {
  117.         Handle resource;
  118.     Str255 curName;
  119.         
  120.     strcpy((char *) curName + 1, string);
  121.     curName[0] = strlen(string);
  122.     resource = GetNamedResource('crsr', curName);
  123.     if (resource != NULL) {
  124.         short id;
  125.         Str255 theName;
  126.         ResType    theType;
  127.  
  128.         HLock(resource);
  129.         GetResInfo(resource, &id, &theType, theName);
  130.         HUnlock(resource);
  131.         macCursorPtr->macCursor = (Handle) GetCCursor(id);
  132.         macCursorPtr->type = COLOR;
  133.     }
  134.  
  135.     if (resource == NULL) {
  136.         macCursorPtr->macCursor = GetNamedResource('CURS', curName);
  137.         macCursorPtr->type = NORMAL;
  138.     }
  139.     }
  140.     if (macCursorPtr->macCursor == NULL) {
  141.     ckfree((char *)macCursorPtr);
  142.     Tcl_AppendResult(interp, "bad cursor spec \"", string, "\"",
  143.         (char *) NULL);
  144.     return NULL;
  145.     } else {
  146.     return (TkCursor *) macCursorPtr;
  147.     }
  148. }
  149.  
  150. /*
  151.  *----------------------------------------------------------------------
  152.  *
  153.  * TkCreateCursorFromData --
  154.  *
  155.  *    Creates a cursor from the source and mask bits.
  156.  *
  157.  * Results:
  158.  *    Returns a new cursor, or NULL on errors.
  159.  *
  160.  * Side effects:
  161.  *    Allocates a new cursor.
  162.  *
  163.  *----------------------------------------------------------------------
  164.  */
  165.  
  166. TkCursor *
  167. TkCreateCursorFromData(
  168.     Tk_Window tkwin,        /* Window in which cursor will be used. */
  169.     char *source,        /* Bitmap data for cursor shape. */
  170.     char *mask,            /* Bitmap data for cursor mask. */
  171.     int width, int height,    /* Dimensions of cursor. */
  172.     int xHot, int yHot,        /* Location of hot-spot in cursor. */
  173.     XColor fgColor,        /* Foreground color for cursor. */
  174.     XColor bgColor)        /* Background color for cursor. */
  175. {
  176.     return NULL;
  177. }
  178.  
  179. /*
  180.  *----------------------------------------------------------------------
  181.  *
  182.  * TkFreeCursor --
  183.  *
  184.  *    This procedure is called to release a cursor allocated by
  185.  *    TkGetCursorByName.
  186.  *
  187.  * Results:
  188.  *    None.
  189.  *
  190.  * Side effects:
  191.  *    The cursor data structure is deallocated.
  192.  *
  193.  *----------------------------------------------------------------------
  194.  */
  195.  
  196. void
  197. TkFreeCursor(
  198.     TkCursor *cursorPtr)
  199. {
  200.     TkMacCursor *macCursorPtr = (TkMacCursor *) cursorPtr;
  201.  
  202.     switch (macCursorPtr->type) {
  203.     case COLOR:
  204.         DisposeCCursor((CCrsrHandle) macCursorPtr->macCursor);
  205.         break;
  206.     case NORMAL:
  207.         ReleaseResource(macCursorPtr->macCursor);
  208.         break;
  209.     }
  210.  
  211.     if (macCursorPtr == gCurrentCursor) {
  212.     gCurrentCursor = NULL;
  213.     }
  214.     
  215.     ckfree((char *) macCursorPtr);
  216. }
  217.  
  218. /*
  219.  *----------------------------------------------------------------------
  220.  *
  221.  * TkMacInstallCursor --
  222.  *
  223.  *    Installs either the current cursor as defined by TkpSetCursor
  224.  *    or a resize cursor as the cursor the Macintosh should currently
  225.  *    display.
  226.  *
  227.  * Results:
  228.  *    None.
  229.  *
  230.  * Side effects:
  231.  *    Changes the Macintosh mouse cursor.
  232.  *
  233.  *----------------------------------------------------------------------
  234.  */
  235.  
  236. void
  237. TkMacInstallCursor(
  238.     int resizeOverride)
  239. {
  240.     TkMacCursor *macCursorPtr = gCurrentCursor;
  241.     CCrsrHandle ccursor;
  242.     CursHandle cursor;
  243.  
  244.     gResizeOverride = resizeOverride;
  245.     
  246.     if (resizeOverride) {
  247.     cursor = (CursHandle) GetNamedResource('CURS', "\presize");
  248.     SetCursor(*cursor);
  249.     } else if (macCursorPtr == NULL || macCursorPtr->type == ARROW) {
  250.     SetCursor(&tcl_macQdPtr->arrow);
  251.     } else {
  252.     switch (macCursorPtr->type) {
  253.         case COLOR:
  254.         ccursor = (CCrsrHandle) macCursorPtr->macCursor;
  255.         SetCCursor(ccursor);
  256.         break;
  257.         case NORMAL:
  258.         cursor = (CursHandle) macCursorPtr->macCursor;
  259.         SetCursor(*cursor);
  260.         break;
  261.     }
  262.     }
  263. }
  264.  
  265. /*
  266.  *----------------------------------------------------------------------
  267.  *
  268.  * TkpSetCursor --
  269.  *
  270.  *    Set the current cursor and install it.
  271.  *
  272.  * Results:
  273.  *    None.
  274.  *
  275.  * Side effects:
  276.  *    Changes the current cursor.
  277.  *
  278.  *----------------------------------------------------------------------
  279.  */
  280.  
  281. void
  282. TkpSetCursor(
  283.     TkpCursor cursor)
  284. {
  285.     if (cursor == None) {
  286.     gCurrentCursor = NULL;
  287.     } else {
  288.     gCurrentCursor = (TkMacCursor *) cursor;
  289.     }
  290.  
  291.     if (tkMacAppInFront) {
  292.     TkMacInstallCursor(gResizeOverride);
  293.     }
  294. }
  295.