//=========================================================================
// CPACont.cpp : implementation of the CPA_Contact class
// This is a part of the CPA project.
//
// Version			1.0
// Creation date	13/06/96
// Revision date
//
// (c) Ubi Studios 1996
//
// DO NOT MODIFY THAT FILE. IF SOMETHING NEEDS TO BE CHANGE, PLEASE CONTACT
// VINCENT GRECO OR CHRISTOPHE BEAUDET.
//=========================================================================

#include "stdafx.h"

#ifdef ACTIVE_EDITOR
#include "acp_base.h"
#include "itf/CPACont.Hpp"
#include "itf/CPARes.h"
#include "itf/DEVMulti.hpp"
#include "itf/ToolDLLb.hpp"
#include "itf/CPAMWorl.hpp"
#include "itf/CPAInter.hpp"

/////////////////////////////////////////////////////////////////////////////
// Local Macros
#define M_GetListOfToolDLLFromMsg()     M_GetMainWorld()->GetToolDLLReceivingWindowMsg()

#define M_ForEachToolDLL(p_oTool)                                       \
    CPA_ToolDLLBase *p_oTool;                                           \
    POSITION stPos;                                                     \
    for( p_oTool = M_GetListOfToolDLLFromMsg()->GetHeadElement(stPos) ; \
         p_oTool ;                                                      \
         p_oTool = M_GetListOfToolDLLFromMsg()->GetNextElement(stPos) )

/////////////////////////////////////////////////////////////////////////////
// CPA_Contact

IMPLEMENT_DYNCREATE(CPA_Contact, CObject)

/////////////////////////////////////////////////////////////////////////////
// CPA_Contact construction/destruction

CPA_Contact::CPA_Contact(DEV_MultiDevice *p_oDevice)
{
	m_p_oDevice = p_oDevice;
}

CPA_Contact::~CPA_Contact()
{
}

CPA_Interface * CPA_Contact::GetInterface (void)
{
	return m_p_oDevice->GetInterface();
}

//###########################################################################
// processing of messages coming from viewport
// Default implementation : send the message to the EvtEditor if it exists.
//###########################################################################

BOOL CPA_Contact::_OnCommand(UINT IDCmdMsg)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnCommand(IDCmdMsg) == TRUE)
				return TRUE;
		}
		// send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnCommand(IDCmdMsg);
        }
        // send message to EvtEditor
        return M_GetEditor()->_OnCommand(IDCmdMsg);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnUpdateCommandUI(CCmdUI *pCmdUI)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnUpdateCommandUI(pCmdUI->m_nID, pCmdUI) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
		M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnUpdateCommandUI(pCmdUI->m_nID, pCmdUI);
        }
		// send message to EvtEditor
        return M_GetEditor()->_OnUpdateCommandUI(pCmdUI->m_nID, pCmdUI);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnKeyDown(nChar, nRepCnt, nFlags) == TRUE)
				return TRUE;
		}

		// send message to DLL of selected object
		if ((M_GetCountSelected() == 1) && (M_GetSingleSelection()->GetObjectDLL() != NULL))
		{
			if (M_GetSingleSelection()->GetObjectDLL()->_OnKeyDown(nChar, nRepCnt, nFlags) == TRUE)
				return TRUE;
		}

        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnKeyDown(nChar, nRepCnt, nFlags);
        }
        // send it to EvtEditor
		return M_GetEditor()->_OnKeyDown(nChar, nRepCnt, nFlags);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnKeyUp(nChar, nRepCnt, nFlags) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnKeyUp(nChar, nRepCnt, nFlags);
        }
        // send it to EvtEditor
		return M_GetEditor()->_OnKeyUp(nChar, nRepCnt, nFlags);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnMouseMove(UINT nFlags, tdstMousePos *p_stPos, MTH3D_tdstVector *p_stObject)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
		// send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnMouseMove(nFlags, p_stPos, p_stObject) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnMouseMove(nFlags, p_stPos, p_stObject);
        }
        // send it to EvtEditor
		return M_GetEditor()->_OnMouseMove(nFlags, p_stPos, p_stObject);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnLButtonDblClk(UINT nFlags, tdstMousePos *p_stPos, ACP_tdxIndex xIndex, HIE_tdstPickInfo *p_stObject)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnLButtonDblClk(nFlags, p_stPos, xIndex, p_stObject) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnLButtonDblClk(nFlags, p_stPos, xIndex, p_stObject);
        }
        // send it to EvtEditor
		return M_GetEditor()->_OnLButtonDblClk(nFlags, p_stPos, xIndex, p_stObject);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnLButtonDown(UINT nFlags, tdstMousePos *p_stPos, ACP_tdxIndex xIndex, HIE_tdstPickInfo *p_stObject)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnLButtonDown(nFlags, p_stPos, xIndex, p_stObject) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnLButtonDown(nFlags, p_stPos, xIndex, p_stObject);
        }
        // send it to EvtEditor
		return M_GetEditor()->_OnLButtonDown(nFlags, p_stPos, xIndex, p_stObject);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnLButtonUp(UINT nFlags, tdstMousePos *p_stPos)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnLButtonUp(nFlags, p_stPos) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnLButtonUp(nFlags, p_stPos);
        }
        // send it to EvtEditor
		return M_GetEditor()->_OnLButtonUp(nFlags, p_stPos);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnRButtonDblClk(UINT nFlags, tdstMousePos *p_stPos, ACP_tdxIndex xIndex, HIE_tdstPickInfo *p_stObject)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnRButtonDblClk(nFlags, p_stPos, xIndex, p_stObject) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnRButtonDblClk(nFlags, p_stPos, xIndex, p_stObject);
        }
        // send it to EvtEditor
		return M_GetEditor()->_OnRButtonDblClk(nFlags, p_stPos, xIndex, p_stObject);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnRButtonUp(UINT nFlags, tdstMousePos *p_stPos)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnRButtonUp(nFlags, p_stPos) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnRButtonUp(nFlags, p_stPos);
        }
        // send it to EvtEditor
		return M_GetEditor()->_OnRButtonUp(nFlags, p_stPos);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnRButtonDown(UINT nFlags, tdstMousePos *p_stPos, ACP_tdxIndex xIndex, HIE_tdstPickInfo *p_stObject)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnRButtonDown(nFlags, p_stPos, xIndex, p_stObject) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnRButtonDown(nFlags, p_stPos, xIndex, p_stObject);
        }
        // send it to EvtEditor
		return M_GetEditor()->_OnRButtonDown(nFlags, p_stPos, xIndex, p_stObject);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnDragDropEnd(WPARAM wParam, LPARAM lParam)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnDragDropEnd(wParam, lParam) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnDragDropEnd(wParam, lParam);
        }
        // send it to EvtEditor
		return M_GetEditor()->_OnDragDropEnd(wParam, lParam);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnDragDropLooseFocus(WPARAM wParam, LPARAM lParam)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnDragDropLooseFocus(wParam, lParam) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnDragDropLooseFocus(wParam, lParam);
        }
        // send it to EvtEditor
		return M_GetEditor()->_OnDragDropLooseFocus(wParam, lParam);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnDragDropGainFocus(WPARAM wParam, LPARAM lParam)
{
	ASSERT (m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnDragDropGainFocus(wParam, lParam) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnDragDropGainFocus(wParam, lParam);
        }
        return M_GetEditor()->_OnDragDropGainFocus(wParam, lParam);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
BOOL CPA_Contact::_OnDragDropMove(WPARAM wParam, LPARAM lParam)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->_OnDragDropMove(wParam, lParam) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->_OnDragDropMove(wParam, lParam);
        }
        // send it to EvtEditor
		return M_GetEditor()->_OnDragDropMove(wParam, lParam);
    }

	return FALSE;
}

