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: Programmer's Guide to MacApp / Part 1 - MacApp Theory and Architecture
Chapter 7 - Document Handling


Publish and Subscribe

MacApp supports publish and subscribe using the Edition Manager, which became available in Macintosh system software starting with System 7. The Edition Manager supports dynamic data exchange between applications--data can be shared within a single document, between documents created by a single application, or between the documents of two or more applications. The data and applications can be on the same disk or distributed across a network. The Edition Manager is described in detail in the "Edition Manager" chapter of Inside Macintosh: Interapplication Communications.

MacApp's built-in support takes care of much of the overhead required to use the Edition Manager in your application. MacApp supplies menu and alert resources, implements a number of classes, and dispatches Edition Manager Apple events to the objects that can handle them. An application can support publish and subscribe for the data types 'PICT', 'TEXT', and 'snd ' by overriding a relatively small number of methods, most of them in its document class. The Calc sample application distributed with MacApp demonstrates support for publishing and subscribing cell data in a spreadsheet.

Edition Manager Terminology

The following terms are used to describe Edition Manager functions:

The Edition Manager sends an Edition Manager event, also called a section event, to inform an application that an action is required. For example, a subscriber may be notified that it should update itself by reading its edition container.

Edition Manager Resources

In the file Edition.r, MacApp supplies a number of resources for its Edition Manager support, including

In the file Defaults.r, MacApp supplies additional Edition Manager resources, including

Edition Manager Menu Commands

MacApp's Edition Manager support adds publish-and-subscribe menu commands to the Edit menu:

Edition Manager Classes

MacApp defines several classes to work with the Edition Manager:

These classes are described in greater detail in the sections that follow.

The Designator Classes

MacApp supplies the TDesignator class and its subclasses, TLinearDesignator, TVRectDesignator, and TRegionDesignator, to aid in specifying the data for a selected area of your document. A designator knows how to read and write its range. If you wish to publish or subscribe to data that can't be designated with one of these classes, you define a subclass to work with your data representation.

Designators are recommended for keeping track of selections in all applications. MacApp's TPublisher and TSubscriber classes use designators to work with the data they publish or subscribe to.

The Section Classes

The TSection class is an abstract class that encapsulates the common behavior for objects that publish and subscribe to data. Each TSection object has a reference to the document that owns it and each can

The TSection class has two subclasses for publishing or subscribing to data. The first, TPublisher, publishes data to an edition container file. Publisher objects tell the document to write the designated data to a stream by calling the TDocument::DoWriteData method.

The other subclass, TSubscriber, reads data from an edition container file. Subscriber objects tell the document to read the data from a stream by calling the TDocument::DoReadData method.

The TPublisher and TSubscriber classes support publish and subscribe for the data types 'PICT', 'TEXT', and 'snd '. To support other data types, your application can extend these classes.

The TEditionDocument Class

The TEditionDocument class is a subclass of TFileBasedDocument that adds a number of fields and methods for publishing and subscribing. The edition document keeps a list containing all publisher and subscriber section objects in use by the document. Its DoSetupMenus method enables publish and subscribe menu commands, and its DoMenuCommand method responds when a user chooses one of those commands.

When you initialize an instance of TEditionDocument (or a subclass), you call the IEditionDocument method, passing a parameter that specifies the document type for publisher edition container files created by the document. The IEditionDocument method calls InitUSectionMgr, which (the first time it is called) creates a behavior object of type TSectionMgr and adds it to your application object.

Methods You Override

Although TEditionDocument has many methods that deal with publishing and subscribing, your document can ignore most of them. A document subclass of TEditionDocument generally overrides just the following methods:

DoReadData, DoWriteData
These are actually methods of TDocument. You override them to read subscribed data from an edition container or write published data to a container.
DoAddBorder, DoDeleteBorder
You must override these methods to add or remove section borders. (You use section adorners to actually draw or erase the borders.)
CanSubscribe
The CanSubscribe method in TEditionDocument returns TRUE if there is a user selection, the selection contains something, and there isn't another subscriber currently selected. If your document has other subscribe criteria, you override this method.
CanPublishSelection
The CanPublishSelection method in TEditionDocument returns TRUE if there is a user selection, the selection contains something, and there isn't already a noncanceled publisher exactly contained in the current selection. If your document has other publish criteria, you override this method.

Reading and Writing Section Data

The edition document's DoWrite method performs several actions:

The edition document's DoRead method creates a section object to read each section resource in the document's resource fork and calls AddSection to add the section to the document's section list.

The TSectionMgr Class

The TSectionMgr class contains code to handle the four section Apple events: Section Read, Section Write, Section Scroll and Section Cancel. These events are dispatched to the section manager's DoAESectionEvent method by the application object's DoScriptCommand method.

When your application receives a section event, MacApp automatically maps it to one of the predefined command constants shown in Table 7-2.

Table 7-2 Table 7-2 MacApp section events
Section event constantWhen specified
cSectionReadWhen subscribers need to read themselves from disk
cSectionWriteWhen publishers need to write themselves to disk
cSectionScrollWhen the user clicks the Open Publisher button in the subscriber options dialog box or in the Finder's edition window
cSectionCancelWhen the user cancels a section in a document

The section manager handles each of the section commands by creating and posting a command object.

The Section Command Classes

MacApp implements four section command classes, based on the TSectionCommand class, each of which responds to a particular Edition Manager event.

Section Adorner Classes

The Macintosh human interface guidelines suggest a 3-pixel border for publishers and subscribers. The border should be 50 percent gray for publishers and 75 percent gray for subscribers, with a 1-pixel white border separating the section from the border.

MacApp uses adorners to provide these borders. The TSectionAdorner class is a subclass of TAdorner that encapsulates common publisher and subscriber adornment. The TPublisherAdorner class draws borders for TPublisher objects and TSubscriberAdorner draws borders for TSubscriber objects.

You can read more about adorners in "Adorners," beginning on page 213. The Calc sample application uses adorners to provide section borders.

The Section Iterator Class

The TSectionIterator class iterates over the publisher and subscriber sections in a document's section list. For example, the TEditionDocument DoNeedDiskSpace method iterates over the document's sections to determine how much space they will take up on disk.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
25 JUL 1996