A More Advanced Document-View Interaction

If you're laying the groundwork for a multiview application, the document-view interaction must be more complex than the simple interaction in example EX16A. The fundamental problem is this: the user edits in view #1, so view #2 (and any other views) must be updated to reflect the changes. Now you need the UpdateAllViews and OnUpdate functions because the document is going to act as the clearinghouse for all view updates. The development steps are shown here:

  1. In your derived document class header file (generated by AppWizard), declare your document's data members. If you want to, you can make these data members private and you can define member functions to access them or declare the view class as a friend of the document class.

  2. In your derived view class, use ClassWizard to override the OnUpdate virtual member function. The application framework calls this function whenever the document data has changed for any reason. OnUpdate should update the view with the current document data.

  3. Evaluate all your command messages. Determine whether each one is document-specific or view-specific. (A good example of a document-specific command is the Clear All command on the Edit menu.) Now map the commands to the appropriate classes.

  4. In your derived view class, allow the appropriate command message handlers to update the document data. Be sure these message handlers call the CDocument::UpdateAllViews function before they exit. Use the type-safe version of the CView::GetDocument member function to access the view's document.

  5. In your derived document class, allow the appropriate command message handlers to update the document data. Be sure that these message handlers call the CDocument::UpdateAllViews function before they exit.

The sequence of events for the complex document-view interaction is shown here.

Application starts               CMyDocument object constructed
                                        CMyView object constructed
                                        Other view objects constructed
                                        View windows created
                                        CMyView::OnCreate called (if mapped)
                                        CDocument::OnNewDocument called
                                        CView::OnInitialUpdate called
                                              Calls CMyView::OnUpdate
                                              Initializes the view
User executes                   CMyView functions update CMyDocument
view command                  data members
                                              Call CDocument::UpdateAllViews
                                              Other views' OnUpdate functions called
User executes                   CMyDocument functions update data
document command          members
                                              Call CDocument::UpdateAllViews
                                              CMyView::OnUpdate called
                                              Other views' OnUpdate functions called
User exits application        View objects destroyed
                                              CMyDocument object destroyed