Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
4.3 KiB

  1. // Event sink for TriEdit/Trident
  2. // Copyright (c)1997-1999 Microsoft Corporation, All Rights Reserved
  3. // Van Kichline
  4. // Create an event sink impl, then immediately call SetOwner with, in this case,
  5. // a pointer to a CProxyFrame, implementing OnTriEditEvent.
  6. // It would be better to require an interface on the OnTriEditEvent when time allows.
  7. // The lifetime of the CProxyFrame is managed by the CTriEditEventSink, below.
  8. //
  9. // OnTriEditEvent will be called with the following DISPIDs for Document events:
  10. // DISPID_HTMLDOCUMENTEVENTS_ONHELP:
  11. // DISPID_HTMLDOCUMENTEVENTS_ONCLICK:
  12. // DISPID_HTMLDOCUMENTEVENTS_ONDBLCLICK:
  13. // DISPID_HTMLDOCUMENTEVENTS_ONKEYDOWN:
  14. // DISPID_HTMLDOCUMENTEVENTS_ONKEYUP:
  15. // DISPID_HTMLDOCUMENTEVENTS_ONKEYPRESS:
  16. // DISPID_HTMLDOCUMENTEVENTS_ONMOUSEMOVE:
  17. // DISPID_HTMLDOCUMENTEVENTS_ONMOUSEDOWN:
  18. // DISPID_HTMLDOCUMENTEVENTS_ONMOUSEUP:
  19. // DISPID_HTMLDOCUMENTEVENTS_ONREADYSTATECHANGE:
  20. //
  21. // Changes: more versital implementation allows creating sinks for various event interfaces.
  22. // The interface IID is sent along to OnTriEditEvent.
  23. //
  24. class ATL_NO_VTABLE CTriEditEventSinkImpl :
  25. public CComObjectRootEx<CComSingleThreadModel>,
  26. public IDispatch
  27. {
  28. public:
  29. BEGIN_COM_MAP(CTriEditEventSinkImpl)
  30. COM_INTERFACE_ENTRY(IDispatch)
  31. END_COM_MAP()
  32. CTriEditEventSinkImpl ()
  33. {
  34. m_pFR = NULL;
  35. }
  36. void SetOwner ( CProxyFrame* pFR, const GUID& iidEventInterface )
  37. {
  38. _ASSERTE ( pFR );
  39. _ASSERTE ( NULL == m_pFR );
  40. if ( NULL == m_pFR )
  41. {
  42. m_pFR = pFR;
  43. m_iidEventInterface = iidEventInterface;
  44. }
  45. }
  46. STDMETHOD(GetTypeInfoCount) ( UINT * )
  47. {
  48. _ASSERTE ( FALSE );
  49. return E_NOTIMPL;
  50. }
  51. STDMETHOD(GetTypeInfo) ( UINT, LCID, ITypeInfo ** )
  52. {
  53. _ASSERTE ( FALSE );
  54. return E_NOTIMPL;
  55. }
  56. STDMETHOD(GetIDsOfNames) ( REFIID, OLECHAR **, UINT, LCID, DISPID * )
  57. {
  58. _ASSERTE ( FALSE );
  59. return E_NOTIMPL;
  60. }
  61. STDMETHOD(Invoke) ( DISPID dispid, REFIID, LCID, USHORT, DISPPARAMS *, VARIANT *, EXCEPINFO *, UINT * )
  62. {
  63. HRESULT hr = E_UNEXPECTED;
  64. _ASSERTE ( m_pFR );
  65. if ( NULL != m_pFR )
  66. {
  67. hr = m_pFR->OnTriEditEvent ( m_iidEventInterface, dispid );
  68. }
  69. return hr;
  70. }
  71. private:
  72. CProxyFrame* m_pFR;
  73. GUID m_iidEventInterface;
  74. };
  75. // This class manages hooking up an event sink on Trident using CTriEditEventSinkImpl above.
  76. // The sink is advised and unadvised by this class, making management simple.
  77. // CProxyFrame must implement OnTriEditEvent.
  78. //
  79. class CTriEditEventSink
  80. {
  81. public:
  82. CTriEditEventSink ( CProxyFrame* pFR, GUID iidEventInterface ) :
  83. m_piConPt ( NULL ), m_dwCookie ( 0 ), m_pSink ( NULL ), m_iidEventInterface ( iidEventInterface )
  84. {
  85. _ASSERTE ( pFR );
  86. m_pFR = pFR;
  87. if ( NULL != m_pFR )
  88. {
  89. m_pFR->AddRef ();
  90. m_pSink = new CComObject<CTriEditEventSinkImpl>;
  91. _ASSERTE ( m_pSink );
  92. m_pSink->AddRef ();
  93. m_pSink->SetOwner ( m_pFR, m_iidEventInterface );
  94. m_bfSunk = FALSE;
  95. }
  96. }
  97. ~CTriEditEventSink ()
  98. {
  99. _ASSERTE ( m_pFR );
  100. Unadvise ();
  101. if ( NULL != m_pSink )
  102. {
  103. m_pSink->Release ();
  104. m_pSink = NULL;
  105. }
  106. if ( NULL != m_pFR )
  107. {
  108. m_pFR->Release ();
  109. m_pFR = NULL;
  110. }
  111. }
  112. HRESULT Advise ( IUnknown* pUnk )
  113. {
  114. HRESULT hr = E_FAIL;
  115. _ASSERTE ( pUnk );
  116. _ASSERTE ( m_pFR );
  117. _ASSERTE ( m_pSink );
  118. if ( NULL == pUnk )
  119. {
  120. _ASSERTE ( FALSE );
  121. return E_UNEXPECTED;
  122. }
  123. if ( m_bfSunk )
  124. {
  125. _ASSERTE ( FALSE );
  126. return E_UNEXPECTED;
  127. }
  128. CComQIPtr<IConnectionPointContainer, &IID_IConnectionPointContainer>picpc ( pUnk );
  129. if ( picpc )
  130. {
  131. hr = picpc->FindConnectionPoint ( m_iidEventInterface, &m_piConPt );
  132. if ( SUCCEEDED ( hr ) )
  133. {
  134. hr = m_piConPt->Advise ( static_cast<IDispatch *>(m_pSink), &m_dwCookie);
  135. _ASSERTE ( SUCCEEDED ( hr ) );
  136. if ( SUCCEEDED ( hr ) )
  137. {
  138. m_bfSunk = TRUE;
  139. }
  140. }
  141. }
  142. return hr;
  143. }
  144. void Unadvise ()
  145. {
  146. if ( !m_bfSunk )
  147. {
  148. return;
  149. }
  150. if ( ( NULL != m_pSink ) && ( NULL != m_piConPt ) )
  151. {
  152. m_piConPt->Unadvise ( m_dwCookie );
  153. m_bfSunk = FALSE;
  154. }
  155. if ( NULL != m_piConPt )
  156. {
  157. m_piConPt->Release ();
  158. m_piConPt = NULL;
  159. }
  160. }
  161. private:
  162. CTriEditEventSinkImpl* m_pSink;
  163. IConnectionPoint* m_piConPt;
  164. DWORD m_dwCookie;
  165. CProxyFrame* m_pFR;
  166. BOOL m_bfSunk;
  167. GUID m_iidEventInterface;
  168. };