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];
    
        if ([str isEqualToString:@"
    "]) {
            // Get the last line of text and send it to Evaluator
            NSString *str = [theText string];
            int i;
            for (i=[str length]-2;i>=0;i--) {
                if (i==0 || [str characterAtIndex:i-1] == '
    ') {
                    NSRange llRange = NSMakeRange(i,[str length]-i);
                    NSString *lastLine = [str substringWithRange:llRange];
                    [toEvaluator writeData:
                    [lastLine dataUsingEncoding:NSASCIIStringEncoding
                         allowLossyConversion:YES] ];
    
                    // Do not allow any more changes to the text
                    [theText setEditable:NO];
                    return;
                }
            }
        }
    }

Recall that the textDidChange: method is called every time a character is typed in a MathPaper window. Thus, immediately after the first character is typed, the textDidChange: method sets the change count flag to NSChangeDone, which tells the multiple-document architecture that a change has been made to the document. When this happens, the architecture places the dot in the window’s close button.

You might think that we need to reset the change count flag when the file is saved, but it turns out that this is not necessary; the multiple-document architecture handles this for us automatically.

Well, that’s it for the “save” code! Let’s test it out:

  1. Build and run the MathPaper application. Move your mouse into the MathPaper window. Notice that the red close button does not have a dot in it. (However, if you mouse over the button itself, you’ll see an X inside:

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

    .)

  2. Type an equation in the MathPaper window. Notice that the dot appears automatically as soon as the first character is typed.

  3. Choose File Save and save the MathPaper document. Notice that the dot disappears.

  4. Now type another equation in the MathPaper window, and the dot will reappear.

  5. Click the red close button with a dot in it. The MathPaper application should ask you, via a Save sheet, if you want to save the window’s contents before you close it, as shown in Figure 13-7.

A Save sheet appears when a user tries to close a window with a dot in its close button

Figure 13-7. A Save sheet appears when a user tries to close a window with a dot in its close button

  1. Click Cancel in the Save sheet.

  2. Try to quit MathPaper. You should be alerted that the window is still not saved and get the same Save sheet shown in Figure 13-7.

  3. Click the Don’t Save button to exit MathPaper.



[34] Alternatively, you can manually set the NSWindow’s docEdited flag when the document is dirtied and unset this flag when it is saved. If you do this, you will need to implement a method called - (BOOL)isDocumentEdited inside the MathDocument class. There are two important advantages to using the change count technique. The first, obviously, is that there is less code. The second is that the change count interfaces cleanly with the NSUndo class, which automates the process of handling undos and redos.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.216.59