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 / chap07 / cosmo / polyline.cpp < prev    next >
C/C++ Source or Header  |  1996-05-02  |  31KB  |  1,411 lines

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