//=========================================================================
// CPAMCapt.cpp : MouseCapturer implementation. virtual class that all objects capturing
// mouse movement messages must derive from.
//
// Version			1.0
// Creation date 24/01/97
// Revision date
//
// (c) Ubi Studios 1997
//
// DO NOT MODIFY THAT FILE. IF SOMETHING NEEDS TO BE CHANGE, PLEASE CONTACT
// BENOIT GERMAIN.
//=========================================================================

#include "stdafx.h"

#ifdef ACTIVE_EDITOR

#include "itf\frmbsmn.hpp"
#include "itf\frmgest.hpp"

//================================================================================
//================================================================================
CPA_MouseCapturer::CPA_MouseCapturer() 
{ 
	m_p_oPreviousCapturer = m_p_oCurrentCapturer = NULL; 
	m_bDestruction = FALSE; 
}

//================================================================================
//================================================================================
CPA_MouseCapturer::~CPA_MouseCapturer() 
{ 
	m_bDestruction = TRUE; 
	m_p_oSurrenderCapture(); 
}

//================================================================================
//================================================================================
CWnd *CPA_MouseCapturer::m_p_oGrabCaptureFor(CWnd *pWnd)
{
	CWnd *p_oCurrentCapturer;

	ASSERT(pWnd);

	// check who has the capture
	p_oCurrentCapturer = pWnd->GetCapture();
	if ( p_oCurrentCapturer != pWnd ) // if it is not the same
	{
		// in case more than one consecutive grab occur...
		if ( !m_p_oPreviousCapturer )
			// remember who had the capture before the new capturer
			m_p_oPreviousCapturer = p_oCurrentCapturer;
		// remember who the new capturer is
		m_p_oCurrentCapturer = pWnd;
		// if both the current and new capturer are valid
		if ( m_p_oCurrentCapturer )
		{
			// release the capture of the current capturer
			if ( p_oCurrentCapturer )
				ReleaseCapture();
			// and grab it for the new capturer
			m_p_oCurrentCapturer->SetCapture();
		}
	}
	else //it is not allowed to grab consecutively for the same CWnd...
	{
		/*if ( g_oFrameGest.ma_p_oOccupyArray[2][2] )
		{
			((FRMBaseMenu *) g_oFrameGest.ma_p_oOccupyArray[2][2])->UpdateStatus(
				"INTERNAL: It is not allowed to grab consecutively for the same CWnd...",
				C_STATUSPANE_INFOS,
				C_STATUS_WARNING
			);
		}*/
	}
	// return the pointer to the capturer that will be surrendered capture
	return m_p_oPreviousCapturer;
}

//================================================================================
//================================================================================
CWnd *CPA_MouseCapturer::m_p_oSurrenderCapture(BOOL bCheckCoherence /*= TRUE*/)
{
	CWnd *pToReturn = m_p_oPreviousCapturer;
	CWnd *p_oInstantCapturer;

	// if there was a capturer before the one we established
	if ( m_p_oPreviousCapturer )
	{
		// check who has the capture right now
		p_oInstantCapturer = m_p_oPreviousCapturer->GetCapture();
		// if nobody took it from us
		if ( p_oInstantCapturer == m_p_oCurrentCapturer )
		{
			// we can release it
			ReleaseCapture();
			// and give it back to our predecessor
			m_p_oPreviousCapturer->SetCapture();
			// and remember it
			m_p_oCurrentCapturer = NULL;
			m_p_oPreviousCapturer = NULL;
		}
	}
	else if ( m_p_oCurrentCapturer ) // we were first to capture the mouse
	{
		// release the capture
		ReleaseCapture();
		// and remember than now nobody has the capture
		m_p_oCurrentCapturer = NULL;
	}
	else // somebody tried to make us surrender whereas we had not grabbed anything
	{
		// the only autorized circumstance is when the destructor calls us
		// while we had not grabbed anything
		/*if ( g_oFrameGest.ma_p_oOccupyArray[2][2] && ((!bCheckCoherence) || (m_bDestruction)) )
		{
			((FRMBaseMenu *) g_oFrameGest.ma_p_oOccupyArray[2][2])->UpdateStatus(
				"INTERNAL: It is not allowed to surrender consecutively for the same CWnd...",
				C_STATUSPANE_INFOS,
				C_STATUS_WARNING
			);
		}*/
	}
	return pToReturn;
}

#endif // ACTIVE_EDITOR

