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

  1. /* 
  2.  * tkMacBitmap.c --
  3.  *
  4.  *    This file handles the implementation of native bitmaps.
  5.  *
  6.  * Copyright (c) 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: @(#) tkMacBitmap.c 1.4 96/12/13 11:13:16
  12.  */
  13.  
  14. #include "tkPort.h"
  15. #include "tk.h"
  16. #include "tkMacInt.h"
  17.  
  18. #include <Icons.h>
  19. #include <Dialogs.h>
  20. #include <Resources.h>
  21. #include <Strings.h>
  22.  
  23. /*
  24.  * Depending on the resource type there are different ways to
  25.  * draw native icons.
  26.  */
  27. #define TYPE1    0    /* Family icon suite. */
  28. #define TYPE2    1    /* ICON resource. */
  29. #define TYPE3    2    /* cicn resource. */
  30.  
  31. /*
  32.  * This data structure describes the id and type of a given icon.
  33.  * It is used as the source for native icons.
  34.  */
  35. typedef struct {
  36.     int id;        /* Resource Id for Icon. */
  37.     long int type;    /* Type of icon. */
  38. } NativeIcon;
  39.  
  40. /*
  41.  * This structure holds information about native bitmaps.
  42.  */
  43.  
  44. typedef struct {
  45.     char *name;            /* Name of icon. */
  46.     long int type;        /* Type of icon. */
  47.     int id;            /* Id of icon. */
  48.     int size;            /* Size of icon. */
  49. } BuiltInIcon;
  50.  
  51. /*
  52.  * This array mapps a string name to the supported builtin icons
  53.  * on the Macintosh.
  54.  */
  55.  
  56. static BuiltInIcon builtInIcons[] = {
  57.     {"document",    TYPE1,    kGenericDocumentIconResource,        32},
  58.     {"stationery",    TYPE1,    kGenericStationeryIconResource,        32},
  59.     {"edition",        TYPE1,    kGenericEditionFileIconResource,    32},
  60.     {"application",    TYPE1,    kGenericApplicationIconResource,    32},
  61.     {"accessory",    TYPE1,    kGenericDeskAccessoryIconResource,    32},
  62.     {"folder",        TYPE1,    kGenericFolderIconResource,        32},
  63.     {"pfolder",        TYPE1,    kPrivateFolderIconResource,        32},
  64.     {"trash",        TYPE1,    kTrashIconResource,            32},
  65.     {"floppy",        TYPE1,    kFloppyIconResource,            32},
  66.     {"ramdisk",        TYPE1,    kGenericRAMDiskIconResource,        32},
  67.     {"cdrom",        TYPE1,    kGenericCDROMIconResource,        32},
  68.     {"preferences",    TYPE1,    kGenericPreferencesIconResource,    32},
  69.     {"querydoc",    TYPE1,    kGenericQueryDocumentIconResource,    32},
  70.     {"stop",        TYPE2,    kStopIcon,                32},
  71.     {"note",        TYPE2,    kNoteIcon,                32},
  72.     {"caution",        TYPE2,    kCautionIcon,                32},
  73.     {(char *) NULL,    0,    0,                     0}
  74. };
  75.  
  76. /*
  77.  *----------------------------------------------------------------------
  78.  *
  79.  * TkpDefineNativeBitmaps --
  80.  *
  81.  *    Add native bitmaps.
  82.  *
  83.  * Results:
  84.  *    A standard Tcl result.  If an error occurs then TCL_ERROR is
  85.  *    returned and a message is left in interp->result.
  86.  *
  87.  * Side effects:
  88.  *    "Name" is entered into the bitmap table and may be used from
  89.  *    here on to refer to the given bitmap.
  90.  *
  91.  *----------------------------------------------------------------------
  92.  */
  93.  
  94. void
  95. TkpDefineNativeBitmaps()
  96. {
  97.     int new;
  98.     Tcl_HashEntry *predefHashPtr;
  99.     TkPredefBitmap *predefPtr;
  100.     char * name;
  101.     BuiltInIcon *builtInPtr;
  102.     NativeIcon *nativeIconPtr;
  103.     
  104.     for (builtInPtr = builtInIcons; builtInPtr->name != NULL; builtInPtr++) {
  105.     name = Tk_GetUid(builtInPtr->name);
  106.     predefHashPtr = Tcl_CreateHashEntry(&tkPredefBitmapTable, name, &new);
  107.     if (!new) {
  108.         continue;
  109.     }
  110.     predefPtr = (TkPredefBitmap *) ckalloc(sizeof(TkPredefBitmap));
  111.     nativeIconPtr = (NativeIcon *) ckalloc(sizeof(NativeIcon));
  112.     nativeIconPtr->id = builtInPtr->id;
  113.     nativeIconPtr->type = builtInPtr->type;
  114.     predefPtr->source = (char *) nativeIconPtr;
  115.     predefPtr->width = builtInPtr->size;
  116.     predefPtr->height = builtInPtr->size;
  117.     predefPtr->native = 1;
  118.     Tcl_SetHashValue(predefHashPtr, predefPtr);
  119.     }
  120. }
  121.  
  122. /*
  123.  *----------------------------------------------------------------------
  124.  *
  125.  * TkpCreateNativeBitmap --
  126.  *
  127.  *    Add native bitmaps.
  128.  *
  129.  * Results:
  130.  *    A standard Tcl result.  If an error occurs then TCL_ERROR is
  131.  *    returned and a message is left in interp->result.
  132.  *
  133.  * Side effects:
  134.  *    "Name" is entered into the bitmap table and may be used from
  135.  *    here on to refer to the given bitmap.
  136.  *
  137.  *----------------------------------------------------------------------
  138.  */
  139.  
  140. Pixmap
  141. TkpCreateNativeBitmap(
  142.     Display *display,
  143.     char * source)        /* Info about the icon to build. */
  144. {
  145.     Pixmap pix;
  146.     GWorldPtr destPort;
  147.     Rect destRect;
  148.     Handle icon;
  149.     CGrafPtr saveWorld;
  150.     GDHandle saveDevice;
  151.     NativeIcon *nativeIconPtr;
  152.     
  153.     pix = Tk_GetPixmap(display, None, 32, 32, 0);
  154.     destPort = TkMacGetDrawablePort(pix);
  155.  
  156.     GetGWorld(&saveWorld, &saveDevice);
  157.     SetGWorld(destPort, NULL);
  158.  
  159.     nativeIconPtr = (NativeIcon *) source;
  160.     SetRect(&destRect, 0, 0, 32, 32);
  161.     if (nativeIconPtr->type == TYPE1) {
  162.     RGBColor white = {0xFFFF, 0xFFFF, 0xFFFF};
  163.  
  164.     RGBForeColor(&white);
  165.     PaintRect(&destRect);
  166.     PlotIconID(&destRect, atAbsoluteCenter, ttNone, nativeIconPtr->id);
  167.     } else if (nativeIconPtr->type == TYPE2) {
  168.     icon = GetIcon(nativeIconPtr->id);
  169.     if (icon != NULL) {
  170.         RGBColor black = {0, 0, 0};
  171.     
  172.         RGBForeColor(&black);
  173.         PlotIcon(&destRect, icon);
  174.         ReleaseResource(icon);
  175.     }
  176.     }
  177.  
  178.     SetGWorld(saveWorld, saveDevice);
  179.     return pix;
  180. }
  181.  
  182. /*
  183.  *----------------------------------------------------------------------
  184.  *
  185.  * TkpGetNativeAppBitmap --
  186.  *
  187.  *    Add native bitmaps.
  188.  *
  189.  * Results:
  190.  *    A standard Tcl result.  If an error occurs then TCL_ERROR is
  191.  *    returned and a message is left in interp->result.
  192.  *
  193.  * Side effects:
  194.  *    "Name" is entered into the bitmap table and may be used from
  195.  *    here on to refer to the given bitmap.
  196.  *
  197.  *----------------------------------------------------------------------
  198.  */
  199.  
  200. Pixmap
  201. TkpGetNativeAppBitmap(
  202.     Display *display,    /* The display. */
  203.     char *name,        /* The name of the bitmap. */
  204.     int *width,        /* The width & height of the bitmap. */
  205.     int *height)
  206. {
  207.     Pixmap pix;
  208.     CGrafPtr saveWorld;
  209.     GDHandle saveDevice;
  210.     GWorldPtr destPort;
  211.     Rect destRect;
  212.     Handle resource;
  213.     int type;
  214.  
  215.     c2pstr(name);
  216.     resource = GetNamedResource('cicn', (StringPtr) name);
  217.     if (resource != NULL) {
  218.     type = TYPE3;
  219.     } else {
  220.     resource = GetNamedResource('ICON', (StringPtr) name);
  221.     if (resource != NULL) {
  222.         type = TYPE2;
  223.     }
  224.     }
  225.     p2cstr((StringPtr) name);
  226.     
  227.     if (resource == NULL) {
  228.     return NULL;
  229.     }
  230.     
  231.     pix = Tk_GetPixmap(display, None, 32, 32, 0);
  232.     destPort = TkMacGetDrawablePort(pix);
  233.  
  234.     GetGWorld(&saveWorld, &saveDevice);
  235.     SetGWorld(destPort, NULL);
  236.     
  237.     SetRect(&destRect, 0, 0, 32, 32);
  238.     if (type == TYPE2) {
  239.     RGBColor black = {0, 0, 0};
  240.     
  241.     RGBForeColor(&black);
  242.     PlotIcon(&destRect, resource);
  243.     ReleaseResource(resource);
  244.     } else if (type == TYPE3) {
  245.     RGBColor white = {0xFFFF, 0xFFFF, 0xFFFF};
  246.     short id;
  247.     ResType theType;
  248.     Str255 dummy;
  249.     
  250.     /*
  251.      * We need to first paint the background white.  Also, for
  252.      * some reason we *must* use GetCIcon instead of GetNamedResource
  253.      * for PlotCIcon to work - so we use GetResInfo to get the id.
  254.      */
  255.     RGBForeColor(&white);
  256.     PaintRect(&destRect);
  257.     GetResInfo(resource, &id, &theType, dummy);
  258.     ReleaseResource(resource);
  259.     resource = (Handle) GetCIcon(id);
  260.     PlotCIcon(&destRect, (CIconHandle) resource);
  261.     DisposeCIcon((CIconHandle) resource);
  262.     }
  263.     
  264.     *width = 32;
  265.     *height = 32;
  266.     SetGWorld(saveWorld, saveDevice);
  267.     return pix;
  268. }
  269.