//---------------------------------------------------------------------------
void CPA_Contact::fn_vBeforeDrawingWorld(GLD_tdstViewportAttributes *p1, GLI_tdxHandleToLight p2, DEV_ViewPort* p3)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
			M_GetCurrentEditor()->fn_vBeforeDrawingWorld(p1,p2,p3);
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->fn_vBeforeDrawingWorld(p1,p2,p3);
        }
        // send it to EvtEditor
		M_GetEditor()->fn_vBeforeDrawingWorld(p1,p2,p3);
    }
}

//---------------------------------------------------------------------------
void CPA_Contact::fn_vAddObjectsToDraw(GLD_tdstViewportAttributes *p1, GLI_tdxHandleToLight p2, DEV_ViewPort* p3)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
			M_GetCurrentEditor()->fn_vAddObjectsToDraw(p1,p2,p3);
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->fn_vAddObjectsToDraw(p1,p2,p3);
        }
        // send it to EvtEditor
		M_GetEditor()->fn_vAddObjectsToDraw(p1,p2,p3);
    }
}

//---------------------------------------------------------------------------
void CPA_Contact::fn_vAddObjectsToDraw2(GLD_tdstViewportAttributes *p1, GLI_tdxHandleToLight p2)
{
	ASSERT(m_p_oDevice != NULL);

    if (GetInterface())
    {
        // send message to current editor
		if (!M_IsCurrentEditor())
			M_GetCurrentEditor()->fn_vAddObjectsToDraw2(p1,p2);
        // send message to Tool DLLs
        M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->fn_vAddObjectsToDraw2(p1,p2);
        }
        // send it to EvtEditor
		M_GetEditor()->fn_vAddObjectsToDraw2(p1,p2);
    }
}


//###########################################################################
// processing of messages coming from ACP
// Default implementation : nothing.
//###########################################################################

//---------------------------------------------------------------------------
BOOL CPA_Contact::fn_bPreTranslateMessage(MSG *p_stMsg)
{
    if (GetInterface())
    {
		// send message to current editor
		if (!M_IsCurrentEditor())
		{
			if (M_GetCurrentEditor()->fn_bPreTranslateMessage(p_stMsg) == TRUE)
				return TRUE;
		}
        // send message to Tool DLLs
		M_ForEachToolDLL(p_oToolDLL)
        {
            if (!p_oToolDLL->fn_bIsCurrentEditor())
				p_oToolDLL->fn_bPreTranslateMessage(p_stMsg);
        }
    }

	return FALSE;
}


#endif // ACTIVE_EDITOR