home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / tutsamp / perclien / drawsink.cpp next >
Encoding:
C/C++ Source or Header  |  1997-08-05  |  17.0 KB  |  521 lines

  1. /*+==========================================================================
  2.   File:      DRAWSINK.CPP
  3.  
  4.   Summary:   Implementation file for the CODrawPageSink COM Object Class.
  5.              Connectable object notifications for drawing-related
  6.              events are handled by CODrawPageSink.
  7.  
  8.              CODrawPageSink offers a main IUnknown standard interface and
  9.              the IDrawPageSink custom interface (with the DrawPage related
  10.              event features). This multiple interface COM Object Class is
  11.              achieved via the technique of nested classes.  The
  12.              implementation of the various interfaces are nested
  13.              inside the CODrawPageSink Class.
  14.  
  15.              For a comprehensive tutorial code tour of this module's
  16.              contents and offerings see the tutorial PERCLIEN.HTM
  17.              file. For more specific technical details on the internal
  18.              workings see the comments dispersed throughout the module's
  19.              source code.
  20.  
  21.   Classes:   CODrawPageSink.
  22.  
  23.   Functions: none.
  24.  
  25.   Origin:    5-24-97: atrent - Editor-inheritance from TEXTSINK.CPP in
  26.              the PERCLIEN Tutorial Code Sample.
  27.  
  28. ----------------------------------------------------------------------------
  29.   This file is part of the Microsoft COM Tutorial Code Samples.
  30.  
  31.   Copyright (C) Microsoft Corporation, 1997.  All rights reserved.
  32.  
  33.   This source code is intended only as a supplement to Microsoft
  34.   Development Tools and/or on-line documentation.  See these other
  35.   materials for detailed information regarding Microsoft code samples.
  36.  
  37.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  38.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  39.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  40.   PARTICULAR PURPOSE.
  41. ==========================================================================+*/
  42.  
  43.  
  44. /*---------------------------------------------------------------------------
  45.   We include WINDOWS.H for all Win32 applications.
  46.   We include OLE2.H because we will be calling the COM/OLE Libraries.
  47.   We include OLECTL.H because it has definitions for connectable objects.
  48.   We include APPUTIL.H because we will be building this application using
  49.     the convenient Virtual Window and Dialog classes and other
  50.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  51.   We include IPAGES.H and PAGEGUID.H for the common Page-related
  52.     Interface class, GUID, and CLSID specifications.
  53.   We include GUIDRAW.H because it declares the class for the main C++
  54.     object that can service the Sink events.
  55.   We include DRAWSINK.H because it has the class CODrawPageSink
  56.     declarations.
  57. ---------------------------------------------------------------------------*/
  58. #include <windows.h>
  59. #include <ole2.h>
  60. #include <olectl.h>
  61. #include <apputil.h>
  62. #include <ipages.h>
  63. #include <pageguid.h>
  64. #include "guidraw.h"
  65. #include "drawsink.h"
  66.  
  67.  
  68. /*---------------------------------------------------------------------------
  69.   CODrawPageSink's implementation of its main COM object class including
  70.   Constructor, Destructor, QueryInterface, AddRef, and Release.
  71. ---------------------------------------------------------------------------*/
  72.  
  73. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  74.   Method:   CODrawPageSink::CODrawPageSink
  75.  
  76.   Summary:  CODrawPageSink Constructor. Note the member initializer:
  77.             "m_ImpIDrawPageSink(this, pUnkOuter)" which is used to pass
  78.             the 'this' and pUnkOuter pointers of this constructor function
  79.             to the constructor in the instantiation of the implementation
  80.             of the CImpIDrawPageSink interface (which is nested inside
  81.             this present CODrawPageSink Object Class).
  82.  
  83.   Args:     IUnknown* pUnkOuter,
  84.               Pointer to the the outer Unknown.  NULL means this COM Object
  85.               is not being Aggregated.  Non NULL means it is being created
  86.               on behalf of an outside COM object that is reusing it via
  87.               aggregation.
  88.             CGuiDraw* pGuiDraw)
  89.               Pointer to the main C++ object that can service the
  90.               DrawPageSink events.
  91.  
  92.   Modifies: m_cRefs, m_pUnkOuter, m_pGuiList.
  93.  
  94.   Returns:  void
  95. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  96. CODrawPageSink::CODrawPageSink(
  97.   IUnknown* pUnkOuter,
  98.   CGuiDraw* pGuiDraw) :
  99.   m_ImpIDrawPageSink(this, pUnkOuter)
  100. {
  101.   // Zero the COM object's reference count.
  102.   m_cRefs = 0;
  103.  
  104.   // No AddRef necessary if non-NULL, as we're nested.
  105.   m_pUnkOuter = pUnkOuter;
  106.  
  107.   // Assign the pointer to the Sink service C++ object.
  108.   m_pGuiDraw = pGuiDraw;
  109.  
  110.   return;
  111. }
  112.  
  113.  
  114. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  115.   Method:   CODrawPageSink::~CODrawPageSink
  116.  
  117.   Summary:  CODrawPageSink Destructor.
  118.  
  119.   Args:     void
  120.  
  121.   Returns:  void
  122. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  123. CODrawPageSink::~CODrawPageSink(void)
  124. {
  125.   return;
  126. }
  127.  
  128.  
  129. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  130.   Method:   CODrawPageSink::QueryInterface
  131.  
  132.   Summary:  QueryInterface of the CODrawPageSink non-delegating
  133.             IUnknown implementation.
  134.  
  135.   Args:     REFIID riid,
  136.               [in] GUID of the Interface being requested.
  137.             PPVOID ppv)
  138.               [out] Address of the caller's pointer variable that will
  139.               receive the requested interface pointer.
  140.  
  141.   Returns:  HRESULT
  142. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  143. STDMETHODIMP CODrawPageSink::QueryInterface(
  144.                REFIID riid,
  145.                PPVOID ppv)
  146. {
  147.   HRESULT hr = E_NOINTERFACE;
  148.  
  149.   *ppv = NULL;
  150.  
  151.   if (IID_IUnknown == riid)
  152.     *ppv = this;
  153.   else if (IID_IDrawPageSink == riid)
  154.     *ppv = &m_ImpIDrawPageSink;
  155.  
  156.   if (NULL != *ppv)
  157.   {
  158.     // We've handed out a pointer to the interface so obey the COM rules
  159.     // and AddRef the reference count.
  160.     ((LPUNKNOWN)*ppv)->AddRef();
  161.     hr = NOERROR;
  162.   }
  163.  
  164.  
  165.   return (hr);
  166. }
  167.  
  168.  
  169. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  170.   Method:   CODrawPageSink::AddRef
  171.  
  172.   Summary:  AddRef of the CODrawPageSink non-delegating IUnknown
  173.             implementation.
  174.  
  175.   Args:     void
  176.  
  177.   Modifies: m_cRefs.
  178.  
  179.   Returns:  ULONG
  180.               New value of m_cRefs (COM object's reference count).
  181. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  182. STDMETHODIMP_(ULONG) CODrawPageSink::AddRef(void)
  183. {
  184.   ULONG cRefs;
  185.  
  186.   cRefs = ++m_cRefs;
  187.  
  188.   return cRefs;
  189. }
  190.  
  191.  
  192. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  193.   Method:   CODrawPageSink::Release
  194.  
  195.   Summary:  Release of the CODrawPageSink non-delegating IUnknown
  196.             implementation.
  197.  
  198.   Args:     void
  199.  
  200.   Modifies: m_cRefs.
  201.  
  202.   Returns:  ULONG
  203.               New value of m_cRefs (COM object's reference count).
  204. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  205. STDMETHODIMP_(ULONG) CODrawPageSink::Release(void)
  206. {
  207.   ULONG cRefs;
  208.  
  209.   cRefs = --m_cRefs;
  210.  
  211.   if (0 == cRefs)
  212.   {
  213.     // We artificially bump the main ref count to prevent reentrancy
  214.     // via the main object destructor.  Not really needed in this
  215.     // CODrawPageSink but a good practice because we are aggregatable and
  216.     // may at some point in the future add something entertaining like
  217.     // some Releases to the CODrawPageSink destructor.
  218.     m_cRefs++;
  219.     delete this;
  220.   }
  221.  
  222.   return cRefs;
  223. }
  224.  
  225.  
  226. /*---------------------------------------------------------------------------
  227.   CODrawPageSink's nested implementation of the IDrawPageSink interface
  228.   including Constructor, Destructor, QueryInterface, AddRef, Release,
  229.   Loaded, Saved, Cleared, Resized, InkStart, InkDraw, and InkStop. Methods
  230.   in this interface are called by COM objects on the server side to send
  231.   notifications to the client.
  232. ---------------------------------------------------------------------------*/
  233.  
  234. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  235.   Method:   CODrawPageSink::CImpIDrawPageSink::CImpIDrawPageSink
  236.  
  237.   Summary:  Constructor for the CImpIDrawPageSink interface instantiation.
  238.  
  239.   Args:     CODrawPageSink* pCO,
  240.               Back pointer to the parent outer object.
  241.             IUnknown* pUnkOuter
  242.               Pointer to the outer Unknown.  For delegation.
  243.  
  244.   Modifies: m_pCO, m_pUnkOuter.
  245.  
  246.   Returns:  void
  247. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  248. CODrawPageSink::CImpIDrawPageSink::CImpIDrawPageSink(
  249.   CODrawPageSink* pCO,
  250.   IUnknown* pUnkOuter)
  251. {
  252.   // Init the main object Pointer to point to the parent object.
  253.   m_pCO = pCO;
  254.  
  255.   // Init the CImpIDrawPageSink interface's delegating Unknown pointer. We
  256.   // use the main object pointer for IUnknown delegation here if we are
  257.   // not being aggregated.  If we are being aggregated we use the supplied
  258.   // pUnkOuter for IUnknown delegation.  In either case the pointer
  259.   // assignment requires no AddRef because the CImpIDrawPageSink lifetime
  260.   // is quaranteed by the lifetime of the parent object in which
  261.   // CImpIDrawPageSink is nested.
  262.   if (NULL == pUnkOuter)
  263.     m_pUnkOuter = pCO;
  264.   else
  265.     m_pUnkOuter = pUnkOuter;
  266.  
  267.   return;
  268. }
  269.  
  270.  
  271. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  272.   Method:   CODrawPageSink::CImpIDrawPageSink::~CImpIDrawPageSink
  273.  
  274.   Summary:  Destructor for the CImpIDrawPageSink interface instantiation.
  275.  
  276.   Args:     void
  277.  
  278.   Returns:  void
  279. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  280. CODrawPageSink::CImpIDrawPageSink::~CImpIDrawPageSink(void)
  281. {
  282.   return;
  283. }
  284.  
  285.  
  286. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  287.   Method:   CODrawPageSink::CImpIDrawPageSink::QueryInterface
  288.  
  289.   Summary:  The QueryInterface IUnknown member of this IDrawPageSink
  290.             interface implementation that delegates to m_pUnkOuter,
  291.             whatever it is.
  292.  
  293.   Args:     REFIID riid,
  294.               [in] GUID of the Interface being requested.
  295.             PPVOID ppv)
  296.               [out] Address of the caller's pointer variable that will
  297.               receive the requested interface pointer.
  298.  
  299.   Returns:  HRESULT
  300.               Returned by the delegated outer QueryInterface call.
  301. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  302. STDMETHODIMP CODrawPageSink::CImpIDrawPageSink::QueryInterface(
  303.                REFIID riid,
  304.                PPVOID ppv)
  305. {
  306.   // Delegate this call to the outer object's QueryInterface.
  307.   return m_pUnkOuter->QueryInterface(riid, ppv);
  308. }
  309.  
  310.  
  311. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  312.   Method:   CODrawPageSink::CImpIDrawPageSink::AddRef
  313.  
  314.   Summary:  The AddRef IUnknown member of this IDrawPageSink interface
  315.             implementation that delegates to m_pUnkOuter, whatever it is.
  316.  
  317.   Args:     void
  318.  
  319.   Returns:  ULONG
  320.               Returned by the delegated outer AddRef call.
  321. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  322. STDMETHODIMP_(ULONG) CODrawPageSink::CImpIDrawPageSink::AddRef(void)
  323. {
  324.   // Delegate this call to the outer object's AddRef.
  325.   return m_pUnkOuter->AddRef();
  326. }
  327.  
  328.  
  329. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  330.   Method:   CODrawPageSink::CImpIDrawPageSink::Release
  331.  
  332.   Summary:  The Release IUnknown member of this IDrawPageSink interface
  333.             implementation that delegates to m_pUnkOuter, whatever it is.
  334.  
  335.   Args:     void
  336.  
  337.   Returns:  ULONG
  338.               Returned by the delegated outer Release call.
  339. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  340. STDMETHODIMP_(ULONG) CODrawPageSink::CImpIDrawPageSink::Release(void)
  341. {
  342.   // Delegate this call to the outer object's Release.
  343.   return m_pUnkOuter->Release();
  344. }
  345.  
  346.  
  347. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  348.   Method:   CODrawPageSink::CImpIDrawPageSink::Loaded
  349.  
  350.   Summary:  The CODrawPage object's ink drawing data was loaded from
  351.             its storage.
  352.  
  353.   Args:     void
  354.  
  355.   Returns:  HRESULT
  356.               Standard result code. NOERROR for success.
  357. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  358. STDMETHODIMP CODrawPageSink::CImpIDrawPageSink::Loaded(
  359.                void)
  360. {
  361.   HRESULT hr;
  362.  
  363.   // We have been notified that a load was done. Thus, a whole new array
  364.   // of ink data must be displayed in a cleared window of this client.
  365.   m_pCO->m_pGuiDraw->ClearWin();
  366.   hr = m_pCO->m_pGuiDraw->PaintWin();
  367.  
  368.   return hr;
  369. }
  370.  
  371.  
  372. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  373.   Method:   CODrawPageSink::CImpIDrawPageSink::Saved
  374.  
  375.   Summary:  The CODrawPage object's drawing DrawPage data was saved to
  376.             its persistent storage.
  377.  
  378.   Args:     void
  379.  
  380.   Returns:  HRESULT
  381.               Standard result code. NOERROR for success.
  382. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  383. STDMETHODIMP CODrawPageSink::CImpIDrawPageSink::Saved(
  384.                void)
  385. {
  386.   HRESULT hr = E_NOTIMPL;
  387.  
  388.   // For future evolution.
  389.  
  390.   return hr;
  391. }
  392.  
  393.  
  394. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  395.   Method:   CODrawPageSink::CImpIDrawPageSink::Cleared
  396.  
  397.   Summary:  The CODrawPage was cleared of all content.
  398.  
  399.   Args:     void
  400.  
  401.   Returns:  HRESULT
  402.               Standard result code. NOERROR for success.
  403. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  404. STDMETHODIMP CODrawPageSink::CImpIDrawPageSink::Cleared(
  405.                void)
  406. {
  407.   HRESULT hr = E_NOTIMPL;
  408.  
  409.   // For future evolution.
  410.  
  411.   return hr;
  412. }
  413.  
  414.  
  415. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  416.   Method:   CODrawPageSink::CImpIDrawPageSink::Resized
  417.  
  418.   Summary:  CODrawPage's drawing rectangle was resized by a client.
  419.  
  420.   Args:     void
  421.  
  422.   Returns:  HRESULT
  423.               Standard result code. NOERROR for success.
  424. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  425. STDMETHODIMP CODrawPageSink::CImpIDrawPageSink::Resized(
  426.                SHORT nWidth,
  427.                SHORT nHeight)
  428. {
  429.   HRESULT hr = E_NOTIMPL;
  430.  
  431.   // For future evolution.
  432.  
  433.   return hr;
  434. }
  435.  
  436.  
  437. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  438.   Method:   CODrawPageSink::CImpIDrawPageSink::InkStart
  439.  
  440.   Summary:  Client is being told to start a display ink drawing sequence.
  441.  
  442.   Args:     SHORT nX,
  443.               X coordinate of the start point.
  444.             SHORT nY,
  445.               Y coordinate of the start point.
  446.             SHORT nWidth,
  447.               Ink Width in pixels.
  448.             COLORREF crInkColor)
  449.               RGB Ink color to be used in the subsequent inking sequence.
  450.  
  451.   Returns:  HRESULT
  452.               Standard result code. NOERROR for success.
  453. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  454. STDMETHODIMP CODrawPageSink::CImpIDrawPageSink::InkStart(
  455.                SHORT nX,
  456.                SHORT nY,
  457.                SHORT nWidth,
  458.                COLORREF crInkColor)
  459. {
  460.   // Turn off ink saving to the CODrawPage object.
  461.   m_pCO->m_pGuiDraw->InkSaving(FALSE);
  462.  
  463.   // Play the data back to the CGuiDraw for display only.
  464.   m_pCO->m_pGuiDraw->InkWidth(nWidth);
  465.   m_pCO->m_pGuiDraw->InkColor(crInkColor);
  466.   m_pCO->m_pGuiDraw->InkStart(nX, nY);
  467.  
  468.   return NOERROR;
  469. }
  470.  
  471.  
  472. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  473.   Method:   CODrawPageSink::CImpIDrawPageSink::InkDraw
  474.  
  475.   Summary:  Client is being told to draw/display ink drawing sequence data.
  476.  
  477.   Args:     SHORT nX,
  478.               X coordinate of the start point.
  479.             SHORT nY,
  480.               Y coordinate of the start point.
  481.  
  482.   Returns:  HRESULT
  483.               Standard result code. NOERROR for success.
  484. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  485. STDMETHODIMP CODrawPageSink::CImpIDrawPageSink::InkDraw(
  486.                SHORT nX,
  487.                SHORT nY)
  488. {
  489.   // Play the data back to the CGuiDraw for display only.
  490.   m_pCO->m_pGuiDraw->InkDraw(nX, nY);
  491.  
  492.   return NOERROR;
  493. }
  494.  
  495.  
  496. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  497.   Method:   CODrawPageSink::CImpIDrawPageSink::InkStop
  498.  
  499.   Summary:  Client is being told to stop an ink drawing sequence.
  500.  
  501.   Args:     SHORT nX,
  502.               X coordinate of the start point.
  503.             SHORT nY,
  504.               Y coordinate of the start point.
  505.  
  506.   Returns:  HRESULT
  507.               Standard result code. NOERROR for success.
  508. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  509. STDMETHODIMP CODrawPageSink::CImpIDrawPageSink::InkStop(
  510.                SHORT nX,
  511.                SHORT nY)
  512. {
  513.   // Stop the display play back of the data to the CGuiDraw.
  514.   m_pCO->m_pGuiDraw->InkStop(nX, nY);
  515.  
  516.   // Turn Ink Data saving back on.
  517.   m_pCO->m_pGuiDraw->InkSaving(TRUE);
  518.  
  519.   return NOERROR;
  520. }
  521.