/*=========================================================================
* DEVMulti.cpp :       Implementation of Multidevice.
* 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 "geo.h"
#include "gli.h"
#include "itf/CPARes.h"
#include "itf/DEVMulti.hpp"
#include "itf/CPACont.hpp"
#include "itf/FRMBsMn.hpp"
#include "itf/CPAProj.hpp"
#include "itf/stddllid.h"
#include "itf/camdllb.hpp"
#include "itf/cpainter.hpp"
#include "itf/DevMul3D.hpp"
#include "itf/CPAMWorl.hpp"

//------------------------------------------------------------------
// Array of all the button-id possible for the Device ToolBar.
#ifdef ACTIVE_EDITOR
UINT pButtonsDevice[] =
{
	CPA_IDB_OPENDEVICE,
    CPA_IDB_DEVICE_TILE_HORZ,
    CPA_IDB_DEVICE_TILE_VERT,
    CPA_IDB_DEVICE_CASCADE,
};
#define nbButtonsDevice 4
//------------------------------------------------------------------
#endif // ACTIVE_EDITOR


IMPLEMENT_DYNCREATE(DEV_MultiDevice, CView)

BEGIN_MESSAGE_MAP(DEV_MultiDevice, CView)
//{{AFX_MSG_MAP(DEV_MultiDevice)
    ON_WM_WINDOWPOSCHANGED()
    ON_WM_CREATE()
    ON_WM_DESTROY()
#ifdef ACTIVE_EDITOR
    ON_MESSAGE(CPA_MSG_DRAWOBJECT, OnDrawObject)
    ON_MESSAGE(CPA_MSG_DRAWOBJECTEXCEPT0, OnDrawObjectExcept0)
#endif // ACTIVE_EDITOR
//}}AFX_MSG_MAP
END_MESSAGE_MAP()



//--------------------------------------------------------------------------------
DEV_MultiDevice::DEV_MultiDevice()
{
	mFlags = 0;
#ifdef ACTIVE_EDITOR
	m_p_oContact = NULL;
	m_p_oInterface = NULL;
    m_p_oParent = NULL;
	m_bActive = FALSE;
#endif // ACTIVE_EDITOR
}

DEV_MultiDevice::DEV_MultiDevice(int flags)
{
	mFlags = (short) flags;
#ifdef ACTIVE_EDITOR
	m_p_oContact = NULL;
	m_p_oInterface = NULL;
	m_p_oEditManager = NULL;
	m_bActive = FALSE;
#endif // ACTIVE_EDITOR
}

DEV_MultiDevice::~DEV_MultiDevice()
{
#ifdef ACTIVE_EDITOR
    if(m_cIsAdditional == 0)
	{
    	delete m_p_oContact;
		delete m_p_oEditManager;
	}
	m_p_oContact = NULL;
#endif // ACTIVE_EDITOR
}

void DEV_MultiDevice::Init()
{
#ifdef ACTIVE_EDITOR
	int i;

	for (i = 0; i < MAX_DEVICE; i++)
		SetActivDevice(i,FALSE);    // Definition of default active device
	m_lFocusDevice = -1;              // To signal that there no active device
	m_lFocusPane = 0;	
	m_p_oContact = NULL;            // No contact
    m_p_oMultiToDraw = NULL;
    m_cIsAdditional = 0;
	m_p_oEditManager = new CPA_EditManager;
	// register new multidevice
	M_GetListOfAllDLL()->RegisterNewMultiDevice(this);
#else
	mActivDeviceAlone = FALSE;
#endif // ACTIVE_EDITOR
	mbOnDestroy = FALSE;
}

DEV_Device* DEV_MultiDevice::OpenNewDevice(int _iLeft, int _iTop, int _iWidth, int _iHeight)
{
    DEV_Device *p_oResult = NULL;

#ifdef ACTIVE_EDITOR
    if(!TestFlags(MD_ONEDEVICE))
	{
		long lNum;

		lNum = GetNextNumDevice();
		if (SetDevice(&mDevice[lNum], lNum))
		{
            // update pos & size
            if( _iLeft   <0 ) _iLeft   = 10+lNum*20;
            if( _iTop    <0 ) _iTop    = 30+lNum*20;
            if( _iWidth  <0 ) _iWidth  = 300;
            if( _iHeight <0 ) _iHeight = 200;
            // create device
			if (TestFlags(MD_VIEW2D))
				GetDevice(lNum)->CreateDevice(this, _iLeft, _iTop, _iWidth, _iHeight, C_DEVICE2D);
			else
				GetDevice(lNum)->CreateDevice(this, _iLeft, _iTop, _iWidth, _iHeight, C_DEVICE3D);
            p_oResult = GetDevice(lNum);
		}
	}
#endif // ACTIVE_EDITOR

    return p_oResult;
}

void DEV_MultiDevice::CloseAllDevices()
{
#ifdef ACTIVE_EDITOR
    OnCloseAllDevice();
#endif // ACTIVE_EDITOR
}

#ifdef ACTIVE_EDITOR
void DEV_MultiDevice::SetContact(CPA_Contact *p_oContact)
{
	// if it's the main multidevice, ok
	if (m_cIsAdditional == 0)
	{
		if (m_p_oContact != NULL)
			m_p_oContact->SetDevice(NULL);
		
		m_p_oContact = p_oContact;
		
		if (p_oContact != NULL)
			p_oContact->SetDevice(this);
	}
	// else, set the main multidevice interface 
	else
		GetPane_0_0()->SetContact(p_oContact);
}

void DEV_MultiDevice::SetInterface(CPA_Interface *p_oInterface)
{
	// if it's the main multidevice, ok
	if (m_cIsAdditional == 0)
		m_p_oInterface = p_oInterface;
	// else, set the main multidevice interface 
	else
		GetPane_0_0()->SetInterface(p_oInterface);
}
void DEV_MultiDevice::SetEditManager(CPA_EditManager *p_oMng)
{
	// if it's the main multidevice, ok
	if (m_cIsAdditional == 0)
		m_p_oEditManager = p_oMng;
	// else, set the main multidevice interface 
	else
		GetPane_0_0()->SetEditManager(p_oMng);
}


void DEV_MultiDevice::SetFocusPane(long lPane)
{
	// if it's the main multidevice, ok
	if (m_cIsAdditional == 0)
		m_lFocusPane = lPane;
	// else, set the main multidevice interface 
	else
		GetPane_0_0()->SetFocusPane(lPane);
}

CPA_Interface *DEV_MultiDevice::GetInterface()
{
	// if it's the main multidevice, ok
	if (m_cIsAdditional == 0)
		return m_p_oInterface;
	// else, return the main multidevice interface 
	return GetPane_0_0()->GetInterface();
}
CPA_Contact *DEV_MultiDevice::GetContact()
{
	// if it's the main multidevice, ok
	if (m_cIsAdditional == 0)
		return m_p_oContact;
	// else, return the main multidevice interface 
	return GetPane_0_0()->GetContact();
}
CPA_EditManager *DEV_MultiDevice::GetEditManager()
{
	// if it's the main multidevice, ok
	if (m_cIsAdditional == 0)
		return m_p_oEditManager;
	// else, return the main multidevice interface 
	return GetPane_0_0()->GetEditManager();
}


DEV_MultiDevice3D *DEV_MultiDevice::GetPane_0_0()
{
	DEV_MultiDevice3D *p_oResult;

	ASSERT(m_cIsAdditional != 0);
	p_oResult = (DEV_MultiDevice3D *) ( ((FRMSplitter*)GetParent())->GetPane(0, 0));
	return p_oResult;
}




#endif // ACTIVE_EDITOR

//--------------------------------------------------------------------------------
BOOL DEV_MultiDevice::PreCreateWindow(CREATESTRUCT& cs)
{
	cs.style |= WS_CLIPCHILDREN;
	return CView::PreCreateWindow(cs);
}

//--------------------------------------------------------------------------------
void DEV_MultiDevice::OnDraw(CDC*)
{
}

//--------------------------------------------------------------------------------
//      GET-FUNCTIONS
//--------------------------------------------------------------------------------

//===========================================================================
//      Description:    Return the device number num.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
DEV_Device* DEV_MultiDevice::GetDevice(int num)
{
#ifdef ACTIVE_EDITOR
	return mpDevice[num];
#else
	return &mDeviceAlone;
#endif // ACTIVE_EDITOR
}




//===========================================================================
//      Description:    Return the device which the have the focus.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
DEV_Device* DEV_MultiDevice::GetFocusDevice2()
{
#ifdef ACTIVE_EDITOR

	if(m_lFocusDevice == -1)
		return NULL;
	else
		return mpDevice[m_lFocusDevice];
#else
	return &mDeviceAlone;
#endif // ACTIVE_EDITOR
}


//===========================================================================
//      Description:    Return the multidevice (in the splitter) which have the focus.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
DEV_Device* DEV_MultiDevice::GetFocusDevice()
{
#ifdef ACTIVE_EDITOR
	if (m_cIsAdditional == 0)
	{
		if (m_lFocusPane == 0)
			return GetFocusDevice2();
		
		else if (m_lFocusPane == 1)
			return ((DEV_MultiDevice *) ( ((FRMSplitter*)GetParent())->GetPane(0, 1)))->GetFocusDevice2();
			 
		else if (m_lFocusPane == 2)
			return ((DEV_MultiDevice *) ( ((FRMSplitter*)GetParent())->GetPane(1, 0)))->GetFocusDevice2();

		else if (m_lFocusPane == 3)
			return ((DEV_MultiDevice *) ( ((FRMSplitter*)GetParent())->GetPane(1, 1)))->GetFocusDevice2();
		else return NULL;
	}
	else
		return GetPane_0_0()->GetFocusDevice();
#else
	return GetFocusDevice2();
#endif
}


#ifdef ACTIVE_EDITOR
//===========================================================================
//      Description:    Return the number of first disactivate device.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
UINT DEV_MultiDevice::GetNextNumDevice()
{
	UINT i = 0;
	// Find first Device not active.
	while ((i < MAX_DEVICE) && IsActivDevice(i)) i++;
	return i;
}
#endif // ACTIVE_EDITOR

//--------------------------------------------------------------------------------
//      SET-FUNCTIONS
//--------------------------------------------------------------------------------

//===========================================================================
//      Description:    Set the background color of all devices of the MultiDevice
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
void DEV_MultiDevice::SetBackgroundColor(COLORREF Color)
{
#ifdef ACTIVE_EDITOR
	UINT i;
	for (i = 0; i < MAX_DEVICE; i++)
		if (IsActivDevice(i))
		GetDevice(i)->SetBackgroundColor(Color);
#else
	if (mActivDeviceAlone)
		mDeviceAlone.SetBackgroundColor(Color);
#endif // ACTIVE_EDITOR
}

//===========================================================================
//      Description:    Set the background color of all devices of the MultiDevice
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
void DEV_MultiDevice::SetBackgroundColor(char Red, char Green, char Blue)
{
#ifdef ACTIVE_EDITOR
	UINT i;
	for (i = 0; i < MAX_DEVICE; i++)
		if (IsActivDevice(i))
		GetDevice(i)->SetBackgroundColor(Red,Green,Blue);
#else
	if (mActivDeviceAlone)
		mDeviceAlone.SetBackgroundColor(Red,Green,Blue);
#endif // ACTIVE_EDITOR
}

//===========================================================================
//      Description:    Get the background color of the active Device
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
void DEV_MultiDevice::GetBackgroundColor(char *Red, char *Green, char *Blue)
{
#ifdef ACTIVE_EDITOR
		GetFocusDevice()->GetBackgroundColor(Red,Green,Blue);
#else
	if (mActivDeviceAlone)
		mDeviceAlone.GetBackgroundColor(Red,Green,Blue);
#endif // ACTIVE_EDITOR
}


#ifdef ACTIVE_EDITOR
//===========================================================================
//      Description:    Set the Device of number num by the value pDevice.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
BOOL DEV_MultiDevice::SetDevice(DEV_Device *pDevice, UINT num)
{
	if (num == MAX_DEVICE)
	{
		MessageBox("No more Device");
		return FALSE;
	}
	mpDevice[num] = pDevice;
	mpDevice[num]->SetNumber(num);
	return TRUE;
}

//===========================================================================
//      Description:    Set the focus to the device number num.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
void DEV_MultiDevice::UpdateColorTitle(void)
{
	UINT i;
	for(i = 0; i < MAX_DEVICE; i++)
        if(IsActivDevice(i))
		    GetDevice(i)->DrawDeviceTitle();
}

void DEV_MultiDevice::SetFocusDevice(int num)
{
    long                lLast = m_lFocusDevice;
    DEV_MultiDevice3D   *pSingle;
    FRMSplitter         *pSplit;

	if((num != -1) && (!mbOnDestroy))
	{
		((FRMSplitter *) GetParent())->SetActivePane(0, 0, this);
	    m_lFocusDevice = num;
        GetDevice(num)->SetWindowPos(&wndTop,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);

       //GetDevice(num)->GetParentFrame()->MoveWindow(10,10,500,690,TRUE);
       //GetDevice(num)->MoveWindow(10,10,500,690,TRUE);
       // GetDevice(num)->SetWindowPos(&wndTop,0,0,500,500,0); //chbani
        GetDevice(num)->GetViewPort()->SetFocus();
	}

	// refresh if we are ine fullscreen editor
	if ( (num != -1) && !M_bFullScreenEditor())
	{
		// No refresh if we are in full screen
		if(((DEV_MultiDevice3D *)M_GetMainWnd()->m_oSplitter.GetPane(0, 0))->GetFocusDevice()->IsFullScreen())
			return;
	}

    // Refresh the titles
    UpdateColorTitle();
    if ( (num != -1) && (GetParent()->IsKindOf(RUNTIME_CLASS(FRMSplitter))) )
    {
        // Update color title of all devices of all splitter panes.
        pSplit = (FRMSplitter *) GetParent();
        pSingle = (DEV_MultiDevice3D *) pSplit->GetPane(0, 0);
        if(pSingle != this)
            pSingle->UpdateColorTitle();
		else
			SetFocusPane(0);
        if(pSplit->GetColumnCount() == 2)
        {
            pSingle = (DEV_MultiDevice3D *) pSplit->GetPane(0, 1);
            if(pSingle != this)
                pSingle->UpdateColorTitle();
			else
				SetFocusPane(1);
        }
        if(pSplit->GetRowCount() == 2)
        {
            pSingle = (DEV_MultiDevice3D *) pSplit->GetPane(1, 0);
            if(pSingle != this)
                pSingle->UpdateColorTitle();
			else
				SetFocusPane(2);

            if(pSplit->GetColumnCount() == 2)
            {
                pSingle = (DEV_MultiDevice3D *) pSplit->GetPane(1, 1);
                if(pSingle != this)
                    pSingle->UpdateColorTitle();
				else
					SetFocusPane(3);
            }
        }
    }

	if (!M_GetMainApp()->m_bLeavingApplication)
	{
		//VL
		//GetParentFrame()->mfn_vUpdateDeviceToolBar();
		if (GetParent()->IsKindOf( RUNTIME_CLASS( FRMSplitter ) ))
		{
			GetParentFrame()->mfn_vUpdateDeviceToolBar();
		}
		//EVL
	}

}
#endif // ACTIVE_EDITOR

//--------------------------------------------------------------------------------
//      TEST-FUNCTIONS
//--------------------------------------------------------------------------------

#ifdef ACTIVE_EDITOR
//===========================================================================
//      Description:    Is pDevice is a pointer on a Device of the MultiDevice ?
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
BOOL DEV_MultiDevice::IsDevice(DEV_Device *pDevice)
{
	int i;
	for (i = 0; i < MAX_DEVICE; i++)
		if (IsActivDevice(i) && (pDevice == GetDevice(i)))
		return TRUE;
	return FALSE;
}
#endif // ACTIVE_EDITOR

//--------------------------------------------------------------------------------
//      ACTION-FUNCTIONS
//--------------------------------------------------------------------------------

//===========================================================================
//      Description:    Open the Background Device. This is the first Device of
//                      MultiDevice, and he always has the number 0. This Device
//                      is created by default.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
void DEV_MultiDevice::OnOpenDeviceBackground()
{
#ifdef ACTIVE_EDITOR
	if (SetDevice(&mDevice[0], 0))
	{
		if (TestFlags(MD_VIEW2D))
			GetDevice(0)->CreateDeviceBackground(this, C_DEVICE2D);
		else
			GetDevice(0)->CreateDeviceBackground(this, C_DEVICE3D);
		
		// if this is not the game multidevice, create a default camera
/*		if (M_GetMainWnd())
		{
			if (M_GetMainDevice() != this)
			{
				CPA_CameraDLLBase	*p_oCamDLL = NULL;
				CPA_Object			*p_oCamera = NULL;
				
				// we are not in the game device
				// so, create a camera for this device
				p_oCamDLL = (CPA_CameraDLLBase*) M_GetMainApp()->m_p_oMainWorld->GetObjectDLLWithName(C_szDLLCameraName);
				ASSERT(p_oCamDLL != NULL);
				
				p_oCamera = p_oCamDLL->CreateDefaultCamera();
				ASSERT(p_oCamera != NULL);
				
				((DEV_ViewPort3D*)GetDevice(0)->GetViewPort())->ChangeCamera(p_oCamera);
			}
		}
*/	}
#else
	if (TestFlags(MD_VIEW2D))
		mDeviceAlone.CreateDeviceBackground(this, C_DEVICE2D);
	else
		mDeviceAlone.CreateDeviceBackground(this, C_DEVICE3D);
