UniqueWindowController finds windows that are miniaturized.
Added some date and more graphics utilities.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/DateUtils.h Wed Apr 02 14:45:33 2008 -0700
1.3 @@ -0,0 +1,12 @@
1.4 +//
1.5 +// DateUtils.h
1.6 +// Cloudy
1.7 +//
1.8 +// Created by Jens Alfke on 3/25/08.
1.9 +// Copyright 2008 __MyCompanyName__. All rights reserved.
1.10 +//
1.11 +
1.12 +#import <Cocoa/Cocoa.h>
1.13 +
1.14 +
1.15 +NSTimeInterval TimeIntervalSinceBoot(void);
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/DateUtils.m Wed Apr 02 14:45:33 2008 -0700
2.3 @@ -0,0 +1,28 @@
2.4 +//
2.5 +// DateUtils.m
2.6 +// Cloudy
2.7 +//
2.8 +// Created by Jens Alfke on 3/25/08.
2.9 +// Copyright 2008 __MyCompanyName__. All rights reserved.
2.10 +//
2.11 +
2.12 +#import "DateUtils.h"
2.13 +
2.14 +#include <assert.h>
2.15 +#include <CoreServices/CoreServices.h>
2.16 +#include <mach/mach.h>
2.17 +#include <mach/mach_time.h>
2.18 +#include <unistd.h>
2.19 +
2.20 +
2.21 +NSTimeInterval TimeIntervalSinceBoot(void)
2.22 +{
2.23 + // From http://developer.apple.com/qa/qa2004/qa1398.html
2.24 + uint64_t abstime = mach_absolute_time();
2.25 + // Have to do some pointer fun because AbsoluteToNanoseconds
2.26 + // works in terms of UnsignedWide, which is a structure rather
2.27 + // than a proper 64-bit integer.
2.28 + Nanoseconds elapsedNano = AbsoluteToNanoseconds( *(AbsoluteTime *) &abstime );
2.29 + return *(uint64_t*)&elapsedNano / 1.0e9;
2.30 +}
2.31 +
3.1 --- a/GraphicsUtils.h Thu Mar 20 09:05:58 2008 -0700
3.2 +++ b/GraphicsUtils.h Wed Apr 02 14:45:33 2008 -0700
3.3 @@ -12,10 +12,17 @@
3.4 - (NSImage*) my_shrunkToFitIn: (NSSize) maxSize;
3.5 - (NSSize) my_sizeOfLargestRep;
3.6 - (NSData*) my_JPEGData;
3.7 -//- (NSData*) my)_PICTData;
3.8 +- (NSData*) my_dataInFormat: (NSBitmapImageFileType)format quality: (float)quality;
3.9 @end
3.10
3.11
3.12 @interface NSBezierPath (MYUtilities)
3.13 + (NSBezierPath*) my_bezierPathWithRoundRect: (NSRect)rect radius: (float)radius;
3.14 @end
3.15 +
3.16 +
3.17 +NSArray* OpenWindowsWithDelegateClass( Class klass );
3.18 +
3.19 +
3.20 +/** Moves/resizes r to fit inside container */
3.21 +NSRect PinRect( NSRect r, NSRect container );
4.1 --- a/GraphicsUtils.m Thu Mar 20 09:05:58 2008 -0700
4.2 +++ b/GraphicsUtils.m Wed Apr 02 14:45:33 2008 -0700
4.3 @@ -11,6 +11,27 @@
4.4 @implementation NSImage (MYUtilities)
4.5
4.6
4.7 +- (NSBitmapImageRep*) my_bitmapRep
4.8 +{
4.9 + NSSize max = {0,0};
4.10 + NSBitmapImageRep *bestRep = nil;
4.11 + for( NSImageRep *rep in self.representations )
4.12 + if( [rep isKindOfClass: [NSBitmapImageRep class]] ) {
4.13 + NSSize size = [rep size];
4.14 + if( size.width > max.width || size.height > max.height ) {
4.15 + bestRep = (NSBitmapImageRep*)rep;
4.16 + max = size;
4.17 + }
4.18 + }
4.19 + if( ! bestRep ) {
4.20 + NSImage *tiffImage = [[NSImage alloc] initWithData:[self TIFFRepresentation]];
4.21 + bestRep = [[tiffImage representations] objectAtIndex:0];
4.22 + [tiffImage autorelease];
4.23 + }
4.24 + return bestRep;
4.25 +}
4.26 +
4.27 +
4.28 - (NSSize) my_sizeOfLargestRep
4.29 {
4.30 NSArray *reps = [self representations];
4.31 @@ -51,13 +72,16 @@
4.32
4.33 - (NSData*) my_JPEGData
4.34 {
4.35 - NSImage *tiffImage = [[NSImage alloc] initWithData:[self TIFFRepresentation]];
4.36 - NSBitmapImageRep *rep = [[tiffImage representations] objectAtIndex:0];
4.37 + return [self my_dataInFormat: NSJPEGFileType quality: 0.75f];
4.38 +}
4.39 +
4.40 +
4.41 +- (NSData*) my_dataInFormat: (NSBitmapImageFileType)format quality: (float)quality
4.42 +{
4.43 + NSBitmapImageRep *rep = self.my_bitmapRep;
4.44 NSDictionary *props = [NSDictionary dictionaryWithObjectsAndKeys:
4.45 - [NSNumber numberWithFloat: 0.75f], NSImageCompressionFactor, nil];
4.46 - NSData *jpeg = [rep representationUsingType: NSJPEGFileType properties: props];
4.47 - [tiffImage release];
4.48 - return jpeg;
4.49 + [NSNumber numberWithFloat: quality], NSImageCompressionFactor, nil];
4.50 + return [rep representationUsingType: format properties: props];
4.51 }
4.52
4.53
4.54 @@ -188,3 +212,32 @@
4.55 }
4.56
4.57 @end
4.58 +
4.59 +
4.60 +
4.61 +NSArray* OpenWindowsWithDelegateClass( Class klass )
4.62 +{
4.63 + NSMutableArray *windows = $marray();
4.64 + for( NSWindow *window in [NSApp windows] ) {
4.65 + id delegate = window.delegate;
4.66 + if( (window.isVisible || window.isMiniaturized) && [delegate isKindOfClass: klass] )
4.67 + [windows addObject: window];
4.68 + }
4.69 + return windows;
4.70 +}
4.71 +
4.72 +
4.73 +
4.74 +NSRect PinRect( NSRect r, NSRect container )
4.75 +{
4.76 + // Push r's origin inside container, and limit its size to the container's:
4.77 + r = NSMakeRect(MAX(r.origin.x, container.origin.x),
4.78 + MAX(r.origin.y, container.origin.y),
4.79 + MIN(r.size.width, container.size.width),
4.80 + MIN(r.size.height, container.size.height));
4.81 + // Push r's outside edges into the container:
4.82 + r.origin.x -= MAX(0, NSMaxX(r)-NSMaxX(container));
4.83 + r.origin.y -= MAX(0, NSMaxY(r)-NSMaxY(container));
4.84 + return r;
4.85 +
4.86 +}
5.1 --- a/UniqueWindowController.m Thu Mar 20 09:05:58 2008 -0700
5.2 +++ b/UniqueWindowController.m Wed Apr 02 14:45:33 2008 -0700
5.3 @@ -7,6 +7,7 @@
5.4 //
5.5
5.6 #import "UniqueWindowController.h"
5.7 +#import "GraphicsUtils.h"
5.8
5.9
5.10 @implementation UniqueWindowController
5.11 @@ -14,13 +15,10 @@
5.12
5.13 + (UniqueWindowController*) instanceWith: (id)model
5.14 {
5.15 - for( NSWindow *window in [NSApp windows] ) {
5.16 - id delegate = window.delegate;
5.17 - if( window.isVisible && [delegate isKindOfClass: [self class]] ) {
5.18 - UniqueWindowController *c = delegate;
5.19 - if( c.model == model )
5.20 - return c;
5.21 - }
5.22 + for( NSWindow *window in OpenWindowsWithDelegateClass(self) ) {
5.23 + UniqueWindowController *c = window.delegate;
5.24 + if( c.model == model )
5.25 + return c;
5.26 }
5.27 return nil;
5.28 }