POLYLINE.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:33k
源码类别:

Windows编程

开发平台:

Visual C++

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