#endif // ACTIVE_EDITOR
}

//===========================================================================
//      Description:    Freeze all the Devices but the device which has the
//                      number : Number.
//                      Device which is freeze is no more refresh.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
void DEV_MultiDevice::FreezeAllButOne(UINT Number)
{
#ifdef ACTIVE_EDITOR
	UINT i;
	for (i = 0; i < MAX_DEVICE; i++)
		if (IsActivDevice(i))
	    {
		    if (i == Number)
			    GetDevice(i)->Unfreeze();
		    else GetDevice(i)->Freeze();
	    }
#else
	if (mActivDeviceAlone)
		mDeviceAlone.Freeze();
#endif // ACTIVE_EDITOR
}

//===========================================================================
//      Description:    Unfreeze all the device.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
void DEV_MultiDevice::UnfreezeAll()
{
#ifdef ACTIVE_EDITOR
	UINT i;
	for (i = 0; i < MAX_DEVICE; i++)
		if (IsActivDevice(i))
		    GetDevice(i)->Unfreeze();
#else
	if (mActivDeviceAlone)
		mDeviceAlone.Unfreeze();
#endif // ACTIVE_EDITOR
}

//===========================================================================
//      Description:    Freeze all the device.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
void DEV_MultiDevice::FreezeAll()
{
#ifdef ACTIVE_EDITOR
	UINT i;
	for (i = 0; i < MAX_DEVICE; i++)
		if (IsActivDevice(i))
		    GetDevice(i)->Freeze();
#else
	if (mActivDeviceAlone)
		mDeviceAlone.Freeze();
#endif // ACTIVE_EDITOR
}



