Add Application-Specific Server Support to the Document Class

To finish adding application-specific server support in the document class, you must:

Suggested Reading

In the following procedure, you’ll use the Add Member Function command to implement the OnSetItemRects function for class CScribbleDoc. The framework calls OnSetItemRects when the position or size of the embedded item has changed in the container, or when the clipping of the embedded item has changed in the container. Because Scribble’s view is a CScrollView, you need to call CScrollView::SetScrollSizes to reflect the change in the size of the item. Because there are multiple places where the logic associated with SetScrollSizes must be performed, you will later write a helper function, CScribbleView::ResyncScrollSizes, which you will call from the override of OnSetItemRects.

To notify the OLE server when the embedded item moves or changes size

  1. In ClassView, right-click CScribbleDoc.

  2. Click Add Member Function.

    The Add Member Function dialog box appears.

  3. Fill in the dialog box as follows:
    • In the Function Type box, type void.

    • In the Function Declaration box, type the following:
    OnSetItemRects(LPCRECT lpPosRect, LPCRECT lpClipRect)
    
    • In the Access area, click Protected.
  4. Click OK.

  5. In ScribbleDoc.cpp, implement the starter definition with the following code:
    // call base class to change the size of the window
    COleServerDoc::OnSetItemRects(lpPosRect, lpClipRect);
    
    // notify first view that scroll info should change
    POSITION pos = GetFirstViewPosition();
    CScribbleView* pView = (CScribbleView*)GetNextView(pos);
    pView->ResyncScrollSizes();
    
  6. Add the following #include statement to ScribbleDoc.cpp, because the above implementation refers to CScribbleView:
    #include "ScribbleView.h" 
    

    The short filename is ScribVw.h.

The next step is to modify CScribbleDoc::InitDocument so Scribble’s fixed document size changes from 8 by 9 inches, to 2 by 2 inches. The current size, 8 by 9 inches, is too large for most containers.

To change the initial size of the document

  1. Use ClassView to jump to CScribbleDoc::InitDocument and modify the comments and the parameters that return the default document size, as follows:
    //default document size is 200 x 200 screen pixels
    ...
    m_sizeDoc = CSize(200, 200);
    
  2. Use ClassView to jump to the CScribbleDoc constructor and replace the //TODO comments with the following line:
    m_sizeDoc = CSize(200, 200);
    

    The m_sizeDoc variable is also initialized in the helper member function, InitDocument, when a document is newly created or reopened in a stand-alone running instance of Scribble. InitDocument is called by Scribble’s overrides of CDocument::OnNewDocument and OnOpenDocument. When Scribble is run as a server, the OnNewDocument and OnOpenDocument functions are not called. Therefore a good place to initialize m_sizeDoc is in the constructor.

Finally, in the following procedure you’ll implement the ability for containers to execute the Paste Link command on the server’s Edit menu.

To implement the document’s support for putting a link format on the Clipboard

  1. Display ScribbleDoc.cpp in the editor window.

  2. From the Filter list in WizardBar, click ID_EDIT_COPY, and from the Members list, click COMMAND.

  3. Click the action button, located on the right end of WizardBar.

    The New Windows Message and Event Handlers dialog box appears.

  4. Click Add Handler.

    The Add Member Function dialog box appears.

  5. Click OK in the Add Member Function dialog box to accept the default name OnEditCopy.

  6. Click Edit Existing to jump to the OnEditCopy member function definition.

  7. Replace the //TODO line with the following code:
    CScribbleItem* pItem = GetEmbeddedItem();
    pItem->CopyToClipboard(TRUE);
    

The framework function COleServerItem::CopyToClipboard creates a COleDataSource object containing the OLE item's data in proper formats, then places the COleDataSource object on the Clipboard. The COleDataSource object includes the item's native data and its representation in CF_METAFILEPICT format, as well as data in any conversion formats you choose to support.