home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Troubleshooting Netware Systems
/
CSTRIAL0196.BIN
/
attach
/
msj
/
v10n08
/
oleq0895.exe
/
ARB.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1995-08-01
|
3KB
|
110 lines
#include "stdafx.h"
#include "Arb.h"
const UINT
WM_ACTIVECHANGING = RegisterWindowMessage(__TEXT("WM_ACTIVECHANGING"));
Arbitrator::Arbitrator(REFCLSID rclsid, LPCTSTR szName)
: m_dwReg(0),
m_bIsRegistered(FALSE),
m_punk(0),
m_clsid(rclsid),
m_threadIds(szName)
{
}
Arbitrator::~Arbitrator(void)
{
RevokeObject(m_punk);
}
// internal function to broadcast change message
void
Arbitrator::PostChangeMessage(void)
{
PostMessage(HWND_BROADCAST,
WM_ACTIVECHANGING,
0, 0);
}
// called when UI code wants its object to become the active object
void
Arbitrator::RegisterObject(LPUNKNOWN punk)
{
// revoke current object if registered
if (m_bIsRegistered)
RevokeActiveObject(m_dwReg, 0);
// cache punk as current object
m_punk = punk;
m_bIsRegistered = FALSE;
// we are assuming that our thread is the foreground thread, so it
// is safe to push ourselves to the head of the array and actually register
if (m_threadIds.Insert(GetCurrentThreadId()))
m_bIsRegistered = SUCCEEDED(::RegisterActiveObject(m_punk,
m_clsid,
0,
&m_dwReg));
else
m_bIsRegistered = FALSE;
}
// called when UI code wants its object no longer be active
void
Arbitrator::RevokeObject(LPUNKNOWN punk)
{
// only revoke if punk is actually registered
if (m_bIsRegistered && m_punk == punk)
{
RevokeActiveObject(m_dwReg, 0);
m_bIsRegistered = FALSE;
m_punk = 0;
m_threadIds.Remove(GetCurrentThreadId());
PostChangeMessage();
}
}
// called when UI code when thread loses foreground status
void
Arbitrator::SuspendApp(void)
{
// broadcast that the activation state has changed
PostChangeMessage();
}
// called when UI code when thread gains foreground status
void
Arbitrator::ResumeApp(void)
{
// promote this thread to the head of the array and
// broadcast that the activation state has changed
m_threadIds.Insert(GetCurrentThreadId());
PostChangeMessage();
}
// called when UI code receives notification that activation status has changed
void
Arbitrator::ActiveChanging(void)
{
// if we are now the foreground thread, register ourselves
if (m_threadIds.IsTop(GetCurrentThreadId()))
{
if (!m_bIsRegistered && m_punk)
m_bIsRegistered = SUCCEEDED(RegisterActiveObject(m_punk,
m_clsid,
0,
&m_dwReg));
}
// if we are not the foreground thread, we need to revoke our object
// to make way for the new foreground thread
else if (m_bIsRegistered)
{
RevokeActiveObject(m_dwReg, 0);
m_bIsRegistered = FALSE;
}
}