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 / textsink.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-05  |  14.2 KB  |  432 lines

  1. /*+==========================================================================
  2.   File:      TEXTSINK.CPP
  3.  
  4.   Summary:   Implementation file for the COTextPageSink COM Object Class.
  5.              Connectable object notifications for textpage-related
  6.              events are handled by COTextPageSink.
  7.  
  8.              COTextPageSink offers a main IUnknown standard interface and
  9.              the ITextPageSink custom interface (with the TextPage related
  10.              event features). This multiple interface COM Object Class is
  11.              achieved via the technique of nested classes.  The
  12.              implementation of the ITextPageSink interface is nested
  13.              inside the COTextPageSink 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:   COTextPageSink.
  22.  
  23.   Functions: none.
  24.  
  25.   Origin:    5-24-97: atrent - Editor-inheritance from SINK.CPP in
  26.              the STOCLIEN Tutorial Code Sample. [Revised]
  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 TEXTWIN.H because CGuiText creates and uses a CTextWin to
  54.     encapsulate the multiline text edit control.
  55.   We include GUITEXT.H because it declares the class for the main C++
  56.     object that can service the text page Sink events.
  57.   We include TEXTSINK.H because it has the COTextPageSink class declaration.
  58. ---------------------------------------------------------------------------*/
  59. #include <windows.h>
  60. #include <ole2.h>
  61. #include <olectl.h>
  62. #include <apputil.h>
  63. #include <ipages.h>
  64. #include <pageguid.h>
  65. #include "textwin.h"
  66. #include "guitext.h"
  67. #include "textsink.h"
  68.  
  69.  
  70. /*---------------------------------------------------------------------------
  71.   COTextPageSink's implementation of its main COM object class including
  72.   Constructor, Destructor, QueryInterface, AddRef, and Release.
  73. ---------------------------------------------------------------------------*/
  74.  
  75. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  76.   Method:   COTextPageSink::COTextPageSink
  77.  
  78.   Summary:  COTextPageSink Constructor. Note the member initializer:
  79.             "m_ImpITextPageSink(this, pUnkOuter)" which is used to pass
  80.             the 'this' and pUnkOuter pointers of this constructor function
  81.             to the constructor in the instantiation of the implementation
  82.             of the CImpITextPageSink interface (which is nested inside
  83.             this present COTextPageSink Object Class).
  84.  
  85.   Args:     IUnknown* pUnkOuter,
  86.               Pointer to the the outer Unknown.  NULL means this COM Object
  87.               is not being Aggregated.  Non NULL means it is being created
  88.               on behalf of an outside COM object that is reusing it via
  89.               aggregation.
  90.             CGuiList* pGuiList)
  91.               Pointer to the main C++ object that can service the
  92.               TextPageSink events.
  93.  
  94.   Modifies: m_cRefs, m_pUnkOuter, m_pGuiList.
  95.  
  96.   Returns:  void
  97. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  98. COTextPageSink::COTextPageSink(
  99.   IUnknown* pUnkOuter,
  100.   CGuiText* pGuiText) :
  101.   m_ImpITextPageSink(this, pUnkOuter)
  102. {
  103.   // Zero the COM object's reference count.
  104.   m_cRefs = 0;
  105.  
  106.   // No AddRef necessary if non-NULL, as we're nested.
  107.   m_pUnkOuter = pUnkOuter;
  108.  
  109.   // Assign the pointer to the Sink service C++ object.
  110.   m_pGuiText = pGuiText;
  111.  
  112.   return;
  113. }
  114.  
  115.  
  116. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  117.   Method:   COTextPageSink::~COTextPageSink
  118.  
  119.   Summary:  COTextPageSink Destructor.
  120.  
  121.   Args:     void
  122.  
  123.   Returns:  void
  124. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  125. COTextPageSink::~COTextPageSink(void)
  126. {
  127.   return;
  128. }
  129.  
  130.  
  131. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  132.   Method:   COTextPageSink::QueryInterface
  133.  
  134.   Summary:  QueryInterface of the COTextPageSink non-delegating
  135.             IUnknown implementation.
  136.  
  137.   Args:     REFIID riid,
  138.               [in] GUID of the Interface being requested.
  139.             PPVOID ppv)
  140.               [out] Address of the caller's pointer variable that will
  141.               receive the requested interface pointer.
  142.  
  143.   Returns:  HRESULT
  144. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  145. STDMETHODIMP COTextPageSink::QueryInterface(
  146.                REFIID riid,
  147.                PPVOID ppv)
  148. {
  149.   HRESULT hr = E_NOINTERFACE;
  150.  
  151.   *ppv = NULL;
  152.  
  153.   if (IID_IUnknown == riid)
  154.     *ppv = this;
  155.   else if (IID_ITextPageSink == riid)
  156.     *ppv = &m_ImpITextPageSink;
  157.  
  158.   if (NULL != *ppv)
  159.   {
  160.     // We've handed out a pointer to the interface so obey the COM rules
  161.     // and AddRef the reference count.
  162.     ((LPUNKNOWN)*ppv)->AddRef();
  163.     hr = NOERROR;
  164.   }
  165.  
  166.  
  167.   return (hr);
  168. }
  169.  
  170.  
  171. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  172.   Method:   COTextPageSink::AddRef
  173.  
  174.   Summary:  AddRef of the COTextPageSink non-delegating IUnknown
  175.             implementation.
  176.  
  177.   Args:     void
  178.  
  179.   Modifies: m_cRefs.
  180.  
  181.   Returns:  ULONG
  182.               New value of m_cRefs (COM object's reference count).
  183. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  184. STDMETHODIMP_(ULONG) COTextPageSink::AddRef(void)
  185. {
  186.   ULONG cRefs;
  187.  
  188.   cRefs = ++m_cRefs;
  189.  
  190.   return cRefs;
  191. }
  192.  
  193.  
  194. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  195.   Method:   COTextPageSink::Release
  196.  
  197.   Summary:  Release of the COTextPageSink non-delegating IUnknown
  198.             implementation.
  199.  
  200.   Args:     void
  201.  
  202.   Modifies: m_cRefs.
  203.  
  204.   Returns:  ULONG
  205.               New value of m_cRefs (COM object's reference count).
  206. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  207. STDMETHODIMP_(ULONG) COTextPageSink::Release(void)
  208. {
  209.   ULONG cRefs;
  210.  
  211.   cRefs = --m_cRefs;
  212.  
  213.   if (0 == cRefs)
  214.   {
  215.     // We artificially bump the main ref count to prevent reentrancy
  216.     // via the main object destructor.  Not really needed in this
  217.     // COTextPageSink but a good practice because we are aggregatable and
  218.     // may at some point in the future add something entertaining like
  219.     // some Releases to the COTextPageSink destructor.
  220.     m_cRefs++;
  221.     delete this;
  222.   }
  223.  
  224.   return cRefs;
  225. }
  226.  
  227.  
  228. /*---------------------------------------------------------------------------
  229.   COTextPageSink's nested implementation of the ITextPageSink interface
  230.   including Constructor, Destructor, QueryInterface, AddRef, Release,
  231.   Loaded, Saved, Put, and Cleared. Methods in this interface are called by
  232.   COM objects on the server side to send notifications to the client.
  233. ---------------------------------------------------------------------------*/
  234.  
  235. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  236.   Method:   COTextPageSink::CImpITextPageSink::CImpITextPageSink
  237.  
  238.   Summary:  Constructor for the CImpITextPageSink interface instantiation.
  239.  
  240.   Args:     COTextPageSink* pCO,
  241.               Back pointer to the parent outer object.
  242.             IUnknown* pUnkOuter
  243.               Pointer to the outer Unknown.  For delegation.
  244.  
  245.   Modifies: m_pCO, m_pUnkOuter.
  246.  
  247.   Returns:  void
  248. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  249. COTextPageSink::CImpITextPageSink::CImpITextPageSink(
  250.   COTextPageSink* pCO,
  251.   IUnknown* pUnkOuter)
  252. {
  253.   // Init the main object pointer to point to the parent object.
  254.   m_pCO = pCO;
  255.  
  256.   // Init the CImpITextPageSink interface's delegating Unknown pointer. We
  257.   // use the main object pointer for IUnknown delegation here if we are
  258.   // not being aggregated.  If we are being aggregated we use the supplied
  259.   // pUnkOuter for IUnknown delegation.  In either case the pointer
  260.   // assignment requires no AddRef because the CImpITextPageSink lifetime
  261.   // is quaranteed by the lifetime of the parent object in which
  262.   // CImpITextPageSink is nested.
  263.   if (NULL == pUnkOuter)
  264.     m_pUnkOuter = pCO;
  265.   else
  266.     m_pUnkOuter = pUnkOuter;
  267.  
  268.   return;
  269. }
  270.  
  271.  
  272. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  273.   Method:   COTextPageSink::CImpITextPageSink::~CImpITextPageSink
  274.  
  275.   Summary:  Destructor for the CImpITextPageSink interface instantiation.
  276.  
  277.   Args:     void
  278.  
  279.   Returns:  void
  280. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  281. COTextPageSink::CImpITextPageSink::~CImpITextPageSink(void)
  282. {
  283.   return;
  284. }
  285.  
  286.  
  287. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  288.   Method:   COTextPageSink::CImpITextPageSink::QueryInterface
  289.  
  290.   Summary:  The QueryInterface IUnknown member of this ITextPageSink
  291.             interface implementation that delegates to m_pUnkOuter,
  292.             whatever it is.
  293.  
  294.   Args:     REFIID riid,
  295.               [in] GUID of the Interface being requested.
  296.             PPVOID ppv)
  297.               [out] Address of the caller's pointer variable that will
  298.               receive the requested interface pointer.
  299.  
  300.   Returns:  HRESULT
  301.               Returned by the delegated outer QueryInterface call.
  302. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  303. STDMETHODIMP COTextPageSink::CImpITextPageSink::QueryInterface(
  304.                REFIID riid,
  305.                PPVOID ppv)
  306. {
  307.   // Delegate this call to the outer object's QueryInterface.
  308.   return m_pUnkOuter->QueryInterface(riid, ppv);
  309. }
  310.  
  311.  
  312. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  313.   Method:   COTextPageSink::CImpITextPageSink::AddRef
  314.  
  315.   Summary:  The AddRef IUnknown member of this ITextPageSink interface
  316.             implementation that delegates to m_pUnkOuter, whatever it is.
  317.  
  318.   Args:     void
  319.  
  320.   Returns:  ULONG
  321.               Returned by the delegated outer AddRef call.
  322. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  323. STDMETHODIMP_(ULONG) COTextPageSink::CImpITextPageSink::AddRef(void)
  324. {
  325.   // Delegate this call to the outer object's AddRef.
  326.   return m_pUnkOuter->AddRef();
  327. }
  328.  
  329.  
  330. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  331.   Method:   COTextPageSink::CImpITextPageSink::Release
  332.  
  333.   Summary:  The Release IUnknown member of this ITextPageSink interface
  334.             implementation that delegates to m_pUnkOuter, whatever it is.
  335.  
  336.   Args:     void
  337.  
  338.   Returns:  ULONG
  339.               Returned by the delegated outer Release call.
  340. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  341. STDMETHODIMP_(ULONG) COTextPageSink::CImpITextPageSink::Release(void)
  342. {
  343.   // Delegate this call to the outer object's Release.
  344.   return m_pUnkOuter->Release();
  345. }
  346.  
  347.  
  348. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  349.   Method:   COTextPageSink::CImpITextPageSink::Loaded
  350.  
  351.   Summary:  The COTextPage object's text data was loaded from a client's
  352.             compound file.
  353.  
  354.   Args:     void
  355.  
  356.   Returns:  HRESULT
  357.               Standard result code. NOERROR for success.
  358. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  359. STDMETHODIMP COTextPageSink::CImpITextPageSink::Loaded(
  360.                void)
  361. {
  362.   HRESULT hr = E_NOTIMPL;
  363.  
  364.   // For future evolution.
  365.  
  366.   return hr;
  367. }
  368.  
  369.  
  370. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  371.   Method:   COTextPageSink::CImpITextPageSink::Saved
  372.  
  373.   Summary:  The COTextPage object's text data was saved to a client's
  374.             compound file.
  375.  
  376.   Args:     void
  377.  
  378.   Returns:  HRESULT
  379.               Standard result code. NOERROR for success.
  380. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  381. STDMETHODIMP COTextPageSink::CImpITextPageSink::Saved(
  382.                void)
  383. {
  384.   HRESULT hr = E_NOTIMPL;
  385.  
  386.   // For future evolution.
  387.  
  388.   return hr;
  389. }
  390.  
  391.  
  392. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  393.   Method:   COTextPageSink::CImpITextPageSink::Put
  394.  
  395.   Summary:  A client put new text data into the shared COTextPage.
  396.  
  397.   Args:     void
  398.  
  399.   Returns:  HRESULT
  400.               Standard result code. NOERROR for success.
  401. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  402. STDMETHODIMP COTextPageSink::CImpITextPageSink::Put(
  403.                void)
  404. {
  405.   HRESULT hr = E_NOTIMPL;
  406.  
  407.   // For future evolution.
  408.  
  409.   return hr;
  410. }
  411.  
  412.  
  413. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  414.   Method:   COTextPageSink::CImpITextPageSink::Cleared
  415.  
  416.   Summary:  A client cleared all text data in the shared COTextPage.
  417.  
  418.   Args:     void
  419.  
  420.   Returns:  HRESULT
  421.               Standard result code. NOERROR for success.
  422. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  423. STDMETHODIMP COTextPageSink::CImpITextPageSink::Cleared(
  424.                void)
  425. {
  426.   HRESULT hr = E_NOTIMPL;
  427.  
  428.   // For future evolution.
  429.  
  430.   return hr;
  431. }
  432.