/////////////////////////////////////////////////////////////////////////////
// DEV_MultiDevice message handlers


#ifdef ACTIVE_EDITOR
//===========================================================================
//      Description:    Open/Close a Device in the MultiDevice.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
void DEV_MultiDevice::OnOpenDevice()
{
	CPA_SuperObject		*p_oCameraSO;
	CPA_CameraDLLBase	*p_oCamDLL;

	if(!TestFlags(MD_ONEDEVICE))
	{
		long lNum;

		lNum = GetNextNumDevice();
		if (SetDevice(&mDevice[lNum], lNum))
		{
			if (TestFlags(MD_VIEW2D))
				GetDevice(lNum)->CreateDevice(this, 10+lNum*20, 30+lNum*20, 300, 200, C_DEVICE2D);
			else
				GetDevice(lNum)->CreateDevice(this, 10+lNum*20, 30+lNum*20, 300, 200, C_DEVICE3D);
		}
		// create the default camera for the device
		p_oCamDLL = (CPA_CameraDLLBase*) M_GetMainApp()->m_p_oMainWorld->GetObjectDLLWithName(C_szDLLCameraName);
		ASSERT(p_oCamDLL != NULL);
		
		p_oCameraSO = p_oCamDLL->CreateDefaultCamera();
		p_oCamDLL->InsertCameraInHierarchy(p_oCameraSO);
		ASSERT(p_oCameraSO != NULL);
		
		((DEV_ViewPort3D*)(GetDevice(lNum)->GetViewPort()))->ChangeCamera(p_oCameraSO);

		GetParentFrame()->mfn_vUpdateDeviceToolBar();
	}
	else
		MessageBox("No more Device");
}

