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 / chap10 / polyline / polyline.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  12.9 KB  |  591 lines

  1. /*
  2.  * POLYLINE.CPP
  3.  * Polyline Component Chapter 10
  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.     m_pST  =NULL;
  48.     m_cf   =0;
  49.     //CHAPTER10MOD
  50.     m_clsID=CLSID_Polyline10;
  51.     //End CHAPTER10MOD
  52.  
  53.     m_pIStorage=NULL;
  54.     m_pIStream =NULL;
  55.  
  56.     m_pImpIPersistStorage=NULL;
  57.     m_pImpIPersistStreamInit=NULL;
  58.  
  59.     //CHAPTER10MOD
  60.     m_pImpIDataObject   =NULL;
  61.     m_pIDataAdviseHolder=NULL;
  62.     //End CHAPTER10MOD
  63.     return;
  64.     }
  65.  
  66.  
  67. CPolyline::~CPolyline(void)
  68.     {
  69.     if (NULL!=m_pST)
  70.         delete m_pST;
  71.  
  72.     //CHAPTER10MOD
  73.     ReleaseInterface(m_pIDataAdviseHolder);
  74.  
  75.     DeleteInterfaceImp(m_pImpIDataObject);
  76.     //End CHAPTER10MOD
  77.  
  78.     DeleteInterfaceImp(m_pImpIPersistStreamInit);
  79.     DeleteInterfaceImp(m_pImpIPersistStorage);
  80.     ReleaseInterface(m_pIStream);
  81.     ReleaseInterface(m_pIStorage);
  82.  
  83.     DeleteInterfaceImp(m_pImpIConnPtCont);
  84.     DeleteInterfaceImp(m_pImpIPolyline);
  85.  
  86.     ReleaseInterface(m_pAdv);
  87.     ReleaseInterface(m_pConnPt);
  88.  
  89.     return;
  90.     }
  91.  
  92.  
  93.  
  94.  
  95. /*
  96.  * CPolyline::Init
  97.  *
  98.  * Purpose:
  99.  *  Performs any intiailization of a CPolyline that's prone to
  100.  *  failure that we also use internally before exposing the
  101.  *  object outside this DLL.
  102.  *
  103.  * Parameters:
  104.  *  None
  105.  *
  106.  * Return Value:
  107.  *  BOOL            TRUE if the function is successful,
  108.  *                  FALSE otherwise.
  109.  */
  110.  
  111. BOOL CPolyline::Init(void)
  112.     {
  113.     LPUNKNOWN       pIUnknown=this;
  114.  
  115.     if (NULL!=m_pUnkOuter)
  116.         pIUnknown=m_pUnkOuter;
  117.  
  118.     m_pST=new CStringTable(m_hInst);
  119.  
  120.     if (!m_pST->Init(IDS_POLYLINEMIN, IDS_POLYLINEMAX))
  121.         return FALSE;
  122.  
  123.     m_cf=RegisterClipboardFormat(SZPOLYLINECLIPFORMAT);
  124.  
  125.     m_pImpIPersistStorage=new CImpIPersistStorage(this, pIUnknown);
  126.  
  127.     if (NULL==m_pImpIPersistStorage)
  128.         return FALSE;
  129.  
  130.     m_pImpIPersistStreamInit=new CImpIPersistStreamInit(this
  131.         , pIUnknown);
  132.  
  133.     if (NULL==m_pImpIPersistStreamInit)
  134.         return FALSE;
  135.  
  136.     m_pImpIPolyline=new CImpIPolyline(this, pIUnknown);
  137.  
  138.     if (NULL==m_pImpIPolyline)
  139.         return FALSE;
  140.  
  141.     m_pImpIConnPtCont=new CImpIConnPtCont(this, pIUnknown);
  142.  
  143.     if (NULL==m_pImpIConnPtCont)
  144.         return FALSE;
  145.  
  146.     m_pConnPt=new CConnectionPoint(this);
  147.  
  148.     if (NULL==m_pConnPt)
  149.         return FALSE;
  150.  
  151.     m_pConnPt->AddRef();    //Reversed in destructor
  152.  
  153.     //CHAPTER10MOD
  154.     m_pImpIDataObject=new CImpIDataObject(this, pIUnknown);
  155.  
  156.     if (NULL==m_pImpIDataObject)
  157.         return FALSE;
  158.     //End CHAPTER10MOD
  159.  
  160.     return TRUE;
  161.     }
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169. /*
  170.  * CPolyline::QueryInterface
  171.  * CPolyline::AddRef
  172.  * CPolyline::Release
  173.  *
  174.  * Purpose:
  175.  *  IUnknown members for CPolyline object.
  176.  */
  177.  
  178. STDMETHODIMP CPolyline::QueryInterface(REFIID riid, PPVOID ppv)
  179.     {
  180.     *ppv=NULL;
  181.  
  182.     if (IID_IUnknown==riid)
  183.         *ppv=this;
  184.  
  185.     if (IID_IConnectionPointContainer==riid)
  186.         *ppv=m_pImpIConnPtCont;
  187.  
  188.     //CHAPTER10MOD
  189.     if (IID_IPolyline10==riid)
  190.         *ppv=m_pImpIPolyline;
  191.     //End CHAPTER10MOD
  192.  
  193.     if (IID_IPersistStorage==riid)
  194.         *ppv=m_pImpIPersistStorage;
  195.  
  196.     if (IID_IPersist==riid || IID_IPersistStream==riid
  197.         || IID_IPersistStreamInit==riid)
  198.         *ppv=m_pImpIPersistStreamInit;
  199.  
  200.     //CHAPTER10MOD
  201.     if (IID_IDataObject==riid)
  202.         *ppv=m_pImpIDataObject;
  203.     //End CHAPTER10MOD
  204.  
  205.     if (NULL!=*ppv)
  206.         {
  207.         ((LPUNKNOWN)*ppv)->AddRef();
  208.         return NOERROR;
  209.         }
  210.  
  211.     return ResultFromScode(E_NOINTERFACE);
  212.     }
  213.  
  214.  
  215. STDMETHODIMP_(ULONG) CPolyline::AddRef(void)
  216.     {
  217.     return ++m_cRef;
  218.     }
  219.  
  220.  
  221. STDMETHODIMP_(ULONG) CPolyline::Release(void)
  222.     {
  223.     if (0L!=--m_cRef)
  224.         return m_cRef;
  225.  
  226.     if (NULL!=m_pfnDestroy)
  227.         (*m_pfnDestroy)();
  228.  
  229.     delete this;
  230.     return 0L;
  231.     }
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239. /*
  240.  * CPolyline::RectConvertMappings
  241.  *
  242.  * Purpose:
  243.  *  Converts the contents of a rectangle from device (MM_TEXT) or
  244.  *  HIMETRIC to the other.
  245.  *
  246.  * Parameters:
  247.  *  pRect           LPRECT containing the rectangle to convert.
  248.  *  fToDevice       BOOL TRUE to convert from HIMETRIC to device,
  249.  *                  FALSE to convert device to HIMETRIC.
  250.  *
  251.  * Return Value:
  252.  *  None
  253.  */
  254.  
  255. void CPolyline::RectConvertMappings(LPRECT pRect, BOOL fToDevice)
  256.     {
  257.     HDC      hDC;
  258.     int      iLpx, iLpy;
  259.  
  260.     if (NULL==pRect)
  261.         return;
  262.  
  263.     hDC=GetDC(NULL);
  264.     iLpx=GetDeviceCaps(hDC, LOGPIXELSX);
  265.     iLpy=GetDeviceCaps(hDC, LOGPIXELSY);
  266.     ReleaseDC(NULL, hDC);
  267.  
  268.     if (fToDevice)
  269.         {
  270.         pRect->left=MulDiv(iLpx, pRect->left, HIMETRIC_PER_INCH);
  271.         pRect->top =MulDiv(iLpy, pRect->top , HIMETRIC_PER_INCH);
  272.  
  273.         pRect->right =MulDiv(iLpx, pRect->right, HIMETRIC_PER_INCH);
  274.         pRect->bottom=MulDiv(iLpy, pRect->bottom,HIMETRIC_PER_INCH);
  275.         }
  276.     else
  277.         {
  278.         pRect->left=MulDiv(pRect->left, HIMETRIC_PER_INCH, iLpx);
  279.         pRect->top =MulDiv(pRect->top , HIMETRIC_PER_INCH, iLpy);
  280.  
  281.         pRect->right =MulDiv(pRect->right, HIMETRIC_PER_INCH, iLpx);
  282.         pRect->bottom=MulDiv(pRect->bottom,HIMETRIC_PER_INCH, iLpy);
  283.         }
  284.  
  285.     return;
  286.     }
  287.  
  288.  
  289.  
  290. //CHAPTER10MOD
  291. //Functions now internal due to IDataObject
  292.  
  293. /*
  294.  * CPolyline::DataSet
  295.  *
  296.  * Purpose:
  297.  *  Sets the current data in this Polyline to a given structure.
  298.  *
  299.  * Parameters:
  300.  *  pplIn           PPOLYLINEDATA to initialize to.
  301.  *  fSizeToData     BOOL indicating if we're to size to the data
  302.  *                  or scale it.
  303.  *  fNotify         BOOL indicating if we're to send an advise
  304.  *                  on this change.
  305.  *
  306.  * Return Value:
  307.  *  HRESULT         NOERROR if successful, otherwise a
  308.  *                  POLYLINE_E_ value.
  309.  */
  310.  
  311. STDMETHODIMP CPolyline::DataSet(PPOLYLINEDATA pplIn
  312.     , BOOL fSizeToData, BOOL fNotify)
  313.     {
  314.     RECT            rc;
  315.  
  316.     /*
  317.      * Copy the structure in pplIn and repaint to reflect the
  318.      * new point set.  Note that unlike the RectSet message, we
  319.      * do no scaling, assuming that the rect in the structure
  320.      * is appropriate for the data.
  321.      */
  322.  
  323.     if (NULL==pplIn)
  324.         return ResultFromScode(POLYLINE_E_INVALIDPOINTER);
  325.  
  326.     m_pl=*pplIn;
  327.  
  328.     //CHAPTER10MOD
  329.     m_fDirty=TRUE;
  330.  
  331.     //Inform our parent of the data change
  332.     if (NULL!=m_pIDataAdviseHolder)
  333.         {
  334.         m_pIDataAdviseHolder->SendOnDataChange(m_pImpIDataObject
  335.             , DVASPECT_CONTENT, ADVF_NODATA);
  336.         }
  337.     //End CHAPTER10MOD
  338.  
  339.     /*
  340.      * If we're scaling the window to fit the data, then use
  341.      * RectSet passing our current rectangle as the new one.
  342.      * That makes sure that the data won't change but that the
  343.      * window is resized.
  344.      */
  345.  
  346.     if (fSizeToData)
  347.         {
  348.         POINT       pt;
  349.  
  350.         /*
  351.          * Get our offset in the parent window so we can RectSet
  352.          * to the right place since RectSet expects rectangle in
  353.          * parent coordinates and we get it in client coordinates.
  354.          */
  355.         GetWindowRect(m_hWnd, &rc);
  356.         pt.x=rc.left;
  357.         pt.y=rc.top;
  358.         ScreenToClient(GetParent(m_hWnd), &pt);
  359.         RECTSTORECT(m_pl.rc, rc);
  360.         OffsetRect(&rc, pt.x, pt.y);
  361.  
  362.         //This will also cause a repaint.
  363.         m_pImpIPolyline->RectSet(&rc, fNotify);
  364.         }
  365.     else
  366.         {
  367.         //Make sure we're updated.
  368.         InvalidateRect(m_hWnd, NULL, TRUE);
  369.         UpdateWindow(m_hWnd);
  370.         }
  371.  
  372.     return NOERROR;
  373.     }
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381. /*
  382.  * CPolyline::DataGet
  383.  *
  384.  * Purpose:
  385.  *  Retrieves the Polyline's current data.
  386.  *
  387.  * Parameters:
  388.  *  pplIn           PPOLYLINEDATA into which we copy the data.
  389.  *
  390.  * Return Value:
  391.  *  HRESULT         NOERROR if successful, otherwise a
  392.  *                  POLYLINE_E_ value.
  393.  */
  394.  
  395. STDMETHODIMP CPolyline::DataGet(PPOLYLINEDATA pplIn)
  396.     {
  397.     if (NULL==pplIn)
  398.         return ResultFromScode(POLYLINE_E_INVALIDPOINTER);
  399.  
  400.     *pplIn=m_pl;
  401.     return NOERROR;
  402.     }
  403.  
  404.  
  405.  
  406.  
  407. /*
  408.  * DataGet/SetMem eliminated:  no need with IDataObject.
  409.  * DataGetMem is somewhat like RenderNative below.
  410.  */
  411.  
  412.  
  413.  
  414. /*
  415.  * CPolyline::RenderNative
  416.  *
  417.  * Purpose:
  418.  *  Retrieves the Polyline's data in a global memory handle.
  419.  *
  420.  * Parameters:
  421.  *  phMem           HGLOBAL * in which to store the handle.
  422.  *
  423.  * Return Value:
  424.  *  HRESULT         NOERROR if successful, otherwise a
  425.  *                  POLYLINE_E_ value.
  426.  */
  427.  
  428. STDMETHODIMP CPolyline::RenderNative(HGLOBAL *phMem)
  429.     {
  430.     HGLOBAL         hMem;
  431.     PPOLYLINEDATA   ppl;
  432.     HRESULT         hr=ResultFromScode(POLYLINE_E_INVALIDPOINTER);
  433.  
  434.     if (NULL==phMem)
  435.         return ResultFromScode(POLYLINE_E_INVALIDPOINTER);
  436.  
  437.     hMem=GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, CBPOLYLINEDATA);
  438.  
  439.     if (NULL!=hMem)
  440.         {
  441.         ppl=(PPOLYLINEDATA)GlobalLock(hMem);
  442.         hr=DataGet(ppl);
  443.  
  444.         GlobalUnlock(hMem);
  445.  
  446.         if (FAILED(hr))
  447.             {
  448.             GlobalFree(hMem);
  449.             hMem=NULL;
  450.             }
  451.         }
  452.  
  453.     *phMem=hMem;
  454.     return hr;
  455.     }
  456.  
  457.  
  458.  
  459.  
  460. /*
  461.  * CPolyline::RenderBitmap
  462.  *
  463.  * Purpose:
  464.  *  Creates a bitmap image of the current Polyline.
  465.  *
  466.  * Parameters:
  467.  *  phBmp           HBITMAP * in which to return the bitmap.
  468.  *
  469.  * Return Value:
  470.  *  HRESULT         NOERROR if successful, otherwise a
  471.  *                  POLYLINE_E_ value.
  472.  */
  473.  
  474. STDMETHODIMP CPolyline::RenderBitmap(HBITMAP *phBmp)
  475.     {
  476.     HDC             hDC;
  477.     HDC             hMemDC;
  478.     HBITMAP         hBmp;
  479.     RECT            rc;
  480.     HGDIOBJ         hObj;
  481.  
  482.     if (NULL==phBmp)
  483.         return ResultFromScode(POLYLINE_E_INVALIDPOINTER);
  484.  
  485.     //Render a bitmap the size of the current rectangle.
  486.     hDC=GetDC(m_hWnd);
  487.     hMemDC=CreateCompatibleDC(hDC);
  488.  
  489.     GetClientRect(m_hWnd, &rc);
  490.     hBmp=CreateCompatibleBitmap(hDC, rc.right, rc.bottom);
  491.  
  492.     if (NULL!=hBmp)
  493.         {
  494.         //Draw the POLYLINEDATA into the bitmap.
  495.         hObj=SelectObject(hMemDC, hBmp);
  496.         Draw(hMemDC, FALSE, TRUE);
  497.         SelectObject(hMemDC, hObj);
  498.         }
  499.  
  500.     DeleteDC(hMemDC);
  501.     ReleaseDC(m_hWnd, hDC);
  502.  
  503.     *phBmp=hBmp;
  504.     return NOERROR;
  505.     }
  506.  
  507.  
  508.  
  509. //RenderMetafile not necessary--now part of RenderMetafilePict.
  510.  
  511.  
  512.  
  513. /*
  514.  * CPolyline::RenderMetafilePict
  515.  *
  516.  * Purpose:
  517.  *  Renders the current Polyline into a METAFILEPICT structure in
  518.  *  global memory.
  519.  *
  520.  * Parameters:
  521.  *  phMem           HGLOBAL * in which to return the
  522.  *                  METAFILEPICT.
  523.  *
  524.  * Return Value:
  525.  *  HRESULT         NOERROR if successful, otherwise a
  526.  *                  POLYLINE_E_ value.
  527.  */
  528.  
  529. STDMETHODIMP CPolyline::RenderMetafilePict(HGLOBAL *phMem)
  530.     {
  531.     HGLOBAL         hMem;
  532.     HMETAFILE       hMF;
  533.     LPMETAFILEPICT  pMF;
  534.     RECT            rc;
  535.     HDC             hDC;
  536.  
  537.     if (NULL==phMem)
  538.         return ResultFromScode(POLYLINE_E_INVALIDPOINTER);
  539.  
  540.     //Create a memory metafile and return its handle.
  541.     hDC=(HDC)CreateMetaFile(NULL);
  542.  
  543.     if (NULL==hDC)
  544.         return ResultFromScode(STG_E_MEDIUMFULL);
  545.  
  546.     SetMapMode(hDC, MM_ANISOTROPIC);
  547.     GetClientRect(m_hWnd, &rc);
  548.     SetWindowOrgEx(hDC, 0, 0, NULL);
  549.     SetWindowExtEx(hDC, rc.right, rc.bottom, NULL);
  550.  
  551.     Draw(hDC, TRUE, TRUE);
  552.     hMF=CloseMetaFile(hDC);
  553.  
  554.     if (NULL==hMF)
  555.         return ResultFromScode(STG_E_MEDIUMFULL);
  556.  
  557.     //Allocate the METAFILEPICT structure.
  558.     hMem=GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE
  559.         , sizeof(METAFILEPICT));
  560.  
  561.     if (NULL==hMem)
  562.         {
  563.         DeleteMetaFile(hMF);
  564.         return ResultFromScode(E_FAIL);
  565.         }
  566.  
  567.     /*
  568.      * Global lock only fails in PMODE if the selector is invalid
  569.      * (like it was discarded) or references a 0 length segment,
  570.      * neither of which can happen here.
  571.      */
  572.     pMF=(LPMETAFILEPICT)GlobalLock(hMem);
  573.  
  574.     pMF->hMF=hMF;
  575.     pMF->mm=MM_ANISOTROPIC;
  576.  
  577.     //Insert the extents in MM_HIMETRIC units.
  578.     GetClientRect(m_hWnd, &rc);
  579.     RectConvertMappings(&rc, FALSE);
  580.     pMF->xExt=rc.right;
  581.     pMF->yExt=rc.bottom;
  582.  
  583.     GlobalUnlock(hMem);
  584.  
  585.     *phMem=hMem;
  586.     return NOERROR;
  587.     }
  588.  
  589.  
  590. //End CHAPTER10MOD
  591.