/*=========================================================================
 * DEVViewp.cpp :	Implementation of Viewport.
 * This is a part of the PCA project.
 *
 * Version			1.0
 * Creation date	26/06/96
 * Revision date
 *
 *
 * (c) Ubi Studios 1996
 *=======================================================================*/

#include "stdafx.h"
#include "acp_base.h"
#include "itf/customid.h"
#include "itf/CPARes.h"
#include "itf/DEVViewp.hpp"
#include "itf/DEVMulti.hpp"
#include "itf/CPACont.hpp"
#include "itf/CPAProj.hpp"

#define M_DegToRad(A) ((A)*0.01745329)

//=========================================================================
//=========================================================================
DEV_ViewPort::DEV_ViewPort()
{
}

DEV_ViewPort::~DEV_ViewPort()
{
}

//=========================================================================
//=========================================================================
DEV_MultiDevice * DEV_ViewPort::GetParentMultiDevice()
{
	return (DEV_MultiDevice *) GetDevice()->GetWindowParent();
}
//=========================================================================
DEV_MultiDevice2D * DEV_ViewPort::GetParentMultiDevice2D()
{
	return (DEV_MultiDevice2D *) GetDevice()->GetWindowParent();
}
//=========================================================================
DEV_MultiDevice3D * DEV_ViewPort::GetParentMultiDevice3D()
{
	return (DEV_MultiDevice3D *) GetDevice()->GetWindowParent();
}
//=========================================================================
#ifdef ACTIVE_EDITOR
CPA_Contact	* DEV_ViewPort::GetParentContact()
{
	return GetParentMultiDevice()->GetContact();
}
#endif

//=========================================================================
#ifdef ACTIVE_EDITOR
FRMBaseMenu * DEV_ViewPort::GetParentFrame()
{
	return GetParentMultiDevice()->GetParentFrame();
}
#else
CFrameWnd *DEV_ViewPort::GetParentFrame()
{
	return GetParentMultiDevice()->GetParentFrame();
}
#endif

#ifdef ACTIVE_EDITOR
//=========================================================================
CPA_DialogBar *	DEV_ViewPort::GetParentDialogBar()
{

#ifdef ACTIVE_EDITOR
	if(!GetParentMultiDevice()->GetParent()->IsKindOf(RUNTIME_CLASS(FRMSplitter)))
	{
		return NULL;
	}
#endif

	return &GetParentFrame()->m_oGeneralDialogBar;
}
#endif
//=========================================================================
//=========================================================================
BEGIN_MESSAGE_MAP(DEV_ViewPort, CWnd)
	//{{AFX_MSG_MAP(DEV_ViewPort)
	ON_WM_WINDOWPOSCHANGING()
	ON_WM_PAINT()
#ifdef ACTIVE_EDITOR
    ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_RBUTTONDBLCLK()
	ON_WM_MOUSEMOVE()
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
    ON_UPDATE_COMMAND_UI_RANGE(C_CustomIDStart, C_EDTPopUpMenuIDEnd, OnUpdateCommandUIRange)
//    ON_COMMAND_RANGE(C_CustomIDUserStart, C_CustomIDUserEnd, OnCommandRange)
#endif // ACTIVE_EDITOR
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//=========================================================================
//=========================================================================
void DEV_ViewPort::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
	CWnd::OnWindowPosChanging(lpwndpos);

	lpwndpos->flags = SWP_NOZORDER | SWP_NOMOVE;
}

/////////////////////////////////////////////////////////////////////////////
//
// TO TRANSMIT MESSAGES TO CONTACT
//
/////////////////////////////////////////////////////////////////////////////

#ifdef ACTIVE_EDITOR
//---------------------------------------------------------------------------
int DEV_ViewPort::OnCreate( LPCREATESTRUCT lpCS)
{
    int iResult = CWnd::OnCreate(lpCS);
    m_p_oDevice = (DEV_Device*)GetParent();
    return iResult;
}
//---------------------------------------------------------------------------
void DEV_ViewPort::OnLButtonDown(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	if(GetParentContact())
		GetParentContact()->_OnLButtonDown(nFlags, NULL, 0, NULL);
}
//---------------------------------------------------------------------------
void DEV_ViewPort::OnLButtonUp(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	if(GetParentContact())
		GetParentContact()->_OnLButtonUp(nFlags, NULL);
}
//---------------------------------------------------------------------------
void DEV_ViewPort::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	if(GetParentContact())
		GetParentContact()->_OnLButtonDblClk(nFlags, NULL, 0, NULL);
}
//---------------------------------------------------------------------------
void DEV_ViewPort::OnRButtonDown(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	if(GetParentContact())
		GetParentContact()->_OnRButtonDown(nFlags, NULL, 0, NULL);
}
//---------------------------------------------------------------------------
void DEV_ViewPort::OnRButtonUp(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	if(GetParentContact())
		GetParentContact()->_OnRButtonUp(nFlags, NULL);
}
//---------------------------------------------------------------------------
void DEV_ViewPort::OnRButtonDblClk(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	if(GetParentContact())
		GetParentContact()->_OnRButtonDblClk(nFlags, NULL, 0, NULL);
}
//---------------------------------------------------------------------------
void DEV_ViewPort::OnMouseMove(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	if(GetParentContact())
		GetParentContact()->_OnMouseMove(nFlags, NULL, NULL);
}
//---------------------------------------------------------------------------
void DEV_ViewPort::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	if(GetParentContact())
		GetParentContact()->_OnKeyDown(nChar, nRepCnt, nFlags);
}
//---------------------------------------------------------------------------
void DEV_ViewPort::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	if(GetParentContact())
		GetParentContact()->_OnKeyUp(nChar, nRepCnt, nFlags);
}

//---------------------------------------------------------------------------
BOOL DEV_ViewPort::OnCommand(WPARAM wParam, LPARAM lParam)
{
	UINT IDCmdMsg = LOWORD(wParam);

	if(GetParentContact())
	{
		if(GetParentContact()->_OnCommand(IDCmdMsg))
			return TRUE;
	}

    return CWnd::OnCommand(wParam,lParam);
}

//---------------------------------------------------------------------------
/*
void DEV_ViewPort::OnCommandRange(UINT uiId)
{
	if(GetParentContact())
		GetParentContact()->_OnCommand(uiId);
}
*/
//---------------------------------------------------------------------------
void DEV_ViewPort::OnUpdateCommandUIRange(CCmdUI *pCmdUI)
{
	if(GetParentContact())
		GetParentContact()->_OnUpdateCommandUI(pCmdUI);
}
#endif // ACTIVE_EDITOR

//---------------------------------------------------------------------------
void DEV_ViewPort::OnPaint()
{
	CWnd::OnPaint();
	
	if (GLI_lWhatIsGLI() == GLI_C_VersionGlide)
	{
		
		/////////////////////////////////////////////////////
		// First paint of a view : We run engine thread.
		// We are then sure that all draw surface have been
		// initialised.
		static  char s_cFirstPaint = 0;
		
		if((s_cFirstPaint == 0) && (M_GetMainApp()->m_p_oEngineThread))
		{
			VERIFY(M_GetMainApp()->m_p_oEngineThread->ResumeThread() <= 1);
			s_cFirstPaint = 1;
		}
	}
}
