home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SAMPLES / OCLIENT / RECTITEM.CP_ / RECTITEM.CP
Encoding:
Text File  |  1993-02-08  |  3.7 KB  |  157 lines

  1. // rectitem.cpp : implementation of the CRectItem class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992 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 Microsoft
  9. // QuickHelp and/or WinHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13.  
  14.  
  15. #include "stdafx.h"
  16. #include "oclient.h"
  17.  
  18. #include "maindoc.h"
  19. #include "rectitem.h"
  20.  
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char BASED_CODE THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27.  
  28. IMPLEMENT_DYNAMIC(CRectItem, COleClientItem)
  29.  
  30. CRectItem::CRectItem(COleClientDoc* pContainer)
  31.     : COleClientItem(pContainer)
  32. {
  33.     m_rect.SetRectEmpty();      // invisible item initially
  34.     m_bTrackServerSize = TRUE;
  35. }
  36.  
  37. CRectItem::~CRectItem()
  38. {
  39. }
  40.  
  41. void CRectItem::Invalidate(CView* pNotThisView)
  42. {
  43.     if (IsVisible())
  44.         GetDocument()->UpdateAllViews(pNotThisView, 0, this);
  45. }
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48.  
  49. BOOL CRectItem::UpdateItemRectFromServer()
  50. {
  51.     ASSERT(m_bTrackServerSize);
  52.     CRect rect;
  53.     if (!GetBounds(&rect))
  54.         return FALSE;       // blank
  55.  
  56.     // map from HIMETRIC to screen coordinates
  57.     {
  58.         CClientDC screenDC(NULL);
  59.         screenDC.SetMapMode(MM_HIMETRIC);
  60.         screenDC.LPtoDP(&rect);
  61.     }
  62.     // just set the item size
  63.     if (m_rect.Size() != rect.Size())
  64.     {
  65.         Invalidate();   // invalidate the old size/position
  66.         m_rect.right = m_rect.left + rect.Width();
  67.         m_rect.bottom = m_rect.top + rect.Height();
  68.         Invalidate();   // as well as the new size/position
  69.     }
  70.     return TRUE;
  71. }
  72.  
  73. BOOL CRectItem::SetItemRectToServer()
  74. {
  75.     // set the official bounds for the embedded item
  76.     CRect rect = m_rect;
  77.     {
  78.         CClientDC screenDC(NULL);
  79.         screenDC.SetMapMode(MM_HIMETRIC);
  80.         screenDC.DPtoLP(&rect);
  81.     }
  82.     TRY
  83.     {
  84.         SetBounds(rect);    // may do a wait
  85.     }
  86.     CATCH(CException, e)
  87.     {
  88.         return FALSE;   // links will not allow SetBounds
  89.     }
  90.     END_CATCH
  91.     return TRUE;
  92. }
  93.  
  94. // Sizing is permitted near the lower right of the selected rectangle
  95. BOOL CRectItem::IsSizing(POINT pt)
  96. {
  97.     static int cxSizing = ::GetSystemMetrics(SM_CXFRAME);
  98.     static int cySizing = ::GetSystemMetrics(SM_CYFRAME);
  99.  
  100.     return (IsVisible() && m_rect.PtInRect(pt) &&
  101.         pt.x > m_rect.right - cxSizing &&
  102.         pt.y > m_rect.bottom - cySizing);
  103. }
  104.  
  105.  
  106. void CRectItem::OnChange(OLE_NOTIFICATION wNotification)
  107. {
  108.     if (m_bTrackServerSize && !UpdateItemRectFromServer())
  109.     {
  110.         // Blank object
  111.         if (wNotification == OLE_CLOSED)
  112.         {
  113.             // no data received for the object - destroy it
  114.             ASSERT(!IsVisible());
  115.             GetDocument()->DeleteItem(this);
  116.             return;     // no update (item is gone now)
  117.         }
  118.     }
  119.     if (wNotification == OLE_SAVED || wNotification == OLE_CHANGED)
  120.         Dirty();
  121.     Invalidate();   // any change will cause a redraw
  122. }
  123.  
  124.  
  125.  
  126. void CRectItem::Serialize(CArchive& ar)
  127. {
  128.     CRect rect;
  129.  
  130.     if (ar.IsStoring())
  131.     {
  132.         ASSERT(IsVisible());        // only save non-blank items
  133.         WORD w = 0x5500;        // magic value
  134.         if (m_bTrackServerSize)
  135.             w += 1;
  136.         ar << w << m_rect;
  137.         // default COleClientItem::Serialize will store type and name
  138.     }
  139.     else
  140.     {
  141.         WORD w;
  142.         ar >> w >> m_rect;
  143.         if (HIBYTE(w) != 0x55)
  144.         {
  145.             TRACE("Bad magic number in front of an item wnd\n");
  146.             AfxThrowArchiveException(CArchiveException::generic);
  147.         }
  148.         m_bTrackServerSize = (w & 1) != 0;
  149.     }
  150.     COleClientItem::Serialize(ar);      // embedded object
  151. }
  152.  
  153.  
  154. /////////////////////////////////////////////////////////////////////////////
  155.  
  156.  
  157.