Source/QuartzUtils.m
changeset 19 3b750982ff39
parent 10 6c78cc6bd7a6
child 21 2eb229411d73
     1.1 --- a/Source/QuartzUtils.m	Thu Jul 03 17:44:30 2008 -0700
     1.2 +++ b/Source/QuartzUtils.m	Thu Jul 17 13:29:04 2008 -0700
     1.3 @@ -105,8 +105,12 @@
     1.4          if( [name hasPrefix: @"/"] )
     1.5              path = name;
     1.6          else {
     1.7 -            path = [[NSBundle mainBundle] pathForResource: name ofType: nil];
     1.8 -            NSCAssert1(path,@"Couldn't find bundle image resource '%@'",name);
     1.9 +            NSString *dir = [name stringByDeletingLastPathComponent];
    1.10 +            name = [name lastPathComponent];
    1.11 +            NSString *ext = name.pathExtension;
    1.12 +            name = [name stringByDeletingPathExtension];
    1.13 +            path = [[NSBundle mainBundle] pathForResource: name ofType: ext inDirectory: dir];
    1.14 +            NSCAssert3(path,@"Couldn't find bundle image resource '%@' type '%@' in '%@'",name,ext,dir);
    1.15          }
    1.16          image = CreateCGImageFromFile(path);
    1.17          NSCAssert1(image,@"Failed to load image from %@",path);
    1.18 @@ -136,7 +140,21 @@
    1.19  
    1.20  
    1.21  #if ! TARGET_OS_IPHONE
    1.22 -CGImageRef GetCGImageFromPasteboard( NSPasteboard *pb )
    1.23 +
    1.24 +BOOL CanGetCGImageFromPasteboard( NSPasteboard *pb )
    1.25 +{
    1.26 +    return [NSImage canInitWithPasteboard: pb] 
    1.27 +        || [[pb types] containsObject: @"PixadexIconPathPboardType"];
    1.28 +
    1.29 +    /*if( [[pb types] containsObject: NSFilesPromisePboardType] ) {
    1.30 +     NSArray *fileTypes = [pb propertyListForType: NSFilesPromisePboardType];
    1.31 +     NSLog(@"Got file promise! Types = %@",fileTypes);
    1.32 +     //FIX: Check file types
    1.33 +     return NSDragOperationCopy;
    1.34 +     }*/
    1.35 +}    
    1.36 +
    1.37 +CGImageRef GetCGImageFromPasteboard( NSPasteboard *pb, id<NSDraggingInfo>dragInfo )
    1.38  {
    1.39      CGImageSourceRef src = NULL;
    1.40      NSArray *paths = [pb propertyListForType: NSFilenamesPboardType];
    1.41 @@ -144,6 +162,28 @@
    1.42          // If a file is being dragged, read it:
    1.43          CFURLRef url = (CFURLRef) [NSURL fileURLWithPath: [paths objectAtIndex: 0]];
    1.44          src = CGImageSourceCreateWithURL(url, NULL);
    1.45 +/*
    1.46 +    } else if( dragInfo && [[pb types] containsObject:NSFilesPromisePboardType] ) {
    1.47 +        NSString *dropDir = NSTemporaryDirectory();
    1.48 +        NSArray *filenames = [dragInfo namesOfPromisedFilesDroppedAtDestination: [NSURL fileURLWithPath: dropDir]];
    1.49 +        NSLog(@"promised files are %@ / %@", dropDir,filenames);
    1.50 +        src = nil; */
    1.51 +    } else if( [[pb types] containsObject: @"PixadexIconPathPboardType"] ) {
    1.52 +        // Candybar 3 (nee Pixadex) doesn't drag out icons in any normal image type.
    1.53 +        // It does support file-promises, but I couldn't get those to work using the Cocoa APIs.
    1.54 +        // So instead I'm using its custom type that provides the path(s) to its internal ".pxicon" files.
    1.55 +        // The icon is really easy to get from one of these: it's just file's custom icon.
    1.56 +        NSArray *files = [pb propertyListForType: @"PixadexIconPathPboardType"];
    1.57 +        if( files.count == 1 ) {
    1.58 +            NSString *path = [files objectAtIndex: 0];
    1.59 +            NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile: path];
    1.60 +            for( NSImageRep *rep in icon.representations ) {
    1.61 +                if( [rep isKindOfClass: [NSBitmapImageRep class]] ) {
    1.62 +                    [rep retain];   //FIX: This leaks; but if the rep goes away, the CGImage breaks...
    1.63 +                    return [(NSBitmapImageRep*)rep CGImage];
    1.64 +                }
    1.65 +            }
    1.66 +        }
    1.67      } else {
    1.68          // Else look for an image type:
    1.69          NSString *type = [pb availableTypeFromArray: [NSImage imageUnfilteredPasteboardTypes]];
    1.70 @@ -205,6 +245,7 @@
    1.71  
    1.72  float GetPixelAlpha( CGImageRef image, CGSize imageSize, CGPoint pt )
    1.73  {
    1.74 +    NSCParameterAssert(image);
    1.75  #if TARGET_OS_IPHONE
    1.76      // iPhone uses "flipped" (i.e. normal) coords, so images are wrong-way-up
    1.77      pt.y = imageSize.height - pt.y;