Implementing the Client Item Rectangle

The AppWizard-provided implementation of CContainerItem does most of the work needed for Container, but some of its functionality needs to be enhanced.

To implement the client item rectangle

  1. Declare m_rect in the public attributes section of ContainerItem.h:
    CRect m_rect;  // position within the document
    

    The AppWizard-provided implementation of CContainerItem assumed an arbitrary rectangle that locates the object in the container document. A CRect is needed to store the location and size of the object.

  2. Use ClassView to jump to the CContainerItem constructor in ContainerItem.cpp and initialize m_rect. Replace the //TODO comment with the following code:
    m_rect.SetRect(10, 10, 50, 50);
    
  3. Use ClassView to jump to OnGetItemPosition and replace its default implementation with the following code (leave the ASSERT_VALID(this) line):
    // return rect relative to client area of view
    rPosition = m_rect;
    

    The AppWizard-provided implementation arbitrarily sets the rectangle to (10, 10, 210, 210) when requested by the framework. Because the rectangle for each CContainerItem item is now being tracked by CRectTracker, the framework’s request is satisfied by returning the CRect member, m_rect.

    In Container Step 2, this implementation is replaced with one that allows the server to negotiate the size of the object.

    The framework calls COleClientItem::OnChangeItemPosition on behalf of a server to change the position of the in-place window. The CContainerItem updates its CRect m_rect according to the value requested by the framework. This means that the container document has changed. Thus views need to be notified and the document needs to be marked as dirty according to normal framework document/view rules.

    In Container Step 2, the simple UpdateAllViews call is replaced with smart invalidation.

  4. Complete the implementation of OnChangeItemPosition by adding the following code in place of the //TODO comment:
    GetDocument()->UpdateAllViews(NULL);
    m_rect = rectPos;                
    
    // mark document as dirty
    GetDocument()->SetModifiedFlag();
    
  5. Serialize the CRect m_rect member variable in CContainerItem::Serialize.
    ar << m_rect;
    ar >> m_rect;
  6. Save the header and implementation files.