Document.h

/*------------------------------------------------------------------------------------------
 
    Program:    CPlusTESample 2.0
    File:       Document.h
    Uses:       Application.h
                List.h
 
    by Andrew Shebanow
    of Apple Macintosh Developer Technical Support
 
    Copyright © 1989-1990 Apple Computer, Inc.
    All rights reserved.
 
------------------------------------------------------------------------------------------*/
 
 
#ifndef __DOCUMENT__
#define __DOCUMENT__
 
// Include necessary interface files
#ifndef __TYPES__
#include <Types.h>
#endif
#ifndef __QUICKDRAW__
#include <QuickDraw.h>
#endif
#ifndef __WINDOWS__
#include <Windows.h>
#endif
#ifndef __FILES__
#include <Files.h>
#endif
#ifndef __STANDARDFILE__
#include <StandardFile.h>
#endif
    /* canonical file specification */
    struct CanonicalFileSpec
    {
        short       vRefNum;    /* volume reference number */
        long        dirID;      /* directory ID */
        Str63       fileName;   /* file name */
    };
 
#ifndef __APPLICATION__
#include "Application.h"
#endif
#ifndef __LIST__
#include "List.h"
#endif
 
// max and min inlines
inline max(long a, long b)
{
    return a > b ? a : b;
}
 
inline min(long a, long b)
{
    return a < b ? a : b;
}
 
// Define inlines to convert between Points & Rects for convenience.
inline Point TopLeft(Rect r)
{
    Point pt;
 
    pt.v = r.top;
    pt.h = r.left;
    return pt;
}
 
inline Point TopRight(Rect r)
{
    Point pt;
 
    pt.v = r.top;
    pt.h = r.right;
    return pt;
}
 
inline Point BotLeft(Rect r)
{
    Point pt;
 
    pt.v = r.bottom;
    pt.h = r.left;
    return pt;
}
 
inline Point BotRight(Rect r)
{
    Point pt;
 
    pt.v = r.bottom;
    pt.h = r.right;
    return pt;
}
 
const long kMaxSleepTime = 60;  // 1 second worth of ticks
 
// we derive from handle object to prevent fragmentation
class TDocument : public HandleObject {
protected:
    WindowPtr           fDocWindow;
    CanonicalFileSpec   fFile;
    short               fFileRefNum;
    OSType              fFileType;
    Boolean             fNewDoc;
    Boolean             fReadOnly;
    Boolean             fDirty;
    Boolean             fActive;
 
    // internal file i/o routines - these are the guys that do the
    // actual work of manipulating files
    virtual void        OpenFile(Boolean readOnly, Boolean createIfNecessary);
        // OpenFile opens the file referred to by fFile. If createIfNecessary
        // is true and the file doesn't exist, it will be created.
        // If readOnly is true, the fReadOnly field will be set correctly
        // and the file will be opened with a read-only access path.
        // You shouldn't need to override this routine.
    virtual void        CloseFile();
        // close file just closes the file specified by fFileRefNum
        // You shouldn't need to override this routine.
    virtual void        ReadFromFile(short refNum) = 0;
        // this routine reads the file's data from the data fork
        // specified by refNum. The file will already be open,
        // and in the correct position for reading.
        // You MUST override this routine.
    virtual void        WriteToFile(short refNum) = 0;
        // this routine writes the files data to the data fork
        // specified by refNum. The file will already be open,
        // and in the correct position for writing.
        // You MUST override this routine.
 
    virtual YNCResult   PresentSaveDialog(Boolean quitting);
        // this routine puts up the standard Save dialog,
        // using the filename specified by fFile. The quitting
        // parameter is used to make sure we have the right
        // phrasing in the dialog box - either
        // 'Save "x" before quitting?' or 'Save "x" before closing?'
 
public:
    // Constructor & Destructor
                        TDocument(short resID, OSType theFileType);
        // creates new, untitled document using resID as window template
    virtual             ~TDocument();
        // our destructor - disposes of window
 
