2 // IconFamily class interface
3 // by Troy Stephens, Thomas Schnitzer, David Remahl, Nathan Day, Ben Haller, Sven Janssen, Peter Hosey, Conor Dearden, Elliot Glaysher, and Dave MacLachlan
7 // http://iconfamily.sourceforge.net/
9 // Problems, shortcomings, and uncertainties that I'm aware of are flagged with "NOTE:". Please address bug reports, bug fixes, suggestions, etc. to the project Forums and bug tracker at https://sourceforge.net/projects/iconfamily/
12 Copyright (c) 2001-2010 Troy N. Stephens
13 Portions Copyright (c) 2007 Google Inc.
15 Use and distribution of this source code is governed by the MIT License, whose terms are as follows.
17 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
19 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #import <Cocoa/Cocoa.h>
25 #import <Carbon/Carbon.h>
27 // This class is a Cocoa/Objective-C wrapper for the Mac OS X Carbon API's
28 // "icon family" data type. Its main purpose is to enable Cocoa applications
29 // to easily create custom file icons from NSImage instances, and thus take
30 // advantage of Mac OS X's new larger RGBA "thumbnail" icon format to provide
31 // richly detailed thumbnail previews of the files' contents.
33 // Using IconFamily, this becomes as simple as:
35 // id iconFamily = [IconFamily iconFamilyWithThumbnailsOfImage:anImage];
36 // [iconFamily setAsCustomIconForFile:anExistingFile];
38 // You can also write an icon family to an .icns file using the -writeToFile:
41 @interface IconFamily
: NSObject
43 IconFamilyHandle hIconFamily
;
46 // Convenience methods. These use the corresponding -init... methods to return
47 // an autoreleased IconFamily instance.
49 + (IconFamily
*) iconFamily
;
50 + (IconFamily
*) iconFamilyWithContentsOfFile
:(NSString
*)path
;
51 + (IconFamily
*) iconFamilyWithIconOfFile
:(NSString
*)path
;
52 + (IconFamily
*) iconFamilyWithIconFamilyHandle
:(IconFamilyHandle
)hNewIconFamily
;
53 + (IconFamily
*) iconFamilyWithSystemIcon
:(int)fourByteCode
;
54 + (IconFamily
*) iconFamilyWithThumbnailsOfImage
:(NSImage
*)image
;
55 + (IconFamily
*) iconFamilyWithThumbnailsOfImage
:(NSImage
*)image usingImageInterpolation
:(NSImageInterpolation
)imageInterpolation
;
57 // Initializes as a new, empty IconFamily. This is IconFamily's designated
58 // initializer method.
62 // Initializes an IconFamily by loading the contents of an .icns file.
64 - (id
) initWithContentsOfFile
:(NSString
*)path
;
66 // Initializes an IconFamily from an existing Carbon IconFamilyHandle.
68 - (id
) initWithIconFamilyHandle
:(IconFamilyHandle
)hNewIconFamily
;
70 // Initializes an IconFamily by loading the Finder icon that's assigned to a
73 - (id
) initWithIconOfFile
:(NSString
*)path
;
75 // Initializes an IconFamily by referencing a standard system icon.
77 - (id
) initWithSystemIcon
:(int)fourByteCode
;
79 // Initializes an IconFamily by creating its elements from a resampled
80 // NSImage. The second form of this method allows you to specify the degree
81 // of antialiasing to be used in resampling the image, by passing in one of
82 // the NSImageInterpolation... constants that are defined in
83 // NSGraphicsContext.h. The first form of this initializer simply calls the
84 // second form with imageInterpolation set to NSImageInterpolationHigh, which
85 // produces highly smoothed thumbnails.
87 - (id
) initWithThumbnailsOfImage
:(NSImage
*)image
;
88 - (id
) initWithThumbnailsOfImage
:(NSImage
*)image usingImageInterpolation
:(NSImageInterpolation
)imageInterpolation
;
90 // Writes the icon family to an .icns file.
92 - (BOOL
) writeToFile
:(NSString
*)path
;
94 // Sets the image data for one of the icon family's elements from an
95 // NSBitmapImageRep. The "elementType" parameter must be one of the icon
96 // family element types listed below, and the format of the "bitmapImageRep"
97 // must match the corresponding requirements specified below. Regardless of
98 // the elementType, the bitmapImageRep must also be non-planar and have 8 bits
101 // elementType dimensions format
102 // ------------------- ---------- ---------------------------------------
103 // kIconServices512PixelDataARGB 512 x 512 32-bit RGBA, 32-bit RGB, or 24-bit RGB
104 // kIconServices256PixelDataARGB 256 x 256 32-bit RGBA, 32-bit RGB, or 24-bit RGB
105 // kThumbnail32BitData 128 x 128 32-bit RGBA, 32-bit RGB, or 24-bit RGB
106 // kThumbnail8BitMask 128 x 128 32-bit RGBA or 8-bit intensity
107 // kLarge32BitData 32 x 32 32-bit RGBA, 32-bit RGB, or 24-bit RGB
108 // kLarge8BitMask 32 x 32 32-bit RGBA or 8-bit intensity
109 // kLarge1BitMask 32 x 32 32-bit RGBA, 8-bit intensity, or 1-bit
110 // kSmall32BitData 16 x 16 32-bit RGBA, 32-bit RGB, or 24-bit RGB
111 // kSmall8BitMask 16 x 16 32-bit RGBA or 8-bit intensity
112 // kSmall1BitMask 16 x 16 32-bit RGBA, 8-bit intensity, or 1-bit
114 // When an RGBA image is supplied to set a "Mask" element, the mask data is
115 // taken from the image's alpha channel.
117 // NOTE: Setting an IconFamily's kLarge1BitMask seems to damage the IconFamily
118 // for some as yet unknown reason. (If you then assign the icon family
119 // as a file's custom icon using -setAsCustomIconForFile:, the custom
120 // icon doesn't appear for the file in the Finder.) However, both
121 // custom icon display and mouse-click hit-testing in the Finder seem to
122 // work fine when we only set the other four elements (thus keeping the
123 // existing kLarge1BitMask from the valid icon family from which we
124 // initialized the IconFamily via -initWithContentsOfFile:, since
125 // IconFamily's -init method is currently broken...), so it seems safe
126 // to just leave the kLarge1BitMask alone.
128 - (BOOL
) setIconFamilyElement
:(OSType
)elementType
129 fromBitmapImageRep
:(NSBitmapImageRep
*)bitmapImageRep
;
131 // Gets the image data for one of the icon family's elements as a new, 32-bit
132 // RGBA NSBitmapImageRep. The specified elementType should be one of
133 // kIconServices512PixelDataARGB, kIconServices256PixelDataARGB,
134 // kThumbnail32BitData, kLarge32BitData, or kSmall32BitData.
136 // The returned NSBitmapImageRep will have the corresponding 8-bit mask data
137 // in its alpha channel, or a fully opaque alpha channel if the icon family
138 // has no 8-bit mask data for the specified alpha channel.
140 // Returns nil if the requested element cannot be retrieved (e.g. if the
141 // icon family has no such 32BitData element).
143 - (NSBitmapImageRep
*) bitmapImageRepWithAlphaForIconFamilyElement
:(OSType
)elementType
;
145 // Creates and returns an NSImage that contains the icon family's various
146 // elements as its NSImageReps.
148 - (NSImage
*) imageWithAllReps
;
150 #if !defined(DISABLE_CUSTOM_ICON)
152 // NOTE: Planned method -- not yet implemented.
154 // Gets the image data for one of the icon family's elements as a new
155 // NSBitmapImageRep. The specified elementType should be one of
156 // kThumbnail32BitData, kThumbnail32BitMask, kLarge32BitData, kLarge8BitMask,
157 // kLarge1BitMask, kSmall32BitData, kSmall8BitMask, or kSmall1BitMask.
159 // - (NSBitmapImageRep*) bitmapImageRepForIconFamilyElement:(OSType)elementType;
161 // Writes the icon family to the resource fork of the specified file as its
162 // kCustomIconResource, and sets the necessary Finder bits so the icon will
163 // be displayed for the file in Finder views.
165 - (BOOL
) setAsCustomIconForFile
:(NSString
*)path
;
166 - (BOOL
) setAsCustomIconForFile
:(NSString
*)path withCompatibility
:(BOOL
)compat
;
168 // Same as the -setAsCustomIconForFile:... methods, but for folders (directories).
170 - (BOOL
) setAsCustomIconForDirectory
:(NSString
*)path
;
171 - (BOOL
) setAsCustomIconForDirectory
:(NSString
*)path withCompatibility
:(BOOL
)compat
;
173 // Removes the custom icon (if any) from the specified file's resource fork,
174 // and clears the necessary Finder bits for the file. (Note that this is a
175 // class method, so you don't need an instance of IconFamily to invoke it.)
177 + (BOOL
) removeCustomIconFromFile
:(NSString
*)path
;
179 //Same as the -removeCustomIconFromFile: method, but for folders (directories).
181 + (BOOL
) removeCustomIconFromDirectory
:(NSString
*)path
;
183 #endif // !defined(DISABLE_CUSTOM_ICON)
187 // Methods for interfacing with the Carbon Scrap Manager (analogous to and
188 // interoperable with the Cocoa Pasteboard).
189 @interface
IconFamily (ScrapAdditions
)
190 + (BOOL
) canInitWithScrap
;
191 + (IconFamily
*) iconFamilyWithScrap
;
192 - (id
) initWithScrap
;