home *** CD-ROM | disk | FTP | other *** search
/ Using Visual C++ 4 (Special Edition) / Using_Visual_C_4_Special_Edition_QUE_1996.iso / ch08 / ocontain / cntritem.cpp next >
Encoding:
C/C++ Source or Header  |  1995-09-22  |  4.4 KB  |  181 lines

  1. // CntrItem.cpp : implementation of the CContainCntrItem class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "OContain.h"
  6.  
  7. #include "ContainDoc.h"
  8. #include "CntrItem.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CContainCntrItem implementation
  18.  
  19. IMPLEMENT_SERIAL(CContainCntrItem, COleClientItem, 0)
  20.  
  21. CContainCntrItem::CContainCntrItem(CContainDoc* pContainer)
  22.     : COleClientItem(pContainer)
  23. {
  24.     m_rect.SetRect(10, 10, 50, 50);
  25.     
  26. }
  27.  
  28. CContainCntrItem::~CContainCntrItem()
  29. {
  30.     // TODO: add cleanup code here
  31.     
  32. }
  33.  
  34. void CContainCntrItem::OnChange(OLE_NOTIFICATION nCode, DWORD dwParam)
  35. {
  36.     ASSERT_VALID(this);
  37.  
  38.     COleClientItem::OnChange(nCode, dwParam);
  39.  
  40.     // When an item is being edited (either in-place or fully open)
  41.     //  it sends OnChange notifications for changes in the state of the
  42.     //  item or visual appearance of its content.
  43.  
  44.     switch (nCode)
  45.     {
  46.        case OLE_CHANGED :
  47.           InvalidateItem();
  48.           UpdateFromServerExtent();
  49.           break;
  50.  
  51.        case OLE_CHANGED_STATE :
  52.        case OLE_CHANGED_ASPECT :
  53.           InvalidateItem();
  54.           break;
  55.     } 
  56. }
  57.  
  58. BOOL CContainCntrItem::OnChangeItemPosition(const CRect& rectPos)
  59. {
  60.     ASSERT_VALID(this);
  61.  
  62.     // During in-place activation CContainCntrItem::OnChangeItemPosition
  63.     //  is called by the server to change the position of the in-place
  64.     //  window.  Usually, this is a result of the data in the server
  65.     //  document changing such that the extent has changed or as a result
  66.     //  of in-place resizing.
  67.     //
  68.     // The default here is to call the base class, which will call
  69.     //  COleClientItem::SetItemRects to move the item
  70.     //  to the new position.
  71.  
  72.     if (!COleClientItem::OnChangeItemPosition(rectPos))
  73.         return FALSE;
  74.  
  75.     InvalidateItem();
  76.     m_rect = rectPos;
  77.     InvalidateItem();
  78.  
  79.     // Mark document as dirty
  80.     GetDocument()->SetModifiedFlag();
  81.  
  82.     return TRUE;
  83. }
  84.  
  85. void CContainCntrItem::OnGetItemPosition(CRect& rPosition)
  86. {
  87.     ASSERT_VALID(this);
  88.  
  89.     // During in-place activation, CContainCntrItem::OnGetItemPosition
  90.     //  will be called to determine the location of this item.  The default
  91.     //  implementation created from AppWizard simply returns a hard-coded
  92.     //  rectangle.  Usually, this rectangle would reflect the current
  93.     //  position of the item relative to the view used for activation.
  94.     //  You can obtain the view by calling CContainCntrItem::GetActiveView.
  95.  
  96.     rPosition = m_rect;
  97. }
  98.  
  99. void CContainCntrItem::OnDeactivateUI(BOOL bUndoable)
  100. {
  101.     COleClientItem::OnDeactivateUI(bUndoable);
  102.  
  103.     // Close an in-place active item whenever it removes the user
  104.     //  interface.  The action here should match as closely as possible
  105.     //  to the handling of the escape key in the view.
  106.  
  107.     Deactivate();   // nothing fancy here -- just deactivate the object
  108. }
  109.  
  110. void CContainCntrItem::Serialize(CArchive& ar)
  111. {
  112.     ASSERT_VALID(this);
  113.  
  114.     // Call base class first to read in COleClientItem data.
  115.     // Since this sets up the m_pDocument pointer returned from
  116.     //  CContainCntrItem::GetDocument, it is a good idea to call
  117.     //  the base class Serialize first.
  118.     COleClientItem::Serialize(ar);
  119.  
  120.     // now store/retrieve data specific to CContainCntrItem
  121.     if (ar.IsStoring())
  122.     {
  123.         ar << m_rect;
  124.     }
  125.     else
  126.     {
  127.         ar >> m_rect; 
  128.     }
  129. }
  130.  
  131.  
  132.  
  133. void CContainCntrItem::InvalidateItem()
  134. {
  135.    GetDocument()->UpdateAllViews(NULL, HINT_UPDATE_ITEM, this);
  136. }
  137.  
  138.  
  139.  
  140. void CContainCntrItem::UpdateFromServerExtent()
  141. {
  142.    CSize size;
  143.    if (GetExtent(&size))
  144.    {
  145.       // OLE returns the extent in HIMETRIC units
  146.       //  however, we need pixels.
  147.       CClientDC dc(NULL);
  148.       dc.HIMETRICtoDP(&size);
  149.  
  150.       // Only invalidate if it has actually changed
  151.       if (size != m_rect.Size())
  152.       {
  153.           // Invalidate old, update, invalidate new
  154.           InvalidateItem();
  155.           m_rect.bottom = m_rect.top + size.cy;
  156.           m_rect.right = m_rect.left + size.cx;
  157.           InvalidateItem();
  158.  
  159.           // Mark document as modified
  160.           GetDocument()->SetModifiedFlag();
  161.       }
  162.  
  163.    }
  164.  
  165. }/////////////////////////////////////////////////////////////////////////////
  166. // CContainCntrItem diagnostics
  167.  
  168. #ifdef _DEBUG
  169. void CContainCntrItem::AssertValid() const
  170. {
  171.     COleClientItem::AssertValid();
  172. }
  173.  
  174. void CContainCntrItem::Dump(CDumpContext& dc) const
  175. {
  176.     COleClientItem::Dump(dc);
  177. }
  178. #endif
  179.  
  180. /////////////////////////////////////////////////////////////////////////////
  181.