home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n08 / oleq0895.exe / ARB.CPP < prev    next >
C/C++ Source or Header  |  1995-08-01  |  3KB  |  110 lines

  1. #include "stdafx.h"
  2. #include "Arb.h"
  3.  
  4. const UINT 
  5. WM_ACTIVECHANGING = RegisterWindowMessage(__TEXT("WM_ACTIVECHANGING"));
  6.  
  7. Arbitrator::Arbitrator(REFCLSID rclsid, LPCTSTR szName)
  8. :    m_dwReg(0),
  9.     m_bIsRegistered(FALSE),
  10.     m_punk(0),
  11.     m_clsid(rclsid),
  12.   m_threadIds(szName)
  13. {
  14. }
  15.  
  16. Arbitrator::~Arbitrator(void)
  17. {
  18.     RevokeObject(m_punk);
  19. }
  20.  
  21. // internal function to broadcast change message
  22. void 
  23. Arbitrator::PostChangeMessage(void)
  24. {
  25.     PostMessage(HWND_BROADCAST,
  26.                             WM_ACTIVECHANGING,
  27.                             0, 0);
  28. }
  29.  
  30.  
  31. // called when UI code wants its object to become the active object
  32. void 
  33. Arbitrator::RegisterObject(LPUNKNOWN punk)
  34. {
  35. // revoke current object if registered
  36.     if (m_bIsRegistered)
  37.         RevokeActiveObject(m_dwReg, 0);
  38.  
  39. // cache punk as current object
  40.     m_punk = punk;
  41.     m_bIsRegistered = FALSE;
  42.  
  43. // we are assuming that our thread is the foreground thread, so it
  44. // is safe to push ourselves to the head of the array and actually register
  45.  
  46.     if (m_threadIds.Insert(GetCurrentThreadId()))
  47.         m_bIsRegistered = SUCCEEDED(::RegisterActiveObject(m_punk,
  48.                                                                                                  m_clsid,
  49.                                                                                                  0,
  50.                                                                                                  &m_dwReg));
  51.     else
  52.         m_bIsRegistered = FALSE;                                                                                         
  53. }
  54.  
  55. // called when UI code wants its object no longer be active
  56. void 
  57. Arbitrator::RevokeObject(LPUNKNOWN punk)
  58. {
  59. // only revoke if punk is actually registered
  60.     if (m_bIsRegistered && m_punk == punk) 
  61.         {
  62.             RevokeActiveObject(m_dwReg, 0);
  63.             m_bIsRegistered = FALSE;
  64.             m_punk = 0;
  65.             m_threadIds.Remove(GetCurrentThreadId());
  66.             PostChangeMessage();
  67.         }
  68. }
  69.  
  70. // called when UI code when thread loses foreground status
  71. void 
  72. Arbitrator::SuspendApp(void)
  73. {
  74. // broadcast that the activation state has changed
  75.     PostChangeMessage();
  76. }
  77.  
  78. // called when UI code when thread gains foreground status
  79. void 
  80. Arbitrator::ResumeApp(void)
  81. {
  82. // promote this thread to the head of the array and
  83. // broadcast that the activation state has changed
  84.  
  85.     m_threadIds.Insert(GetCurrentThreadId());
  86.     PostChangeMessage();
  87. }
  88.  
  89. // called when UI code receives notification that activation status has changed
  90. void 
  91. Arbitrator::ActiveChanging(void)
  92. {
  93. // if we are now the foreground thread, register ourselves
  94.     if (m_threadIds.IsTop(GetCurrentThreadId()))
  95.     {
  96.           if (!m_bIsRegistered && m_punk)
  97.               m_bIsRegistered = SUCCEEDED(RegisterActiveObject(m_punk,
  98.                                                                                                              m_clsid,
  99.                                                                                                              0,
  100.                                                                                                              &m_dwReg));
  101.     }
  102. // if we are not the foreground thread, we need to revoke our object
  103. // to make way for the new foreground thread
  104.     else if (m_bIsRegistered)
  105.         {
  106.             RevokeActiveObject(m_dwReg, 0);
  107.             m_bIsRegistered = FALSE;
  108.         }
  109. }
  110.