I created a custom view class because I wanted to have a status item you could drag items to.
Here's the definition of the view:
@interface DragStatusView : NSImageView <NSMenuDelegate>{
BOOL highlight;
}
@end
In my ApplicationDelegate.m I instantiate a NSStatusItem, and an instance of my DragStatusView. I set the image on the DragStatusView, and also set its menu to an instance of NSMenu containing a few NSMenuItems.
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
// Install icon into the menu bar
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
NSImage *statusImage = [NSImage imageNamed:@"Status"];
[statusItem setImage:statusImage];
[menuItem setTitle:NSLocalizedString(@"Special Status", @"imgur menu item text")];
CGFloat itemHeight = [[NSStatusBar systemStatusBar] thickness];
NSRect itemRect = NSMakeRect(0.0, 0.0, NSSquareStatusItemLength, itemHeight);
DragStatusView* dragView = [[DragStatusView alloc] initWithFrame:itemRect];
[dragView retain];
[dragView setImage:statusImage];
[dragView setMenu:menu];
[statusItem setHighlightMode:YES];
[statusItem setView:dragView];
}
Here is the method in the DragStatusView controller that triggers the menu to pop up:
- (void)mouseDown:(NSEvent *)event {
[[[NSApp delegate] statusItem] popUpStatusItemMenu:[self menu]]; // or another method that returns a menu
}
This mostly works, however the menu appears too high when you click on the status item.
How it looks before clicking: http://imgur.com/fpJcd,quS3c#1
How it looks after clicking: http://imgur.com/fpJcd,quS3c#0 (the menu appears at the top of the screen -- ahh!)
How can I make the menu appear at the bottom of the status bar?
Thanks!