void DEV_MultiDevice::OnCloseDevice()
{
    GetFocusDevice()->SendMessage(WM_CLOSE, 0, 0);
	GetParentFrame()->mfn_vUpdateDeviceToolBar();
}

void DEV_MultiDevice::OnCloseAllDevice()
{
	for (int i = 1; i < MAX_DEVICE; i++)
		if (IsActivDevice(i))
			GetDevice(i)->SendMessage(WM_CLOSE, 0, 0);
	GetParentFrame()->mfn_vUpdateDeviceToolBar();
}

#endif // ACTIVE_EDITOR

//===========================================================================
//      Description:    Create the ToolBar, the background Device, and the Light
//                      for displaying the world.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
int DEV_MultiDevice::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

#ifdef ACTIVE_EDITOR
	CWnd::EnableToolTips(TRUE);

	if (GetParent()->IsKindOf( RUNTIME_CLASS( FRMSplitter ) ))
	{
		GetParentFrame()->RecalcLayout();
	}
	else
	{
		ASSERT( GetParent()->IsKindOf( RUNTIME_CLASS( CFrameWnd ) ) );
		((CFrameWnd *) GetParent())->RecalcLayout();
	}
#else
	GetParentFrame()->RecalcLayout();
#endif // ACTIVE_EDITOR


    //
	// Always create one Device on BackGround.
    //
	OnOpenDeviceBackground();

