I had written this NSOpenPanel category to work in a plugin environment, and I think it should do the right thing. Just set up the NSOpenPanel as you like then call - runModalForDirectory:file:types:relativeToWindow: and it will return when the user has selected (or not) a file.

steve


// category on NSOpenPanel that runs the open sheet modally and returns when done
@interface NSOpenPanel(ModalSheets)

- (NSInteger)runModalForDirectory:(NSString*)path file:(NSString*) name types:(NSArray*)fileTypes
                        relativeToWindow:(NSWindow*)window;

@end

@implementation NSOpenPanel(ModalSheets)

- (void)__modalOpenPanelDidEnd:(NSOpenPanel*)panel returnCode:(int) returnCode contextInfo:(void*)contextInfo
{
        #pragma unused(panel, contextInfo)

        [NSApp stopModalWithCode:returnCode];
}

- (NSInteger)runModalForDirectory:(NSString*)path file:(NSString*) name types:(NSArray*)fileTypes
                                                
relativeToWindow:(NSWindow*)window
{
        NSInteger       result;

        if (window != nil)
        {
[self beginSheetForDirectory:path file:name modalForWindow:window modalDelegate:self didEndSelector:@selector (__modalOpenPanelDidEnd:returnCode:contextInfo:) contextInfo:nil];

                result = [NSApp runModalForWindow:self];

                [NSApp endSheet:self];
        }
        else
        {
                result = [self runModalForDirectory:path file:name 
types:fileTypes];
        }

        return result;
}

@end




On Oct 14, 2009, at 6:02 AM, Motti Shneor wrote:

I'm in a strange situation, where I am implementing a plugin component that runs within a host application which I don't have access to.

Within this context, The host sometimes calls my plug-in to open an NSSavePanel (or NSOpenPanel). The host expects that I'm synchronous --- i.e. I only return when the NSSavePanel is dismissed, and there's a result.

However, The host also provides me with its own Window, and I need to open my NSSavePanel as a Sheet-window over the host's window.

Now NSSavePanel (and NSOpenPanel) provide 2 different ways to run them

1. runModal (or a vaiant) that is synchronous --- but it does not create a sheet window 2 beginSheetFor... (or variants) that are asynchronous (I must supply with a callback selector to be called as the NSSavePanel is dismissed) --- these DO create a sheet over the parent window.

Is there a decent way to combine these two requirements?

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to