home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Control Common / CConnectionPoint.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-14  |  4.2 KB  |  196 lines  |  [TEXT/CWIE]

  1. #include "ocHeaders.h"
  2. #include <LArray.h>
  3. #include "CEnumConnections.h"
  4. #include "CConnectionPoint.h"
  5.  
  6. #define APPENDITEM 32000    // Large number to force appending to an array
  7.  
  8. //
  9. //  CConnectionPoint::CConnectionPoint
  10. //
  11. CConnectionPoint::CConnectionPoint(void* Owner, IID InterfaceID)
  12. {
  13.     m_Owner = (IUnknown*) Owner;
  14.     m_InterfaceID = InterfaceID;
  15.     m_CookieNext = 100;
  16.     
  17.     m_IUnknown = new LArray(sizeof(IUnknown*));
  18.     m_Cookies = new LArray(sizeof(DWORD));
  19. }
  20.  
  21.  
  22. //
  23. //  CConnectionPoint::~CConnectionPoint
  24. //
  25. CConnectionPoint::~CConnectionPoint(void)
  26. {
  27.     delete m_IUnknown;
  28.     delete m_Cookies;
  29. }
  30.  
  31.  
  32. //
  33. //  CConnectionPoint::IUnknown::QueryInterface
  34. //
  35. //  Returns a pointer to the specified interface on a component to which a
  36. //  client currently holds an interface pointer.
  37. //
  38. STDMETHODIMP
  39. CConnectionPoint::QueryInterface(REFIID RefID, void** Obj)
  40. {
  41.     void* pv;
  42.  
  43.     if (RefID == IID_IUnknown)
  44.         pv = (void*) this;
  45.     else if (RefID == IID_IConnectionPoint )
  46.         pv = (void*)(IConnectionPoint*) this;
  47.     else {
  48.         *Obj = NULL;
  49.         return ResultFromScode(E_NOINTERFACE);
  50.     }
  51.  
  52.     *Obj = pv;
  53.     ((IUnknown*) pv)->AddRef();
  54.     return ResultFromScode(S_OK);
  55. }
  56.  
  57.  
  58. //
  59. //  CConnectionPoint::IUnknown::AddRef
  60. //
  61. //  Increments the reference count for the calling interface.
  62. //
  63. STDMETHODIMP_(ULONG)
  64. CConnectionPoint::AddRef(void)
  65. {
  66.     return ++m_RefCount;
  67. }
  68.  
  69.  
  70. //
  71. //  CConnectionPoint::IUnknown::Release
  72. //
  73. //  Decrements the reference count for the calling interface on a object.  If
  74. //  the reference count on the object falls to zero, the object is freed.
  75. //
  76. STDMETHODIMP_(ULONG)
  77. CConnectionPoint::Release(void)
  78. {
  79.     if (--m_RefCount != 0)
  80.         return m_RefCount;
  81.  
  82.     delete this;
  83.     return 0;
  84. }
  85.  
  86.  
  87.  
  88. //
  89. //  CConnectionPoint::IConnectionPoint::GetConnectionInterface
  90. //
  91. STDMETHODIMP
  92. CConnectionPoint::GetConnectionInterface(IID* InterfaceID)
  93. {
  94.     *InterfaceID = m_InterfaceID;
  95.     
  96.     return ResultFromScode(S_OK);
  97. }
  98.  
  99.  
  100. //
  101. //  CConnectionPoint::IConnectionPoint::GetConnectionPointContainer
  102. //
  103. STDMETHODIMP
  104. CConnectionPoint::GetConnectionPointContainer(IConnectionPointContainer** CPContainer)
  105. {
  106.     return m_Owner->QueryInterface(IID_IConnectionPointContainer, (void**) CPContainer);
  107. }
  108.  
  109.  
  110. //
  111. //  CConnectionPoint::IConnectionPoint::Advise
  112. //
  113. STDMETHODIMP
  114. CConnectionPoint::Advise(IUnknown* UnknownSink, DWORD* Cookie)
  115. {
  116.     IUnknown*    Sink;
  117.     
  118.     // Initialize the cookie
  119.     *Cookie = 0;
  120.     
  121.     // Get the interface this connection point wants from the UnknownSink
  122.     if ( FAILED(UnknownSink->QueryInterface(m_InterfaceID, (void**) &Sink)))
  123.         return ResultFromScode(CONNECT_E_CANNOTCONNECT);
  124.     
  125.     // Append the new sink and cookie    
  126.     m_IUnknown->InsertItemsAt(1, APPENDITEM, &Sink);
  127.     m_Cookies->InsertItemsAt(1, APPENDITEM, &m_CookieNext);
  128.     
  129.     // Set the return cookie to the one we assigned
  130.     *Cookie = m_CookieNext++;
  131.     
  132.     return ResultFromScode(S_OK);
  133. }
  134.  
  135.  
  136. //
  137. //  CConnectionPoint::IConnectionPoint::Unadvise
  138. //
  139. STDMETHODIMP
  140. CConnectionPoint::Unadvise(DWORD Cookie)
  141. {
  142.     ArrayIndexT    i;
  143.     ErrorCode    Result = S_OK;
  144.  
  145.     if ((i = m_Cookies->FetchIndexOf(&Cookie)) == LArray::index_Bad)
  146.         Result = E_FAIL;
  147.     else
  148.     {
  149.         IUnknown*    Sink;
  150.         m_IUnknown->FetchItemAt(i, &Sink);
  151.         m_IUnknown->RemoveItemsAt(1, i);
  152.         m_Cookies->RemoveItemsAt(1, i);
  153.         Sink->Release();
  154.     }
  155.  
  156.     return Result;
  157. }
  158.  
  159.  
  160. //
  161. //  CConnectionPoint::IConnectionPoint::EnumConnections
  162. //
  163. STDMETHODIMP
  164. CConnectionPoint::EnumConnections(IEnumConnections** EnumConnections)
  165. {
  166.     LArray*             ConnectArray;
  167.     CONNECTDATA            ConnectData;
  168.     unsigned long         i, NumConnected;
  169.     CEnumConnections*    EnumObject;
  170.     
  171.     // No items connected, return failure
  172.     if ( (NumConnected = m_IUnknown->GetCount()) == 0 )
  173.         return ResultFromScode(E_FAIL);
  174.     
  175.     // Allocate an array for the CONNECTDATA 
  176.     ConnectArray = new LArray(sizeof(CONNECTDATA));
  177.     
  178.     // Move all the pertinent information from our list of IUnknowns and cookies
  179.     for ( i = 1; i <= NumConnected; i++)
  180.     {
  181.         m_IUnknown->FetchItemAt(i, &ConnectData.pUnk);
  182.         m_Cookies->FetchItemAt(i, &ConnectData.dwCookie);
  183.         ConnectArray->InsertItemsAt(1, i, &ConnectData);
  184.     }
  185.     
  186.     // Create a new enumerator object
  187.     EnumObject = new CEnumConnections(ConnectArray);
  188.     
  189.     // Get the IEnumConnections interface pointer
  190.     EnumObject->QueryInterface(IID_IEnumConnections, (void**) EnumConnections);
  191.     
  192.     return ResultFromScode(S_OK);
  193. }
  194.  
  195.  
  196.