    // Routines to handle basic user interface events
    // you MUST override these in your subclasses!!!
    virtual void        DoZoom(short partCode) = 0;
    virtual void        DoGrow(EventRecord* theEvent) = 0;
    virtual void        DoContent(EventRecord* theEvent) = 0;
    virtual void        DoKeyDown(EventRecord* theEvent) = 0;
    virtual void        DoUpdate() = 0;
 
    // called when activating/deactivating document.
    // default version just sets fActive variable, so
    // you will need to override this to hilite your selection
    // and such
    virtual void        DoActivate(Boolean becomingActive);
 
    // these routines are called after a TDocument object has been
    // created to initialize its contents. You probably won't need to
    // override these, since they are fairly generic.
    virtual void        OpenNewDoc() {};
        // OpenNewDoc sets up a new, untitled document - by default, this
        // has already been done in the TDocument constructor, so this routine
        // is just a placeholder.
    virtual void        OpenOldDoc(CanonicalFileSpec theFile, Boolean readOnly);
        // OpenOldDoc opens a document, reads its contents, sets the window
        // title, and so on.
 
    // high level, user-oriented file handling routines
    // You probably won't need to override these
    virtual YNCResult   DoClose(Boolean askUserToSave,
                                YNCResult defaultAnswer,
                                Boolean quitting);
        // DoClose closes the document. If askUserToSave is true,
        // the user is asked whether or not he wants to save the documents
        // contents. If it is false, the document will be saved if defaultAnswer
        // is yesResult.
    virtual void        DoSave();
        // DoSave saves the documents contents. If the document is a new,
        // untitled document, DoSave will call DoSaveAs (below). Otherwise,
        // it sets up the file for writing and calls WriteToFile to save the
        // actual data.
    virtual void        DoSaveAs();
        // DoSaveAs asks the user for a file to save the document into.
        // It saves the data in the same manner as DoSave, and sets up
        // the file-related data members (fFile, fFileRefNum, etc).
    virtual void        DoRevert() {};
        // DoRevert is a hook for a revert routine. Currently, this
        // isn't implemented, but it is here for future enhancement.
    virtual void        DoPrint() {};
        // we don't support printing yet either
 
    // standard edit menu actions
    // with the exception of DoUndo, you MUST override these routines
    virtual void        DoUndo() {};
        // by default, undo is unimplemented, so you don't have to
        // override this method
    virtual void        DoCut() = 0;
    virtual void        DoCopy() = 0;
    virtual void        DoPaste() = 0;
    virtual void        DoClear() = 0;
    virtual void        DoSelectAll() = 0;
 
    // idle time routines: you can override these to do cursor handling,
    // TE caret blinking, marquee effects, etc...
    virtual void        DoIdle() {};
    virtual unsigned long CalcIdle() { return kMaxSleepTime; };
        // we never need idle in typical applications, so return a very large number
    virtual void        AdjustCursor() {};
        // where is in local coords
 
    // query state of document - useful for adjusting menu state
    // You will probably need to override at least a few of
    // these to accurately reflect the state of your document
    virtual Boolean     HaveUndo() { return false; };
    virtual Boolean     HaveSelection() { return false; };
    virtual Boolean     HavePaste() { return false; };
    virtual Boolean     CanClose() { return (FrontWindow() != nil); };
    virtual Boolean     CanSave() { return fDirty; };
    virtual Boolean     CanSaveAs() { return true; };
    virtual Boolean     CanRevert() { return false; };  // not implemented
    virtual Boolean     CanPrint() { return false; };   // not implemented
 
    // utility routine to get window pointer for document
    inline WindowPtr    GetDocWindow() { return fDocWindow; }
};
 
// TDocumentList is a simple linked list of documents,
// implemented C++ style.
class TDocumentList : public TList {
public:
    virtual void        AddDoc(TDocument* doc);
    virtual void        RemoveDoc(TDocument* doc);
    // find the TDocument associated with the window
    virtual TDocument*  FindDoc(WindowPtr window);
    // return number of active documents
    inline int          NumDocs() { return Count(); }
};
 
#endif