home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap08 / polyline / polyline.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  6KB  |  264 lines

  1. /*
  2.  * POLYLINE.CPP
  3.  * Polyline Component Object Chapter 8
  4.  *
  5.  * Implementation of the CPolyline class that we expose as a
  6.  * component object.
  7.  *
  8.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Microsoft
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "polyline.h"
  17.  
  18.  
  19. /*
  20.  * CPolyline:CPolyline
  21.  * CPolyline::~CPolyline
  22.  *
  23.  * Constructor Parameters:
  24.  *  pUnkOuter       LPUNKNOWN of the controlling unknown.
  25.  *  pfnDestroy      PFNDESTROYED to call when an object is
  26.  *                  destroyed.
  27.  *  hInst           HINSTANCE of the application we're in.
  28.  */
  29.  
  30. CPolyline::CPolyline(LPUNKNOWN pUnkOuter, PFNDESTROYED pfnDestroy
  31.     , HINSTANCE hInst)
  32.     {
  33.     m_hWnd=NULL;
  34.     m_hInst=hInst;
  35.  
  36.     m_cRef=0;
  37.     m_pUnkOuter=pUnkOuter;
  38.     m_pfnDestroy=pfnDestroy;
  39.     m_fDirty=FALSE;
  40.  
  41.     m_pImpIPolyline=NULL;
  42.     m_pImpIConnPtCont=NULL;
  43.  
  44.     m_pAdv=NULL;
  45.     m_pConnPt=NULL;
  46.  
  47.     //CHAPTER8MOD
  48.     m_pST  =NULL;
  49.     m_cf   =0;
  50.     m_clsID=CLSID_Polyline8;
  51.  
  52.     m_pIStorage=NULL;
  53.     m_pIStream =NULL;
  54.  
  55.     m_pImpIPersistStorage=NULL;
  56.     m_pImpIPersistStreamInit=NULL;
  57.     //End CHAPTER8MOD
  58.  
  59.     return;
  60.     }
  61.  
  62.  
  63. CPolyline::~CPolyline(void)
  64.     {
  65.     //CHAPTER8MOD
  66.     if (NULL!=m_pST)
  67.         delete m_pST;
  68.  
  69.     DeleteInterfaceImp(m_pImpIPersistStreamInit);
  70.     DeleteInterfaceImp(m_pImpIPersistStorage);
  71.     ReleaseInterface(m_pIStream);
  72.     ReleaseInterface(m_pIStorage);
  73.     //End CHAPTER8MOD
  74.  
  75.     DeleteInterfaceImp(m_pImpIConnPtCont);
  76.     DeleteInterfaceImp(m_pImpIPolyline);
  77.  
  78.     ReleaseInterface(m_pAdv);
  79.     ReleaseInterface(m_pConnPt);
  80.     return;
  81.     }
  82.  
  83.  
  84.  
  85.  
  86. /*
  87.  * CPolyline::Init
  88.  *
  89.  * Purpose:
  90.  *  Performs any intiailization of a CPolyline that's prone to
  91.  *  failure that we also use internally before exposing the
  92.  *  object outside this DLL.
  93.  *
  94.  * Parameters:
  95.  *  None
  96.  *
  97.  * Return Value:
  98.  *  BOOL            TRUE if the function is successful,
  99.  *                  FALSE otherwise.
  100.  */
  101.  
  102. BOOL CPolyline::Init(void)
  103.     {
  104.     LPUNKNOWN       pIUnknown=this;
  105.  
  106.     if (NULL!=m_pUnkOuter)
  107.         pIUnknown=m_pUnkOuter;
  108.  
  109.     //CHAPTER8MOD
  110.     m_pST=new CStringTable(m_hInst);
  111.  
  112.     if (!m_pST->Init(IDS_POLYLINEMIN, IDS_POLYLINEMAX))
  113.         return FALSE;
  114.  
  115.     m_cf=RegisterClipboardFormat(PSZ(IDS_STORAGEFORMAT));
  116.  
  117.     m_pImpIPersistStorage=new CImpIPersistStorage(this, pIUnknown);
  118.  
  119.     if (NULL==m_pImpIPersistStorage)
  120.         return FALSE;
  121.  
  122.     m_pImpIPersistStreamInit=new CImpIPersistStreamInit(this
  123.         , pIUnknown);
  124.  
  125.     if (NULL==m_pImpIPersistStreamInit)
  126.         return FALSE;
  127.     //End CHAPTER8MOD
  128.  
  129.     m_pImpIPolyline=new CImpIPolyline(this, pIUnknown);
  130.  
  131.     if (NULL==m_pImpIPolyline)
  132.         return FALSE;
  133.  
  134.     m_pImpIConnPtCont=new CImpIConnPtCont(this, pIUnknown);
  135.  
  136.     if (NULL==m_pImpIConnPtCont)
  137.         return FALSE;
  138.  
  139.     m_pConnPt=new CConnectionPoint(this);
  140.  
  141.     if (NULL==m_pConnPt)
  142.         return FALSE;
  143.  
  144.     m_pConnPt->AddRef();    //Reversed in destructor
  145.     return TRUE;
  146.     }
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. /*
  155.  * CPolyline::QueryInterface
  156.  * CPolyline::AddRef
  157.  * CPolyline::Release
  158.  */
  159.  
  160. STDMETHODIMP CPolyline::QueryInterface(REFIID riid, PPVOID ppv)
  161.     {
  162.     *ppv=NULL;
  163.  
  164.     if (IID_IUnknown==riid)
  165.         *ppv=this;
  166.  
  167.     if (IID_IConnectionPointContainer==riid)
  168.         *ppv=m_pImpIConnPtCont;
  169.  
  170.     //CHAPTER8MOD
  171.     if (IID_IPolyline8==riid)
  172.         *ppv=m_pImpIPolyline;
  173.  
  174.     if (IID_IPersistStorage==riid)
  175.         *ppv=m_pImpIPersistStorage;
  176.  
  177.     if (IID_IPersist==riid || IID_IPersistStream==riid
  178.         || IID_IPersistStreamInit==riid)
  179.         *ppv=m_pImpIPersistStreamInit;
  180.     //End CHAPTER8MOD
  181.  
  182.     if (NULL!=*ppv)
  183.         {
  184.         ((LPUNKNOWN)*ppv)->AddRef();
  185.         return NOERROR;
  186.         }
  187.  
  188.     return ResultFromScode(E_NOINTERFACE);
  189.     }
  190.  
  191.  
  192. STDMETHODIMP_(ULONG) CPolyline::AddRef(void)
  193.     {
  194.     return ++m_cRef;
  195.     }
  196.  
  197.  
  198. STDMETHODIMP_(ULONG) CPolyline::Release(void)
  199.     {
  200.     if (0L!=--m_cRef)
  201.         return m_cRef;
  202.  
  203.     if (NULL!=m_pfnDestroy)
  204.         (*m_pfnDestroy)();
  205.  
  206.     delete this;
  207.     return 0L;
  208.     }
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. /*
  217.  * CPolyline::RectConvertMappings
  218.  *
  219.  * Purpose:
  220.  *  Converts the contents of a rectangle from device (MM_TEXT) or
  221.  *  HIMETRIC to the other.
  222.  *
  223.  * Parameters:
  224.  *  pRect           LPRECT containing the rectangle to convert.
  225.  *  fToDevice       BOOL TRUE to convert from HIMETRIC to device,
  226.  *                  FALSE to convert device to HIMETRIC.
  227.  *
  228.  * Return Value:
  229.  *  None
  230.  */
  231.  
  232. void CPolyline::RectConvertMappings(LPRECT pRect, BOOL fToDevice)
  233.     {
  234.     HDC      hDC;
  235.     int      iLpx, iLpy;
  236.  
  237.     if (NULL==pRect)
  238.         return;
  239.  
  240.     hDC=GetDC(NULL);
  241.     iLpx=GetDeviceCaps(hDC, LOGPIXELSX);
  242.     iLpy=GetDeviceCaps(hDC, LOGPIXELSY);
  243.     ReleaseDC(NULL, hDC);
  244.  
  245.     if (fToDevice)
  246.         {
  247.         pRect->left=MulDiv(iLpx, pRect->left, HIMETRIC_PER_INCH);
  248.         pRect->top =MulDiv(iLpy, pRect->top , HIMETRIC_PER_INCH);
  249.  
  250.         pRect->right =MulDiv(iLpx, pRect->right, HIMETRIC_PER_INCH);
  251.         pRect->bottom=MulDiv(iLpy, pRect->bottom,HIMETRIC_PER_INCH);
  252.         }
  253.     else
  254.         {
  255.         pRect->left=MulDiv(pRect->left, HIMETRIC_PER_INCH, iLpx);
  256.         pRect->top =MulDiv(pRect->top , HIMETRIC_PER_INCH, iLpy);
  257.  
  258.         pRect->right =MulDiv(pRect->right, HIMETRIC_PER_INCH, iLpx);
  259.         pRect->bottom=MulDiv(pRect->bottom,HIMETRIC_PER_INCH, iLpy);
  260.         }
  261.  
  262.     return;
  263.     }
  264.