From dbbcc4144a7abef479ae9982563ed3a1cbdb725c Mon Sep 17 00:00:00 2001 From: Bibiko Date: Mon, 6 Dec 2010 08:58:38 +0000 Subject: =?UTF-8?q?=E2=80=A2=20finished=20implementation=20of=20SPChooseMe?= =?UTF-8?q?nuItemDialog=20=E2=80=A2=20simplified=20bundle=20command=20hand?= =?UTF-8?q?ling=20and=20handling=20for=20bundle=20commands=20which=20have?= =?UTF-8?q?=20the=20same=20key=20equivalent=20to=20be=20able=20to=20group?= =?UTF-8?q?=20similar=20bundle=20commands=20=E2=80=A2=20minor=20code=20imp?= =?UTF-8?q?rovements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPChooseMenuItemDialog.m | 113 ++++++++++++++++++++++++++++++++-------- 1 file changed, 92 insertions(+), 21 deletions(-) (limited to 'Source/SPChooseMenuItemDialog.m') diff --git a/Source/SPChooseMenuItemDialog.m b/Source/SPChooseMenuItemDialog.m index e966613b..8be639e3 100644 --- a/Source/SPChooseMenuItemDialog.m +++ b/Source/SPChooseMenuItemDialog.m @@ -24,52 +24,111 @@ #import "SPChooseMenuItemDialog.h" +@interface SPChooseMenuItemDialogTextView : NSTextView +{ +} + +- (IBAction)menuItemHandler:(id)sender; + +@end + +@implementation SPChooseMenuItemDialogTextView +{ +} +- (id)init; +{ + if(self = [super initWithFrame:NSMakeRect(1,1,2,2)]) + { + ; + } + return self; +} + +- (IBAction)menuItemHandler:(id)sender +{ + [[self delegate] setSelectedItemIndex:[sender tag]]; + [[self delegate] setWaitForChoice:NO]; +} + +- (NSMenu *)menuForEvent:(NSEvent *)event +{ + return [[self delegate] contextMenu]; +} + +@end @implementation SPChooseMenuItemDialog @synthesize contextMenu; +@synthesize selectedItemIndex; +@synthesize waitForChoice; - (id)init; { - if(self = [self initWithContentRect:NSMakeRect(10,10,10,10) + if(self = [super initWithContentRect:NSMakeRect(1,1,2,2) styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]) { - ; + waitForChoice = YES; + selectedItemIndex = -1; } return self; } - (void)dealloc { - [tv release]; + [dummyTextView release]; [super dealloc]; } -- (void)initMeWithOptions:(NSDictionary *)displayOptions +- (void)initDialog { [self setReleasedWhenClosed:YES]; [self setLevel:NSNormalWindowLevel]; [self setHidesOnDeactivate:YES]; [self setHasShadow:YES]; - [self setAlphaValue:0.9]; - tv = [[NSTextView alloc] initWithFrame:NSMakeRect(10,10,10,10)]; - [self setContentView:tv]; - [tv setDelegate:self]; - [tv setEditable:YES]; + [self setAlphaValue:0.0]; + + dummyTextView = [[SPChooseMenuItemDialogTextView alloc] init]; + [dummyTextView setDelegate:self]; + + [self setContentView:dummyTextView]; + } -+ (void)displayMenu:(NSMenu*)theMenu atPosition:(NSPoint)location ++ (NSInteger)withItems:(NSArray*)theList atPosition:(NSPoint)location { - SPChooseMenuItemDialog *dialog = [SPChooseMenuItemDialog new]; - [dialog initMeWithOptions:nil]; + if(!theList || ![theList count]) return -1; - NSMenuItem *returnItem = nil; + SPChooseMenuItemDialog *dialog = [SPChooseMenuItemDialog new]; + [dialog initDialog]; + + NSMenu *theMenu = [[[NSMenu alloc] init] autorelease]; + NSInteger cnt = 0; + for(id item in theList) { + NSMenuItem *aMenuItem; + if([item isKindOfClass:[NSString class]]) + aMenuItem = [[NSMenuItem alloc] initWithTitle:item action:@selector(menuItemHandler:) keyEquivalent:@""]; + else if([item isKindOfClass:[NSDictionary class]]) { + NSString *title = ([item objectForKey:@"title"]) ?: @""; + SEL action = ([item objectForKey:@"action"]) ? NSSelectorFromString([item objectForKey:@"action"]) : @selector(menuItemHandler:); + NSString *keyEquivalent = ([item objectForKey:@"key"]) ?: @""; + aMenuItem = [[NSMenuItem alloc] initWithTitle:title action:action keyEquivalent:keyEquivalent]; + if([item objectForKey:@"tooltip"]) + [aMenuItem setToolTip:[item objectForKey:@"tooltip"]]; + } + [aMenuItem setTag:cnt++]; + [theMenu addItem:aMenuItem]; + [aMenuItem release]; + } [dialog setContextMenu:theMenu]; + [dialog setFrameTopLeftPoint:location]; - [dialog orderFront:nil]; + [dialog makeKeyAndOrderFront:nil]; + + // Send a right-click to order front the context menu NSEvent *theEvent = [NSEvent mouseEventWithType:NSRightMouseDown location:NSMakePoint(1,1) @@ -77,18 +136,30 @@ timestamp:1 windowNumber:[dialog windowNumber] context:[NSGraphicsContext currentContext] - eventNumber:1 + eventNumber:0 clickCount:1 pressure:0.0]; - [[NSApplication sharedApplication] postEvent:theEvent atStart:NO]; + [[NSApplication sharedApplication] sendEvent:theEvent]; -} + while([dialog waitForChoice] && [[[NSApp keyWindow] firstResponder] isKindOfClass:[SPChooseMenuItemDialogTextView class]]) { -- (NSMenu *)menuForEvent:(NSEvent *)event -{ - NSLog(@"asdasdasd"); - return contextMenu; + NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask + untilDate:[NSDate distantFuture] + inMode:NSDefaultRunLoopMode + dequeue:YES]; + + if(!event) continue; + + [NSApp sendEvent:event]; + + usleep(1000); + + } + + [dialog performSelector:@selector(close) withObject:nil afterDelay:0.01]; + + return [dialog selectedItemIndex]; } @end -- cgit v1.2.3