CoreImage effect loads only with OS X 10.4.0 through 10.4.5. It crashes 10.4.6 currently.
[ITKit.git] / ITStatusItem.m
old mode 100755 (executable)
new mode 100644 (file)
index 31ff2e3..0978705
 #import "ITStatusItem.h"
 #import "ITStatusItem.h"
+#import <CoreServices/CoreServices.h>
 
 
-/*************************************************************************/
-#pragma mark -
-#pragma mark EVIL HACKERY
-/*************************************************************************/
+@interface ITStatusItemMenuProxy : NSProxy {
+       id <ITStatusItemMenuProvider> menuProvider;
+       ITStatusItem *statusItem;
+       AbsoluteTime cachedTime;
+       NSMenu *menu;
+}
 
 
-// This stuff is actually implemented by the AppKit.
-// We declare it here to cancel out warnings.
+- (id)initWithMenuProvider:(id <ITStatusItemMenuProvider>)provider statusItem:(ITStatusItem *)item;
 
 
-@interface NSStatusBarButton : NSButton
 @end
 
 @end
 
-@interface NSStatusItem (HACKHACKHACKHACK)
-- (id)_initInStatusBar:(NSStatusBar*)statusBar
-             withLength:(float)length
-           withPriority:(int)priority;
-- (NSStatusBarButton*)_button;
+@implementation ITStatusItemMenuProxy
+
++ (BOOL)respondsToSelector:(SEL)aSelector {
+       if (![super respondsToSelector:aSelector]) {
+               return [NSMenu respondsToSelector:aSelector];
+       }
+       return YES;
+}
+
+- (id)initWithMenuProvider:(id <ITStatusItemMenuProvider>)provider statusItem:(ITStatusItem *)item {
+       menuProvider = [provider retain];
+       statusItem = [item retain];
+       cachedTime = UpTime();
+       menu = nil;
+       return self;
+}
+
+- (void)forwardInvocation:(NSInvocation *)anInvocation {
+       AbsoluteTime diff = SubAbsoluteFromAbsolute(UpTime(),cachedTime);
+       
+       if (!menu || diff.lo > 1000000) {
+               [menu release];
+               menu = [[menuProvider menuForStatusItem:statusItem] retain];
+               cachedTime = UpTime();
+       }
+       
+       [anInvocation setTarget:menu];
+       [anInvocation invoke];
+}
+
+- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
+       return [NSMenu instanceMethodSignatureForSelector:aSelector];
+}
+
+- (void)dealloc {
+       [menu release];
+       [statusItem release];
+       [menuProvider release];
+       [super dealloc];
+}
+
 @end
 
 @end
 
+@class NSStatusBarButton;
 
 
-/*************************************************************************/
-#pragma mark -
-#pragma mark PRIVATE METHOD DECLARATIONS
-/*************************************************************************/
+@interface NSStatusItem (ITStatusItemHacks)
+- (id)_initInStatusBar:(NSStatusBar *)statusBar withLength:(float)length withPriority:(int)priority;
+- (NSStatusBarButton *)_button;
+@end
 
 
-@interface ITStatusItem (Private)
-- (void)setImage:(NSImage*)image;
-- (NSString*) title;
-- (void)setTitle:(NSString*)title;
-- (void)setSmallTitle:(NSString*)title;
+@protocol _ITStatusItemNSStatusBarButtonMethods
+- (NSMenu *)statusMenu;
+- (void)setStatusMenu:(NSMenu *)menu;
 @end
 
 @end
 
+@protocol _ITStatusItemNSStatusItemPantherCompatability
+- (void)setAlternateImage:(NSImage *)image;
+- (NSImage *)alternateImage;
+@end
 
 @implementation ITStatusItem
 
 
 @implementation ITStatusItem
 
