paulmelnikow
paulmelnikow

Reputation: 17208

Overriding "Edited" in window title for NSDocument

How do I prevent a window title from displaying "Edited" for an NSDocument which is dirty?

I'm managing saving and autosaving myself, using a web service, and just don't want the distraction in the title bar.

I've tried overriding:

In all cases, the title bar still changes to Edited when I make changes to a saved document.

If I override -[NSDocument updateChangeCount:] and -[NSDocument updateChangeCountWithToken:forSaveOperation:] to do nothing, I can prevent this from happening, but it affects saving, autosaving, and other document behaviors, too.

I also tried this:

[[self.window standardWindowButton: NSWindowDocumentVersionsButton] setTitle:nil];

That displayed a blank string instead of Edited, but the dash still appeared – the one which normally separates the document name and Edited.

Any idea how to pry apart this part of the window from the document?

Upvotes: 9

Views: 2432

Answers (3)

jvarela
jvarela

Reputation: 3847

Although this is a late answer, you can easily determine what is going to be the title of your NSDocument window by overriding

- (NSString *)windowTitleForDocumentDisplayName:(NSString *)displayName

in your NSWindowController and return the appropriate title.

You can do that also by overriding the property of your NSDocument:

- (NSString *)displayName

but this is not recommended by Apple, because that is normally used by the OS error handlers.

I added this answer, because none of the other answers really set me on the right path.

Upvotes: 0

Yoav
Yoav

Reputation: 6098

Several options:

  1. To get a pointer to the "dash", look for a TextField in [window.contentView.superview.subviews] with a stringValue equals to "-". You can set its text to an empty string as well.

    @implementation NSWindow (DashRetrivalMethod)
    - (NSTextField*)versionsDashTextField
    {
        NSTextField* res = nil;
        NSView* themeFrame = [self.contentView superview];
        for (NSView* tmp in [themeFrame subviews])
        {
            if ([tmp isKindOfClass:[NSTextField class]])
            {
                if ([[(NSTextField*)tmp stringValue] isEqualToString:@"—"])
                {
                      res = (NSTextField*)tmp;
                      break;
                }
            }
        }
        return res;
    }
    @end
    
  2. You can override NSWindow's -setRepresentedURL:. This would also affect the NSWindowDocumentIconButton and the popup menu, but you can manually create it if you want by: [NSWindow standardWindowButton: NSWindowDocumentIconButton].

  3. Override one of these three NSDocument's undocumented methods:

    // Always return here NO if you don't want the version button to appear. 
    // This seems to be the cleanest options, besides the fact that you are 
    /// overriding a private method.
    - (BOOL)_shouldShowAutosaveButtonForWindow:(NSWindow*)window;
    
    // Call super with NO
    - (void)_setShowAutosaveButton:(BOOL)flag; 
    
    // Here the button and the dash are actually created
    - (void)_endVersionsButtonUpdates; 
    
    // Here Cocoa hide or unhide the edited button
    - (void)_updateDocumentEditedAndAnimate:(BOOL)flag
    

Upvotes: 5

thundersteele
thundersteele

Reputation: 673

Have you tried overriding NSDocuments - (BOOL)hasUnautosavedChanges in addition to overriding - (BOOL) isDocumentEdited?

Upvotes: 1

Related Questions