home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / drawcli / cntritem.cpp next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  4.8 KB  |  192 lines

  1. // cntritem.h : interface of the CDrawItem class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "drawcli.h"
  15.  
  16. #include "drawdoc.h"
  17. #include "drawobj.h"
  18. #include "drawvw.h"
  19. #include "cntritem.h"
  20.  
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char BASED_CODE THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CDrawItem implementation
  28.  
  29. IMPLEMENT_SERIAL(CDrawItem, COleClientItem, 0)
  30.  
  31. CDrawItem::CDrawItem(CDrawDoc* pContainer, CDrawOleObj* pDrawObj)
  32.     : COleClientItem(pContainer)
  33. {
  34.     m_pDrawObj = pDrawObj;
  35. }
  36.  
  37. CDrawItem::~CDrawItem()
  38. {
  39.     if (m_pDrawObj != NULL)
  40.         m_pDrawObj->m_pClientItem = NULL;
  41. }
  42.  
  43. void CDrawItem::OnChange(OLE_NOTIFICATION nCode, DWORD dwParam)
  44. {
  45.     ASSERT_VALID(this);
  46.  
  47.     COleClientItem::OnChange(nCode, dwParam);
  48.  
  49.     switch(nCode)
  50.     {
  51.     case OLE_CHANGED_STATE:
  52.     case OLE_CHANGED_ASPECT:
  53.         m_pDrawObj->Invalidate();
  54.         break;
  55.     case OLE_CHANGED:
  56.         UpdateExtent(); // extent may have changed
  57.         m_pDrawObj->Invalidate();
  58.         break;
  59.     }
  60. }
  61.  
  62. BOOL CDrawItem::OnChangeItemPosition(const CRect& rectPos)
  63. {
  64.     ASSERT_VALID(this);
  65.  
  66.     CDrawView* pView = GetActiveView();
  67.     ASSERT_VALID(pView);
  68.     CRect rect = rectPos;
  69.     pView->ClientToDoc(rect);
  70.  
  71.     if (rect != m_pDrawObj->m_position)
  72.     {
  73.         // invalidate old rectangle
  74.         m_pDrawObj->Invalidate();
  75.  
  76.         // update to new rectangle
  77.         m_pDrawObj->m_position = rect;
  78.         GetExtent(&m_pDrawObj->m_extent);
  79.  
  80.         // and invalidate new rectangle
  81.         m_pDrawObj->Invalidate();
  82.  
  83.         // mark document as dirty
  84.         GetDocument()->SetModifiedFlag();
  85.     }
  86.     return COleClientItem::OnChangeItemPosition(rectPos);
  87. }
  88.  
  89. void CDrawItem::OnGetItemPosition(CRect& rPosition)
  90. {
  91.     ASSERT_VALID(this);
  92.  
  93.     // update to extent of item if m_position is not initialized
  94.     if (m_pDrawObj->m_position.IsRectEmpty())
  95.         UpdateExtent();
  96.  
  97.     // copy m_position, which is in document coordinates
  98.     CDrawView* pView = GetActiveView();
  99.     ASSERT_VALID(pView);
  100.     rPosition = m_pDrawObj->m_position;
  101.     pView->DocToClient(rPosition);
  102. }
  103.  
  104. void CDrawItem::Serialize(CArchive& ar)
  105. {
  106.     ASSERT_VALID(this);
  107.  
  108.     // Call base class first to read in COleClientItem data.
  109.     // Note: this sets up the m_pDocument pointer returned from
  110.     //  CDrawItem::GetDocument, therefore it is a good idea
  111.     //  to call the base class Serialize first.
  112.     COleClientItem::Serialize(ar);
  113.  
  114.     // now store/retrieve data specific to CDrawItem
  115.     if (ar.IsStoring())
  116.     {
  117.         // TODO: add storing code here
  118.     }
  119.     else
  120.     {
  121.         // TODO: add loading code here
  122.     }
  123. }
  124.  
  125. BOOL CDrawItem::UpdateExtent()
  126. {
  127.     CSize size;
  128.     if (!GetExtent(&size) || size == m_pDrawObj->m_extent)
  129.         return FALSE;       // blank
  130.  
  131.     // if new object (i.e. m_extent is empty) setup position
  132.     if (m_pDrawObj->m_extent == CSize(0, 0))
  133.     {
  134.         m_pDrawObj->m_position.right =
  135.             m_pDrawObj->m_position.left + MulDiv(size.cx, 10, 254);
  136.         m_pDrawObj->m_position.bottom =
  137.             m_pDrawObj->m_position.top - MulDiv(size.cy, 10, 254);
  138.     }
  139.     // else data changed so scale up rect as well
  140.     else if (!IsInPlaceActive() && size != m_pDrawObj->m_extent)
  141.     {
  142.         m_pDrawObj->m_position.right = m_pDrawObj->m_position.left +
  143.             MulDiv(m_pDrawObj->m_position.Width(), size.cx, m_pDrawObj->m_extent.cx);
  144.         m_pDrawObj->m_position.bottom = m_pDrawObj->m_position.top +
  145.             MulDiv(m_pDrawObj->m_position.Height(), size.cy, m_pDrawObj->m_extent.cy);
  146.     }
  147.  
  148.     m_pDrawObj->m_extent = size;
  149.     m_pDrawObj->Invalidate();   // redraw to the new size/position
  150.     return TRUE;
  151. }
  152.  
  153. void CDrawItem::OnActivate()
  154. {
  155.     // allow only one inplace active item per frame
  156.     CView* pView = GetActiveView();
  157.     ASSERT_VALID(pView);
  158.     COleClientItem* pItem = GetDocument()->GetInPlaceActiveItem(pView);
  159.     if (pItem != NULL && pItem != this)
  160.         pItem->Close();
  161.  
  162.     COleClientItem::OnActivate();
  163. }
  164.  
  165. void CDrawItem::OnDeactivateUI(BOOL bUndoable)
  166. {
  167.     COleClientItem::OnDeactivateUI(bUndoable);
  168.  
  169.     // hide the object if it is not an outside-in object
  170.     DWORD dwMisc = 0;
  171.     m_lpObject->GetMiscStatus(GetDrawAspect(), &dwMisc);
  172.     if (dwMisc & OLEMISC_INSIDEOUT)
  173.         DoVerb(OLEIVERB_HIDE, NULL);
  174. }
  175.  
  176. /////////////////////////////////////////////////////////////////////////////
  177. // CDrawItem diagnostics
  178.  
  179. #ifdef _DEBUG
  180. void CDrawItem::AssertValid() const
  181. {
  182.     COleClientItem::AssertValid();
  183. }
  184.  
  185. void CDrawItem::Dump(CDumpContext& dc) const
  186. {
  187.     COleClientItem::Dump(dc);
  188. }
  189. #endif
  190.  
  191. /////////////////////////////////////////////////////////////////////////////
  192.