aboutsummaryrefslogtreecommitdiffstats
path: root/Source/SPChooseMenuItemDialog.m
diff options
context:
space:
mode:
Diffstat (limited to 'Source/SPChooseMenuItemDialog.m')
-rw-r--r--Source/SPChooseMenuItemDialog.m113
1 files changed, 92 insertions, 21 deletions
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