home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n04 / olecont.exe / CONTITEM.CPP < prev    next >
C/C++ Source or Header  |  1995-04-01  |  5KB  |  227 lines

  1. // contitem.cpp : implementation of the CContainerItem class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "contain.h"
  6.  
  7. #include "cntlinfo.h"
  8. #include "contdoc.h"
  9. #include "cntlitem.h"
  10. #include "contitem.h"
  11.  
  12. #ifdef _DEBUG
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CContainerItem implementation
  19.  
  20. IMPLEMENT_SERIAL(CContainerItem, CControlItem, 0)
  21.  
  22. CContainerItem::CContainerItem(CContainerDoc* pContainer)
  23.     : CControlItem(pContainer)
  24. {
  25.     m_rect.SetRect(10, 10, 50, 50);        // An Initial Size for the control
  26.     m_extent = CSize(0, 0);                // Set extent to zero
  27. }
  28.  
  29. CContainerItem::~CContainerItem()
  30. {
  31.     // TODO: add cleanup code here
  32.     
  33. }
  34.  
  35. void CContainerItem::Invalidate(CView* pNotThisView)
  36. {
  37.     GetDocument()->UpdateAllViews(pNotThisView, 0, this);
  38. }
  39.  
  40. void CContainerItem::OnChange(OLE_NOTIFICATION nCode, DWORD dwParam)
  41. {
  42.     ASSERT_VALID(this);
  43.  
  44.     CControlItem::OnChange(nCode, dwParam);
  45.  
  46.     switch (nCode)
  47.     {    
  48.         case OLE_CHANGED:
  49.             UpdateExtent();
  50.             Invalidate();
  51.             break;
  52.         case OLE_CHANGED_ASPECT:
  53.         case OLE_CHANGED_STATE:
  54.             Invalidate();
  55.             break;
  56.     }
  57. }
  58.  
  59. BOOL CContainerItem::OnChangeItemPosition(const CRect& rectPos)
  60. {
  61.     ASSERT_VALID(this);
  62.  
  63.     if (!CControlItem::OnChangeItemPosition(rectPos))
  64.         return FALSE;
  65.  
  66.     // Erase the old image of the item, set the new position and draw again.
  67.     Invalidate();
  68.     m_rect = rectPos;
  69.     Invalidate();
  70.  
  71.     // mark document as dirty
  72.     GetDocument()->SetModifiedFlag();
  73.  
  74.     return TRUE;
  75. }
  76.  
  77. void CContainerItem::OnGetItemPosition(CRect& rPosition)
  78. {
  79.     ASSERT_VALID(this);
  80.  
  81.     if (m_rect.Size() == CSize(0,0))
  82.         UpdateItemExtentFromServer();
  83.  
  84.     // copy m_rect, which is in document coordinates
  85.     rPosition = m_rect;
  86.  
  87.     // TODO:  Change any document coordinates here.
  88.     return;
  89. }
  90.  
  91. void CContainerItem::OnDeactivateUI(BOOL bUndoable)
  92. {
  93.     CControlItem::OnDeactivateUI(bUndoable);
  94.  
  95.     // Close an in-place active item whenever it removes the user
  96.     //  interface.  The action here should match as closely as possible
  97.     //  to the handling of the escape key in the view.
  98.  
  99.     Deactivate();   // nothing fancy here -- just deactivate the object
  100. }
  101.  
  102. void CContainerItem::OnUpdateFrameTitle()
  103. {
  104.     // Do Nothing.  A Good OCX Container doesn't display the name
  105.     // of its UIActive control.
  106.  
  107.     // Optionally, you could switch on the registry entry of the UIActive
  108.     // item to determine if it is not a control and then call the base
  109.     // class to update the title properly.
  110. }
  111.  
  112. void CContainerItem::Serialize(CArchive& ar)
  113. {
  114.     ASSERT_VALID(this);
  115.  
  116.     // Call base class first to read in CControlItem data.
  117.     // Since this sets up the m_pDocument pointer returned from
  118.     //  CContainerItem::GetDocument, it is a good idea to call
  119.     //  the base class Serialize first.
  120.     CControlItem::Serialize(ar);
  121.  
  122.     // now store/retrieve data specific to CContainerItem
  123.     if (ar.IsStoring())
  124.     {
  125.         ar << m_rect << m_extent;
  126.     }
  127.     else
  128.     {
  129.         ar >> m_rect << m_extent;
  130.     }
  131. }
  132.  
  133. /////////////////////////////////////////////////////////////////////////////
  134. //  Operations
  135.  
  136. BOOL CContainerItem::UpdateExtent()
  137. {
  138.     CSize size;
  139.     if (GetExtent(&size))
  140.     {
  141.         // OLE returns the extent in HIMETRIC units -- we need pixels
  142.         CClientDC dc(NULL);
  143.         dc.HIMETRICtoDP(&size);
  144.  
  145.         // only invalidate if it has actually changed
  146.         if (size != m_rect.Size())
  147.         {
  148.             // invalidate old, update, invalidate new
  149.             Invalidate();
  150.             m_rect.bottom = m_rect.top + size.cy;
  151.             m_rect.right = m_rect.left + size.cx;
  152.             Invalidate();
  153.  
  154.             // mark document as modified
  155.             GetDocument()->SetModifiedFlag();
  156.         }         
  157.         return TRUE;
  158.     }               
  159.     else
  160.         return FALSE;
  161. }
  162.                     
  163.                     
  164. void CContainerItem::Move(CRect &rc)
  165. {
  166.     // invalidate old rect
  167.     Invalidate();
  168.     // invalidate new
  169.     m_rect = rc;
  170.     Invalidate();
  171.  
  172.     // update item rect when in-place active
  173.     if (IsInPlaceActive())
  174.         SetItemRects();
  175. }
  176.  
  177. BOOL CContainerItem::UpdateItemExtentFromServer()
  178. {
  179.     // get size in pixels
  180.     CSize size;
  181.     if (!GetExtent(&size))
  182.         return FALSE;       // blank
  183.  
  184.     if (size == m_extent)
  185.         return FALSE;
  186.  
  187.     // if new object (i.e. m_extent is empty) setup position
  188.     if (m_extent == CSize(0,0))
  189.     {
  190.         m_rect.right = m_rect.left + MulDiv(size.cx,10,254);
  191.         m_rect.bottom = m_rect.top - MulDiv(size.cy,10,254);
  192.     }
  193.     else
  194.     {
  195.         if (!IsInPlaceActive() && size != m_extent)
  196.         {
  197.             // data changed and not inplace, so scale up rect as well
  198.             m_rect.right = m_rect.left +
  199.                 MulDiv(m_rect.Width(),size.cx,m_extent.cx);
  200.             m_rect.bottom = m_rect.top +
  201.                 MulDiv(m_rect.Height(),size.cy,m_extent.cy);
  202.         }
  203.     }
  204.  
  205.     m_extent = size;
  206.     Invalidate();   // as well as the new size/position
  207.     return TRUE;
  208. }
  209.  
  210.  
  211. /////////////////////////////////////////////////////////////////////////////
  212. // CContainerItem diagnostics
  213.  
  214. #ifdef _DEBUG
  215. void CContainerItem::AssertValid() const
  216. {
  217.     CControlItem::AssertValid();
  218. }
  219.  
  220. void CContainerItem::Dump(CDumpContext& dc) const
  221. {
  222.     CControlItem::Dump(dc);
  223. }
  224. #endif
  225.  
  226. /////////////////////////////////////////////////////////////////////////////
  227.