home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / wordpad / srvritem.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  4KB  |  141 lines

  1. // srvritem.cpp : implementation of the CWordPadSrvrItem 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 "wordpad.h"
  15. #include "wordpdoc.h"
  16. #include "wordpvw.h"
  17. #include "srvritem.h"
  18. #include <limits.h>
  19.  
  20. IMPLEMENT_DYNAMIC(CEmbeddedItem, COleServerItem)
  21.  
  22. extern CLIPFORMAT cfRTF;
  23.  
  24. CEmbeddedItem::CEmbeddedItem(CWordPadDoc* pContainerDoc, int nBeg, int nEnd)
  25.     : COleServerItem(pContainerDoc, TRUE)
  26. {
  27.     ASSERT(pContainerDoc != NULL);
  28.     ASSERT_VALID(pContainerDoc);
  29.     m_nBeg = nBeg;
  30.     m_nEnd = nEnd;
  31. }
  32.  
  33. CWordPadView* CEmbeddedItem::GetView() const
  34. {
  35.     CDocument* pDoc = GetDocument();
  36.     ASSERT_VALID(pDoc);
  37.     POSITION pos = pDoc->GetFirstViewPosition();
  38.     if (pos == NULL)
  39.         return NULL;
  40.  
  41.     CWordPadView* pView = (CWordPadView*)pDoc->GetNextView(pos);
  42.     ASSERT_VALID(pView);
  43.     ASSERT(pView->IsKindOf(RUNTIME_CLASS(CWordPadView)));
  44.     return pView;
  45. }
  46.  
  47. void CEmbeddedItem::Serialize(CArchive& ar)
  48. {
  49.     if (m_lpRichDataObj != NULL)
  50.     {
  51.         ASSERT(ar.IsStoring());
  52.         FORMATETC etc = {NULL, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  53.         etc.cfFormat = (CLIPFORMAT)cfRTF;
  54.         STGMEDIUM stg;
  55.         if (SUCCEEDED(m_lpRichDataObj->GetData(&etc, &stg)))
  56.         {
  57.             LPBYTE p = (LPBYTE)GlobalLock(stg.hGlobal);
  58.             if (p != NULL)
  59.             {
  60.                 ar.Write(p, GlobalSize(stg.hGlobal));
  61.                 GlobalUnlock(stg.hGlobal);
  62.             }
  63.             ASSERT(stg.tymed == TYMED_HGLOBAL);
  64.             ReleaseStgMedium(&stg);
  65.         }
  66.     }
  67.     else
  68.         GetDocument()->Serialize(ar);
  69. }
  70.  
  71. BOOL CEmbeddedItem::OnGetExtent(DVASPECT dwDrawAspect, CSize& rSize)
  72. {
  73.     if (dwDrawAspect != DVASPECT_CONTENT)
  74.         return COleServerItem::OnGetExtent(dwDrawAspect, rSize);
  75.  
  76.     CClientDC dc(NULL);
  77.     return OnDrawEx(&dc, rSize, FALSE);
  78. }
  79.  
  80. BOOL CEmbeddedItem::OnDraw(CDC* pDC, CSize& rSize)
  81. {
  82.     return OnDrawEx(pDC, rSize, TRUE);
  83. }
  84.  
  85. BOOL CEmbeddedItem::OnDrawEx(CDC* pDC, CSize& rSize, BOOL bOutput)
  86. {
  87.     CDisplayIC dc;
  88.     CWordPadView* pView = GetView();
  89.     if (pView == NULL)
  90.         return FALSE;
  91.     ASSERT_VALID(pView);
  92.  
  93.     int nWrap = pView->m_nWordWrap;
  94.  
  95.     CRect rect;//rect in twips
  96.     rect.left = rect.top = 0;
  97.     rect.bottom = 32767; // bottomless
  98.  
  99.     rect.right = 32767;
  100.     if (nWrap == 0) // no word wrap
  101.         rect.right = 32767;
  102.     else if (nWrap == 1) // wrap to window
  103.     {
  104.         CRect rectClient;
  105.         pView->GetClientRect(&rectClient);
  106.         rect.right = rectClient.right - HORZ_TEXTOFFSET;
  107.         rect.right = MulDiv(rect.right, 1440, dc.GetDeviceCaps(LOGPIXELSX));
  108.     }
  109.     else if (nWrap == 2) // wrap to ruler
  110.         rect.right = pView->GetPrintWidth();
  111.  
  112.     // first just determine the correct extents of the text
  113.     pDC->SetBkMode(TRANSPARENT);
  114.  
  115.     if (pView->PrintInsideRect(pDC, rect, m_nBeg, m_nEnd, FALSE) == 0)
  116.     {
  117.         // default to 12pts high and 4" wide if no text
  118.         rect.bottom = rect.top+12*20+1; // 12 pts high
  119.         rect.right = rect.left+ 4*1440;
  120.     }
  121.     rect.bottom+=3*(1440/dc.GetDeviceCaps(LOGPIXELSX)); // three pixels
  122.  
  123.     // then, really output the text
  124.     CRect rectOut = rect; // don't pass rect because it will get clobbered
  125.     if (bOutput)
  126.         pView->PrintInsideRect(pDC, rectOut, m_nBeg, m_nEnd, TRUE);
  127.     ASSERT(rectOut.right == rect.right);
  128.  
  129.     // adjust for border (rect.left is already adjusted)
  130.     if (pView->GetStyle() & WS_HSCROLL)
  131.         ++rect.bottom;  // account for border on scroll bar!
  132.  
  133.     // return HIMETRIC size
  134.     rSize = rect.Size();
  135.     rSize.cx = MulDiv(rSize.cx, 2540, 1440); // convert twips to HIMETRIC
  136.     rSize.cy = MulDiv(rSize.cy, 2540, 1440); // convert twips to HIMETRIC
  137.     return TRUE;
  138. }
  139.  
  140. /////////////////////////////////////////////////////////////////////////////
  141.