tkMacOSXBitmap.c
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:7k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /*
  2.  * tkMacOSXBitmap.c --
  3.  *
  4.  * This file handles the implementation of native bitmaps.
  5.  *
  6.  * Copyright (c) 1996-1997 Sun Microsystems, Inc.
  7.  * Copyright 2001, Apple Computer, Inc.
  8.  * Copyright (c) 2006-2007 Daniel A. Steffen <das@users.sourceforge.net>
  9.  *
  10.  * See the file "license.terms" for information on usage and redistribution
  11.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12.  *
  13.  * RCS: @(#) $Id: tkMacOSXBitmap.c,v 1.2.2.3 2007/04/29 02:26:47 das Exp $
  14.  */
  15. #include "tkMacOSXInt.h"
  16. /*
  17.  * Depending on the resource type there are different ways to
  18.  * draw native icons.
  19.  */
  20. #define TYPE1 0 /* Family icon suite. */
  21. #define TYPE2 1 /* ICON resource. */
  22. #define TYPE3 2 /* cicn resource. */
  23. /*
  24.  * This data structure describes the id and type of a given icon.
  25.  * It is used as the source for native icons.
  26.  */
  27. typedef struct {
  28.     int id; /* Resource Id for Icon. */
  29.     long int type; /* Type of icon. */
  30. } NativeIcon;
  31. /*
  32.  * This structure holds information about native bitmaps.
  33.  */
  34. typedef struct {
  35.     const char *name; /* Name of icon. */
  36.     long int type; /* Type of icon. */
  37.     int id; /* Id of icon. */
  38.     int size; /* Size of icon. */
  39. } BuiltInIcon;
  40. /*
  41.  * This array mapps a string name to the supported builtin icons
  42.  * on the Macintosh.
  43.  */
  44. static BuiltInIcon builtInIcons[] = {
  45.     {"document", TYPE1, kGenericDocumentIconResource, 32},
  46.     {"stationery", TYPE1, kGenericStationeryIconResource, 32},
  47.     {"edition", TYPE1, kGenericEditionFileIconResource, 32},
  48.     {"application", TYPE1, kGenericApplicationIconResource, 32},
  49.     {"accessory", TYPE1, kGenericDeskAccessoryIconResource, 32},
  50.     {"folder", TYPE1, kGenericFolderIconResource, 32},
  51.     {"pfolder", TYPE1, kPrivateFolderIconResource, 32},
  52.     {"trash", TYPE1, kTrashIconResource, 32},
  53.     {"floppy", TYPE1, kFloppyIconResource, 32},
  54.     {"ramdisk", TYPE1, kGenericRAMDiskIconResource, 32},
  55.     {"cdrom", TYPE1, kGenericCDROMIconResource, 32},
  56.     {"preferences", TYPE1, kGenericPreferencesIconResource, 32},
  57.     {"querydoc", TYPE1, kGenericQueryDocumentIconResource, 32},
  58.     {"stop", TYPE2, kStopIcon, 32},
  59.     {"note", TYPE2, kNoteIcon, 32},
  60.     {"caution", TYPE2, kCautionIcon, 32},
  61.     {NULL, 0, 0, 0}
  62. };
  63. /*
  64.  *----------------------------------------------------------------------
  65.  *
  66.  * TkpDefineNativeBitmaps --
  67.  *
  68.  * Add native bitmaps.
  69.  *
  70.  * Results:
  71.  * A standard Tcl result. If an error occurs then TCL_ERROR is
  72.  * returned and a message is left in the interp's result.
  73.  *
  74.  * Side effects:
  75.  * "Name" is entered into the bitmap table and may be used from
  76.  * here on to refer to the given bitmap.
  77.  *
  78.  *----------------------------------------------------------------------
  79.  */
  80. void
  81. TkpDefineNativeBitmaps(void)
  82. {
  83.     Tcl_HashTable *tablePtr = TkGetBitmapPredefTable();
  84.     BuiltInIcon *builtInPtr;
  85.     for (builtInPtr = builtInIcons; builtInPtr->name != NULL; builtInPtr++) {
  86. Tcl_HashEntry *predefHashPtr;
  87. const char * name;
  88. int isNew;
  89. name = Tk_GetUid(builtInPtr->name);
  90. predefHashPtr = Tcl_CreateHashEntry(tablePtr, name, &isNew);
  91. if (isNew) {
  92.     TkPredefBitmap *predefPtr = (TkPredefBitmap *)
  93.     ckalloc(sizeof(TkPredefBitmap));
  94.     NativeIcon *nativeIconPtr = (NativeIcon *)
  95.     ckalloc(sizeof(NativeIcon));
  96.     nativeIconPtr->id = builtInPtr->id;
  97.     nativeIconPtr->type = builtInPtr->type;
  98.     predefPtr->source = (char *) nativeIconPtr;
  99.     predefPtr->width = builtInPtr->size;
  100.     predefPtr->height = builtInPtr->size;
  101.     predefPtr->native = 1;
  102.     Tcl_SetHashValue(predefHashPtr, predefPtr);
  103. }
  104.     }
  105. }
  106. /*
  107.  *----------------------------------------------------------------------
  108.  *
  109.  * TkpCreateNativeBitmap --
  110.  *
  111.  * Add native bitmaps.
  112.  *
  113.  * Results:
  114.  * A standard Tcl result. If an error occurs then TCL_ERROR is
  115.  * returned and a message is left in the interp's result.
  116.  *
  117.  * Side effects:
  118.  * "Name" is entered into the bitmap table and may be used from
  119.  * here on to refer to the given bitmap.
  120.  *
  121.  *----------------------------------------------------------------------
  122.  */
  123. Pixmap
  124. TkpCreateNativeBitmap(
  125.     Display *display,
  126.     CONST char *source) /* Info about the icon to build. */
  127. {
  128.     Pixmap pix;
  129.     Rect destRect;
  130.     CGrafPtr savePort;
  131.     Boolean portChanged;
  132.     const NativeIcon *nativeIconPtr;
  133.     pix = Tk_GetPixmap(display, None, 32, 32, 0);
  134.     portChanged = QDSwapPort(TkMacOSXGetDrawablePort(pix), &savePort);
  135.     nativeIconPtr = (const NativeIcon *) source;
  136.     SetRect(&destRect, 0, 0, 32, 32);
  137.     if (nativeIconPtr->type == TYPE1) {
  138. RGBColor white = {0xFFFF, 0xFFFF, 0xFFFF};
  139. RGBForeColor(&white);
  140. PaintRect(&destRect);
  141. PlotIconID(&destRect, atAbsoluteCenter, ttNone, nativeIconPtr->id);
  142.     } else if (nativeIconPtr->type == TYPE2) {
  143. Handle icon = GetIcon(nativeIconPtr->id);
  144. if (icon != NULL) {
  145.     RGBColor black = {0, 0, 0};
  146.     RGBForeColor(&black);
  147.     PlotIcon(&destRect, icon);
  148.     ReleaseResource(icon);
  149. }
  150.     }
  151.     if (portChanged) {
  152. QDSwapPort(savePort, NULL);
  153.     }
  154.     return pix;
  155. }
  156. /*
  157.  *----------------------------------------------------------------------
  158.  *
  159.  * TkpGetNativeAppBitmap --
  160.  *
  161.  * Add native bitmaps.
  162.  *
  163.  * Results:
  164.  * A standard Tcl result. If an error occurs then TCL_ERROR is
  165.  * returned and a message is left in the interp's result.
  166.  *
  167.  * Side effects:
  168.  * "Name" is entered into the bitmap table and may be used from
  169.  * here on to refer to the given bitmap.
  170.  *
  171.  *----------------------------------------------------------------------
  172.  */
  173. Pixmap
  174. TkpGetNativeAppBitmap(
  175.     Display *display, /* The display. */
  176.     CONST char *name, /* The name of the bitmap. */
  177.     int *width, /* The width & height of the bitmap. */
  178.     int *height)
  179. {
  180.     Pixmap pix;
  181.     CGrafPtr savePort;
  182.     Boolean portChanged;
  183.     Rect destRect;
  184.     Handle resource;
  185.     int type = -1, destWrote;
  186.     Str255 nativeName;
  187.     Tcl_Encoding encoding;
  188.     /*
  189.      * macRoman is the encoding that the resource fork uses.
  190.      */
  191.     encoding = Tcl_GetEncoding(NULL, "macRoman");
  192.     Tcl_UtfToExternal(NULL, encoding, name, strlen(name), 0, NULL,
  193.     (char *) &nativeName[1], 255, NULL, &destWrote, NULL);
  194.     nativeName[0] = destWrote;
  195.     Tcl_FreeEncoding(encoding);
  196.     resource = GetNamedResource('cicn', nativeName);
  197.     if (resource != NULL) {
  198. type = TYPE3;
  199.     } else {
  200. resource = GetNamedResource('ICON', nativeName);
  201. if (resource != NULL) {
  202.     type = TYPE2;
  203. }
  204.     }
  205.     if (resource == NULL) {
  206. return (Pixmap) NULL;
  207.     }
  208.     pix = Tk_GetPixmap(display, None, 32, 32, 0);
  209.     portChanged = QDSwapPort(TkMacOSXGetDrawablePort(pix), &savePort);
  210.     SetRect(&destRect, 0, 0, 32, 32);
  211.     if (type == TYPE2) {
  212. RGBColor black = {0, 0, 0};
  213. RGBForeColor(&black);
  214. PlotIcon(&destRect, resource);
  215. ReleaseResource(resource);
  216.     } else if (type == TYPE3) {
  217. RGBColor white = {0xFFFF, 0xFFFF, 0xFFFF};
  218. short id;
  219. ResType theType;
  220. Str255 dummy;
  221. /*
  222.  * We need to first paint the background white. Also, for
  223.  * some reason we *must* use GetCIcon instead of GetNamedResource
  224.  * for PlotCIcon to work - so we use GetResInfo to get the id.
  225.  */
  226. RGBForeColor(&white);
  227. PaintRect(&destRect);
  228. GetResInfo(resource, &id, &theType, dummy);
  229. ReleaseResource(resource);
  230. resource = (Handle) GetCIcon(id);
  231. PlotCIcon(&destRect, (CIconHandle) resource);
  232. DisposeCIcon((CIconHandle) resource);
  233.     }
  234.     *width = 32;
  235.     *height = 32;
  236.     if (portChanged) {
  237. QDSwapPort(savePort, NULL);
  238.     }
  239.     return pix;
  240. }