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: Files /
Chapter 1 - Introduction to File Management / Using Files


Creating a New File

The user expects to be able to create a new document using the New command in the File menu. Listing 1-4 illustrates one way to handle the New menu command.

Listing 1-4 Handling the New menu command

FUNCTION DoNewCmd: OSErr;
VAR
   myWindow:   WindowPtr;  {the new document window; ignored here}
BEGIN
   {Create a new window and make it visible.}
   DoNewCmd := DoNewDocWindow(TRUE, myWindow);
END;
The DoNewCmd function simply calls the application-defined function DoNewDocWindow (shown in Listing 1-5). The first parameter to DoNewDocWindow determines whether the new window should be visible or not; the value TRUE indicates that the new window should be visible. If DoNewDocWindow completes successfully, it returns a window pointer to the calling routine in the second parameter. The DoNewCmd function ignores that returned window pointer.

Listing 1-5 Creating a new document window

FUNCTION DoNewDocWindow (newDocument: Boolean; var myWindow: WindowPtr):
                                  OSErr;
VAR
   myData:     MyDocRecHnd;   {the window's data record}
CONST
   rDocWindow = 1000;         {resource ID of window template}
BEGIN
   {Allocate a new window; see Window Mgr chapter for details.}
   myWindow := GetNewWindow(rDocWindow, NIL, WindowPtr(-1));
   IF myWindow = NIL THEN
      BEGIN
         DoNewDocWindow := MemError;
         Exit(DoNewDocWindow);
      END;

   {Allocate space for the window's data record.}
   myData := MyDocRecHnd(NewHandle(SizeOf(MyDocRec)));
   IF myData = NIL THEN
      BEGIN
         DoNewDocWindow := MemError;
         DisposeWindow(myWindow);
         Exit(DoNewDocWindow);
      END;
      
   MoveHHi(Handle(myData));               {move the handle high}
   HLock(Handle(myData));                 {lock the handle}
   WITH myData^^ DO                       {fill in window data}
      BEGIN
         editRec := TENew(gDestRect, gViewRect);
         vScroll := GetNewControl(rVScroll, myWindow);
         hScroll := GetNewControl(rHScroll, myWindow);
         fileRefNum := 0;                 {no file yet!}
         windowDirty := FALSE;
         IF (editRec = NIL) OR (vScroll = NIL) OR (hScroll = NIL) THEN
            BEGIN
               DoNewDocWindow := memFullErr;
               DisposeWindow(myWindow);
               DisposeControl(vScroll);
               DisposeControl(hScroll);
               TEDispose(editRec);
               DisposeHandle(myData);
               Exit(DoNewDocWindow);
            END;
      END;

   IF newDocument THEN                    {if new document, show it}
      ShowWindow(myWindow);

   SetWRefCon(myWindow, LongInt(myData)); {link record to window}
   HUnlock(Handle(myData));               {unlock the handle}
   DoNewDocWindow := noErr;
END;
Note that the DoNewDocWindow function does not actually create a new file. The reason for this is that it is usually better to wait until the user actually saves a new document before creating a file (mainly because the user might decide not to save the document). The DoNewDocWindow function creates a window, allocates a new document record, and fills out the fields of that record. However, it sets the fileRefNum field of the document record to 0 to indicate that no file is currently associated with this window.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
2 JUL 1996