-/*************************************************************************/
-#pragma mark -
-#pragma mark INITIALIZATION METHODS
-/*************************************************************************/
-
-- (id)initWithStatusBar:(NSStatusBar*)statusBar withLength:(float)length
-{
-    if ( ( self = [super _initInStatusBar:statusBar
-                               withLength:length
-                             withPriority:1000] ) ) {
-                             
-        //Eliminate the fucking shadow...
-        [[[self _button] cell] setType:NSNullCellType];
-        
-        //Be something other than a dumbshit about highlighting...
-        [self setHighlightMode:YES];
-    }
-    return self;
+static BOOL _ITStatusItemShouldKillShadow = NO;
+
++ (void)initialize {
+       if ((floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_1) && (floor(NSAppKitVersionNumber) <= 663.6)) {
+               _ITStatusItemShouldKillShadow = YES;
+       }
 }
 
 }
 
+- (id)initWithStatusBar:(NSStatusBar *)statusBar withLength:(float)length {
+       return [self _initInStatusBar:statusBar withLength:length withPriority:1000];
+}
 
 
-/*************************************************************************/
-#pragma mark -
-#pragma mark ACCESSOR METHODS
-/*************************************************************************/
+- (id)_initInStatusBar:(NSStatusBar *)statusBar withLength:(float)length withPriority:(int)priority {
+       if ((self = [super _initInStatusBar:statusBar withLength:length withPriority:priority])) {
+               _menuProvider = nil;
+               _menuProxy = nil;
+               if (_ITStatusItemShouldKillShadow) {
+                       [[(NSButton *)[self _button] cell] setType:NSNullCellType];
+               }
+           [self setHighlightMode:YES];
+       }
+       return self;
+}
 
 
-- (NSImage*)alternateImage {
-    return [[self _button] alternateImage];
+- (NSImage *)alternateImage {
+       if ([super respondsToSelector:@selector(alternateImage)]) {
+               return [(id <_ITStatusItemNSStatusItemPantherCompatability>)super alternateImage];
+       }
+       return [(NSButton *)[self _button] alternateImage];
 }
 
 - (void)setAlternateImage:(NSImage*)image {
 }
 
 - (void)setAlternateImage:(NSImage*)image {
-    [[self _button] setAlternateImage:image];
+       if ([super respondsToSelector:@selector(setAlternateImage:)]) {
+               [(id <_ITStatusItemNSStatusItemPantherCompatability>)super setAlternateImage:image];
+               return;
+       }
+       [(NSButton *)[self _button] setAlternateImage:image];
 }
 
 }
 
-- (void)setImage:(NSImage*)image {
-    [super setImage:image];
-    if ( [self title] ) {
-        [self setTitle:[self title]];
-    } 
+- (id <ITStatusItemMenuProvider>)menuProvider {
+       return _menuProvider;
 }
 
 }
 
-- (void)setTitle:(NSString*)title {
-    if ( [self image] && (title != nil) ) {
-        [self setSmallTitle:title];
-    } else {
-        [super setTitle:title];
-    }
+- (void)setMenuProvider:(id <ITStatusItemMenuProvider>)provider {
+       [_menuProxy autorelease];
+       _menuProxy = nil;
+       _menuProvider = provider;
+       if (_menuProvider) {
+               _menuProxy = [[ITStatusItemMenuProxy alloc] initWithMenuProvider:_menuProvider statusItem:self];
+               [(id <_ITStatusItemNSStatusBarButtonMethods>)[self _button] setStatusMenu:_menuProxy];
+       } else {
+               [self setMenu:[self menu]];
+       }
 }
 
 }
 
-
-/*************************************************************************/
-#pragma mark -
-#pragma mark PRIVATE METHODS
-/*************************************************************************/
-
-- (void)setSmallTitle:(NSString*)title {
-    NSAttributedString *attrTitle = [[[NSAttributedString alloc] initWithString:title attributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Lucida Grande" size:12.0] forKey:NSFontAttributeName]] autorelease];
-    [self setAttributedTitle:attrTitle];
+- (void)dealloc {
+       [_menuProxy release];
+       [super dealloc];
 }
 
 }
 
-
-@end
+@end
\ No newline at end of file