home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- *
- * DocumentHelpers.c
- *
- ****************************************************************************/
-
- #include "DocumentHelpers.h"
-
- #include "Structs.h"
- #include "DocumentADT.h"
-
- #include "ElementADT.h"
-
- #include "WindowUtils.h"
-
- #include "Assertion.h"
-
- /*****************************************************************************
- *
- * GetDocumentByWindow PUBLIC
- *
- * Given a specified window returns the document that it represents
- *
- *****************************************************************************/
- DocumentReference GetDocumentByWindow(DocumentList list, WindowPtr window)
- {
- #pragma unused (list)
-
- DocumentReference document = nil;
- long refcon;
- long pointer;
- OSType tag;
-
- if (window != nil)
- {
- refcon = GetWRefCon((WindowPtr) window);
-
- if (refcon != 0)
- {
- Assert((refcon & 0x0001) == 0, "DOCUMENTS: window refcon is an odd address");
-
- if ((refcon & 0x0001) == 0) // ensure that the refcon is an even address
- {
- pointer = *((long *)refcon); // get what refcon points at
-
- Assert((pointer & 0x0001) == 0, "DOCUMENTS: window's dereferenced refcon is an odd address");
-
- if ((pointer & 0x0001) == 0) // ensure that the pointer is an even address
- {
- tag = *((OSType *) pointer); // extract the tag at the pointer
- Assert(tag == kDocumentRecordID, "DOCUMENTS: window's dereferenced refcon is not our document tag!");
-
- if (tag == kDocumentRecordID) // is it our document tag?
- document = (DocumentReference) refcon; // we can return a valid document!
- }
- }
- }
- }
-
- return document;
- }
-
-
-
-
- /*****************************************************************************
- *
- * GetDocumentByName PUBLIC
- *
- * Returns the document with the given name if found. Otherwise, nil is returned.
- *
- * • NOTE: we look through the list from frontmost to backmost, which is not really
- * required in a name lookup, but we do it anyway in case there are some non-ad
- * documents in the list, or incase there are two ad documents with the same name.
- *
- *****************************************************************************/
- DocumentReference GetDocumentByName(ConstStr63Param nameToFind)
- {
- DocumentReference document = GetFrontDocument(GetDocumentList());
- Str63 documentName;
-
- while (document != nil) {
- GetDocumentName(document, documentName);
- if (EqualString(documentName, nameToFind, false, false))
- break;
- document = GetNextFrontDocument(GetDocumentList(), document);
- }
-
- return document;
- }
-
-
- /*****************************************************************************
- *
- * GetDocumentByIndex PUBLIC
- *
- * Returns the document with the given index if found. Otherwise, nil is returned.
- * Documents are indexed according to the Macintosh window list, so that document 1
- * is frontmost, document 2 is immediately behind it, etc.
- *
- *****************************************************************************/
- DocumentReference GetDocumentByIndex(long index)
- {
- DocumentReference document = GetFrontDocument(GetDocumentList());
- long i = 1L;
-
- while (document != nil && (i < index))
- {
- document = GetNextFrontDocument(GetDocumentList(), document);
- i++;
- }
-
- if (i != index)
- document = nil;
-
- return document;
- }
-
-
- /*****************************************************************************
- *
- * GetDocumentByDocumentNumber PUBLIC
- *
- * Returns the document with the given creation number, if found. Otherwise,
- * nil is returned.
- *
- *****************************************************************************/
- OSErr GetDocumentByDocumentNumber(long internalNumber, DocumentReference *document)
- {
- OSErr error = noErr;
-
- DocumentReference tempDocument = GetFirstDocument(GetDocumentList());
- Boolean foundIt = false;
-
- while (tempDocument != nil)
- {
- if (internalNumber == GetDocumentNumber(tempDocument))
- {
- foundIt = true;
- break;
- }
- tempDocument = GetNextDocument(tempDocument);
- }
-
- if (foundIt)
- *document = tempDocument;
- else
- *document = nil;
-
- return error;
- }
-
-
- /*****************************************************************************
- *
- * GetFrontDocument PUBLIC
- *
- * Returns the frontmost document. Note that this does not necessarily mean the
- * frontmost non-floating window since all windows may not have a document
- * associated with it! Note that this function ignores hidden windows
- *
- *****************************************************************************/
- DocumentReference GetFrontDocument(DocumentList list)
- {
- DocumentReference document = nil;
- WindowPtr window;
-
- for (window = FrontWindow(); window != nil; window = GetNextWindow(window))
- {
- document = GetDocumentByWindow(list, window); // does this window have a document associated with it?
- if (document != nil) // if so, then we are done! we have found the frontmost document!
- break;
- }
-
- return document;
- }
-
-
-
- /*****************************************************************************
- *
- * GetNextFrontDocument PUBLIC
- *
- * Returns the next document following a specified document with respect to
- * window order (NOT with respect to document list order!). Note that this
- * does not necessarily mean the next window’s document since windows may not
- * necessarily have documents associated with them!
- *
- *****************************************************************************/
- DocumentReference GetNextFrontDocument(DocumentList list, DocumentReference anchorDocument)
- {
- DocumentReference document = nil;
- WindowPtr window;
- WindowPtr anchorWindow = GetDocumentWindow(anchorDocument);
-
- for (window = GetNextWindow(anchorWindow); window != nil; window = GetNextWindow(window))
- { // walk the window list after the anchor document looking for more documents
- document = GetDocumentByWindow(list, window); // does this window have a document associated with it?
- if (document != nil) // if so, then we are done! we have found the frontmost document!
- break;
- }
-
- return document;
- }
-
- // --------------------------------------------------------------------------
-
- DocumentReference GetNextDocument(DocumentReference document)
- {
- return GetDocumentNextDocument(document);
- }
-
- // --------------------------------------------------------------------------
-
- DocumentReference GetPreviousDocument(DocumentReference document)
- {
- return GetDocumentPreviousDocument(document);
- }
-
-
- // --------------------------------------------------------------------------
-
- Boolean DocumentIsModified(DocumentReference document)
- {
- return (GetDocumentNumberOfChanges(document) != 0);
- }
-
- // --------------------------------------------------------------------------
-
- void MarkDocumentAsChanged(DocumentReference document)
- {
- SetDocumentNumberOfChanges(document, 1 + GetDocumentNumberOfChanges(document));
- }
-
-
- // --------------------------------------------------------------------------
-
- void InvalDocument(DocumentReference document)
- {
- GrafPtr curPort;
- WindowPtr window;
- Rect windowRect;
-
- if (document != nil)
- {
- window = GetDocumentWindow(document);
- if (window != nil)
- {
- GetPort(&curPort);
- SetPort(window);
- GetWindowPortRect(window, &windowRect);
- InvalRect(&windowRect);
- SetPort(curPort);
- }
- }
- }
-
-
- // --------------------------------------------------------------------------
-
- void HideDocument(DocumentReference document)
- {
- WindowPtr window = GetDocumentWindow(document);
-
- if (window != nil)
- HideWindow(window);
- }
-
- // --------------------------------------------------------------------------
-
- void ShowDocument(DocumentReference document)
- {
- WindowPtr window = GetDocumentWindow(document);
-
- if (window != nil)
- ShowWindow(window);
- }
-
- // --------------------------------------------------------------------------
-