#ifndef ACTIVE_EDITOR
	mActivDeviceAlone = TRUE;
#endif // ACTIVE_EDITOR

	return 0;
}

#ifdef ACTIVE_EDITOR

//===========================================================================
//      Description:    Do an horizontal tile with all the device but the
//                      background one.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:         CB
//===========================================================================
void DEV_MultiDevice::OnDeviceTileHorz()
{
	CRect       stWinRect;
	int         nbDevice, i;
	DEV_Device  *a_p_oTabDevices[MAX_DEVICE];
    int         iHeight, iLastHeight;

	GetClientRect(&stWinRect);

	for(nbDevice = 0, i = 1; i <MAX_DEVICE; i++)
	{
		if(IsActivDevice(i))
			a_p_oTabDevices[nbDevice++] = GetDevice(i);
	}

    if(nbDevice)
    {
        iHeight = stWinRect.bottom / nbDevice;
	    for(i = 0; i < nbDevice; i++)
	    {
            iLastHeight = iHeight;
            if(i == nbDevice - 1)
                iHeight = stWinRect.bottom - (i * iLastHeight);
            a_p_oTabDevices[i]->MoveWindow(0, i * iLastHeight, stWinRect.right, iHeight);
	    }
    }
}

//===========================================================================
//      Description:    Do an vertical tile with all the device but the
//                      background one.
//      Creation date:  26/07/96
//      Author:         LOL
//---------------------------------------------------------------------------
//      Revision date:
//      Author:         CB
//===========================================================================
void DEV_MultiDevice::OnDeviceTileVert()
{
	CRect       stWinRect;
	int         nbDevice, i;
	DEV_Device  *a_p_oTabDevices[MAX_DEVICE];
    int         iWidth, iLastWidth;

	GetClientRect(&stWinRect);

	for(nbDevice = 0, i = 1; i <MAX_DEVICE; i++)
	{
		if(IsActivDevice(i))
			a_p_oTabDevices[nbDevice++] = GetDevice(i);
	}

    if(nbDevice)
    {
        iWidth = stWinRect.right / nbDevice;
	    for(i = 0; i < nbDevice; i++)
	    {
            iLastWidth = iWidth;
            if(i == nbDevice - 1)
                iWidth = stWinRect.right - (i * iLastWidth);
            a_p_oTabDevices[i]->MoveWindow(i * iLastWidth, 0, iWidth, stWinRect.bottom);
	    }
    }
}

