Adding API to ITStatusItem for callbacks to get dynamic menus.
[ITKit.git] / ITStatusItem.m
1 #import "ITStatusItem.h"
2
3 @class NSStatusBarButton;
4
5 @interface NSStatusItem (ITStatusItemHacks)
6 - (id)_initInStatusBar:(NSStatusBar *)statusBar withLength:(float)length withPriority:(int)priority;
7 - (NSStatusBarButton *)_button;
8 @end
9
10 @protocol _ITStatusItemNSStatusItemPantherCompatability
11 - (void)setAlternateImage:(NSImage *)image;
12 - (NSImage *)alternateImage;
13 @end
14
15 @implementation ITStatusItem
16
17 static BOOL _ITStatusItemShouldKillShadow = NO;
18
19 + (void)initialize {
20         if ((floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_1) && (floor(NSAppKitVersionNumber) <= 663.6)) {
21                 _ITStatusItemShouldKillShadow = YES;
22         }
23 }
24
25 - (id)initWithStatusBar:(NSStatusBar *)statusBar withLength:(float)length {
26         return [self _initInStatusBar:statusBar withLength:length withPriority:1000];
27 }
28
29 - (id)_initInStatusBar:(NSStatusBar *)statusBar withLength:(float)length withPriority:(int)priority {
30         if ((self = [super _initInStatusBar:statusBar withLength:length withPriority:priority])) {
31                 if (_ITStatusItemShouldKillShadow) {
32                         [[(NSButton *)[self _button] cell] setType:NSNullCellType];
33                 }
34             [self setHighlightMode:YES];
35         }
36         return self;
37 }
38
39 - (NSImage *)alternateImage {
40         if ([super respondsToSelector:@selector(alternateImage)]) {
41                 return [(id <_ITStatusItemNSStatusItemPantherCompatability>)super alternateImage];
42         }
43         return [(NSButton *)[self _button] alternateImage];
44 }
45
46 - (void)setAlternateImage:(NSImage*)image {
47         if ([super respondsToSelector:@selector(setAlternateImage:)]) {
48                 [(id <_ITStatusItemNSStatusItemPantherCompatability>)super setAlternateImage:image];
49                 return;
50         }
51         [(NSButton *)[self _button] setAlternateImage:image];
52 }
53
54 - (id <ITStatusItemMenuProvider>)menuProvider {
55         return _menuProvider;
56 }
57
58 - (void)setMenuProvider:(id <ITStatusItemMenuProvider>)provider {
59         [_menuProvider autorelease];
60         _menuProvider = [provider retain];
61 }
62
63 - (NSMenu *)menu {
64         if (_menuProvider) {
65                 return [_menuProvider menuForStatusItem:self];
66         } else {
67                 return [super menu];
68         }
69 }
70
71 @end