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 / chap14 / cosmo / polyline.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  30.6 KB  |  1,408 lines

  1. /*
  2.  * POLYLINE.CPP
  3.  * Cosmo Chapter 14
  4.  *
  5.  * Implementation of the CPolyline class.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. #include "cosmo.h"
  16.  
  17.  
  18. /*
  19.  * CPolyline:CPolyline
  20.  * CPolyline::~CPolyline
  21.  *
  22.  * Constructor Parameters:
  23.  *  hInst           HINSTANCE of the application we're in.
  24.  */
  25.  
  26. CPolyline::CPolyline(HINSTANCE hInst)
  27.     : CWindow(hInst)
  28.     {
  29.     m_pAdv=NULL;
  30.     m_hWnd=NULL;
  31.     return;
  32.     }
  33.  
  34.  
  35. CPolyline::~CPolyline(void)
  36.     {
  37.     return;
  38.     }
  39.  
  40.  
  41.  
  42.  
  43.  
  44. /*
  45.  * CPolyline::Init
  46.  *
  47.  * Purpose:
  48.  *  Instantiates a polyline window within a given parent.  The
  49.  *  parent may be a main application window, could be an MDI child
  50.  *  window. We really do not care.
  51.  *
  52.  * Parameters:
  53.  *  hWndParent      HWND of the parent of this window
  54.  *  pRect           LPRECT that this window should occupy
  55.  *  dwStyle         DWORD containing the window's style flags
  56.  *  uID             UINT ID to associate with this window
  57.  *  pAdv            PCPolylineAdviseSink of the sink wanting our
  58.  *                  notifications.
  59.  *
  60.  * Return Value:
  61.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  62.  */
  63.  
  64. BOOL CPolyline::Init(HWND hWndParent, LPRECT pRect, DWORD dwStyle
  65.     , UINT uID, PCPolylineAdviseSink pAdv)
  66.     {
  67.     m_hWnd=CreateWindowEx(WS_EX_NOPARENTNOTIFY, SZCLASSPOLYLINE
  68.         , SZCLASSPOLYLINE, dwStyle, pRect->left, pRect->top
  69.         , pRect->right-pRect->left, pRect->bottom-pRect->top
  70.         , hWndParent, (HMENU)uID, m_hInst, this);
  71.  
  72.     if (NULL!=m_hWnd)
  73.         m_pAdv=pAdv;
  74.  
  75.     return (NULL!=m_hWnd);
  76.     }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. /*
  85.  * CPolyline::New
  86.  *
  87.  * Purpose:
  88.  *  Cleans out and reinitializes the data to defaults.
  89.  *
  90.  * Parameters:
  91.  *  None
  92.  *
  93.  * Return Value:
  94.  *  None
  95.  */
  96.  
  97. void CPolyline::New(void)
  98.     {
  99.     UINT        i;
  100.     RECT        rc;
  101.  
  102.     m_pl.wVerMaj=VERSIONMAJOR;
  103.     m_pl.wVerMin=VERSIONMINOR;
  104.  
  105.     //Our rectangle is the size of our window's client area.
  106.     GetClientRect(m_hWnd, &rc);
  107.     RECTTORECTS(rc, m_pl.rc);
  108.  
  109.     //Clean out the POLYLINE structure and repaint the window.
  110.     for (i=0; i< CPOLYLINEPOINTS; i++)
  111.         {
  112.         m_pl.rgpt[i].x=0;
  113.         m_pl.rgpt[i].y=0;
  114.         }
  115.  
  116.     m_pl.cPoints      =0;
  117.     m_pl.rgbBackground=GetSysColor(COLOR_WINDOW);
  118.     m_pl.rgbLine      =GetSysColor(COLOR_WINDOWTEXT);
  119.     m_pl.iLineStyle   =PS_SOLID;
  120.  
  121.     InvalidateRect(m_hWnd, NULL, TRUE);
  122.     UpdateWindow(m_hWnd);
  123.  
  124.     //Inform the advise sink of this data change.
  125.     if (NULL!=m_pAdv)
  126.         m_pAdv->OnDataChange();
  127.  
  128.     return;
  129.     }
  130.  
  131.  
  132.  
  133.  
  134. /*
  135.  * CPolyline::Undo
  136.  *
  137.  * Purpose:
  138.  *  Reverses previous actions in a Polyline.
  139.  *
  140.  * Parameters:
  141.  *  None
  142.  *
  143.  * Return Value:
  144.  *  BOOL            TRUE if we can Undo more, FALSE otherwise.
  145.  */
  146.  
  147. BOOL CPolyline::Undo(void)
  148.     {
  149.     //Decrement the number of active points and repaint.
  150.     if (m_pl.cPoints > 0)
  151.         {
  152.         m_pl.cPoints--;
  153.         InvalidateRect(m_hWnd, NULL, TRUE);
  154.         UpdateWindow(m_hWnd);
  155.         }
  156.  
  157.     if (NULL!=m_pAdv)
  158.         m_pAdv->OnPointChange();
  159.  
  160.     //Return if we can undo any more.
  161.     return (0!=m_pl.cPoints);
  162.     }
  163.  
  164.  
  165.  
  166.  
  167.  
  168. /*
  169.  * CPolyline::ReadFromStorage
  170.  *
  171.  * Purpose:
  172.  *  Loads our data (any known version) from an IStorage returning
  173.  *  the version number of the data or an error value.
  174.  *
  175.  * Parameters:
  176.  *  pIStorage       LPSTORAGE from which we'll read.
  177.  *
  178.  * Return Value:
  179.  *  LONG            Version number or negative POLYLINE_E_* value.
  180.  */
  181.  
  182. LONG CPolyline::ReadFromStorage(LPSTORAGE pIStorage)
  183.     {
  184.     POLYLINEDATA    pl;
  185.     ULONG           cb=(ULONG)-1;
  186.     ULONG           cbExpect=0;
  187.     LPSTREAM        pIStream;
  188.     HRESULT         hr;
  189.     LARGE_INTEGER   li;
  190.  
  191.     if (NULL==pIStorage)
  192.         return POLYLINE_E_READFAILURE;
  193.  
  194.     //Open the CONTENTS stream
  195.     hr=pIStorage->OpenStream(SZSTREAM, 0, STGM_DIRECT | STGM_READ
  196.         | STGM_SHARE_EXCLUSIVE, 0, &pIStream);
  197.  
  198.     if (FAILED(hr))
  199.         return POLYLINE_E_READFAILURE;
  200.  
  201.     //Read version numbers and seek back to file beginning.
  202.     hr=pIStream->Read(&pl, 2*sizeof(WORD), &cb);
  203.  
  204.     LISet32(li, 0);
  205.     pIStream->Seek(li, STREAM_SEEK_SET, NULL);
  206.  
  207.     if (FAILED(hr) || 2*sizeof(WORD)!=cb)
  208.         {
  209.         pIStream->Release();
  210.         return POLYLINE_E_READFAILURE;
  211.         }
  212.  
  213.     /*
  214.      * For version 2.0, read the entire file.  For version 1.0 read
  215.      * the file up to CBPOLYLINEDATAVER10.  For anything else, give
  216.      * an error.
  217.      */
  218.  
  219.     switch (pl.wVerMaj)
  220.         {
  221.         case VERSIONMAJOR:  //2.x
  222.             switch (pl.wVerMin)
  223.                 {
  224.                 case VERSIONMINOR:  //2.0
  225.                     cbExpect=CBPOLYLINEDATA;
  226.                     break;
  227.  
  228.                 default:
  229.                     break;
  230.                 }
  231.             break;
  232.  
  233.         case 1: //1.x
  234.             switch (pl.wVerMin)
  235.                 {
  236.                 case 0:  //1.0
  237.                     cbExpect=CBPOLYLINEDATA10;
  238.                     break;
  239.  
  240.                 default:
  241.                     break;
  242.                 }
  243.             break;
  244.  
  245.         default:
  246.             break;
  247.         }
  248.  
  249.     if (0==cbExpect)
  250.         {
  251.         pIStream->Release();
  252.         return POLYLINE_E_UNSUPPORTEDVERSION;
  253.         }
  254.  
  255.     hr=pIStream->Read(&pl, cbExpect, &cb);
  256.     pIStream->Release();
  257.  
  258.     if (cbExpect!=cb)
  259.         return POLYLINE_E_READFAILURE;
  260.  
  261.     /*
  262.      * If we loaded successfully, make the data current.  By using
  263.      * DataSet we centralize our version upgrading.  We size the
  264.      * polyline window to the data AND notify the document so it
  265.      * sizes to the polyline.
  266.      */
  267.     DataSet(&pl, TRUE, TRUE);
  268.  
  269.     //Return what version we just loaded.
  270.     return MAKELONG(pl.wVerMin, pl.wVerMaj);
  271.     }
  272.  
  273.  
  274.  
  275.  
  276. /*
  277.  * CPolyline::WriteToStorage
  278.  *
  279.  * Purpose:
  280.  *  Ignorantly writes our current data into a storage in a
  281.  *  particular version.
  282.  *
  283.  * Parameters:
  284.  *  pIStorage       LPSTORAGE in which to save.
  285.  *  lVer            LONG providing version number Major (HI) and
  286.  *                  Minor (Low)
  287.  *
  288.  * Return Value:
  289.  *  LONG            A POLYLINE_E_* value.
  290.  */
  291.  
  292. LONG CPolyline::WriteToStorage(LPSTORAGE pIStorage, LONG lVer)
  293.     {
  294.     ULONG           cb;
  295.     ULONG           cbExpect=0;
  296.     WORD            wVerMaj=HIWORD(lVer);
  297.     WORD            wVerMin=LOWORD(lVer);
  298.     POLYLINEDATA    pl;
  299.     LPSTREAM        pIStream;
  300.     HRESULT         hr;
  301.  
  302.     if (NULL==pIStorage)
  303.         return POLYLINE_E_READFAILURE;
  304.  
  305.     //Get a copy of our data in the version we're going to save.
  306.     DataGet(&pl, lVer);
  307.  
  308.     switch (wVerMaj)
  309.         {
  310.         case VERSIONMAJOR:  //2.x
  311.             switch (wVerMin)
  312.                 {
  313.                 case VERSIONMINOR:  //2.0
  314.                     cbExpect=CBPOLYLINEDATA;
  315.                     break;
  316.  
  317.                 default:
  318.                     break;
  319.                 }
  320.             break;
  321.  
  322.         case 1: //1.x
  323.             switch (wVerMin)
  324.                 {
  325.                 case 0:  //1.0
  326.                     cbExpect=CBPOLYLINEDATA10;
  327.                     break;
  328.  
  329.                 default:
  330.                     break;
  331.                 }
  332.             break;
  333.  
  334.         default:
  335.             break;
  336.         }
  337.  
  338.     if (0==cbExpect)
  339.         return POLYLINE_E_UNSUPPORTEDVERSION;
  340.  
  341.     hr=pIStorage->CreateStream(SZSTREAM, STGM_DIRECT | STGM_CREATE
  342.         | STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &pIStream);
  343.  
  344.     if (FAILED(hr))
  345.         return POLYLINE_E_WRITEFAILURE;
  346.  
  347.     hr=pIStream->Write(&pl, cbExpect, &cb);
  348.     pIStream->Release();
  349.  
  350.     if (FAILED(hr) || cbExpect!=cb)
  351.         return POLYLINE_E_WRITEFAILURE;
  352.  
  353.     return POLYLINE_E_NONE;
  354.     }
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361. /*
  362.  * CPolyline::ReadFromFile
  363.  *
  364.  * Purpose:
  365.  *  Loads our data (any known version) from a file handle returning
  366.  *  the version number of the data or an error value.
  367.  *
  368.  * Parameters:
  369.  *  pszFile         LPTSTR of the file to open.
  370.  *
  371.  * Return Value:
  372.  *  LONG            Version number or negative POLYLINE_E_* value.
  373.  */
  374.  
  375. LONG CPolyline::ReadFromFile(LPTSTR pszFile)
  376.     {
  377.     OFSTRUCT        of;
  378.     HFILE           hFile;
  379.     POLYLINEDATA    pl;
  380.     UINT            cb=(UINT)-1;
  381.     UINT            cbExpect=0;
  382.  
  383.     if (NULL==pszFile)
  384.         return POLYLINE_E_READFAILURE;
  385.  
  386.     //OpenFileW is a member of CPolyline.
  387.     hFile=OpenFileW(pszFile, &of, OF_READ);
  388.  
  389.     if (HFILE_ERROR==hFile)
  390.         return POLYLINE_E_READFAILURE;
  391.  
  392.     //Read version numbers and seek back to file beginning.
  393.     cb=_lread(hFile, &pl, 2*sizeof(WORD));
  394.     _llseek(hFile, 0L, 0);
  395.  
  396.     if (2*sizeof(WORD)!=cb)
  397.         {
  398.         _lclose(hFile);
  399.         return POLYLINE_E_READFAILURE;
  400.         }
  401.  
  402.     /*
  403.      * For version 2.0, read the entire file.  For version 1.0 read
  404.      * the file up to CBPOLYLINEDATAVER10.  For anything else, give
  405.      * an error.
  406.      */
  407.  
  408.     switch (pl.wVerMaj)
  409.         {
  410.         case VERSIONMAJOR:  //2.x
  411.             switch (pl.wVerMin)
  412.                 {
  413.                 case VERSIONMINOR:  //2.0
  414.                     cbExpect=CBPOLYLINEDATA;
  415.                     break;
  416.  
  417.                 default:
  418.                     break;
  419.                 }
  420.             break;
  421.  
  422.         case 1: //1.x
  423.             switch (pl.wVerMin)
  424.                 {
  425.                 case 0:  //1.0
  426.                     cbExpect=CBPOLYLINEDATA10;
  427.                     break;
  428.  
  429.                 default:
  430.                     break;
  431.                 }
  432.             break;
  433.  
  434.         default:
  435.             break;
  436.         }
  437.  
  438.     if (0==cbExpect)
  439.         {
  440.         _lclose(hFile);
  441.         return POLYLINE_E_UNSUPPORTEDVERSION;
  442.         }
  443.  
  444.     cb=_lread(hFile, &pl, cbExpect);
  445.     _lclose(hFile);
  446.  
  447.     if (cbExpect!=cb)
  448.         return POLYLINE_E_READFAILURE;
  449.  
  450.     /*
  451.      * If we loaded successfully, make the data current.  By using
  452.      * DataSet we centralize our version upgrading.  We size the
  453.      * polyline window to the data AND notify the document so it
  454.      * sizes to the polyline.
  455.      */
  456.     DataSet(&pl, TRUE, TRUE);
  457.  
  458.     //Return what version we just loaded.
  459.     return MAKELONG(pl.wVerMin, pl.wVerMaj);
  460.     }
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467. /*
  468.  * CPolyline::WriteToFile
  469.  *
  470.  * Purpose:
  471.  *  Ignorantly writes our current data into an opened file in a
  472.  *  particular version.
  473.  *
  474.  * Parameters:
  475.  *  pszFile         LPTSTR filename in which to store the data.
  476.  *  lVer            LONG providing version number Major (HI)
  477.  *                  and Minor (Low)
  478.  *
  479.  * Return Value:
  480.  *  LONG            A POLYLINE_E_* value.
  481.  */
  482.  
  483. LONG CPolyline::WriteToFile(LPTSTR pszFile, LONG lVer)
  484.     {
  485.     OFSTRUCT        of;
  486.     HFILE           hFile;
  487.     UINT            cb;
  488.     UINT            cbExpect=0;
  489.     WORD            wVerMaj=HIWORD(lVer);
  490.     WORD            wVerMin=LOWORD(lVer);
  491.     POLYLINEDATA    pl;
  492.  
  493.     if (NULL==pszFile)
  494.         return POLYLINE_E_READFAILURE;
  495.  
  496.     //Get a copy of our data in the version we're going to save.
  497.     DataGet(&pl, lVer);
  498.  
  499.     switch (wVerMaj)
  500.         {
  501.         case VERSIONMAJOR:  //2.x
  502.             switch (wVerMin)
  503.                 {
  504.                 case VERSIONMINOR:  //2.0
  505.                     cbExpect=CBPOLYLINEDATA;
  506.                     break;
  507.  
  508.                 default:
  509.                     break;
  510.                 }
  511.             break;
  512.  
  513.         case 1: //1.x
  514.             switch (wVerMin)
  515.                 {
  516.                 case 0:  //1.0
  517.                     cbExpect=CBPOLYLINEDATA10;
  518.                     break;
  519.  
  520.                 default:
  521.                     break;
  522.                 }
  523.             break;
  524.  
  525.         default:
  526.             break;
  527.         }
  528.  
  529.     if (0==cbExpect)
  530.         return POLYLINE_E_UNSUPPORTEDVERSION;
  531.  
  532.     hFile=OpenFileW(pszFile, &of, OF_CREATE | OF_WRITE);
  533.  
  534.     if (HFILE_ERROR==hFile)
  535.         return DOCERR_COULDNOTOPEN;
  536.  
  537.     cb=_lwrite(hFile, (LPCSTR)&pl, cbExpect);
  538.     _lclose(hFile);
  539.  
  540.     return (cbExpect==cb) ? POLYLINE_E_NONE
  541.         : POLYLINE_E_WRITEFAILURE;
  542.     }
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549. /*
  550.  * CPolyline::OpenFileW (Private)
  551.  *
  552.  * Purpose:
  553.  *  Under Win32, OpenFile does not take Unicode strings.  This
  554.  *  function converts a Unicode string into an ANSI string and
  555.  *  calls OpenFile.  This just maps to OpenFile without Unicode.
  556.  *
  557.  * Parameters, Return Value:
  558.  *  Same as OpenFile.
  559.  */
  560.  
  561. HFILE CPolyline::OpenFileW(LPTSTR pszFile, LPOFSTRUCT pof
  562.     , UINT uFlags)
  563.     {
  564.    #ifdef UNICODE
  565.     CHAR        szTemp[CCHPATHMAX];
  566.  
  567.     UNICODETOANSI(pszFile, szTemp, CCHPATHMAX);
  568.     return OpenFile(szTemp, pof, uFlags);
  569.    #else
  570.     return OpenFile(pszFile, pof, uFlags);
  571.    #endif
  572.     }
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580. /*
  581.  * CPolyline::DataSet
  582.  *
  583.  * Purpose:
  584.  *  Sets the current data in this Polyline to a given structure.
  585.  *
  586.  * Parameters:
  587.  *  ppl             PPOLYLINEDATA to initialize to.
  588.  *  fSizeToData     BOOL indicating if we're to size to the data or
  589.  *                  scale it.
  590.  *  fNotify         BOOL indicating if we're to send an advise on
  591.  *                  this change.
  592.  *
  593.  * Return Value:
  594.  *  LONG            A POLYLINE_E_* value.
  595.  */
  596.  
  597. LONG CPolyline::DataSet(PPOLYLINEDATA ppl, BOOL fSizeToData
  598.     , BOOL fNotify)
  599.     {
  600.     RECTS       rcs;
  601.     RECT        rc;
  602.     UINT        i;
  603.  
  604.     /*
  605.      * Copy the structure in ppl and repaint to reflect the new
  606.      * point set.  Note that unlike the RectSet message, we do no
  607.      * scaling, assuming that the rect in the structure is
  608.      * appropriate for the data.
  609.      */
  610.  
  611.     if (NULL==ppl)
  612.         return POLYLINE_E_INVALIDPOINTER;
  613.  
  614.     //Preserve the old rectangle
  615.     rcs=m_pl.rc;
  616.  
  617.     /*
  618.      * For version 2.0 we perform a straight copy.  For version
  619.      * 1.0 we copy the 1.0 structure and fill in defaults for
  620.      * the 2.0 additions.
  621.      */
  622.  
  623.     switch (ppl->wVerMaj)
  624.         {
  625.         case VERSIONMAJOR:          //2.x
  626.             switch (ppl->wVerMin)
  627.                 {
  628.                 case VERSIONMINOR:  //2.0
  629.                     m_pl=*ppl;
  630.                     break;
  631.  
  632.                 default:
  633.                     return POLYLINE_E_UNSUPPORTEDVERSION;
  634.                 }
  635.             break;
  636.  
  637.         case 1:                     //1.x
  638.             switch (ppl->wVerMin)
  639.                 {
  640.                 case 0:             //1.0
  641.                     *((PPOLYLINEDATA10)&m_pl)=
  642.                         *((PPOLYLINEDATA10)ppl);
  643.  
  644.                     /*
  645.                      * Update this structure to 2.0.  Note that we
  646.                      * assume whoever loaded us to save the loaded
  647.                      * version so it can later ask what version
  648.                      * the user wants to save.
  649.                      */
  650.                     m_pl.wVerMaj=VERSIONMAJOR;
  651.                     m_pl.wVerMin=VERSIONMINOR;
  652.  
  653.                     /*
  654.                      * Version 1.0 stored rc in parent coordinates.
  655.                      * We need those now in our client coodinates.
  656.                      */
  657.                     RECTSTORECT(m_pl.rc, rc);
  658.                     OffsetRect(&rc, -m_pl.rc.left, -m_pl.rc.top);
  659.  
  660.                     /*
  661.                      * 1.0 data had points relative to size of the
  662.                      * rectangle.  We need to scale these to 0-32767
  663.                      * independent of the rectangle for the version
  664.                      * upgrade.
  665.                      */
  666.                     for (i=0; i < m_pl.cPoints; i++)
  667.                         PointScale(&rc, &m_pl.rgpt[i], FALSE);
  668.  
  669.                     RECTTORECTS(rc, m_pl.rc);
  670.  
  671.                     //New 2.0 features
  672.                     m_pl.rgbBackground=GetSysColor(COLOR_WINDOW);
  673.                     m_pl.rgbLine=GetSysColor(COLOR_WINDOWTEXT);
  674.                     m_pl.iLineStyle=PS_SOLID;
  675.                     break;
  676.  
  677.                 default:
  678.                     return POLYLINE_E_UNSUPPORTEDVERSION;
  679.                 }
  680.             break;
  681.  
  682.         default:
  683.             return POLYLINE_E_UNSUPPORTEDVERSION;
  684.         }
  685.  
  686.  
  687.     //Inform our parent of the data change
  688.     if (NULL!=m_pAdv)
  689.         m_pAdv->OnDataChange();
  690.  
  691.     /*
  692.      * If we're scaling the window to fit the data, then use
  693.      * RectSet passing our current rectangle as the new one.
  694.      * That makes sure that the data won't change but that the
  695.      * window is resized.
  696.      */
  697.  
  698.     if (fSizeToData)
  699.         {
  700.         POINT       pt;
  701.  
  702.         /*
  703.          * Get our offset in the parent window so we can RectSet
  704.          * to the right place since RectSet expects rectangle in
  705.          * parent coordinates and we get it in client coordinates.
  706.          */
  707.         GetWindowRect(m_hWnd, &rc);
  708.         pt.x=rc.left;
  709.         pt.y=rc.top;
  710.         ScreenToClient(GetParent(m_hWnd), &pt);
  711.         RECTSTORECT(m_pl.rc, rc);
  712.         OffsetRect(&rc, pt.x, pt.y);
  713.  
  714.         //This will also cause a repaint.
  715.         RectSet(&rc, fNotify);
  716.         }
  717.     else
  718.         {
  719.         //Make sure we're updated.
  720.         InvalidateRect(m_hWnd, NULL, TRUE);
  721.         UpdateWindow(m_hWnd);
  722.         }
  723.  
  724.     return POLYLINE_E_NONE;
  725.     }
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733. /*
  734.  * CPolyline::DataGet
  735.  *
  736.  * Purpose:
  737.  *  Retrieves the Polyline's current data.
  738.  *
  739.  * Parameters:
  740.  *  ppl             PPOLYLINEDATA into which we copy the data.
  741.  *  lVer            LONG version of the data to retrieve.  Use
  742.  *                  VERSIONCURRENT to retrieve the most current.
  743.  *
  744.  * Return Value:
  745.  *  LONG            A POLYLINE_E_* value
  746.  */
  747.  
  748. LONG CPolyline::DataGet(PPOLYLINEDATA ppl, LONG lVer)
  749.     {
  750.     UINT        i;
  751.     RECT        rc;
  752.  
  753.     //Retieve the current version
  754.     if (lVer==MAKELONG(VERSIONMINOR, VERSIONMAJOR)
  755.         || VERSIONCURRENT==lVer)
  756.         {
  757.         *ppl=m_pl;
  758.         return POLYLINE_E_NONE;
  759.         }
  760.  
  761.     //Check for versions we support, 1.x
  762.     if (HIWORD(lVer)!=1)
  763.         return POLYLINE_E_UNSUPPORTEDVERSION;
  764.  
  765.     if (LOWORD(lVer)==0)    //Check for 1.0
  766.         {
  767.         //Do 2.0 to 1.0 conversion
  768.         *((PPOLYLINEDATA10)ppl)=*((PPOLYLINEDATA10)&m_pl);
  769.  
  770.         RECTSTORECT(ppl->rc, rc);
  771.  
  772.         //Get the parent coordinates of our rectangle
  773.         if (!IsWindow(m_hWnd))
  774.             OffsetRect(&rc, 8, 8); //This is always the offset.
  775.         else
  776.             RectGet(&rc);
  777.  
  778.         /*
  779.          * 1.0 data has points relative to size of the rectangle.
  780.          * We need to scale these from 0-32767 so we have the
  781.          * right values.
  782.          */
  783.         for (i=0; i < ppl->cPoints; i++)
  784.             PointScale(&rc, &ppl->rgpt[i], TRUE);
  785.  
  786.         RECTTORECTS(rc, ppl->rc);
  787.  
  788.         //Insure old version numbers.
  789.         ppl->wVerMaj=1;
  790.         ppl->wVerMin=0;
  791.  
  792.         ((PPOLYLINEDATA10)ppl)->fDrawEntire=TRUE;
  793.         return POLYLINE_E_NONE;
  794.         }
  795.  
  796.     return POLYLINE_E_UNSUPPORTEDVERSION;
  797.     }
  798.  
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806. /*
  807.  * CPolyline::DataSetMem
  808.  *
  809.  * Purpose:
  810.  *  Sets the Polyline's data using a global memory handle
  811.  *  instead of a pointer.
  812.  *
  813.  * Parameters:
  814.  *  hMem            HGLOBAL containing the data.
  815.  *  fFree           BOOL indicating if we're to free the data.
  816.  *                  The memory will be freed regardless of any
  817.  *                  error returned from here.
  818.  *  fSizeToData     BOOL indicating if we're to size to the data
  819.  *                  or scale it.
  820.  *  fNotify         BOOL indicating if we're to send an advise
  821.  *                  on this change.
  822.  *
  823.  * Return Value:
  824.  *  LONG            A POLYLINE_E_* value.
  825.  */
  826.  
  827. LONG CPolyline::DataSetMem(HGLOBAL hMem, BOOL fFree
  828.     , BOOL fSizeToData, BOOL fNotify)
  829.     {
  830.     PPOLYLINEDATA   ppl;
  831.     LONG            lRet=POLYLINE_E_INVALIDPOINTER;
  832.  
  833.     if (NULL!=hMem)
  834.         {
  835.         ppl=(PPOLYLINEDATA)GlobalLock(hMem);
  836.         lRet=DataSet(ppl, fSizeToData, fNotify);
  837.  
  838.         GlobalUnlock(hMem);
  839.  
  840.         if (fFree)
  841.             GlobalFree(hMem);
  842.         }
  843.  
  844.     return lRet;
  845.     }
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853. /*
  854.  * CPolyline::DataGetMem
  855.  *
  856.  * Purpose:
  857.  *  Retrieves the Polyline's data in a global memory handle.
  858.  *
  859.  * Parameters:
  860.  *  lVer            LONG version of data to retrieve.
  861.  *  phMem           HGLOBAL * in which to store the handle.
  862.  *
  863.  * Return Value:
  864.  *  LONG            A POLYLINE_E_* value.
  865.  */
  866.  
  867. LONG CPolyline::DataGetMem(LONG lVer, HGLOBAL *phMem)
  868.     {
  869.     HGLOBAL         hMem;
  870.     PPOLYLINEDATA   ppl;
  871.     LONG            lRet;
  872.  
  873.     if (NULL==phMem)
  874.         return POLYLINE_E_INVALIDPOINTER;
  875.  
  876.     hMem=GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE
  877.         , CBPOLYLINEDATA);
  878.  
  879.     if (NULL!=hMem)
  880.         {
  881.         ppl=(PPOLYLINEDATA)GlobalLock(hMem);
  882.         lRet=DataGet(ppl, lVer);
  883.         GlobalUnlock(hMem);
  884.  
  885.         if (POLYLINE_E_NONE!=lRet)
  886.             {
  887.             GlobalFree(hMem);
  888.             hMem=NULL;
  889.             }
  890.         }
  891.  
  892.     *phMem=hMem;
  893.     return lRet;
  894.     }
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902. /*
  903.  * CPolyline::RectGet
  904.  *
  905.  * Purpose:
  906.  *  Returns the rectangle of the Polyline in parent coordinates.
  907.  *
  908.  * Parameters:
  909.  *  pRect           LPRECT in which to return the rectangle.
  910.  *
  911.  * Return Value:
  912.  *  None
  913.  */
  914.  
  915. void CPolyline::RectGet(LPRECT pRect)
  916.     {
  917.     RECT        rc;
  918.     POINT       pt;
  919.  
  920.     //Retrieve the size of our rectangle in parent coordinates.
  921.     GetWindowRect(m_hWnd, &rc);
  922.     pt.x=rc.left;
  923.     pt.y=rc.top;
  924.     ScreenToClient(GetParent(m_hWnd), &pt);
  925.  
  926.     SetRect(pRect, pt.x, pt.y, pt.x+(rc.right-rc.left)
  927.         , pt.y+(rc.bottom-rc.top));
  928.  
  929.     return;
  930.     }
  931.  
  932.  
  933.  
  934.  
  935.  
  936. /*
  937.  * CPolyline::SizeGet
  938.  *
  939.  * Purpose:
  940.  *  Retrieves the size of the Polyline in parent coordinates.
  941.  *
  942.  * Parameters:
  943.  *  pRect           LPRECT in which to return the size.  The right
  944.  *                  and bottom fields will contain the dimensions.
  945.  *
  946.  * Return Value:
  947.  *  None
  948.  */
  949.  
  950. void CPolyline::SizeGet(LPRECT pRect)
  951.     {
  952.     RectGet(pRect);
  953.     return;
  954.     }
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961. /*
  962.  * CPolyline::RectSet
  963.  *
  964.  * Purpose:
  965.  *  Sets a new rectangle for the Polyline which sizes to fit.
  966.  *
  967.  * Parameters:
  968.  *  pRect           LPRECT containing the new rectangle.
  969.  *  fNotify         BOOL indicating if we're to notify anyone of
  970.  *                  the change.
  971.  *
  972.  * Return Value:
  973.  *  None
  974.  */
  975.  
  976. void CPolyline::RectSet(LPRECT pRect, BOOL fNotify)
  977.     {
  978.     UINT        cx, cy;
  979.     RECT        rc;
  980.  
  981.     //Scale the points from our current size to the new size
  982.     cx=pRect->right-pRect->left;
  983.     cy=pRect->bottom-pRect->top;
  984.  
  985.     SetWindowPos(m_hWnd, NULL, pRect->left, pRect->top
  986.         , cx, cy, SWP_NOZORDER);
  987.  
  988.     SetRect(&rc, 0, 0, cx, cy);
  989.     RECTTORECTS(rc, m_pl.rc);
  990.  
  991.     if (fNotify && NULL!=m_pAdv)
  992.         m_pAdv->OnSizeChange();
  993.  
  994.     InvalidateRect(m_hWnd, NULL, TRUE);
  995.  
  996.     return;
  997.     }
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005. /*
  1006.  * CPolyline::SizeSet
  1007.  *
  1008.  * Purpose:
  1009.  *  Sets a new size for the Polyline which sizes to fit.
  1010.  *
  1011.  * Parameters:
  1012.  *  pRect           LPRECT containing the new rectangle.
  1013.  *  fNotify         BOOL indicating if we're to notify anyone of
  1014.  *                  the change.
  1015.  *
  1016.  * Return Value:
  1017.  *  None
  1018.  */
  1019.  
  1020. void CPolyline::SizeSet(LPRECT pRect, BOOL fNotify)
  1021.     {
  1022.     UINT        cx, cy;
  1023.  
  1024.     //Scale the points from our current size to the new size
  1025.     cx=pRect->right-pRect->left;
  1026.     cy=pRect->bottom-pRect->top;
  1027.  
  1028.     SetWindowPos(m_hWnd, NULL, 0, 0, (UINT)cx, (UINT)cy
  1029.         , SWP_NOMOVE | SWP_NOZORDER);
  1030.  
  1031.     if (fNotify && NULL!=m_pAdv)
  1032.         m_pAdv->OnSizeChange();
  1033.  
  1034.     InvalidateRect(m_hWnd, NULL, TRUE);
  1035.  
  1036.     return;
  1037.     }
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044. /*
  1045.  * CPolyline::ColorSet
  1046.  *
  1047.  * Purpose:
  1048.  *  Changes for background or line color in the Polyline
  1049.  *
  1050.  * Parameters:
  1051.  *  iColor          UINT index of the color to change.
  1052.  *  cr              COLORREF new color to use.
  1053.  *
  1054.  * Return Value:
  1055.  *  COLORREF        Previous color for the index iColor.
  1056.  */
  1057.  
  1058. COLORREF CPolyline::ColorSet(UINT iColor, COLORREF cr)
  1059.     {
  1060.     COLORREF    crRet;
  1061.  
  1062.     switch (iColor)
  1063.         {
  1064.         case POLYLINECOLOR_BACKGROUND:
  1065.             crRet=m_pl.rgbBackground;
  1066.             m_pl.rgbBackground=cr;
  1067.             break;
  1068.  
  1069.         case POLYLINECOLOR_LINE:
  1070.             crRet=m_pl.rgbLine;
  1071.             m_pl.rgbLine=cr;
  1072.             break;
  1073.         }
  1074.  
  1075.     //If the color changed, repaint
  1076.     if (crRet!=cr)
  1077.         {
  1078.         if (NULL!=m_pAdv)
  1079.             m_pAdv->OnColorChange();
  1080.  
  1081.         InvalidateRect(m_hWnd, NULL, TRUE);
  1082.         UpdateWindow(m_hWnd);
  1083.         }
  1084.  
  1085.     return crRet;
  1086.     }
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094. /*
  1095.  * CPolyline::ColorGet
  1096.  *
  1097.  * Purpose:
  1098.  *  Retrieves one of the colors currently in use by the Polyline.
  1099.  *
  1100.  * Parameters:
  1101.  *  iColor          UINT identifying the color of interest.
  1102.  *
  1103.  * Return Value:
  1104.  *  COLORREF        Current color for iColor.
  1105.  */
  1106.  
  1107. COLORREF CPolyline::ColorGet(UINT iColor)
  1108.     {
  1109.     COLORREF    crRet;
  1110.  
  1111.     crRet=(POLYLINECOLOR_BACKGROUND==iColor)
  1112.         ? m_pl.rgbBackground : m_pl.rgbLine;
  1113.  
  1114.     return crRet;
  1115.     }
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124. /*
  1125.  * CPolyline::LineStyleSet
  1126.  *
  1127.  * Purpose:
  1128.  *  Changes the line style in use by the Polyline
  1129.  *
  1130.  * Parameters:
  1131.  *  iStyle          UINT style of the line to use.
  1132.  *
  1133.  * Return Value:
  1134.  *  UINT            Previous style.
  1135.  */
  1136.  
  1137. UINT CPolyline::LineStyleSet(UINT iStyle)
  1138.     {
  1139.     UINT        uRet=(UINT)m_pl.iLineStyle;
  1140.  
  1141.     //Validate the line style
  1142.     if (PS_SOLID==iStyle || PS_DASH==iStyle || PS_DOT==iStyle
  1143.         || PS_DASHDOT==iStyle || PS_DASHDOTDOT==iStyle)
  1144.         {
  1145.         m_pl.iLineStyle=iStyle;
  1146.  
  1147.         if (uRet!=(UINT)m_pl.iLineStyle)
  1148.             {
  1149.             if (NULL!=m_pAdv)
  1150.                 m_pAdv->OnLineStyleChange();
  1151.  
  1152.             InvalidateRect(m_hWnd, NULL, TRUE);
  1153.             UpdateWindow(m_hWnd);
  1154.             }
  1155.         }
  1156.  
  1157.     return uRet;
  1158.     }
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166. /*
  1167.  * CPolyline::LineStyleGet
  1168.  *
  1169.  * Purpose:
  1170.  *  Retrieves the current line style in use in the Polyline
  1171.  *
  1172.  * Parameters:
  1173.  *  None
  1174.  *
  1175.  * Return Value:
  1176.  *  UINT            Current line style.
  1177.  */
  1178.  
  1179. UINT CPolyline::LineStyleGet(void)
  1180.     {
  1181.     return m_pl.iLineStyle;
  1182.     }
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189. /*
  1190.  * CPolyline::RenderBitmap
  1191.  *
  1192.  * Purpose:
  1193.  *  Creates a bitmap image of the current Polyline.
  1194.  *
  1195.  * Parameters:
  1196.  *  None
  1197.  *
  1198.  * Return Value:
  1199.  *  HBITMAP         Handle to the newly rendered bitmap.
  1200.  */
  1201.  
  1202. HBITMAP CPolyline::RenderBitmap(void)
  1203.     {
  1204.     HDC         hDC;
  1205.     HDC         hMemDC;
  1206.     HBITMAP     hBmp;
  1207.     RECT        rc;
  1208.     HGDIOBJ     hObj;
  1209.  
  1210.     //Render a bitmap the size of the current rectangle.
  1211.     hDC=GetDC(m_hWnd);
  1212.     hMemDC=CreateCompatibleDC(hDC);
  1213.  
  1214.     GetClientRect(m_hWnd, &rc);
  1215.     hBmp=CreateCompatibleBitmap(hDC, rc.right, rc.bottom);
  1216.  
  1217.     if (NULL!=hBmp)
  1218.         {
  1219.         //Draw the POLYLINEDATA into the bitmap.
  1220.         hObj=SelectObject(hMemDC, hBmp);
  1221.         Draw(hMemDC, FALSE, TRUE);
  1222.         SelectObject(hMemDC, hObj);
  1223.         }
  1224.  
  1225.     DeleteDC(hMemDC);
  1226.     ReleaseDC(m_hWnd, hDC);
  1227.  
  1228.     return hBmp;
  1229.     }
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237. /*
  1238.  * CPolyline::RenderMetafile
  1239.  *
  1240.  * Purpose:
  1241.  *  Renders the current image of the Polyline into a metafile.
  1242.  *
  1243.  * Parameters:
  1244.  *  None
  1245.  *
  1246.  * Return Value:
  1247.  *  HMETAFILE       Handle to the newly created metafile.
  1248.  */
  1249.  
  1250. HMETAFILE CPolyline::RenderMetafile(void)
  1251.     {
  1252.     HDC         hDC;
  1253.     HMETAFILE   hMF;
  1254.     RECT        rc;
  1255.  
  1256.     //Create a memory metafile and return its handle.
  1257.     hDC=(HDC)CreateMetaFile(NULL);
  1258.     hMF=NULL;
  1259.  
  1260.     if (NULL!=hDC)
  1261.         {
  1262.         /*
  1263.          * This is absolutely essential to the metafile so it
  1264.          * can be scaled in the clipboard and any destination
  1265.          * application.
  1266.          */
  1267.         SetMapMode(hDC, MM_ANISOTROPIC);
  1268.         GetClientRect(m_hWnd, &rc);
  1269.         SetWindowOrgEx(hDC, 0, 0, NULL);
  1270.         SetWindowExtEx(hDC, rc.right, rc.bottom, NULL);
  1271.  
  1272.         Draw(hDC, TRUE, TRUE);
  1273.         hMF=CloseMetaFile(hDC);
  1274.         }
  1275.  
  1276.     return hMF;
  1277.     }
  1278.  
  1279.  
  1280.  
  1281.  
  1282.  
  1283.  
  1284. /*
  1285.  * CPolyline::RenderMetafilePict
  1286.  *
  1287.  * Purpose:
  1288.  *  Renders the current Polyline into a METAFILEPICT structure in
  1289.  *  global memory.
  1290.  *
  1291.  * Parameters:
  1292.  *  None
  1293.  *
  1294.  * Return Value:
  1295.  *  HGLOBAL         Memory containing the METAFILEPICT structure.
  1296.  */
  1297.  
  1298. HGLOBAL CPolyline::RenderMetafilePict(void)
  1299.     {
  1300.     HGLOBAL         hMem;
  1301.     HMETAFILE       hMF;
  1302.     LPMETAFILEPICT  pMF;
  1303.     RECT            rc;
  1304.  
  1305.  
  1306.     //Get the metafile
  1307.     hMF=RenderMetafile();
  1308.  
  1309.     if (NULL==hMF)
  1310.         return NULL;
  1311.  
  1312.     //Allocate the METAFILEPICT structure.
  1313.     hMem=GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE
  1314.         , sizeof(METAFILEPICT));
  1315.  
  1316.     if (NULL==hMem)
  1317.         {
  1318.         DeleteMetaFile(hMF);
  1319.         return NULL;
  1320.         }
  1321.  
  1322.     /*
  1323.      * Global lock only fails in PMODE if the selector is invalid
  1324.      * (like it was discarded) or references a 0 length segment,
  1325.      * neither of which can happen here.
  1326.      */
  1327.     pMF=(LPMETAFILEPICT)GlobalLock(hMem);
  1328.  
  1329.     pMF->hMF=hMF;
  1330.     pMF->mm=MM_ANISOTROPIC;
  1331.  
  1332.     //Insert the extents in MM_HIMETRIC units.
  1333.     GetClientRect(m_hWnd, &rc);
  1334.     RectConvertMappings(&rc, FALSE);
  1335.     pMF->xExt=rc.right;
  1336.     pMF->yExt=rc.bottom;
  1337.  
  1338.     GlobalUnlock(hMem);
  1339.     return hMem;
  1340.     }
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347. /*
  1348.  * CPolyline::RectConvertMappings
  1349.  *
  1350.  * Purpose:
  1351.  *  Converts the contents of a rectangle from device (MM_TEXT) or
  1352.  *  HIMETRIC to the other.
  1353.  *
  1354.  * Parameters:
  1355.  *  pRect           LPRECT containing the rectangle to convert.
  1356.  *  fToDevice       BOOL TRUE to convert from HIMETRIC to device,
  1357.  *                  FALSE to convert device to HIMETRIC.
  1358.  *
  1359.  * Return Value:
  1360.  *  None
  1361.  */
  1362.  
  1363. void CPolyline::RectConvertMappings(LPRECT pRect, BOOL fToDevice)
  1364.     {
  1365.     HDC      hDC;
  1366.     int      iLpx, iLpy;
  1367.  
  1368.     if (NULL==pRect)
  1369.         return;
  1370.  
  1371.     hDC=GetDC(NULL);
  1372.     iLpx=GetDeviceCaps(hDC, LOGPIXELSX);
  1373.     iLpy=GetDeviceCaps(hDC, LOGPIXELSY);
  1374.     ReleaseDC(NULL, hDC);
  1375.  
  1376.     if (fToDevice)
  1377.         {
  1378.         pRect->left=MulDiv(iLpx, pRect->left, HIMETRIC_PER_INCH);
  1379.         pRect->top =MulDiv(iLpy, pRect->top , HIMETRIC_PER_INCH);
  1380.  
  1381.         pRect->right=MulDiv(iLpx, pRect->right,  HIMETRIC_PER_INCH);
  1382.         pRect->bottom=MulDiv(iLpy, pRect->bottom,HIMETRIC_PER_INCH);
  1383.  
  1384.        #ifdef NEVER
  1385.         /*
  1386.          * In this conversion we may get situations where the top
  1387.          * coordinate is larger than the bottom, which messes us up.
  1388.          */
  1389.         if (pRect->bottom < pRect->top)
  1390.             {
  1391.             iLpy=pRect->top;
  1392.             pRect->top=pRect->bottom;
  1393.             pRect->bottom=iLpy;
  1394.             }
  1395.        #endif
  1396.         }
  1397.     else
  1398.         {
  1399.         pRect->left=MulDiv(pRect->left, HIMETRIC_PER_INCH, iLpx);
  1400.         pRect->top =MulDiv(pRect->top , HIMETRIC_PER_INCH, iLpy);
  1401.  
  1402.         pRect->right =MulDiv(pRect->right, HIMETRIC_PER_INCH, iLpx);
  1403.         pRect->bottom=MulDiv(pRect->bottom,HIMETRIC_PER_INCH, iLpy);
  1404.         }
  1405.  
  1406.     return;
  1407.     }
  1408.