I have menu which popped out after button click.
Add some items to menu and make first item submenu.
I want to handle the "select event" in all items and with standard menu all is fine. But when i try to make custom View at NSMenuItem the submenu lost the ability to send events to action selector and custom NSView can't handle the mouseUp and mouseDown events.
Here my code to make all clear
#import "CRGAppDelegate.h"
@interface customItemView : NSView
{
NSTrackingArea *_trackArea;
}
@end
@implementation customItemView
-(void)drawRect:(NSRect)dirtyRect{
[[NSColor redColor] set];
[[NSBezierPath bezierPathWithRect:NSMakeRect(0, 0, 1, 100)] fill];
NSString* str = self.enclosingMenuItem.title;
[str drawInRect:NSMakeRect(10, 4, 80, 22) withAttributes:nil];
}
-(void)mouseDown:(NSEvent *)theEvent
{
NSLog(@"Mouse Down");
}
-(void)mouseUp:(NSEvent *)theEvent
{
NSLog(@"Mouse Up");
}
-(void)updateTrackingAreas
{
[self removeTrackingArea:_trackArea];
_trackArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options:(NSTrackingMouseEnteredAndExited |
NSTrackingActiveAlways |
NSTrackingEnabledDuringMouseDrag
) owner:self userInfo:nil];
[self addTrackingArea:_trackArea];
}
@end
@implementation CRGAppDelegate
{
NSMenu* mainMenu;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
mainMenu = [[NSMenu alloc] initWithTitle:@"mainMenu"];
for (int i = 0; i < 10; ++i)
{
NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:@"title"
action:@selector(firstLevelItems:)
keyEquivalent:@""];
[item setView:[[customItemView alloc] initWithFrame:NSMakeRect(0, 0, 100, 30)]];
[mainMenu addItem:item];
if (i == 0)
{
//add submenu
NSMenu* submenu = [[NSMenu alloc] initWithTitle:@"Submenu"];
[item setSubmenu:submenu];
for (int j = 0; j < 5; ++j)
{
NSMenuItem* submenuItem = [[NSMenuItem alloc] initWithTitle:@"submenuitem"
action:@selector(secondLevelItems:)
keyEquivalent:@""];
[submenu addItem:submenuItem];
}
}
}
}
-(void)secondLevelItems:(id)sender
{
NSLog(@"%@",sender);
}
-(void)firstLevelItems:(id)sender
{
NSLog(@"%@",sender);
}
- (IBAction)ExpandMenuAction:(id)sender {
[mainMenu popUpMenuPositioningItem:nil atLocation:NSMakePoint(0, 35) inView:_OpenMenuButton];
}
@end
How can i receive select event in this case?