| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | #import <Adium/IconFamily.h> |
| 26 | #import <Adium/NSString+CarbonFSRefCreation.h> |
| 27 | |
| 28 | @interface IconFamily (Internals) |
| 29 | |
| 30 | + (NSImage*) resampleImage:(NSImage*)image toIconWidth:(int)width usingImageInterpolation:(NSImageInterpolation)imageInterpolation; |
| 31 | |
| 32 | + (Handle) get32BitDataFromBitmapImageRep:(NSBitmapImageRep*)bitmapImageRep requiredPixelSize:(int)requiredPixelSize; |
| 33 | |
| 34 | + (Handle) get8BitDataFromBitmapImageRep:(NSBitmapImageRep*)bitmapImageRep requiredPixelSize:(int)requiredPixelSize; |
| 35 | |
| 36 | + (Handle) get8BitMaskFromBitmapImageRep:(NSBitmapImageRep*)bitmapImageRep requiredPixelSize:(int)requiredPixelSize; |
| 37 | |
| 38 | + (Handle) get1BitMaskFromBitmapImageRep:(NSBitmapImageRep*)bitmapImageRep requiredPixelSize:(int)requiredPixelSize; |
| 39 | |
| 40 | - (BOOL) addResourceType:(OSType)type asResID:(int)resID; |
| 41 | |
| 42 | @end |
| 43 | |
| 44 | @implementation IconFamily |
| 45 | |
| 46 | + (IconFamily*) iconFamily |
| 47 | { |
| 48 | return [[[IconFamily alloc] init] autorelease]; |
| 49 | } |
| 50 | |
| 51 | + (IconFamily*) iconFamilyWithContentsOfFile:(NSString*)path |
| 52 | { |
| 53 | return [[[IconFamily alloc] initWithContentsOfFile:path] autorelease]; |
| 54 | } |
| 55 | |
| 56 | + (IconFamily*) iconFamilyWithIconOfFile:(NSString*)path |
| 57 | { |
| 58 | return [[[IconFamily alloc] initWithIconOfFile:path] autorelease]; |
| 59 | } |
| 60 | |
| 61 | + (IconFamily*) iconFamilyWithIconFamilyHandle:(IconFamilyHandle)hNewIconFamily |
| 62 | { |
| 63 | return [[[IconFamily alloc] initWithIconFamilyHandle:hNewIconFamily] autorelease]; |
| 64 | } |
| 65 | |
| 66 | + (IconFamily*) iconFamilyWithSystemIcon:(int)fourByteCode |
| 67 | { |
| 68 | return [[[IconFamily alloc] initWithSystemIcon:fourByteCode] autorelease]; |
| 69 | } |
| 70 | |
| 71 | + (IconFamily*) iconFamilyWithThumbnailsOfImage:(NSImage*)image |
| 72 | { |
| 73 | return [[[IconFamily alloc] initWithThumbnailsOfImage:image] autorelease]; |
| 74 | } |
| 75 | |
| 76 | + (IconFamily*) iconFamilyWithThumbnailsOfImage:(NSImage*)image usingImageInterpolation:(NSImageInterpolation)imageInterpolation |
| 77 | { |
| 78 | return [[[IconFamily alloc] initWithThumbnailsOfImage:image usingImageInterpolation:imageInterpolation] autorelease]; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | - init |
| 85 | { |
| 86 | self = [super init]; |
| 87 | if (self) { |
| 88 | hIconFamily = (IconFamilyHandle) NewHandle( 0 ); |
| 89 | if (hIconFamily == NULL( ( void * ) 0 )) { |
| 90 | [self autorelease]; |
| 91 | return nil0; |
| 92 | } |
| 93 | } |
| 94 | return self; |
| 95 | } |
| 96 | |
| 97 | - initWithContentsOfFile:(NSString*)path |
| 98 | { |
| 99 | FSRef ref; |
| 100 | OSStatus result; |
| 101 | |
| 102 | self = [self init]; |
| 103 | if (self) { |
| 104 | if (hIconFamily) { |
| 105 | DisposeHandle( (Handle)hIconFamily ); |
| 106 | hIconFamily = NULL( ( void * ) 0 ); |
| 107 | } |
| 108 | if (![path getFSRef:&ref createFileIfNecessary:NO( BOOL ) 0]) { |
| 109 | [self autorelease]; |
| 110 | return nil0; |
| 111 | } |
| 112 | result = ReadIconFromFSRef( &ref, &hIconFamily ); |
| 113 | if (result != noErr) { |
| 114 | [self autorelease]; |
| 115 | return nil0; |
| 116 | } |
| 117 | } |
| 118 | return self; |
| 119 | } |
| 120 | |
| 121 | - initWithIconFamilyHandle:(IconFamilyHandle)hNewIconFamily |
| 122 | { |
| 123 | self = [self init]; |
| 124 | if (self) { |
| 125 | if (hIconFamily) { |
| 126 | DisposeHandle( (Handle)hIconFamily ); |
| 127 | hIconFamily = NULL( ( void * ) 0 ); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | hIconFamily = hNewIconFamily; |
| 132 | } |
| 133 | return self; |
| 134 | } |
| 135 | |
| 136 | - initWithIconOfFile:(NSString*)path |
| 137 | { |
| 138 | IconRef iconRef; |
| 139 | OSStatus result; |
| 140 | SInt16 label; |
| 141 | FSRef ref; |
| 142 | |
| 143 | self = [self init]; |
| 144 | if (self) |
| 145 | { |
| 146 | if (hIconFamily) |
| 147 | { |
| 148 | DisposeHandle( (Handle)hIconFamily ); |
| 149 | hIconFamily = NULL( ( void * ) 0 ); |
| 150 | } |
| 151 | |
| 152 | if( ![path getFSRef:&ref createFileIfNecessary:NO( BOOL ) 0] ) |
| 153 | { |
| 154 | [self autorelease]; |
| 155 | return nil0; |
| 156 | } |
| 157 | |
| 158 | result = GetIconRefFromFileInfo( |
| 159 | &ref, |
| 160 | 0, |
| 161 | NULL( ( void * ) 0 ), |
| 162 | kFSCatInfoNone, |
| 163 | NULL( ( void * ) 0 ), |
| 164 | kIconServicesNormalUsageFlag, |
| 165 | &iconRef, |
| 166 | &label ); |
| 167 | |
| 168 | if (result != noErr) |
| 169 | { |
| 170 | [self autorelease]; |
| 171 | return nil0; |
| 172 | } |
| 173 | |
| 174 | result = IconRefToIconFamily( |
| 175 | iconRef, |
| 176 | kSelectorAllAvailableData, |
| 177 | &hIconFamily ); |
| 178 | |
| 179 | ReleaseIconRef( iconRef ); |
| 180 | |
| 181 | if (result != noErr || !hIconFamily) |
| 182 | { |
| 183 | [self autorelease]; |
| 184 | return nil0; |
| 185 | } |
| 186 | } |
| 187 | return self; |
| 188 | } |
| 189 | |
| 190 | - initWithSystemIcon:(int)fourByteCode |
| 191 | { |
| 192 | IconRef iconRef; |
| 193 | OSErr result; |
| 194 | |
| 195 | self = [self init]; |
| 196 | if (self) |
| 197 | { |
| 198 | if (hIconFamily) |
| 199 | { |
| 200 | DisposeHandle( (Handle)hIconFamily ); |
| 201 | hIconFamily = NULL( ( void * ) 0 ); |
| 202 | } |
| 203 | |
| 204 | result = GetIconRef(kOnSystemDisk, kSystemIconsCreator, fourByteCode, &iconRef); |
| 205 | |
| 206 | if (result != noErr) |
| 207 | { |
| 208 | [self autorelease]; |
| 209 | return nil0; |
| 210 | } |
| 211 | |
| 212 | result = IconRefToIconFamily( |
| 213 | iconRef, |
| 214 | kSelectorAllAvailableData, |
| 215 | &hIconFamily ); |
| 216 | |
| 217 | if (result != noErr || !hIconFamily) |
| 218 | { |
| 219 | [self autorelease]; |
| 220 | return nil0; |
| 221 | } |
| 222 | |
| 223 | ReleaseIconRef( iconRef ); |
| 224 | } |
| 225 | return self; |
| 226 | } |
| 227 | |
| 228 | - initWithThumbnailsOfImage:(NSImage*)image |
| 229 | { |
| 230 | |
| 231 | return [self initWithThumbnailsOfImage:image usingImageInterpolation:NSImageInterpolationHigh]; |
| 232 | } |
| 233 | |
| 234 | - initWithThumbnailsOfImage:(NSImage*)image usingImageInterpolation:(NSImageInterpolation)imageInterpolation |
| 235 | { |
| 236 | NSImage* iconImage128x128; |
| 237 | NSImage* iconImage32x32; |
| 238 | NSImage* iconImage16x16; |
| 239 | NSBitmapImageRep* iconBitmap128x128; |
| 240 | NSBitmapImageRep* iconBitmap32x32; |
| 241 | NSBitmapImageRep* iconBitmap16x16; |
| 242 | NSImage* bitmappedIconImage128x128; |
| 243 | |
| 244 | |
| 245 | self = [self init]; |
| 246 | if (self == nil0) |
| 247 | return nil0; |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | iconImage128x128 = [IconFamily resampleImage:image toIconWidth:128 usingImageInterpolation:imageInterpolation]; |
| 257 | [iconImage128x128 lockFocus]; |
| 258 | iconBitmap128x128 = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, 128, 128)]; |
| 259 | [iconImage128x128 unlockFocus]; |
| 260 | if (iconBitmap128x128) { |
| 261 | [self setIconFamilyElement:kThumbnail32BitData fromBitmapImageRep:iconBitmap128x128]; |
| 262 | [self setIconFamilyElement:kThumbnail8BitMask fromBitmapImageRep:iconBitmap128x128]; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | bitmappedIconImage128x128 = [[NSImage alloc] initWithSize:NSMakeSize(128,128)]; |
| 271 | [bitmappedIconImage128x128 addRepresentation:iconBitmap128x128]; |
| 272 | |
| 273 | |
| 274 | |
| 275 | iconImage32x32 = [IconFamily resampleImage:bitmappedIconImage128x128 toIconWidth:32 usingImageInterpolation:imageInterpolation]; |
| 276 | [iconImage32x32 lockFocus]; |
| 277 | iconBitmap32x32 = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, 32, 32)]; |
| 278 | [iconImage32x32 unlockFocus]; |
| 279 | if (iconBitmap32x32) { |
| 280 | [self setIconFamilyElement:kLarge32BitData fromBitmapImageRep:iconBitmap32x32]; |
| 281 | [self setIconFamilyElement:kLarge8BitData fromBitmapImageRep:iconBitmap32x32]; |
| 282 | [self setIconFamilyElement:kLarge8BitMask fromBitmapImageRep:iconBitmap32x32]; |
| 283 | [self setIconFamilyElement:kLarge1BitMask fromBitmapImageRep:iconBitmap32x32]; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | |
| 288 | iconImage16x16 = [IconFamily resampleImage:bitmappedIconImage128x128 toIconWidth:16 usingImageInterpolation:imageInterpolation]; |
| 289 | [iconImage16x16 lockFocus]; |
| 290 | iconBitmap16x16 = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, 16, 16)]; |
| 291 | [iconImage16x16 unlockFocus]; |
| 292 | if (iconBitmap16x16) { |
| 293 | [self setIconFamilyElement:kSmall32BitData fromBitmapImageRep:iconBitmap16x16]; |
| 294 | [self setIconFamilyElement:kSmall8BitData fromBitmapImageRep:iconBitmap16x16]; |
| 295 | [self setIconFamilyElement:kSmall8BitMask fromBitmapImageRep:iconBitmap16x16]; |
| 296 | [self setIconFamilyElement:kSmall1BitMask fromBitmapImageRep:iconBitmap16x16]; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | [bitmappedIconImage128x128 release]; |
| 301 | [iconBitmap128x128 release]; |
| 302 | [iconBitmap32x32 release]; |
| 303 | [iconBitmap16x16 release]; |
| 304 | |
| 305 | |
| 306 | return self; |
| 307 | } |
| 308 | |
| 309 | - (void) dealloc |
| 310 | { |
| 311 | DisposeHandle( (Handle)hIconFamily ); |
| 312 | [super dealloc]; |
| 313 | } |
| 314 | |
| 315 | - (NSBitmapImageRep*) bitmapImageRepWithAlphaForIconFamilyElement:(OSType)elementType; |
| 316 | { |
| 317 | NSBitmapImageRep* bitmapImageRep; |
| 318 | int pixelsWide; |
| 319 | Handle hRawBitmapData; |
| 320 | Handle hRawMaskData; |
| 321 | OSType maskElementType; |
| 322 | OSErr result; |
| 323 | unsigned long* pRawBitmapData; |
| 324 | unsigned long* pRawBitmapDataEnd; |
| 325 | unsigned char* pRawMaskData; |
| 326 | unsigned char* pBitmapImageRepBitmapData; |
| 327 | |
| 328 | |
| 329 | |
| 330 | switch (elementType) { |
| 331 | |
| 332 | case kThumbnail32BitData: |
| 333 | maskElementType = kThumbnail8BitMask; |
| 334 | pixelsWide = 128; |
| 335 | break; |
| 336 | |
| 337 | |
| 338 | case kLarge32BitData: |
| 339 | maskElementType = kLarge8BitMask; |
| 340 | pixelsWide = 32; |
| 341 | break; |
| 342 | |
| 343 | |
| 344 | case kSmall32BitData: |
| 345 | maskElementType = kSmall8BitMask; |
| 346 | pixelsWide = 16; |
| 347 | break; |
| 348 | |
| 349 | default: |
| 350 | return nil0; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | hRawBitmapData = NewHandle( pixelsWide * pixelsWide * 4 ); |
| 355 | result = GetIconFamilyData( hIconFamily, elementType, hRawBitmapData ); |
| 356 | if (result != noErr) { |
| 357 | DisposeHandle( hRawBitmapData ); |
| 358 | return nil0; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | hRawMaskData = NewHandle( pixelsWide * pixelsWide ); |
| 363 | result = GetIconFamilyData( hIconFamily, maskElementType, hRawMaskData ); |
| 364 | if (result != noErr) { |
| 365 | DisposeHandle( hRawMaskData ); |
| 366 | hRawMaskData = NULL( ( void * ) 0 ); |
| 367 | } |
| 368 | |
| 369 | |
| 370 | HLock( hRawBitmapData ); |
| 371 | pRawBitmapData = (unsigned long*) *hRawBitmapData; |
| 372 | pRawBitmapDataEnd = pRawBitmapData + pixelsWide * pixelsWide; |
| 373 | if (hRawMaskData) { |
| 374 | HLock( hRawMaskData ); |
| 375 | pRawMaskData = (unsigned char*) *hRawMaskData; |
| 376 | while (pRawBitmapData < pRawBitmapDataEnd) { |
| 377 | *pRawBitmapData = NSSwapBigLongToHost((NSSwapHostLongToBig(*pRawBitmapData) << 8) | *pRawMaskData++); |
| 378 | ++pRawBitmapData; |
| 379 | } |
| 380 | HUnlock( hRawMaskData ); |
| 381 | } else { |
| 382 | while (pRawBitmapData < pRawBitmapDataEnd) { |
| 383 | *pRawBitmapData = NSSwapBigLongToHost((NSSwapHostLongToBig(*pRawBitmapData) << 8) | 0xff); |
| 384 | ++pRawBitmapData; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | bitmapImageRep = [[[NSBitmapImageRep alloc] |
| 400 | initWithBitmapDataPlanes:NULL( ( void * ) 0 ) |
| 401 | pixelsWide:pixelsWide |
| 402 | pixelsHigh:pixelsWide |
| 403 | bitsPerSample:8 |
| 404 | samplesPerPixel:4 |
| 405 | hasAlpha:YES( BOOL ) 1 |
| 406 | isPlanar:NO( BOOL ) 0 |
| 407 | colorSpaceName:NSDeviceRGBColorSpace |
| 408 | bytesPerRow:0 |
| 409 | bitsPerPixel:0] autorelease]; |
| 410 | pBitmapImageRepBitmapData = [bitmapImageRep bitmapData]; |
| 411 | if (pBitmapImageRepBitmapData) { |
| 412 | memcpy( pBitmapImageRepBitmapData, *hRawBitmapData, |
| 413 | pixelsWide * pixelsWide * 4 ); |
| 414 | } |
| 415 | HUnlock( hRawBitmapData ); |
| 416 | |
| 417 | |
| 418 | DisposeHandle( hRawBitmapData ); |
| 419 | if (hRawMaskData) |
| 420 | DisposeHandle( hRawMaskData ); |
| 421 | |
| 422 | |
| 423 | if (pBitmapImageRepBitmapData == NULL( ( void * ) 0 )) |
| 424 | return nil0; |
| 425 | |
| 426 | |
| 427 | return bitmapImageRep; |
| 428 | } |
| 429 | |
| 430 | - (NSImage*) imageWithAllReps |
| 431 | { |
| 432 | NSImage* image = NULL( ( void * ) 0 ); |
| 433 | image = [[[NSImage alloc] initWithData:[NSData dataWithBytes:*hIconFamily length:GetHandleSize((Handle)hIconFamily)]] autorelease]; |
| 434 | return image; |
| 435 | } |
| 436 | |
| 437 | - (BOOL) setIconFamilyElement:(OSType)elementType fromBitmapImageRep:(NSBitmapImageRep*)bitmapImageRep |
| 438 | { |
| 439 | Handle hRawData = NULL( ( void * ) 0 ); |
| 440 | OSErr result; |
| 441 | |
| 442 | switch (elementType) { |
| 443 | |
| 444 | case kThumbnail32BitData: |
| 445 | hRawData = [IconFamily get32BitDataFromBitmapImageRep:bitmapImageRep requiredPixelSize:128]; |
| 446 | break; |
| 447 | |
| 448 | |
| 449 | case kThumbnail8BitMask: |
| 450 | hRawData = [IconFamily get8BitMaskFromBitmapImageRep:bitmapImageRep requiredPixelSize:128]; |
| 451 | break; |
| 452 | |
| 453 | |
| 454 | case kLarge32BitData: |
| 455 | hRawData = [IconFamily get32BitDataFromBitmapImageRep:bitmapImageRep requiredPixelSize:32]; |
| 456 | break; |
| 457 | |
| 458 | |
| 459 | case kLarge8BitMask: |
| 460 | hRawData = [IconFamily get8BitMaskFromBitmapImageRep:bitmapImageRep requiredPixelSize:32]; |
| 461 | break; |
| 462 | |
| 463 | |
| 464 | case kLarge1BitMask: |
| 465 | hRawData = [IconFamily get1BitMaskFromBitmapImageRep:bitmapImageRep requiredPixelSize:32]; |
| 466 | break; |
| 467 | |
| 468 | |
| 469 | case kLarge8BitData: |
| 470 | hRawData = [IconFamily get8BitDataFromBitmapImageRep:bitmapImageRep requiredPixelSize:32]; |
| 471 | break; |
| 472 | |
| 473 | |
| 474 | case kSmall32BitData: |
| 475 | hRawData = [IconFamily get32BitDataFromBitmapImageRep:bitmapImageRep requiredPixelSize:16]; |
| 476 | break; |
| 477 | |
| 478 | |
| 479 | case kSmall8BitMask: |
| 480 | hRawData = [IconFamily get8BitMaskFromBitmapImageRep:bitmapImageRep requiredPixelSize:16]; |
| 481 | break; |
| 482 | |
| 483 | |
| 484 | case kSmall1BitMask: |
| 485 | hRawData = [IconFamily get1BitMaskFromBitmapImageRep:bitmapImageRep requiredPixelSize:16]; |
| 486 | break; |
| 487 | |
| 488 | |
| 489 | case kSmall8BitData: |
| 490 | hRawData = [IconFamily get8BitDataFromBitmapImageRep:bitmapImageRep requiredPixelSize:16]; |
| 491 | break; |
| 492 | |
| 493 | default: |
| 494 | return NO( BOOL ) 0; |
| 495 | } |
| 496 | |
| 497 | |
| 498 | |
| 499 | if (hRawData == NULL( ( void * ) 0 )) |
| 500 | { |
| 501 | NSLog(@"Null data returned to setIconFamilyElement:fromBitmapImageRep:"); |
| 502 | return NO( BOOL ) 0; |
| 503 | } |
| 504 | |
| 505 | result = SetIconFamilyData( hIconFamily, elementType, hRawData ); |
| 506 | DisposeHandle( hRawData ); |
| 507 | |
| 508 | if (result != noErr) |
| 509 | { |
| 510 | NSLog(@"SetIconFamilyData() returned error %d", result); |
| 511 | return NO( BOOL ) 0; |
| 512 | } |
| 513 | |
| 514 | return YES( BOOL ) 1; |
| 515 | } |
| 516 | |
| 517 | - (BOOL) setAsCustomIconForFile:(NSString*)path |
| 518 | { |
| 519 | return( [self setAsCustomIconForFile:path withCompatibility:NO( BOOL ) 0] ); |
| 520 | } |
| 521 | |
| 522 | - (BOOL) setAsCustomIconForFile:(NSString*)path withCompatibility:(BOOL)compat |
| 523 | { |
| 524 | FSRef targetFileFSRef; |
| 525 | FSRef parentDirectoryFSRef; |
| 526 | SInt16 file; |
| 527 | OSStatus result; |
| 528 | struct FSCatalogInfo catInfo; |
| 529 | struct FileInfo *finderInfo = (struct FileInfo *)&catInfo.finderInfo; |
| 530 | Handle hExistingCustomIcon; |
| 531 | Handle hIconFamilyCopy; |
| 532 | NSString *parentDirectory; |
| 533 | |
| 534 | |
| 535 | NSDate* modificationDate = [[[NSFileManager defaultManager] fileAttributesAtPath:path traverseLink:NO( BOOL ) 0] objectForKey:NSFileModificationDate]; |
| 536 | |
| 537 | if ([path isAbsolutePath]) |
| 538 | parentDirectory = [path stringByDeletingLastPathComponent]; |
| 539 | else |
| 540 | parentDirectory = [[[NSFileManager defaultManager] currentDirectoryPath] stringByAppendingPathComponent:[path stringByDeletingLastPathComponent]]; |
| 541 | |
| 542 | |
| 543 | |
| 544 | if (![parentDirectory getFSRef:&parentDirectoryFSRef createFileIfNecessary:NO( BOOL ) 0]) |
| 545 | return NO( BOOL ) 0; |
| 546 | |
| 547 | |
| 548 | struct HFSUniStr255 filename; |
| 549 | NSString *filenameString = [path lastPathComponent]; |
| 550 | filename.length = [filenameString length]; |
| 551 | [filenameString getCharacters:filename.unicode]; |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | FSCreateResFile( |
| 560 | &parentDirectoryFSRef, |
| 561 | filename.length, |
| 562 | filename.unicode, |
| 563 | kFSCatInfoNone, |
| 564 | NULL( ( void * ) 0 ), |
| 565 | &targetFileFSRef, |
| 566 | NULL( ( void * ) 0 )); |
| 567 | result = ResError(); |
| 568 | if (result == dupFNErr) { |
| 569 | |
| 570 | if (![path getFSRef:&targetFileFSRef createFileIfNecessary:NO( BOOL ) 0]) |
| 571 | return NO( BOOL ) 0; |
| 572 | } else if (result != noErr) { |
| 573 | return NO( BOOL ) 0; |
| 574 | } |
| 575 | |
| 576 | |
| 577 | file = FSOpenResFile( &targetFileFSRef, fsRdWrPerm ); |
| 578 | if (file == -1) |
| 579 | return NO( BOOL ) 0; |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | hIconFamilyCopy = (Handle) hIconFamily; |
| 587 | result = HandToHand( &hIconFamilyCopy ); |
| 588 | if (result != noErr) { |
| 589 | CloseResFile( file ); |
| 590 | return NO( BOOL ) 0; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | |
| 595 | hExistingCustomIcon = GetResource( kIconFamilyType, kCustomIconResource ); |
| 596 | if( hExistingCustomIcon ) |
| 597 | RemoveResource( hExistingCustomIcon ); |
| 598 | |
| 599 | |
| 600 | AddResource( (Handle)hIconFamilyCopy, kIconFamilyType, |
| 601 | kCustomIconResource, "\p"); |
| 602 | if (ResError() != noErr) { |
| 603 | CloseResFile( file ); |
| 604 | return NO( BOOL ) 0; |
| 605 | } |
| 606 | |
| 607 | if( compat ) |
| 608 | { |
| 609 | [self addResourceType:kLarge8BitData asResID:kCustomIconResource]; |
| 610 | [self addResourceType:kLarge1BitMask asResID:kCustomIconResource]; |
| 611 | [self addResourceType:kSmall8BitData asResID:kCustomIconResource]; |
| 612 | [self addResourceType:kSmall1BitMask asResID:kCustomIconResource]; |
| 613 | } |
| 614 | |
| 615 | |
| 616 | |
| 617 | CloseResFile( file ); |
| 618 | if (ResError() != noErr) |
| 619 | return NO( BOOL ) 0; |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | result = FSGetCatalogInfo( |
| 626 | &targetFileFSRef, |
| 627 | kFSCatInfoFinderInfo, |
| 628 | &catInfo, |
| 629 | NULL( ( void * ) 0 ), |
| 630 | NULL( ( void * ) 0 ), |
| 631 | NULL( ( void * ) 0 )); |
| 632 | if (result != noErr) |
| 633 | return NO( BOOL ) 0; |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | finderInfo->finderFlags = (finderInfo->finderFlags | kHasCustomIcon ) & ~kHasBeenInited; |
| 642 | |
| 643 | |
| 644 | result = FSSetCatalogInfo( &targetFileFSRef, kFSCatInfoFinderInfo, &catInfo ); |
| 645 | if (result != noErr) |
| 646 | return NO( BOOL ) 0; |
| 647 | |
| 648 | |
| 649 | NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:modificationDate, NSFileModificationDate, nil0]; |
| 650 | [[NSFileManager defaultManager] changeFileAttributes:attributes atPath:path]; |
| 651 | |
| 652 | |
| 653 | |
| 654 | result = FNNotify( &parentDirectoryFSRef, kFNDirectoryModifiedMessage, kNilOptions ); |
| 655 | if (result != noErr) |
| 656 | return NO( BOOL ) 0; |
| 657 | |
| 658 | return YES( BOOL ) 1; |
| 659 | } |
| 660 | |
| 661 | + (BOOL) removeCustomIconFromFile:(NSString*)path |
| 662 | { |
| 663 | FSRef targetFileFSRef; |
| 664 | FSRef parentDirectoryFSRef; |
| 665 | SInt16 file; |
| 666 | OSStatus result; |
| 667 | struct FSCatalogInfo catInfo; |
| 668 | struct FileInfo *finderInfo = (struct FileInfo *)&catInfo.finderInfo; |
| 669 | Handle hExistingCustomIcon; |
| 670 | |
| 671 | |
| 672 | if (![path getFSRef:&targetFileFSRef createFileIfNecessary:NO( BOOL ) 0]) |
| 673 | return NO( BOOL ) 0; |
| 674 | |
| 675 | |
| 676 | file = FSOpenResFile( &targetFileFSRef, fsRdWrPerm ); |
| 677 | if (file == -1) |
| 678 | return NO( BOOL ) 0; |
| 679 | |
| 680 | |
| 681 | |
| 682 | hExistingCustomIcon = GetResource( kIconFamilyType, kCustomIconResource ); |
| 683 | if( hExistingCustomIcon ) |
| 684 | RemoveResource( hExistingCustomIcon ); |
| 685 | |
| 686 | |
| 687 | CloseResFile( file ); |
| 688 | if (ResError() != noErr) |
| 689 | return NO( BOOL ) 0; |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | result = FSGetCatalogInfo( |
| 696 | &targetFileFSRef, |
| 697 | kFSCatInfoFinderInfo, |
| 698 | &catInfo, |
| 699 | NULL( ( void * ) 0 ), |
| 700 | NULL( ( void * ) 0 ), |
| 701 | &parentDirectoryFSRef ); |
| 702 | if (result != noErr) |
| 703 | return NO( BOOL ) 0; |
| 704 | |
| 705 | |
| 706 | finderInfo->finderFlags = finderInfo->finderFlags & ~(kHasCustomIcon | kHasBeenInited); |
| 707 | |
| 708 | |
| 709 | result = FSSetCatalogInfo( &targetFileFSRef, kFSCatInfoFinderInfo, &catInfo ); |
| 710 | if (result != noErr) |
| 711 | return NO( BOOL ) 0; |
| 712 | |
| 713 | |
| 714 | result = FNNotify( &parentDirectoryFSRef, kFNDirectoryModifiedMessage, kNilOptions ); |
| 715 | if (result != noErr) |
| 716 | return NO( BOOL ) 0; |
| 717 | |
| 718 | return YES( BOOL ) 1; |
| 719 | } |
| 720 | |
| 721 | - (BOOL) setAsCustomIconForDirectory:(NSString*)path |
| 722 | { |
| 723 | return [self setAsCustomIconForDirectory:path withCompatibility:NO( BOOL ) 0]; |
| 724 | } |
| 725 | |
| 726 | - (BOOL) setAsCustomIconForDirectory:(NSString*)path withCompatibility:(BOOL)compat |
| 727 | { |
| 728 | NSFileManager *fm = [NSFileManager defaultManager]; |
| 729 | BOOL isDir; |
| 730 | BOOL exists; |
| 731 | NSString *iconrPath; |
| 732 | FSRef targetFolderFSRef, iconrFSRef; |
| 733 | SInt16 file; |
| 734 | OSErr result; |
| 735 | struct HFSUniStr255 filename; |
| 736 | struct FSCatalogInfo catInfo; |
| 737 | Handle hExistingCustomIcon; |
| 738 | Handle hIconFamilyCopy; |
| 739 | |
| 740 | |
| 741 | exists = [fm fileExistsAtPath:path isDirectory:&isDir]; |
| 742 | if( !isDir || !exists ) |
| 743 | return NO( BOOL ) 0; |
| 744 | |
| 745 | |
| 746 | if( ![path getFSRef:&targetFolderFSRef createFileIfNecessary:NO( BOOL ) 0] ) |
| 747 | return NO( BOOL ) 0; |
| 748 | |
| 749 | |
| 750 | iconrPath = [path stringByAppendingPathComponent:@"Icon\r"]; |
| 751 | if( [fm fileExistsAtPath:iconrPath] ) |
| 752 | { |
| 753 | if( ![fm removeFileAtPath:iconrPath handler:nil0] ) |
| 754 | return NO( BOOL ) 0; |
| 755 | } |
| 756 | if( ![iconrPath getFSRef:&iconrFSRef createFileIfNecessary:YES( BOOL ) 1] ) |
| 757 | return NO( BOOL ) 0; |
| 758 | |
| 759 | |
| 760 | result = FSGetCatalogInfo( |
| 761 | &iconrFSRef, |
| 762 | kFSCatInfoFinderInfo, |
| 763 | &catInfo, |
| 764 | NULL( ( void * ) 0 ), |
| 765 | NULL( ( void * ) 0 ), |
| 766 | NULL( ( void * ) 0 ) ); |
| 767 | if( result == fnfErr ) { |
| 768 | |
| 769 | |
| 770 | struct FileInfo *finderInfo = (struct FileInfo *)catInfo.finderInfo; |
| 771 | |
| 772 | |
| 773 | |
| 774 | finderInfo->fileType = 'icon'; |
| 775 | finderInfo->fileCreator = 'MACS'; |
| 776 | |
| 777 | |
| 778 | finderInfo->finderFlags = kIsInvisible; |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | finderInfo->location.h = finderInfo->location.v = 0; |
| 788 | |
| 789 | |
| 790 | finderInfo->reservedField = 0; |
| 791 | } else if( result != noErr ) |
| 792 | return NO( BOOL ) 0; |
| 793 | |
| 794 | |
| 795 | filename.length = [@"Icon\r" length]; |
| 796 | [@"Icon\r" getCharacters:filename.unicode]; |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | FSCreateResFile( |
| 802 | &targetFolderFSRef, |
| 803 | filename.length, |
| 804 | filename.unicode, |
| 805 | kFSCatInfoFinderInfo, |
| 806 | &catInfo, |
| 807 | &iconrFSRef, |
| 808 | NULL( ( void * ) 0 )); |
| 809 | result = ResError(); |
| 810 | if (!(result == noErr || result == dupFNErr)) |
| 811 | return NO( BOOL ) 0; |
| 812 | |
| 813 | |
| 814 | file = FSOpenResFile( &iconrFSRef, fsRdWrPerm ); |
| 815 | if (file == -1) |
| 816 | return NO( BOOL ) 0; |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | hIconFamilyCopy = (Handle) hIconFamily; |
| 824 | result = HandToHand( &hIconFamilyCopy ); |
| 825 | if (result != noErr) { |
| 826 | CloseResFile( file ); |
| 827 | return NO( BOOL ) 0; |
| 828 | } |
| 829 | |
| 830 | |
| 831 | |
| 832 | hExistingCustomIcon = GetResource( kIconFamilyType, kCustomIconResource ); |
| 833 | if( hExistingCustomIcon ) |
| 834 | RemoveResource( hExistingCustomIcon ); |
| 835 | |
| 836 | |
| 837 | AddResource( (Handle)hIconFamilyCopy, kIconFamilyType, |
| 838 | kCustomIconResource, "\p"); |
| 839 | |
| 840 | if (ResError() != noErr) { |
| 841 | CloseResFile( file ); |
| 842 | return NO( BOOL ) 0; |
| 843 | } |
| 844 | |
| 845 | if( compat ) |
| 846 | { |
| 847 | [self addResourceType:kLarge8BitData asResID:kCustomIconResource]; |
| 848 | [self addResourceType:kLarge1BitMask asResID:kCustomIconResource]; |
| 849 | [self addResourceType:kSmall8BitData asResID:kCustomIconResource]; |
| 850 | [self addResourceType:kSmall1BitMask asResID:kCustomIconResource]; |
| 851 | } |
| 852 | |
| 853 | |
| 854 | |
| 855 | CloseResFile( file ); |
| 856 | if (ResError() != noErr) |
| 857 | return NO( BOOL ) 0; |
| 858 | |
| 859 | result = FSGetCatalogInfo( &targetFolderFSRef, |
| 860 | kFSCatInfoFinderInfo, |
| 861 | &catInfo, |
| 862 | NULL( ( void * ) 0 ), |
| 863 | NULL( ( void * ) 0 ), |
| 864 | NULL( ( void * ) 0 )); |
| 865 | if( result != noErr ) |
| 866 | return NO( BOOL ) 0; |
| 867 | |
| 868 | |
| 869 | ((struct FolderInfo *)catInfo.finderInfo)->finderFlags = ( ((struct FolderInfo *)catInfo.finderInfo)->finderFlags | kHasCustomIcon ) & ~kHasBeenInited; |
| 870 | |
| 871 | result = FSSetCatalogInfo( &targetFolderFSRef, |
| 872 | kFSCatInfoFinderInfo, |
| 873 | &catInfo); |
| 874 | if( result != noErr ) |
| 875 | return NO( BOOL ) 0; |
| 876 | |
| 877 | |
| 878 | |
| 879 | result = FNNotify( &targetFolderFSRef, kFNDirectoryModifiedMessage, kNilOptions ); |
| 880 | if (result != noErr) |
| 881 | return NO( BOOL ) 0; |
| 882 | |
| 883 | return YES( BOOL ) 1; |
| 884 | } |
| 885 | |
| 886 | - (BOOL) writeToFile:(NSString*)path |
| 887 | { |
| 888 | NSData* iconData = nil0; |
| 889 | |
| 890 | HLock((Handle)hIconFamily); |
| 891 | |
| 892 | iconData = [NSData dataWithBytes:*hIconFamily length:GetHandleSize((Handle)hIconFamily)]; |
| 893 | BOOL success = [iconData writeToFile:path atomically:NO( BOOL ) 0]; |
| 894 | |
| 895 | HUnlock((Handle)hIconFamily); |
| 896 | |
| 897 | return success; |
| 898 | } |
| 899 | |
| 900 | @end |
| 901 | |
| 902 | @implementation IconFamily (Internals) |
| 903 | |
| 904 | + (NSImage*) resampleImage:(NSImage*)image toIconWidth:(int)iconWidth usingImageInterpolation:(NSImageInterpolation)imageInterpolation |
| 905 | { |
| 906 | NSGraphicsContext* graphicsContext; |
| 907 | BOOL wasAntialiasing; |
| 908 | NSImageInterpolation previousImageInterpolation; |
| 909 | NSImage* newImage; |
| 910 | NSImage* workingImage; |
| 911 | NSImageRep* workingImageRep; |
| 912 | NSSize size, pixelSize, newSize; |
| 913 | NSRect iconRect; |
| 914 | NSRect targetRect; |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | workingImage = [image copyWithZone:[image zone]]; |
| 925 | [workingImage setScalesWhenResized:YES( BOOL ) 1]; |
| 926 | size = [workingImage size]; |
| 927 | workingImageRep = [workingImage bestRepresentationForDevice:nil0]; |
| 928 | if ([workingImageRep isKindOfClass:[NSBitmapImageRep class]]) { |
| 929 | pixelSize.width = [workingImageRep pixelsWide]; |
| 930 | pixelSize.height = [workingImageRep pixelsHigh]; |
| 931 | if (!NSEqualSizes( size, pixelSize )) { |
| 932 | [workingImage setSize:pixelSize]; |
| 933 | [workingImageRep setSize:pixelSize]; |
| 934 | size = pixelSize; |
| 935 | } |
| 936 | } |
| 937 | if (size.width >= size.height) { |
| 938 | newSize.width = iconWidth; |
| 939 | newSize.height = floor( (float) iconWidth * size.height / size.width + 0.5 ); |
| 940 | } else { |
| 941 | newSize.height = iconWidth; |
| 942 | newSize.width = floor( (float) iconWidth * size.width / size.height + 0.5 ); |
| 943 | } |
| 944 | [workingImage setSize:newSize]; |
| 945 | |
| 946 | |
| 947 | newImage = [[NSImage alloc] initWithSize:NSMakeSize(iconWidth,iconWidth)]; |
| 948 | [newImage lockFocus]; |
| 949 | iconRect.origin.x = iconRect.origin.y = 0; |
| 950 | iconRect.size.width = iconRect.size.height = iconWidth; |
| 951 | [[NSColor clearColor] set]; |
| 952 | NSRectFill( iconRect ); |
| 953 | |
| 954 | |
| 955 | |
| 956 | graphicsContext = [NSGraphicsContext currentContext]; |
| 957 | wasAntialiasing = [graphicsContext shouldAntialias]; |
| 958 | previousImageInterpolation = [graphicsContext imageInterpolation]; |
| 959 | [graphicsContext setShouldAntialias:YES( BOOL ) 1]; |
| 960 | [graphicsContext setImageInterpolation:imageInterpolation]; |
| 961 | |
| 962 | |
| 963 | targetRect.origin.x = ((float)iconWidth - newSize.width ) / 2.0; |
| 964 | targetRect.origin.y = ((float)iconWidth - newSize.height) / 2.0; |
| 965 | targetRect.size.width = newSize.width; |
| 966 | targetRect.size.height = newSize.height; |
| 967 | [workingImageRep drawInRect:targetRect]; |
| 968 | |
| 969 | |
| 970 | [graphicsContext setShouldAntialias:wasAntialiasing]; |
| 971 | [graphicsContext setImageInterpolation:previousImageInterpolation]; |
| 972 | |
| 973 | [newImage unlockFocus]; |
| 974 | |
| 975 | [workingImage release]; |
| 976 | |
| 977 | |
| 978 | return [newImage autorelease]; |
| 979 | } |
| 980 | |
| 981 | + (Handle) get32BitDataFromBitmapImageRep:(NSBitmapImageRep*)bitmapImageRep requiredPixelSize:(int)requiredPixelSize |
| 982 | { |
| 983 | Handle hRawData; |
| 984 | unsigned char* pRawData; |
| 985 | Size rawDataSize; |
| 986 | unsigned char* pSrc; |
| 987 | unsigned char* pDest; |
| 988 | int x, y; |
| 989 | unsigned char alphaByte; |
| 990 | float oneOverAlpha; |
| 991 | |
| 992 | |
| 993 | int pixelsWide = [bitmapImageRep pixelsWide]; |
| 994 | int pixelsHigh = [bitmapImageRep pixelsHigh]; |
| 995 | int bitsPerSample = [bitmapImageRep bitsPerSample]; |
| 996 | int samplesPerPixel = [bitmapImageRep samplesPerPixel]; |
| 997 | int bitsPerPixel = [bitmapImageRep bitsPerPixel]; |
| 998 | BOOL isPlanar = [bitmapImageRep isPlanar]; |
| 999 | int bytesPerRow = [bitmapImageRep bytesPerRow]; |
| 1000 | unsigned char* bitmapData = [bitmapImageRep bitmapData]; |
| 1001 | |
| 1002 | |
| 1003 | if (pixelsWide != requiredPixelSize || pixelsHigh != requiredPixelSize) |
| 1004 | return NULL( ( void * ) 0 ); |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | if (isPlanar) |
| 1010 | { |
| 1011 | NSLog(@"get32BitDataFromBitmapImageRep:requiredPixelSize: returning NULL due to isPlanar == YES"); |
| 1012 | return NULL( ( void * ) 0 ); |
| 1013 | } |
| 1014 | if (bitsPerSample != 8) |
| 1015 | { |
| 1016 | NSLog(@"get32BitDataFromBitmapImageRep:requiredPixelSize: returning NULL due to bitsPerSample == %d", bitsPerSample); |
| 1017 | return NULL( ( void * ) 0 ); |
| 1018 | } |
| 1019 | |
| 1020 | if (((samplesPerPixel == 3) && (bitsPerPixel == 24)) || ((samplesPerPixel == 4) && (bitsPerPixel == 32))) |
| 1021 | { |
| 1022 | rawDataSize = pixelsWide * pixelsHigh * 4; |
| 1023 | hRawData = NewHandle( rawDataSize ); |
| 1024 | if (hRawData == NULL( ( void * ) 0 )) |
| 1025 | return NULL( ( void * ) 0 ); |
| 1026 | pRawData = (unsigned char*) *hRawData; |
| 1027 | |
| Value stored to 'pSrc' is never read |
| 1028 | pSrc = bitmapData; |
| 1029 | pDest = pRawData; |
| 1030 | |
| 1031 | if (bitsPerPixel == 32) { |
| 1032 | for (y = 0; y < pixelsHigh; y++) { |
| 1033 | pSrc = bitmapData + y * bytesPerRow; |
| 1034 | for (x = 0; x |