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 / chap12 / cosmo / polyline.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  31KB  |  1,409 lines

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