# HG changeset patch # User Jens Alfke # Date 1207172733 25200 # Node ID 3d3dcc3116d58278905c09215c83dc4f17549435 # Parent e55a17cdabd29568a4e8bc84606f373c83e0d522 UniqueWindowController finds windows that are miniaturized. Added some date and more graphics utilities. diff -r e55a17cdabd2 -r 3d3dcc3116d5 DateUtils.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DateUtils.h Wed Apr 02 14:45:33 2008 -0700 @@ -0,0 +1,12 @@ +// +// DateUtils.h +// Cloudy +// +// Created by Jens Alfke on 3/25/08. +// Copyright 2008 __MyCompanyName__. All rights reserved. +// + +#import + + +NSTimeInterval TimeIntervalSinceBoot(void); diff -r e55a17cdabd2 -r 3d3dcc3116d5 DateUtils.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DateUtils.m Wed Apr 02 14:45:33 2008 -0700 @@ -0,0 +1,28 @@ +// +// DateUtils.m +// Cloudy +// +// Created by Jens Alfke on 3/25/08. +// Copyright 2008 __MyCompanyName__. All rights reserved. +// + +#import "DateUtils.h" + +#include +#include +#include +#include +#include + + +NSTimeInterval TimeIntervalSinceBoot(void) +{ + // From http://developer.apple.com/qa/qa2004/qa1398.html + uint64_t abstime = mach_absolute_time(); + // Have to do some pointer fun because AbsoluteToNanoseconds + // works in terms of UnsignedWide, which is a structure rather + // than a proper 64-bit integer. + Nanoseconds elapsedNano = AbsoluteToNanoseconds( *(AbsoluteTime *) &abstime ); + return *(uint64_t*)&elapsedNano / 1.0e9; +} + diff -r e55a17cdabd2 -r 3d3dcc3116d5 GraphicsUtils.h --- a/GraphicsUtils.h Thu Mar 20 09:05:58 2008 -0700 +++ b/GraphicsUtils.h Wed Apr 02 14:45:33 2008 -0700 @@ -12,10 +12,17 @@ - (NSImage*) my_shrunkToFitIn: (NSSize) maxSize; - (NSSize) my_sizeOfLargestRep; - (NSData*) my_JPEGData; -//- (NSData*) my)_PICTData; +- (NSData*) my_dataInFormat: (NSBitmapImageFileType)format quality: (float)quality; @end @interface NSBezierPath (MYUtilities) + (NSBezierPath*) my_bezierPathWithRoundRect: (NSRect)rect radius: (float)radius; @end + + +NSArray* OpenWindowsWithDelegateClass( Class klass ); + + +/** Moves/resizes r to fit inside container */ +NSRect PinRect( NSRect r, NSRect container ); 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; + +} diff -r e55a17cdabd2 -r 3d3dcc3116d5 UniqueWindowController.m --- a/UniqueWindowController.m Thu Mar 20 09:05:58 2008 -0700 +++ b/UniqueWindowController.m Wed Apr 02 14:45:33 2008 -0700 @@ -7,6 +7,7 @@ // #import "UniqueWindowController.h" +#import "GraphicsUtils.h" @implementation UniqueWindowController @@ -14,13 +15,10 @@ + (UniqueWindowController*) instanceWith: (id)model { - for( NSWindow *window in [NSApp windows] ) { - id delegate = window.delegate; - if( window.isVisible && [delegate isKindOfClass: [self class]] ) { - UniqueWindowController *c = delegate; - if( c.model == model ) - return c; - } + for( NSWindow *window in OpenWindowsWithDelegateClass(self) ) { + UniqueWindowController *c = window.delegate; + if( c.model == model ) + return c; } return nil; }