OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
Step 8: Loading and saving compound documents

When the user pastes or drops an OLE object into a container, the object becomes data in the container's document.

The container must store and load the object along with the rest of the document whenever the user chooses Save or Open from the File menu. The new Commit and Open methods of TOleDocument perform this chore for you. All you have to do is add calls to the base class in your own implementation of Open and Commit. The code that reads and writes your document's native data remains unchanged.

Because TOleDocument is derived from TStorageDocument rather than TFileDocument, it always creates compound files. Compound files are a feature of OLE 2 used to organize the contents of a disk file into separate compartments . You can ask to read or write from any compartment in the file without worrying about where on the disk the compartment begins or ends. OLE calls the compartments storages. The storages in a file can be ordered hierarchically, just like directories and subdirectories. Any storage compartment can contain other sub-storages.

Compound files are good for storing compound documents. When you call Open or Commit, ObjectComponents automatically creates storages in your file to hold whatever objects the document contains. All the document's native data is saved in the file's root storage. Your existing file data structure remains intact, isolated in a separate compartment. The following code shows how load compound documents. // document class declaration derived from TOleDocument class _DOCVIEWCLASS TMyDocument : public TOleDocument { // declarations }

// document class implementation

bool
TDrawDocument::Open(int mode, const char far* path) {
TOleDocument::Open(mode, path); // load any embedded objects
// code to load other document data
}

The TOleDocument::Open command does not actually copy the data for all the objects into memory. ObjectComponents is smart enough to load the data for particular objects only when the user activates them. The next code shows how to save compound documents.

bool
TMyDocument::Commit(bool force) {
TOleDocument::Commit(force); // save the embedded objects
// code to save other document data
TOleDocument::CommitTransactedStorage(); // commit if in transacted mode
}

By default, TOleDocument opens compound files in transacted mode. Transacted mode saves changes in a temporary buffer and merges them with the file only after an explicit command. A revert command discards any uncommitted changes. Commit buffers a new transaction. CommitTransactedStorage merges all pending transactions.

The opposite of transacted mode is direct mode. Direct mode eliminates buffers and makes each change take effect immediately. To alter the default mode, override TOleDocument::PreOpen. Omit the ofTransacted flag to specify direct mode.

Note
In order for compound file I/O to work correctly, you need to include the dtAutoOpen flag when you register docflags in the document registration table.