Marking a Document Window as Edited

An application should properly handle the closing of a document window by setting the docEdited flag, an instance variable declared in the NSWindow class. The docEdited flag determines whether an on-screen window’s close button (in the upper-left corner of the window) is a solid red disk (docEdited=NO) or has a dot in its center (docEdited=YES). The two possibilities are shown in Figure 13-6.

MathPaper windows without (left) and with (right) dot in close button

Figure 13-6. MathPaper windows without (left) and with (right) dot in close button

In addition to providing feedback to the user in the close button, the docEdited flag is used by Cocoa programs to determine whether a document window can safely be closed without any loss of data. Obviously, the docEdited flag should be set when the text inside the MathPaper page has been edited and unset when it has been saved or newly opened.

The Cocoa multiple-document architecture automates much of the maintenance of the docEdited flag with a second flag, called the change count . The framework knows to reset a document’s change count when it is saved. All you need to do is to set the change count when the document is first “dirtied.”[34]

  1. Insert the statement shown here in bold into the textDidChange: method in PaperController.m:

    - (void)textDidChange:(NSNotification *)notification
    {
        NSString *str = [ [ [self window] currentEvent] characters];
    
        [[self document] updateChangeCount:NSChangeDone]; ...

Get Building Cocoa Applications: A Step by Step Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.