//===========================================================================
//      Description:    Do an vertical tile with all the device but the
//                      background one.
//      Creation date:  13/09/96
//      Author:         CB
//---------------------------------------------------------------------------
//      Revision date:
//      Author:
//===========================================================================
void DEV_MultiDevice::OnDeviceCascade()
{
	CRect       stWinRect;
	int         nbDevice, i, j, lj;
	DEV_Device  *a_p_oTabDevices[MAX_DEVICE];
    int         iWidth, iTotalWidth, iHeight, iTotalHeight;

	GetClientRect(&stWinRect);

	for(nbDevice = 0, i = 1; i <MAX_DEVICE; i++)
	{
		if(IsActivDevice(i))
			a_p_oTabDevices[nbDevice++] = GetDevice(i);
	}

    if(nbDevice)
    {
        j = 0;
	    for(i = 0; i < nbDevice; i++)
	    {
            iWidth = 3*(stWinRect.right / 4);
            iHeight = 3*(stWinRect.bottom / 4);

            iTotalWidth = (iWidth + (j * (DEV_YCaption*2)));
            iTotalHeight = (iHeight + (j * (DEV_YCaption*2)));

            lj = j;
            if((iTotalWidth > stWinRect.right) || (iTotalHeight > stWinRect.bottom))
            {
                iWidth = stWinRect.right - (j * (DEV_YCaption*2));
                iHeight = stWinRect.bottom - (j * (DEV_YCaption*2));
                j = 0;
            }
            else
                j++;

            a_p_oTabDevices[i]->MoveWindow
            (
                lj * (DEV_YCaption*2), 
                lj * (DEV_YCaption*2), 
                iWidth, 
                iHeight
            );
	    }
    }
}

