diff options
author | culler <culler> | 2019-07-05 17:36:28 (GMT) |
---|---|---|
committer | culler <culler> | 2019-07-05 17:36:28 (GMT) |
commit | 283b2b146b43c89ab2a7fbca364ab961844ce8ae (patch) | |
tree | 57ca11f8ce318dac45702034e28a4de31897fd38 /macosx | |
parent | 8a3e8fe5694a99d5b91fb75803a6976aa259eb66 (diff) | |
download | tk-283b2b146b43c89ab2a7fbca364ab961844ce8ae.zip tk-283b2b146b43c89ab2a7fbca364ab961844ce8ae.tar.gz tk-283b2b146b43c89ab2a7fbca364ab961844ce8ae.tar.bz2 |
Eliminate calls to GetIconRef, which is deprecated in macOS 10.15
Diffstat (limited to 'macosx')
-rw-r--r-- | macosx/tkMacOSXBitmap.c | 96 |
1 files changed, 32 insertions, 64 deletions
diff --git a/macosx/tkMacOSXBitmap.c b/macosx/tkMacOSXBitmap.c index cf46202..e8cb211 100644 --- a/macosx/tkMacOSXBitmap.c +++ b/macosx/tkMacOSXBitmap.c @@ -49,6 +49,9 @@ static BuiltInIcon builtInIcons[] = { #define builtInIconSize 32 +#define OSTYPE_TO_UTI(x) (NSString *)UTTypeCreatePreferredIdentifierForTag( \ + kUTTagClassOSType, UTCreateStringForOSType(x), nil) + static Tcl_HashTable iconBitmapTable = {}; typedef struct { int kind, width, height; @@ -107,14 +110,15 @@ TkpDefineNativeBitmaps(void) } } } + /* *---------------------------------------------------------------------- * - * GetBitmapForIcon -- + * PixmapFromImage -- * * Results: - * Bitmap for the given IconRef. + * Returns a Pixmap with an NSImage drawn into it. * * Side effects: * None. @@ -123,29 +127,33 @@ TkpDefineNativeBitmaps(void) */ static Pixmap -GetBitmapForIcon( +PixmapFromImage( Display *display, - IconRef icon, + NSImage* image, CGSize size) { TkMacOSXDrawingContext dc; Pixmap pixmap; - + pixmap = Tk_GetPixmap(display, None, size.width, size.height, 0); if (TkMacOSXSetupDrawingContext(pixmap, NULL, 1, &dc)) { if (dc.context) { - const CGAffineTransform t = { .a = 1, .b = 0, .c = 0, .d = -1, - .tx = 0, .ty = size.height }; - const CGRect r = { .origin = { .x = 0, .y = 0 }, .size = size }; - + CGAffineTransform t = { .a = 1, .b = 0, .c = 0, .d = -1, + .tx = 0, .ty = size.height}; CGContextConcatCTM(dc.context, t); - PlotIconRefInContext(dc.context, &r, kAlignAbsoluteCenter, - kTransformNone, NULL, kPlotIconRefNormalFlags, icon); + [NSGraphicsContext saveGraphicsState]; + [NSGraphicsContext setCurrentContext:[NSGraphicsContext + graphicsContextWithGraphicsPort:dc.context + flipped:NO]]; + [image drawAtPoint:NSZeroPoint fromRect:NSZeroRect + operation:NSCompositeCopy fraction:1.0]; + [NSGraphicsContext restoreGraphicsState]; } TkMacOSXRestoreDrawingContext(&dc); } return pixmap; } + /* *---------------------------------------------------------------------- @@ -168,22 +176,14 @@ TkpCreateNativeBitmap( Display *display, const void *source) /* Info about the icon to build. */ { - Pixmap pixmap; - IconRef icon; - OSErr err; - - err = ChkErr(GetIconRef, kOnSystemDisk, kSystemIconsCreator, - PTR2UINT(source), &icon); - if (err == noErr) { - pixmap = GetBitmapForIcon(display, icon, CGSizeMake(builtInIconSize, - builtInIconSize)); - ReleaseIconRef(icon); - } else { - pixmap = Tk_GetPixmap(display, None, builtInIconSize, - builtInIconSize, 0); - } + NSString *iconUTI = OSTYPE_TO_UTI(PTR2UINT(source)); + NSImage *iconImage = [[NSWorkspace sharedWorkspace] + iconForFileType: iconUTI]; + CGSize size = CGSizeMake(builtInIconSize, builtInIconSize); + Pixmap pixmap = PixmapFromImage(display, iconImage, NSSizeToCGSize(size)); return pixmap; } + /* *---------------------------------------------------------------------- @@ -219,6 +219,7 @@ OSTypeFromString(const char *s, OSType *t) { Tcl_FreeEncoding(encoding); return result; } + /* *---------------------------------------------------------------------- @@ -309,50 +310,17 @@ TkpGetNativeAppBitmap( } } if (image) { - TkMacOSXDrawingContext dc; - int depth = 0; - -#ifdef MAC_OSX_TK_TODO - for (NSImageRep *r in [image representations]) { - NSInteger bitsPerSample = [r bitsPerSample]; - if (bitsPerSample && bitsPerSample > depth) { - depth = bitsPerSample; - }; - } - if (depth == 1) { - /* TODO: convert BW NSImage to CGImageMask */ - } -#endif - pixmap = Tk_GetPixmap(display, None, size.width, size.height, depth); *width = size.width; *height = size.height; - if (TkMacOSXSetupDrawingContext(pixmap, NULL, 1, &dc)) { - if (dc.context) { - CGAffineTransform t = { .a = 1, .b = 0, .c = 0, .d = -1, - .tx = 0, .ty = size.height}; - - CGContextConcatCTM(dc.context, t); - [NSGraphicsContext saveGraphicsState]; - [NSGraphicsContext setCurrentContext:[NSGraphicsContext - graphicsContextWithGraphicsPort:dc.context flipped:NO]]; - [image drawAtPoint:NSZeroPoint fromRect:NSZeroRect - operation:NSCompositeCopy fraction:1.0]; - [NSGraphicsContext restoreGraphicsState]; - } - TkMacOSXRestoreDrawingContext(&dc); - } + pixmap = PixmapFromImage(display, image, NSSizeToCGSize(size)); } else if (name) { OSType iconType; if (OSTypeFromString(name, &iconType) == TCL_OK) { - IconRef icon; - OSErr err = ChkErr(GetIconRef, kOnSystemDisk, kSystemIconsCreator, - iconType, &icon); - if (err == noErr) { - pixmap = GetBitmapForIcon(display, icon, NSSizeToCGSize(size)); - *width = size.width; - *height = size.height; - ReleaseIconRef(icon); - } + NSString *iconUTI = OSTYPE_TO_UTI(iconType); + printf("Found image for UTI %s\n", iconUTI.UTF8String); + NSImage *iconImage = [[NSWorkspace sharedWorkspace] + iconForFileType: iconUTI]; + pixmap = PixmapFromImage(display, iconImage, NSSizeToCGSize(size)); } } return pixmap; |