Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Macintosh Toolbox Essentials /
Chapter 4 - Window Manager / Using the Window Manager


Closing a Window

The user closes a window either by clicking the close box, in the upper-left corner of the window, or by choosing Close from the File menu.

When the user presses the mouse button while the cursor is in the close box, your application calls the TrackGoAway function to track the mouse until the user releases the button, as illustrated in Listing 4-9 on page 4-39. If the user releases the button while the cursor is outside the close box, TrackGoAway returns FALSE, and your application does nothing. If TrackGoAway returns TRUE, your application invokes its own procedure for closing a window.

The specific steps you take when closing a window depend on what kind of information the window contains and whether the contents need to be saved. The sample code in this chapter recognizes four kinds of windows: the modeless dialog box containing the Find dialog, the modeless dialog box containing the Spell Check dialog, a standard document window, and a window associated with a desk accessory that was launched in the application's partition.

Listing 4-16 illustrates an application-defined procedure, DoCloseCmd, that determines what kind of window is being closed and follows the appropriate strategy. The application calls DoCloseCmd when the user clicks a window's close box or chooses Close from the File menu.

Listing 4-16 Handling a close command

PROCEDURE DoCloseCmd;
VAR
   myWindow:   WindowPtr;     {pointer to window's record}
   myData:     MyDocRecHnd;   {handle to a document record}
   windowType: Integer;       {application-defined window type}
BEGIN
   myWindow := FrontWindow;
   windowType := MyGetWindowType(myWindow);
   CASE windowType OF
      kMyFindModelessDialog:        {for modeless dialog boxes, }
         HideWindow(myWindow);      { hide window}
      kMySpellModelessDialog:       {for modeless dialog boxes, }
         HideWindow(myWindow);      { hide window}
      kDAWindow:              {for desk accessories, close the DA}
         CloseDeskAcc(WindowPeek(myWindow)^.windowKind);
      kMyDocWindow:           {for documents, handle file first}
         BEGIN
            myData := MyDocRecHnd(GetWRefCon(myWindow));
            MyCloseDocument(myData);
         END;
   END;     {of CASE}
END;
The DoCloseCmd procedure first determines which window is the active window
and then calls the application-defined function MyGetWindowType to identify the window's type, as defined by the application. If the window is a modeless dialog box, MyCloseCmd merely hides the window, leaving the data structures in memory. For
a sample routine that displays a hidden window, see Listing 4-18 on page 4-56.

If the window is associated with a desk accessory, the DoCloseCmd procedure calls
the CloseDeskAcc procedure to close the desk accessory. This case is included
only for compatibility; in System 7 desk accessories are seldom launched in an application's partition.

If the window is associated with a document, DoCloseCmd reads the document
record and then calls the application-defined procedure MyCloseDocument to handle the closing of a document window. Listing 4-17 illustrates the MyCloseDocument procedure.

Listing 4-17 Closing a document

PROCEDURE MyCloseDocument (myData: MyDocRecHnd);
VAR
   title:      Str255;        {window/document title}
   item:       Integer;       {item in Save Alert dialog box}
   docWindow:  WindowPtr;     {pointer to window record}
   event:      EventRecord;   {dummy record for DoActivate}
   myErr:      OSErr;         {variable for error-checking}
BEGIN
   docWindow := FrontWindow;
   IF (myData^^.windowDirty) THEN   {changed since last save}
   BEGIN
      GetWTitle(docWindow, title);     {get window title}
      ParamText(title, '', '', '');    {set up dialog text}
      {deactivate window before displaying Save dialog}
      DoActivate(docWindow, FALSE, event);
      {put up Save dialog and retrieve user response}
      item := CautionAlert(kSaveAlertID, @MyEventFilter);
      IF item = kCancel THEN     {user clicked Cancel}
         Exit(MyCloseDocument);  {exit without closing}
      IF item = kSave THEN       {user clicked Save}
         DoSaveCmd;              {save the document}
      {otherwise user clicked Don't Save-- }
      { close document in either case}
      myErr := DoCloseFile(myData); {close document}
      {Add your own error handling.}
   END;
   {close window whether or not user saved}
   CloseWindow(docWindow);          {close window}
   DisposePtr(Ptr(docWindow));      {dispose of window record}
END;
The MyCloseDocument procedure checks the windowDirty field in the document record (described in "Managing Multiple Windows" beginning on page 4-20). If the value of windowDirty is TRUE, MyCloseDocument displays a dialog box giving the user a chance to save the document before closing the window. The dialog box gives
the user the choices of canceling the close, saving the document before closing
the window, or closing the window without saving the document. If the user
cancels, MyCloseDocument merely exits. If the user opts to save the document, MyCloseDocument calls the application-defined routine DoSaveCmd, which is
not shown here. (For a description of how to save and close a file, see the chapter "Introduction to File Management" in Inside Macintosh: Files.) Whether or not the
user saves the document before closing the window, MyCloseDocument closes the document and finally removes the window from the screen and diposes of the memory allocated to the window record.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
11 JUL 1996