Storing a Document Window Into a Collection

Using the function StoreWindowIntoCollection , your application can store any window, not just those created with Mac OS 8.5 Window Manager functions, into a collection. You can also store data associated with the window into the same collection. This provides a quick way for your application to save a simple document.

From a collection, your application can create a flattened collection--that is, a stream of address-independent data--using the Collection Manager. Because the 'wind' resource consists of an extensible flattened collection, your application can store a flattened collection consisting of a window and its data into a 'wind' resource using the Resource Manager. Listing 2-11 provides an example of how your application might store a window and its data as a single flattened collection in an extended 'wind' resource.

Listing 2-11 Writing a document window into a flattened collection resource

enum
{
    kDocumentResType    = 'Docu', // 'Docu' is an extended 'wind' resource
    kResID_Document     = 128
};

static pascal OSStatus MyWriteDocumentFile (WindowPtr window, short fileRefNum)
{
    OSStatus err = noErr;

    TEHandle teHandle = (TEHandle) GetWRefCon (window);

        Collection  collection  = nil;
        Handle      flatDoc     = nil;
        Handle      flatDocRes  = nil;
                
        do
        {
            // Temporarily create a collection into which the Window Manager will put
            // a description of the window

            if (!(collection = NewCollection ( )))
            {
                err = MemError ( );
                break;
            }

            // Store the window into the collection

            err = StoreWindowIntoCollection (window, collection);
            if (err != noErr) break;

            // Stash a copy of the text into the collection

            err = AddCollectionItemHdl (collection, 'TEXT', 1, (**teHandle).hText);
            if (err != noErr) break;

            // Allocate a new 0-length handle to hold the flattened collection

            flatDoc = NewHandle (0);
            if (!flatDoc)
            {
                err = MemError ( );
                break;
            }

            // Flatten the collection into the handle

            err = FlattenCollectionToHdl (collection, flatDoc);
            if (err != noErr) break;

            // Save the flattened collection as a resource in the file
            // whose resource map is topmost in the chain

            AddResource (flatDoc, kDocumentResType, kResID_Document, "\p");
            err = ResError ( );
            if (err != noErr) break;

            flatDocRes = flatDoc;
            flatDoc = nil;

            WriteResource (flatDocRes);
            err = ResError ( );
            if (err != noErr) break;

            // We've changed the resource map, so force it to be updated on disk

            UpdateResFile (fileRefNum);
            err = ResError ( );
            if (err != noErr) break;

            // The document has been written, so it's OK to say so

            err = SetWindowModified (window, false);
            if (err != noErr) break;
        }
        while (false);

        if (collection)
            DisposeCollection (collection);
        if (flatDocRes)
            ReleaseResource (flatDocRes);
        if (flatDoc)
            DisposeHandle (flatDoc);

    return err;
}


� 1999 Apple Computer, Inc. – (Last Updated 18 March 99)