//===========================================================================
//      Description:    Maximize/Restore active device.
//                      background one.
//      Creation date:  18/09/96
//      Author:         CB
//---------------------------------------------------------------------------
//      Revision date:                                                                                          
//      Author:         
//===========================================================================
void DEV_MultiDevice::OnMaximizeDevice(void)
{
    DEV_Device  *p_oDevice = GetFocusDevice();
    p_oDevice->SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
}
void DEV_MultiDevice::OnRestoreDevice(void)
{
    DEV_Device  *p_oDevice = GetFocusDevice();
    p_oDevice->SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
}
#endif // ACTIVE_EDITOR

//==========================================================================
//==========================================================================
void DEV_MultiDevice::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos) 
{
	RECT    r;

	CView::OnWindowPosChanged(lpwndpos);
	GetClientRect(&r);

#ifdef ACTIVE_EDITOR
	int     i;
	for(i = 0; i < MAX_DEVICE; i++) 
	{
		if(IsActivDevice(i))
        {
			if(GetDevice(i)->IsMaximize())
                GetDevice(i)->SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
        }
	}
#else
	if (mActivDeviceAlone)
		mDeviceAlone.SetWindowPos(&wndTop,r.left,r.top,r.right-r.left,r.bottom-r.top,0);
#endif // ACTIVE_EDITOR
}

//==========================================================================
void DEV_MultiDevice::OnDestroy() 
{
#ifdef ACTIVE_EDITOR
	if (m_cIsAdditional == 0)
#endif
	{
		M_GetMainApp()->m_bLeavingApplication = TRUE;
	}
	mbOnDestroy = TRUE;
	CView::OnDestroy();     
}

//==========================================================================
#ifdef ACTIVE_EDITOR
LONG DEV_MultiDevice::OnDrawObject(UINT, LONG)
{
    DrawObject();
    return 0;
}

LONG DEV_MultiDevice::OnDrawObjectExcept0(UINT, LONG)
{
    DrawObjectExcept0();
    return 0;
}
#endif // ACTIVE_EDITOR
