diff -r e55a17cdabd2 -r 3d3dcc3116d5 GraphicsUtils.m --- a/GraphicsUtils.m Thu Mar 20 09:05:58 2008 -0700 +++ b/GraphicsUtils.m Wed Apr 02 14:45:33 2008 -0700 @@ -11,6 +11,27 @@ @implementation NSImage (MYUtilities) +- (NSBitmapImageRep*) my_bitmapRep +{ + NSSize max = {0,0}; + NSBitmapImageRep *bestRep = nil; + for( NSImageRep *rep in self.representations ) + if( [rep isKindOfClass: [NSBitmapImageRep class]] ) { + NSSize size = [rep size]; + if( size.width > max.width || size.height > max.height ) { + bestRep = (NSBitmapImageRep*)rep; + max = size; + } + } + if( ! bestRep ) { + NSImage *tiffImage = [[NSImage alloc] initWithData:[self TIFFRepresentation]]; + bestRep = [[tiffImage representations] objectAtIndex:0]; + [tiffImage autorelease]; + } + return bestRep; +} + + - (NSSize) my_sizeOfLargestRep { NSArray *reps = [self representations]; @@ -51,13 +72,16 @@ - (NSData*) my_JPEGData { - NSImage *tiffImage = [[NSImage alloc] initWithData:[self TIFFRepresentation]]; - NSBitmapImageRep *rep = [[tiffImage representations] objectAtIndex:0]; + return [self my_dataInFormat: NSJPEGFileType quality: 0.75f]; +} + + +- (NSData*) my_dataInFormat: (NSBitmapImageFileType)format quality: (float)quality +{ + NSBitmapImageRep *rep = self.my_bitmapRep; NSDictionary *props = [NSDictionary dictionaryWithObjectsAndKeys: - [NSNumber numberWithFloat: 0.75f], NSImageCompressionFactor, nil]; - NSData *jpeg = [rep representationUsingType: NSJPEGFileType properties: props]; - [tiffImage release]; - return jpeg; + [NSNumber numberWithFloat: quality], NSImageCompressionFactor, nil]; + return [rep representationUsingType: format properties: props]; } @@ -188,3 +212,32 @@ } @end + + + +NSArray* OpenWindowsWithDelegateClass( Class klass ) +{ + NSMutableArray *windows = $marray(); + for( NSWindow *window in [NSApp windows] ) { + id delegate = window.delegate; + if( (window.isVisible || window.isMiniaturized) && [delegate isKindOfClass: klass] ) + [windows addObject: window]; + } + return windows; +} + + + +NSRect PinRect( NSRect r, NSRect container ) +{ + // Push r's origin inside container, and limit its size to the container's: + r = NSMakeRect(MAX(r.origin.x, container.origin.x), + MAX(r.origin.y, container.origin.y), + MIN(r.size.width, container.size.width), + MIN(r.size.height, container.size.height)); + // Push r's outside edges into the container: + r.origin.x -= MAX(0, NSMaxX(r)-NSMaxX(container)); + r.origin.y -= MAX(0, NSMaxY(r)-NSMaxY(container)); + return r; + +}