Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Objective Chart User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

5.4 Step Three: ComDocs and Document Linking


Estimated time to complete this step: 60 minutes

5.4.1 Introduction

The third step demonstrates how Objective Chart's ComDoc system can be used to expand the capabilities of an existing application. You will create a new chart view for use as a child document view, as well as a second document template for the child document. The CDocManager used by the application will be replaced with an SRGDocManager, which allows document linking. A new menu item and handler will create and show linked documents. Finally, you will convert the Scribble document into a ComDoc and enable data passing between the two ComDocs to allow dynamic data changes.

The secondary view class can be created in the same manner as the view for the second pane in the Step Two project.


You do not need to finish the first two steps of this tutorial to start this step. Simply use the scribble<ver>.sln file in the step2 sample folder.

5.4.2 Create the Scroll View

  1. Open the scribble<ver>.sln solution file from the scribble\step2 folder.

  2. On the Insert menu, click New Class. Create a new class, named CChartScrollView, based on CScrollView class.

    Figure 58: Adding a New Class

  3. Open the ChartScrollView.cpp file. Perform a global search and replace of CScrollView with SRGScrollView (about 5 occurrences).


    Your view class now has access to all the capabilities of Objective Chart.

  4. Open ChartScrollView.h and replace CScrollView with SRGScrollView.

5.4.3 Modify the Scroll View

Several modifications to the scroll view are required to create an effective chart. The next steps will create a scatter chart and make it visible.


As in steps one and two of this tutorial, bold text indicates code that needs to be added or modified.

  1. On the ChartScrollView.cpp file, modify the OnInitialUpdate() routine of the scroll view to create a standard scatter chart with fixed axis scales. The final product should look like this:

  2. Once you create the scatter chart, you need to make it visible. The OnDraw() function of the base-class knows how to draw the graph. In the ChartScrollView.cpp file, modify the OnDraw() function as follows:

5.4.4 Allow Linked Documents

The next task is to allow document linking. The application object needs several modifications to allow the creation of linked documents.

The first modification replaces the standard CDocManager with the specialized SRGDocManager class by overriding the AddDocTemplate() routine.

  1. In scribble.h insert the following code just below the //}}AFX_VIRTUAL line:

      virtual void AddDocTemplate(CDocTemplate* pTemplate);
      
  2. In scribble.cpp insert

      void CScribbleApp::AddDocTemplate(CDocTemplate * pTemplate)
      {
        if (m_pDocManager == NULL)
          m_pDocManager = new SECDocManager;
        m_pDocManager->AddDocTemplate(pTemplate);
      }
      

    The second modification overloads the OnFileNew() command with a function that allows creation of documents by their names as specified in the document template string. The original OnFileNew() function is overridden with one that recognizes the new document template.

  3. In scribble.h add the following code below the //}}AFX_VIRTUAL line, as shown in Figure 59.

    Figure 59: Scribble.h

  4. In scribble.cpp insert:

  5. While still in scribble.cpp find the line

    ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) and remove "CWinApp::" so that our new override will be used.

  6. You need to create the document template string itself by inserting an IDR_CHARTTYPE string into the resources. Locate the String Table folder on the Resources tab and open the String Table.

    Figure 60: Finding the String Table

  7. Click on an empty row at the end of the string table and modify the string ID (IDR_CHARTTYPE) and value:


    Don't press return until you are finished typing the above text. It will automatically wrap around for you as you type.

  8. Insert the following line near the top of scribble.cpp.

  9. To add the second template declaration in the InitInstance() function, modify the InitInstance() function (in file scribble.cpp) according to the following code:


    Note that the default behavior is changed in regard to the command-line parameter parsing. To prevent the display of the document-type choice dialog, the application explicitly creates a default Scribble document. If another document type is requested on the command line, the command-line parser creates a new file.

5.4.5 Create a Linked Document

Now you're ready to create a linked document. The original Scribble view must now be told how to create a linked document and how to maintain the data in the document once it has been displayed.

  1. On the Resources tab, locate the Menu folder, and open the IDR_SCRIBBTYPE menu for editing.

  2. On the View menu, double-click the blank area at the bottom to add a new menu item (Linked chart command) to the View menu resource.

    Figure 61: Adding a New Menu Item

  3. Type the ID and Caption as seen in Figure 62:

    Figure 62: Menu Item Properties

  4. Add a handler for the menu item. Select ID_VIEWED_LINKEDCHART as the Object ID.

  5. Fill in the handler function body as shown below:

    Notice how the new OnFileNew() overload is used to create a document of type Chart. This secondary (or child) document is of the CGraphDoc class. (CGraphDoc is an Objective Chart class.) The chart document and view objects are created by the application framework. The CGraphDoc class has all the functionality required for this demonstration, so no code has been added for it. In particular, CGraphDoc has a member variable m_graph of type SRGraph that stores our graph. After the child document (and view) is created, the routine loops through the points "scribbled" so far, calling SecDocUpdate to copy the data to the SRGraph object in the new child document. Document updates are performed when the child document is created.

  6. In the scribvw.cpp file, modify the OnLButtonDown() and OnMouseMove() functions so the Scribble view updates the chart in the child document as mouse events occur:

    Revised OnLButtonDown()

    Revised OnMouseMove()

    A one-way link between Scribble and the child document and chart is complete. Stroke data entered in the Scribble window are copied to the linked document as well as the chart created in Step 2 (stored in CScribDoc and displayed in the splitter window).

5.4.6 Update the Linked Documents

A useful feature of the ComDoc system is that it can allow the linked document to update its parent. CGraphDoc already has a routine designed to allow Objective Chart to update an Objective Grid application when visual data objects in the linked chart are dragged with the mouse. By trapping this update call, Objective Chart can adjust points in the original Scribble data.

  1. First the original Scribble document must be updated to derive from ComDoc. Simply use the search and replace facility on the scribdoc.h and scribdoc.cpp files to change the base class from CDocument to SECComDoc. (SECComDoc is an Objective Chart class.)

  2. Override the SECComDoc function SECDocUpdate() to trap the update commands from the child document and modify the local data structures to reflect the changes.

  3. Insert the following line to the public implementation section of scribdoc.h.

  4. Add the function below to scribdoc.cpp.

    This routine updates both the Scribble data and chart displayed in the splitter window. The two documents are now linked in both directions. Strokes added to CScribDoc are copied to the linked chart. And, if the user repositions a data object in the chart, the changed values are updated in CScribDoc.

  5. Build (F7) and execute (Ctrl+F5) your application.

  6. Draw some strokes in Scribb1 window.

    Figure 63: Final Step Three Application

  7. On the View menu, click Linked chart.

    Figure 64: Viewing the Linked Chart

    Figure 65: Viewing the Linked Chart

  8. Click any data object in the Chart1 window, drag it, and view the resulting change to the image in the Scribb1 window.

Congratulations! You have completed the Objective Chart Scribble tutorial.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.