0
votes

I'm creating a simple text editor for Mac using the Cocoa API and all works fine. The only problem is that the Close menu item is always disabled. Should I implement some delegate method in the document controller? Anyone who already had this problem?

2

2 Answers

0
votes

Any chance the Close menu item isn't sending the -performClose: method to the "First Responder" object (which ends up being a NSWindow)? I've noticed in the past that if menu items don't have a target for their actions, they appear grayed out.

0
votes

I finally figured it out. The problem is that every window of my document-based app is borderless, and I recently read that borderless windows cannot get closed via the performClose: method that the menu item (or the Cmd-W key) sends to firstResponder. So I had to implement some additional methods in the window's subclass:

- (void)performClose:(id)sender {
    [documentClass canCloseDocumentWithDelegate:self shouldCloseSelector:@selector(document:shouldClose:contextInfo:) contextInfo:NULL];
}

- (void)document:(NSDocument*)doc shouldClose:(BOOL)shouldClose contextInfo:(void*)contextInfo {
    if (shouldClose)
        [doc close];
}

- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
    return ([menuItem action]==@selector(performClose:))?YES:[super validateMenuItem:menuItem];
}

- (BOOL)canBecomeMainWindow {
    return YES;
}

- (BOOL)canBecomeKeyWindow {
    return YES;
}

The last two methods make sure that the window can get the focus and that some other functions can be executed (like the Find command for textviews).