Skip navigation.
 
mlNSDocumentController singleton problem, part 2
FROM : Brad Oliver
DATE : Mon Dec 16 07:42:33 2002

I've got an app for which I need to have at most one document open at a
time. Per some prior suggestions, I've tried variations on hacking my
NSDocument subclass and also an NSDocumentController subclass.

In a nutshell, I can't get it to work 100%. By overriding
NSDocumentController, I can get my app to Do The Right Thing, but with one
major caveat: it puts up an error message (spawned somewhere in AppKit)
about how it can't create a new document, due most likely to the fact that I
return NULL in my openUntitledDocumentOfType, etc methods.

Can someone suggest a better way to do this? I'm guessing that I have to
give up those nice sheet dialogs that AppKit provides automagically and
implement my own "save" dialogs that run modally, but that seems nasty.

Surely someone has run into this before? :-)

@interface NSDocumentController_Single : NSDocumentController
{
    BOOL    m_secondPass;
}

- (void)documentController:(NSDocumentController *)docController
didCloseAll:(BOOL)didCloseAll  contextInfo:(void *)contextInfo;
- (void)openUntitled:(NSDocumentController *)docController
didCloseAll:(BOOL)didCloseAll  contextInfo:(void *)contextInfo;

@end

...

@implementation NSDocumentController_Single

- (id)openDocumentWithContentsOfFile:(NSString *)fileName display:(BOOL)flag
{
    if (m_secondPass == NO)
    {
        [super closeAllDocumentsWithDelegate: self
            didCloseAllSelector: @selector(documentController: didCloseAll:
contextInfo:)
            contextInfo: fileName];
        return NULL;
    }

    m_secondPass = NO;
    return [super openDocumentWithContentsOfFile: fileName display: flag];
}

- (id)openUntitledDocumentOfType:(NSString *)docType display:(BOOL)flag
{
    if (m_secondPass == NO)
    {
        [self closeAllDocumentsWithDelegate: self
            didCloseAllSelector: @selector(openUntitled: didCloseAll:
contextInfo:)
            contextInfo: docType];
        return NULL;
    }

    m_secondPass = NO;
    return [super openUntitledDocumentOfType: docType display: flag];
}

- (void)documentController:(NSDocumentController *)docController
didCloseAll:(BOOL)didCloseAll  contextInfo:(void *)contextInfo
{
    if (didCloseAll)
    {
        m_secondPass = YES;
        [[NSDocumentController sharedDocumentController]
openDocumentWithContentsOfFile: (NSString *) contextInfo display: YES];
    }
    else
        m_secondPass = NO;
}

- (void)openUntitled:(NSDocumentController *)docController
didCloseAll:(BOOL)didCloseAll  contextInfo:(void *)contextInfo
{
    if (didCloseAll)
    {
        m_secondPass = YES;
        [[NSDocumentController sharedDocumentController]
openUntitledDocumentOfType: (NSString *) contextInfo display: YES];
    }
    else
        m_secondPass = NO;
}

--
Brad Oliver
<email_removed>
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

Related mailsAuthorDate
mlNSDocumentController singleton problem, part 2 Brad Oliver Dec 16, 07:42
mlRe: NSDocumentController singleton problem, part 2 matt neuburg Dec 17, 19:01
mlRe: NSDocumentController singleton problem, part 2 Brad Oliver Dec 17, 22:58
mlRe: NSDocumentController singleton problem, part 2 matt neuburg Dec 18, 18:49
mlRe: NSDocumentController singleton problem, part 2 Brad Oliver Dec 18, 20:36
mlRe: NSDocumentController singleton problem, part 2 matt neuburg Dec 19, 17:57
mlRe: NSDocumentController singleton problem, part 2 Brad Oliver Dec 19, 19:18
mlRe: NSDocumentController singleton problem, part 2 Mike Ferris Dec 20, 18:59
mlRe: NSDocumentController singleton problem, part 2 Brad Oliver Jan 3, 22:29