home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / cdinfo / cdinfo1.h < prev    next >
C/C++ Source or Header  |  1998-03-26  |  8KB  |  250 lines

  1. // CDInfo1.h : Declaration of the CCDInfo
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #include <commctrl.h>
  14. #include "resource.h"       // main symbols
  15. #include "CCDInfo.h"
  16. #include "CPCDInfo.h"
  17.  
  18. #define PI 3.14159265358979
  19. #define BORDER 5
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CCDInfo
  23. class CCDInfo :
  24.     public CComObjectRoot,
  25.     public CComCoClass<CCDInfo, &CLSID_CCDInfo>,
  26.     public CComControl<CCDInfo>,
  27.     public IDispatchImpl<ICDInfo, &IID_ICDInfo, &LIBID_CDINFOLib>,
  28.     public IProvideClassInfo2Impl<&CLSID_CCDInfo, &DIID_CDEvents, &LIBID_CDINFOLib>,
  29.     public IPersistStreamInitImpl<CCDInfo>,
  30.     public IPersistStorageImpl<CCDInfo>,
  31.     public IQuickActivateImpl<CCDInfo>,
  32.     public IOleControlImpl<CCDInfo>,
  33.     public IOleObjectImpl<CCDInfo>,
  34.     public IOleInPlaceActiveObjectImpl<CCDInfo>,
  35.     public IViewObjectExImpl<CCDInfo>,
  36.     public IOleInPlaceObjectWindowlessImpl<CCDInfo>,
  37.     public IDataObjectImpl<CCDInfo>,
  38.     public IPropertyNotifySinkCP<CCDInfo>,
  39.     public IRunnableObjectImpl<CCDInfo>,
  40.     public IPerPropertyBrowsingImpl<CCDInfo>,
  41.     public IConnectionPointContainerImpl<CCDInfo>,
  42.     public CProxyCDEvents<CCDInfo>,
  43.     public IOleLinkImpl<CCDInfo>
  44. {
  45. public:
  46.     CCDInfo()
  47.     {
  48.         ZeroMemory(&m_rcEllipse, sizeof(RECT));
  49.         m_hPalette = NULL;
  50.         m_wndToolTip.m_hWnd = NULL;
  51.     }
  52.     CWindow m_wndToolTip;
  53.  
  54. DECLARE_REGISTRY_RESOURCEID(IDR_CDInfo)
  55.  
  56. BEGIN_COM_MAP(CCDInfo)
  57.     COM_INTERFACE_ENTRY(ICDInfo)
  58.     COM_INTERFACE_ENTRY(IDispatch)
  59.     COM_INTERFACE_ENTRY_IMPL(IViewObjectEx)
  60.     COM_INTERFACE_ENTRY_IMPL_IID(IID_IViewObject2, IViewObjectEx)
  61.     COM_INTERFACE_ENTRY_IMPL_IID(IID_IViewObject, IViewObjectEx)
  62.     COM_INTERFACE_ENTRY_IMPL(IOleInPlaceObjectWindowless)
  63.     COM_INTERFACE_ENTRY_IMPL_IID(IID_IOleInPlaceObject, IOleInPlaceObjectWindowless)
  64.     COM_INTERFACE_ENTRY_IMPL_IID(IID_IOleWindow, IOleInPlaceObjectWindowless)
  65.     COM_INTERFACE_ENTRY_IMPL(IOleInPlaceActiveObject)
  66.     COM_INTERFACE_ENTRY_IMPL(IOleControl)
  67.     COM_INTERFACE_ENTRY_IMPL(IOleObject)
  68.     COM_INTERFACE_ENTRY_IMPL(IQuickActivate)
  69.     COM_INTERFACE_ENTRY_IMPL(IPersistStorage)
  70.     COM_INTERFACE_ENTRY_IMPL(IPersistStreamInit)
  71.     COM_INTERFACE_ENTRY_IMPL(IDataObject)
  72.     COM_INTERFACE_ENTRY(IProvideClassInfo)
  73.     COM_INTERFACE_ENTRY(IProvideClassInfo2)
  74.     COM_INTERFACE_ENTRY_IMPL(IRunnableObject)
  75.     COM_INTERFACE_ENTRY_IMPL(IPerPropertyBrowsing)
  76.     COM_INTERFACE_ENTRY_IMPL(IConnectionPointContainer)
  77.     COM_INTERFACE_ENTRY_IMPL(IOleLink)
  78. END_COM_MAP()
  79.  
  80. BEGIN_PROPERTY_MAP(CCDInfo)
  81.     // PROP_ENTRY(OLESTR("Description"), dispid, clsid)
  82.     // PROP_PAGE(clsid)
  83. END_PROPERTY_MAP()
  84.  
  85. BEGIN_CONNECTION_POINT_MAP(CCDInfo)
  86.     CONNECTION_POINT_ENTRY(IID_IPropertyNotifySink)
  87.     CONNECTION_POINT_ENTRY(DIID_CDEvents)
  88. END_CONNECTION_POINT_MAP()
  89.  
  90. BEGIN_MSG_MAP(CCDInfo)
  91.     MESSAGE_HANDLER(WM_PAINT         , OnPaint)
  92.     MESSAGE_HANDLER(WM_ERASEBKGND , OnEraseBkgnd)
  93.     MESSAGE_HANDLER(WM_MOUSEMOVE     , OnMouseMove)
  94.     MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
  95.     MESSAGE_HANDLER(WM_LBUTTONUP  , RelayEvent)
  96.     MESSAGE_HANDLER(WM_RBUTTONDOWN, RelayEvent)
  97.     MESSAGE_HANDLER(WM_RBUTTONUP  , RelayEvent)
  98.     MESSAGE_HANDLER(WM_MBUTTONDOWN, RelayEvent)
  99.     MESSAGE_HANDLER(WM_MBUTTONUP  , RelayEvent)
  100. END_MSG_MAP()
  101.  
  102.     LRESULT OnEraseBkgnd(UINT /* uMsg */, WPARAM /* wParam */, LPARAM /* lParam */, BOOL& /* bHandled */)
  103.     {
  104.         // We don't paint the background here as it is done in the OnDraw function.
  105.         // This is to stop flickering when a tooltip is displayed.
  106.         return 1;
  107.     }
  108.     HRESULT OnDraw(ATL_DRAWINFO& di);
  109.     LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  110.     LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  111.  
  112.     LRESULT RelayEvent(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /* bHandled */)
  113.     {
  114.         if (m_wndToolTip.m_hWnd)
  115.         {
  116.             MSG msg;
  117.             msg.lParam  = lParam;
  118.             msg.wParam  = wParam;
  119.             msg.message = uMsg;
  120.             msg.hwnd    = m_hTheWnd;
  121.             ::SendMessage(m_wndToolTip.m_hWnd, TTM_RELAYEVENT, 0, (LPARAM)&msg);
  122.         }
  123.         return 0;
  124.     }
  125.  
  126.     HRESULT InPlaceActivate(LONG iVerb, const RECT* prcPosRect=NULL)
  127.     {
  128.         HRESULT hr;
  129.  
  130.         hr = CComControlBase::InPlaceActivate(iVerb, prcPosRect);
  131.         if (m_wndToolTip.m_hWnd == NULL)
  132.         {
  133.             GetTheWindow();
  134.             CreateTooltipWindow();
  135.         }
  136.         return hr;
  137.     }
  138.  
  139.     STDMETHOD(InPlaceDeactivate)(void)
  140.     {
  141.         if (m_wndToolTip.m_hWnd)
  142.             m_wndToolTip.DestroyWindow();
  143.  
  144.         return IOleInPlaceObjectWindowlessImpl<CCDInfo>::InPlaceDeactivate();
  145.     }
  146.  
  147.     STDMETHOD(QuickActivate)(QACONTAINER *pqacontainer, QACONTROL *pqacontrol)
  148.     {
  149.         m_hPalette = pqacontainer->hpal;
  150.         return IQuickActivateImpl<CCDInfo>::QuickActivate(pqacontainer, pqacontrol);
  151.     }
  152.  
  153. // ICDInfo
  154.     STDMETHOD(get_Tracks)(short* pTracks)
  155.     {
  156.         *pTracks = m_cd.GetNumberOfTracks();
  157.         return S_OK;
  158.     }
  159.     STDMETHOD(put_Tracks)(short nTracks)
  160.     {
  161.         m_dblCurrentAngle = 3 * PI / 2; // Reset the drawing angle
  162.         m_cd.SetNumberOfTracks(nTracks);
  163.         return S_OK;
  164.     }
  165.     STDMETHOD(get_Length)(short nTrack, short* pLength)
  166.     {
  167.         *pLength = m_cd.GetTrackLength(nTrack);
  168.         return S_OK;
  169.     }
  170.     STDMETHOD(put_Length)(short nTrack, short nLength)
  171.     {
  172.         m_cd.SetTrackLength(nTrack, nLength);
  173.         return S_OK;
  174.     }
  175.     STDMETHOD(get_TotalLength)(short* pLength)
  176.     {
  177.         *pLength = m_cd.GetTotalLength();
  178.         return S_OK;
  179.     }
  180.     STDMETHOD(Read)(short* pTracks)
  181.     {
  182.         m_dblCurrentAngle = 3 * PI / 2; // Reset the drawing angle
  183.         *pTracks = m_cd.Read();
  184.         Redraw();
  185.         return S_OK;
  186.     }
  187.     STDMETHOD(Redraw)()
  188.     {
  189.         if (m_bWndLess)
  190.             m_spInPlaceSite->InvalidateRect(NULL, TRUE);
  191.         else if (m_hWnd)
  192.             InvalidateRect(NULL, TRUE);
  193.         return S_OK;
  194.     }
  195.     STDMETHOD(Play)(short nTrack)
  196.     {
  197.         DrawToTrack(nTrack);
  198.         m_cd.Play(nTrack);
  199.         return S_OK;
  200.     }
  201.  
  202.     // We override the next couple of methods to trap when our size changes
  203.     // so we can set our m_rcEllipse accordingly
  204.     STDMETHOD(SetExtent)(DWORD  dwDrawAspect, SIZEL *psizel)
  205.     {
  206.         HRESULT hr = IOleObjectImpl<CCDInfo>::SetExtent(dwDrawAspect, psizel);
  207.         m_rcEllipse = m_rcPos;
  208.         ReduceRect(&m_rcEllipse);
  209.         if (!m_bWndLess)
  210.             OffsetRect(&m_rcEllipse, -m_rcEllipse.left+BORDER, -m_rcEllipse.top+BORDER);
  211.         return hr;
  212.     }
  213.     STDMETHOD(SetObjectRects)(LPCRECT prcPos,LPCRECT prcClip)
  214.     {
  215.         HRESULT hr = IOleInPlaceObjectWindowlessImpl<CCDInfo>::SetObjectRects(prcPos, prcClip);
  216.         m_rcEllipse = m_rcPos;
  217.         ReduceRect(&m_rcEllipse);
  218.         if (!m_bWndLess)
  219.             OffsetRect(&m_rcEllipse, -m_rcEllipse.left+BORDER, -m_rcEllipse.top+BORDER);
  220.         return hr;
  221.     }
  222.     void ReduceRect(RECT* pRect)
  223.     {
  224.         InflateRect(pRect, BORDER * ((pRect->left < pRect->right) ? -1 : 1),
  225.             BORDER * ((pRect->top < pRect->bottom) ? -1 : 1));
  226.     }
  227.  
  228.     HWND        m_hTheWnd;
  229. private:
  230.     void        CreateTooltipWindow();
  231.     void        GetTheWindow();
  232.     void        DrawOffScreen(HDC hdc, RECT rc);
  233.     void        DrawCD(HDC hdc, RECT rc);
  234.     POINT       CalcPoint(POINT ptCenter, double dwRadius, double fAngle);
  235.     double      CalcAngle(POINT ptCenter, POINT point);
  236.     short       GetTrackFromPoint(const POINT& pt);
  237.     BOOL        InEllipse(RECT& rect, POINT point);
  238.     double      GetTrackAngle(short nWantedTrack);
  239.     void        DrawToTrack(short nTrack);
  240.  
  241.     short       m_nCurrentTrack;
  242.     short       m_nMouseTrack;
  243.     CDInfo      m_cd;
  244.     RECT        m_rcEllipse;
  245.     HPALETTE    m_hPalette;
  246.     double      m_dblCurrentAngle;
  247.  
  248.     static COLORREF     m_colSegment[];
  249. };
  250.