/*
 *=======================================================================================
 * Name        :ipo_dlgs.cpp
 *
 * Author      : VL                          Date :30/06/97
 *
 * Description : implementation for dialogs box for IPO interface
 *=======================================================================================
 * Modification -> Author :                  Date :
 * Description :
 *=======================================================================================
 */

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include "stdafx.h"
#include "acp_base.h"
#include "ITF.h"
#include "TUT.h"
#include "ipo_intf.hpp"
#include "ipo_dlgs.h"
#include "DPT.h"
#include "x:\Cpa\Main\inc\_EditId.h"
#include "..\..\GLIGLOU\MULTIDRV\inc\VisuS_st.h"

//ROMTEAM WorldEditor (Cristian Stegaru 11/97)
#include "ogd.h"
//ENDROMTEAM WorldEditor (Cristian Stegaru)

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

/*
 =======================================================================================
 =======================================================================================
	CPhysicalObjectClassDialog
 =======================================================================================
 =======================================================================================
 */
IMPLEMENT_DYNCREATE(CPhysicalObjectClassDialog, CFormView)


/*
 ----------------------------------------------------------------------------------------
  Description   :  Constructor
 ----------------------------------------------------------------------------------------
 */
CPhysicalObjectClassDialog::CPhysicalObjectClassDialog() : CFormView(CPhysicalObjectClassDialog::IDD)
{
//ANNECY Shaitan Correction (15/04/98) {

	DEV_MultiDevice *p_oMultiDevice;

	m_p_oMainDevice = NULL;
	p_oMultiDevice = ((DEV_MultiDevice*)g_oFrameGest.ma_p_oWinArray[2][2]->GetActiveView());
	if (p_oMultiDevice)
		m_p_oMainDevice = p_oMultiDevice->GetDevice(0);


//ENDANNECY Shaitan Correction }
	m_p_oFrameForDevice3D = NULL;
	m_p_oDevice3D = NULL;
//ANNECY Shaitan Correction (15/04/98) {
/*
	m_bMainDeviceWasSwapped = FALSE;
	m_b3DView = FALSE;
*/
//ENDANNECY Shaitan Correction }
	//{{AFX_DATA_INIT(CPhysicalObjectClassDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


/*
 ----------------------------------------------------------------------------------------
  Description   : destructor
 ----------------------------------------------------------------------------------------
 */
CPhysicalObjectClassDialog::~CPhysicalObjectClassDialog()
{
}

/*
 ----------------------------------------------------------------------------------------
  Description   : Message Map
 ----------------------------------------------------------------------------------------
 */
BEGIN_MESSAGE_MAP(CPhysicalObjectClassDialog, CFormView)
	//{{AFX_MSG_MAP(CPhysicalObjectClassDialog)
	ON_WM_DRAWITEM()
	ON_WM_MEASUREITEM()
	ON_LBN_DBLCLK(IDC_LIST_POCLASS, OnDblclkListPoclass)
	ON_LBN_SELCHANGE(IDC_LIST_POCLASS, OnSelchangeListPoclass)
	ON_LBN_DBLCLK(IDC_LIST_POMODEL, OnDblclkListPomodel)
	ON_LBN_SELCHANGE(IDC_LIST_POMODEL, OnSelchangeListPomodel)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_RADIO_ALLCLASSES, OnRadioAllclasses)
	ON_BN_CLICKED(IDC_RADIO_LOADEDMODELS, OnRadioLoadedmodels)
	ON_BN_CLICKED(IDC_RADIO_ALLMODELS, OnRadioAllmodels)
	ON_BN_CLICKED(IDC_RADIO_SELECTEDCLASS, OnRadioSelectedclass)
	ON_BN_CLICKED(IDC_BUTTON_CREATEVIGNETTE, OnButtonCreatevignette)
	ON_BN_CLICKED(IDC_CHECK_3DVIEW, OnCheck3dview)
	ON_WM_CANCELMODE()
	ON_WM_DESTROY()
//ROMTEAM WorldEditor (Cristian Stegaru 11/97) 
	ON_BN_CLICKED(IDC_CREATE_I_TRIANGLE, OnButtonIndexedTriangle)
//ENDROMTEAM WorldEditor (Cristian Stegaru) 
// CPA2 ASilvescu 98-04-09
	ON_BN_CLICKED(IDC_MODELICON, OnCreateModelIcon)
// End CPA2 ASilvescu 98-04-09
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/*
 ----------------------------------------------------------------------------------------
  Description   : Creation of FormView, register window and control
 ----------------------------------------------------------------------------------------
 */
BOOL CPhysicalObjectClassDialog::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	if ( !CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext) )
		return FALSE;

	TUT_M_vGetTutDll();
	TUT_M_vRegisterControl( GetSafeHwnd(),				"OIT_PhysicalObjectClassesDialog",	TUT_e_Window ); 
	TUT_M_vRegisterControlID( IDC_BUTTON_CREATEVIGNETTE,"OIT_Poc_CreateVignette",			TUT_e_Button );
	TUT_M_vRegisterControlID( IDC_CHECK_3DVIEW,			"OIT_Poc_CheckToSwap3DViewDisplay",	TUT_e_Button );
	TUT_M_vRegisterControlID( IDC_LIST_POCLASS,			"OIT_Poc_ListOfClasses",			TUT_e_ListBox );
	TUT_M_vRegisterControlID( IDC_LIST_POMODEL,			"OIT_Poc_ListOfModels",				TUT_e_ListBox );
	TUT_M_vRegisterControlID( IDC_RADIO_ALLCLASSES,		"OIT_Poc_AllClassesRadioButton",	TUT_e_Button );
	TUT_M_vRegisterControlID( IDC_RADIO_ALLMODELS,		"OIT_Poc_AllModelsRadioButton",		TUT_e_Button );
	TUT_M_vRegisterControlID( IDC_RADIO_SELECTEDCLASS,	"OIT_Poc_SelectedClassRadioButton",	TUT_e_Button );
	TUT_M_vRegisterControlID( IDC_RADIO_LOADEDMODELS,	"OIT_Poc_LoadedModelsRadioButton",	TUT_e_Button );
// CPA2 ASilvescu 98-04-09
	TUT_M_vRegisterControlID( IDC_MODELICON, "OIT_Poc_CreateModelIcon",	TUT_e_Button );
// End CPA2 ASilvescu 98-04-09

	return TRUE;
}

/*
 ----------------------------------------------------------------------------------------
  Description   : Unregister control from TUT
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnDestroy() 
{
	TUT_M_vGetTutDll();
	TUT_M_vUnregisterControl( GetSafeHwnd() ); 
	TUT_M_vUnregisterControlID( IDC_BUTTON_CREATEVIGNETTE );
	TUT_M_vUnregisterControlID( IDC_CHECK_3DVIEW );
	TUT_M_vUnregisterControlID( IDC_LIST_POCLASS );
	TUT_M_vUnregisterControlID( IDC_LIST_POMODEL );
	TUT_M_vUnregisterControlID( IDC_RADIO_ALLCLASSES );
	TUT_M_vUnregisterControlID( IDC_RADIO_ALLMODELS );
	TUT_M_vUnregisterControlID( IDC_RADIO_SELECTEDCLASS );
	TUT_M_vUnregisterControlID( IDC_RADIO_LOADEDMODELS );
// CPA2 ASilvescu 98-04-09
	TUT_M_vUnregisterControlID(IDC_MODELICON);
// End CPA2 ASilvescu 98-04-09

	CFormView::OnDestroy();
}

/*
 =======================================================================================
	Virtual function override
 =======================================================================================
 */


/*
 ----------------------------------------------------------------------------------------
  Description   : pre translating message
  pMsg                ->
  Returns (BOOL )
 ----------------------------------------------------------------------------------------
 */
BOOL CPhysicalObjectClassDialog::PreTranslateMessage(MSG* pMsg) 
{
	if (m_p_oDevice3D->GetDevice(0) != NULL)
	{
		if ( pMsg->hwnd == m_p_oDevice3D->GetDevice(0)->GetViewPort()->GetSafeHwnd() )
		{
			if (pMsg->message == WM_KEYDOWN)
			{
				PostMessage( WM_USER );
			}

			if (
					(pMsg->message == WM_LBUTTONDOWN) ||
					(pMsg->message == WM_RBUTTONDOWN) ||
					(pMsg->message == WM_RBUTTONDBLCLK) ||
					(pMsg->message == WM_LBUTTONDBLCLK)
				)
				return 1;
			else
				return 0;

		}
	}

	if ( pMsg->message == WM_USER )
	{
		m_p_oEditor->GetMainWorld()->m_lNbLocalLights = 0;
		m_p_oEditor->GetMainWorld()->m_a_tdxhArrayOfLocalLights[ m_p_oEditor->GetMainWorld()->m_lNbLocalLights++ ] = m_hLightDown;
		m_p_oEditor->GetMainWorld()->m_a_tdxhArrayOfLocalLights[ m_p_oEditor->GetMainWorld()->m_lNbLocalLights++ ] = m_hLightDown;
		m_p_oEditor->GetMainWorld()->m_a_tdxhArrayOfLocalLights[ m_p_oEditor->GetMainWorld()->m_lNbLocalLights++ ] = m_hLightUp;
		m_p_oEditor->GetMainWorld()->m_a_tdxhArrayOfLocalLights[ m_p_oEditor->GetMainWorld()->m_lNbLocalLights++ ] = m_hLightUp;
				
		m_p_oDevice3D->DrawObject();
	}

	return CFormView::PreTranslateMessage(pMsg);
}


/*
 =======================================================================================
	CPhysicalObjectClassDialog diagnostics
 =======================================================================================
 */
#ifdef _DEBUG
void CPhysicalObjectClassDialog::AssertValid() const
{
	CFormView::AssertValid();
}

void CPhysicalObjectClassDialog::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/*
 =======================================================================================
	CPhysicalObjectClassDialog message handlers
 =======================================================================================
 */


/*
 ----------------------------------------------------------------------------------------
  Description   : WM_MEASUREITEM
  nIDCtl              -> control
  lpMeasureItemStruct -> measure data
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
	CFormView::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}

/*
 ----------------------------------------------------------------------------------------
  Description   : WM_DRAWITEM
  nIDCtl              -> controle to draw
  lpDIS               -> drawing data
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDIS) 
{
	if ( ( nIDCtl == IDC_LIST_POCLASS) || (nIDCtl == IDC_LIST_POMODEL ) )
	{
		if (lpDIS->itemID == -1)
			return;

		CDC                 *pDC = CDC::FromHandle( lpDIS->hDC );
		CListBox            *pLB = (CListBox *) GetDlgItem( nIDCtl );
		CPA_BaseObject		*p_oClass = (CPA_BaseObject *) pLB->GetItemData( lpDIS->itemID );

//ROMTEAM WorldEditor (Cristian Stegaru 11/97)
		if (NULL == p_oClass)
		{
			CFormView::OnDrawItem(nIDCtl, lpDIS);
			return;
		}
//ENDROMTEAM WorldEditor (Cristian Stegaru)

		BOOL                bSelected = (lpDIS->itemState & ODS_SELECTED );
		CFont				oBoldFont;
		CFont				*p_oOldFont = NULL;

		pDC->FillSolidRect( &lpDIS->rcItem, GetSysColor( COLOR_WINDOW ));

        if (p_oClass->fn_bIsAvailable())
		{
			PLOGFONT plf = (PLOGFONT) LocalAlloc(LPTR, sizeof(LOGFONT));
			lstrcpy(plf->lfFaceName, "MS Sans Serif");
			plf->lfWeight = FW_BOLD ;
			plf->lfHeight = 8;
			plf->lfEscapement = 0;
			oBoldFont.CreateFontIndirect(plf);
			p_oOldFont = pDC->SelectObject( &oBoldFont );
		}

		pDC->SetBkMode( TRANSPARENT);
		pDC->SetTextColor( GetSysColor( COLOR_WINDOWTEXT ) );

		pDC->DrawText( p_oClass->GetName(), strlen(p_oClass->GetName()), &lpDIS->rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER );
		if (bSelected)
			pDC->InvertRect( &lpDIS->rcItem );
		if (p_oOldFont)
		{
			pDC->SelectObject( p_oOldFont );
			oBoldFont.DeleteObject();
		}
	}
	else
		CFormView::OnDrawItem(nIDCtl, lpDIS);
}

/*
 ----------------------------------------------------------------------------------------
  Description   : LBN_DBLCLK on IDC_LIST_POCLASS
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnDblclkListPoclass() 
{
	CListBox		*pLB = (CListBox *) GetDlgItem( IDC_LIST_POCLASS );
	long			lIndex = pLB->GetCurSel();
	CPA_BaseObject	*p_oObject;

	if (lIndex == LB_ERR)
		return;

	p_oObject = (CPA_BaseObject *) pLB->GetItemData( lIndex );
	if ( !p_oObject->fn_bIsAvailable() )
		if (m_p_oEditor->fn_bLoadBaseObject( p_oObject ) )
		{
			pLB->Invalidate();
			OnSelchangeListPoclass();
			if ( ((CButton *) GetDlgItem( IDC_RADIO_ALLCLASSES ))->GetCheck() == 1)
				fn_vRefreshModelList();
		}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : LBN_SELCHANGE on IDC_LIST_POCLASS
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnSelchangeListPoclass() 
{
	CListBox	*pLB = (CListBox *) GetDlgItem( IDC_LIST_POCLASS );
	long		lIndex = pLB->GetCurSel();

	if (lIndex != LB_ERR)
		fn_vRefreshModelList( (CPA_BaseObject *) pLB->GetItemData( lIndex ) );

	if ( ((CButton *) GetDlgItem( IDC_RADIO_SELECTEDCLASS ))->GetCheck() == 1)
		fn_vRefreshModelList();		
}

/*
 ----------------------------------------------------------------------------------------
  Description   : LBN_DBLCLK on IDC_LIST_POMODEL
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnDblclkListPomodel() 
{
	CListBox			*pLB = (CListBox *) GetDlgItem( IDC_LIST_POMODEL );
	long				lIndex = pLB->GetCurSel();
	CPA_BaseObject		*p_oPO;
	CPA_ObjectDLLBase	*p_oPODll;

	if (lIndex == LB_ERR)
		return;

	p_oPO = (CPA_BaseObject *) pLB->GetItemData( lIndex );
	if ( !p_oPO->fn_bIsAvailable() )
	{
		p_oPODll = m_p_oEditor->GetMainWorld()->GetObjectDLLWithName( C_szDLLPhysicalObjectName );
		if (p_oPODll != NULL)
		{
			if (p_oPODll->fn_bLoadBaseObject( p_oPO ))
			{
				pLB->Invalidate();
				OnSelchangeListPomodel();
				if ( ((CButton *) GetDlgItem( IDC_RADIO_LOADEDMODELS ))->GetCheck() == 1)
					fn_vRefreshModelList();		
			}
		}
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : LBN_SELCHANGE on IDC_LIST_POCLASS
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnSelchangeListPomodel() 
{
	CListBox	*pLB = (CListBox *) GetDlgItem( IDC_LIST_POMODEL );
	long		lIndex = pLB->GetCurSel();
	EditorPO	*p_oPO;


	m_p_oRootSO->SetObject( NULL );
	if (lIndex == LB_ERR)
		p_oPO = NULL;
	else
	{
		p_oPO = (EditorPO *) pLB->GetItemData( lIndex );
		if ( !p_oPO->fn_bIsAvailable() )
			p_oPO = NULL;
	}

	/*
	 * if there's a po selected center camera on object
	 */
	if (p_oPO != NULL)
	{
	    CPA_CameraDLLBase		*p_oCameraInterface = ((DEV_ViewPort3D*)(m_p_oDevice3D->GetDevice(0)->GetViewPort()))->GetCameraInterface();
		CPA_BaseObject			*p_oCamera          = ((DEV_ViewPort3D*)(m_p_oDevice3D->GetDevice(0)->GetViewPort()))->GetCamera();
		GLI_tdxHandleToCamera	p_hCamera			= ((DEV_ViewPort3D*)(m_p_oDevice3D->GetDevice(0)->GetViewPort()))->GetEngineCamera();
		MTH_tdxReal				xRadius;
		MTH_tdxReal				xAlphaX,xRatio;
		CPA_CameraCoords		CameraCoords;
		CPA_ObjectDLLBase		*p_oPODll;
		ACP_tdxHandleOfObject	hBV;
		MTH3D_tdstVector		stCenter;
		float					fD1, fD2;

// CPA2 Nicu 98-04-27
		if(m_p_oWorld->GetRoot() != m_p_oRootSO)
		{
			m_p_oWorld->fn_vSetRoot(m_p_oRootSO);
			m_p_oEditor->GetInterface()->fn_vUpdateAll(E_mc_JustDraw);
		}
// CPA2 Nicu 98-04-27
		p_oPODll = m_p_oEditor->GetMainWorld()->GetObjectDLLWithName( C_szDLLPhysicalObjectName );
		if (p_oPODll != NULL)
		{
			hBV = p_oPODll->fn_hGetBoundingVolume( p_oPO );
//ANNECY TQ 22/06/98{
			GEO_fn_vGetInfoFromGeometricSphere( &stCenter, &xRadius, hBV );
//ENDANNECY TQ}

			//MTH_M_xMul( xRadius, 2 );

			GLI_xGetCameraAspectAndRatio( p_hCamera ,&xAlphaX, &xRatio);

			fD1 = MTH_M_xRealToFloat(MTH_M_xDiv(xRadius, MTH_M_xTan(MTH_M_xDiv(xAlphaX,2))));
			fD2 = MTH_M_xRealToFloat(MTH_M_xDiv(xRadius, MTH_M_xTan(MTH_M_xDiv(MTH_M_xMul(xAlphaX,xRatio),2))));

			xRadius = ((fD1 > fD2)? fD1 : fD2);

			// target camera
			CameraCoords.SetCoords( &stCenter );
			CameraCoords.SetAxisSystem( WorldCoordinates );
			p_oCameraInterface->SetTargetPoint( p_oCamera , &CameraCoords );
			p_oCameraInterface->SetTargetType (p_oCamera , tPoint );
			// position camera
			MTH3D_M_vSetYofVector( &stCenter, MTH3D_M_xGetYofVector( &stCenter ) -xRadius );
			CameraCoords.SetCoords( &stCenter );
			p_oCameraInterface -> SetPosition( p_oCamera , &CameraCoords );
		}
	}
	
	m_p_oRootSO->SetObject( p_oPO );

//ANNECY Shaitan Correction (15/04/98) {
	if ( m_p_oEditor->fn_bIs3DView() )
//ENDANNECY Shaitan Correction }
	{
		m_p_oEditor->GetMainWorld()->m_lNbLocalLights = 0;
		m_p_oEditor->GetMainWorld()->m_a_tdxhArrayOfLocalLights[ m_p_oEditor->GetMainWorld()->m_lNbLocalLights++ ] = m_hLightDown;
		m_p_oEditor->GetMainWorld()->m_a_tdxhArrayOfLocalLights[ m_p_oEditor->GetMainWorld()->m_lNbLocalLights++ ] = m_hLightDown;
		m_p_oEditor->GetMainWorld()->m_a_tdxhArrayOfLocalLights[ m_p_oEditor->GetMainWorld()->m_lNbLocalLights++ ] = m_hLightUp;
		m_p_oEditor->GetMainWorld()->m_a_tdxhArrayOfLocalLights[ m_p_oEditor->GetMainWorld()->m_lNbLocalLights++ ] = m_hLightUp;
		m_p_oDevice3D->DrawObject();
	}
}


/*
 ----------------------------------------------------------------------------------------
  Description   : handle for WM_SIZE message
  nType               -> sizing type
  cx                  -> width
  cy                  -> height
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnSize(UINT nType, int cx, int cy) 
{
	int		iHeight;

	CFormView::OnSize(nType, cx, cy);

	if (GetDlgItem( IDC_STATIC_POCLASS ) == NULL)
		return;

	if ( (cx > 50) && (cy > 360) )
	{
		iHeight = (cy - 350) / 2;

		GetDlgItem( IDC_STATIC_POCLASS        )->SetWindowPos( NULL, 5, 5, 0, 0, SWP_NOSIZE | SWP_NOZORDER  );
		GetDlgItem( IDC_LIST_POCLASS          )->SetWindowPos( NULL, 5, 20, cx-10, iHeight, SWP_NOZORDER  );
		GetDlgItem( IDC_STATIC_POMODEL        )->SetWindowPos( NULL, 5, 25 + iHeight, 0, 0, SWP_NOSIZE | SWP_NOZORDER  );
		GetDlgItem( IDC_LIST_POMODEL          )->SetWindowPos( NULL, 5, 40 + iHeight, cx-10, iHeight, SWP_NOZORDER  );

//ROMTEAM WorldEditor (Cristian Stegaru 11/97) 
		GetDlgItem (IDC_CREATE_I_TRIANGLE)->SetWindowPos (NULL, 5, cy - 310, cx - 10, 20, SWP_NOZORDER);
//ENDROMTEAM WorldEditor (Cristian Stegaru) 

		GetDlgItem( IDC_CHECK_3DVIEW          )->SetWindowPos( NULL, 5, cy - 290, cx - 10, 16, SWP_NOZORDER  );
		GetDlgItem( IDC_RADIO_ALLCLASSES      )->SetWindowPos( NULL, 5, cy - 80, 0, 0, SWP_NOSIZE | SWP_NOZORDER  );
		GetDlgItem( IDC_RADIO_SELECTEDCLASS   )->SetWindowPos( NULL, 5, cy - 64, 0, 0, SWP_NOSIZE | SWP_NOZORDER  );
		GetDlgItem( IDC_RADIO_ALLMODELS       )->SetWindowPos( NULL, 5, cy - 40, 0, 0, SWP_NOSIZE | SWP_NOZORDER  );
		GetDlgItem( IDC_RADIO_LOADEDMODELS    )->SetWindowPos( NULL, 5, cy - 24, 0, 0, SWP_NOSIZE | SWP_NOZORDER  );
		GetDlgItem( IDC_BUTTON_CREATEVIGNETTE )->SetWindowPos( NULL, 5, cy - 112, cx - 10, 16, SWP_NOZORDER  );	// CPA2 ASilvescu 98-04-09
		GetDlgItem( IDC_STATIC_3DVIEW		  )->SetWindowPos( NULL, 5, cy - 270, cx - 10, 174, SWP_NOZORDER  );	 // CPA2 ASilvescu 98-04-09
// CPA2 ASilvescu 98-04-09
		GetDlgItem(IDC_MODELICON)->SetWindowPos(NULL, 5, cy-96, cx-10, 16, SWP_NOZORDER);
// End CPA2 ASilvescu 98-04-09

		if ( m_p_oFrameForDevice3D != NULL )
			m_p_oFrameForDevice3D->MoveWindow( 5, cy - 270, cx - 10, 160 );
	}
}


/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_RADIO_ALLCLASSES
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnRadioAllclasses() 
{
	fn_vRefreshModelList();		
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_RADIO_SELECTEDCLASS
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnRadioSelectedclass() 
{
	fn_vRefreshModelList();
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_RADIO_LOADEDMODELS
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnRadioLoadedmodels() 
{
	fn_vRefreshModelList();
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_RADIO_ALLMODELS
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnRadioAllmodels() 
{
	fn_vRefreshModelList();
}


//ROMTEAM WorldEditor (Cristian Stegaru 11/97) 

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : OnButtonIndexedTriangle (
// Date   : 97-11
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Cristian Stegaru CPA2
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
void CPhysicalObjectClassDialog::OnButtonIndexedTriangle ()
{
	ASSERT (m_p_oEditor);
	CPA_ObjectDLLBase *pGeometryDLL = m_p_oEditor->GetMainWorld()->GetObjectDLLWithName(C_szDLLGeometryName);
	if (pGeometryDLL)
	{
		pGeometryDLL->OnQueryAction (m_p_oEditor, C_uiStartITEditor, C_IPO_uiCreateITModel);
	}
}


//ENDROMTEAM WorldEditor (Cristian Stegaru) 

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : DisplayModelIcon()
// Date   : 98-04-09
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description : Display an icon for selected model
// Author      : ASilvescu CPA2
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
void CPhysicalObjectClassDialog::DisplayModelIcon()
{
	m_p_oRootSO->SetObject( NULL );

	if (m_p_oEditor->m_pSelectedSPO)
	{
	  CPA_CameraDLLBase		*p_oCameraInterface = ((DEV_ViewPort3D*)(m_p_oDevice3D->GetDevice(0)->GetViewPort()))->GetCameraInterface();
		CPA_BaseObject			*p_oCamera          = ((DEV_ViewPort3D*)(m_p_oDevice3D->GetDevice(0)->GetViewPort()))->GetCamera();
		GLI_tdxHandleToCamera	p_hCamera			= ((DEV_ViewPort3D*)(m_p_oDevice3D->GetDevice(0)->GetViewPort()))->GetEngineCamera();
		MTH_tdxReal				xRadius = 4;
		CPA_CameraCoords		CameraCoords;
		CPA_ObjectDLLBase		*p_oPODll;
		MTH3D_tdstVector		stCenter={0,0,0};
		GEO_tdxHandleToBoundingSphere hBoundingSphere;

		p_oPODll = m_p_oEditor->GetMainWorld()->GetObjectDLLWithName( C_szDLLPhysicalObjectName );
		if (p_oPODll != NULL)
		{
//ANNECY TQ 22/06/98{
				hBoundingSphere = (GEO_tdxHandleToBoundingSphere)HIE_fn_hGetSuperObjectBoundingVolume(m_p_oEditor->m_pSelectedSPO->GetStruct());
				if(hBoundingSphere) 
				{
					MTH3D_M_vCopyVector(&stCenter, GEO_fn_pGetCenterPointOfBoundingSphere(hBoundingSphere));
					xRadius = GEO_fn_xGetRadiusOfBoundingSphere(hBoundingSphere);
				}
//ENDANNECY TQ}


			// target camera
			CameraCoords.SetCoords( &stCenter );
			CameraCoords.SetAxisSystem( WorldCoordinates );
			p_oCameraInterface->SetTargetPoint( p_oCamera , &CameraCoords );
			p_oCameraInterface->SetTargetType (p_oCamera , tPoint );
			// position camera
			MTH3D_M_vSetYofVector( &stCenter, -xRadius);
			CameraCoords.SetCoords( &stCenter );
			p_oCameraInterface -> SetPosition( p_oCamera , &CameraCoords );
		}
	}

	m_p_oWorld->fn_vSetRoot( m_p_oEditor->m_pSelectedSPO );
	if ( m_p_oEditor->fn_bIs3DView() )
	{
		m_p_oEditor->GetMainWorld()->m_lNbLocalLights = 0;
		m_p_oEditor->GetMainWorld()->m_a_tdxhArrayOfLocalLights[ m_p_oEditor->GetMainWorld()->m_lNbLocalLights++ ] = m_hLightDown;
		m_p_oEditor->GetMainWorld()->m_a_tdxhArrayOfLocalLights[ m_p_oEditor->GetMainWorld()->m_lNbLocalLights++ ] = m_hLightDown;
		m_p_oEditor->GetMainWorld()->m_a_tdxhArrayOfLocalLights[ m_p_oEditor->GetMainWorld()->m_lNbLocalLights++ ] = m_hLightUp;
		m_p_oEditor->GetMainWorld()->m_a_tdxhArrayOfLocalLights[ m_p_oEditor->GetMainWorld()->m_lNbLocalLights++ ] = m_hLightUp;
		m_p_oEditor->GetInterface()->fn_vUpdateAll(E_mc_JustDraw);
	}

}


//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : OnCreateModelIcon()
// Date   : 98-04-21
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description : Create an icon for selected model
// Author      : ASilvescu CPA2
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
void CPhysicalObjectClassDialog::OnCreateModelIcon() 
{
	int			iX, iY;
	RECT		stRect;
	COLORREF	*p_stPixel;
	COLORREF	*d_stPixel;
	CWnd		*p_oWnd;
	CDC			*p_oDC, oDCCopy;
	CBitmap		oBitmap;
	BITMAP		stBitmap;
	char		szVignetteName[ _MAX_PATH ], *p_szCurDir, *icon, *ModelList;
	CPA_BaseObject *obj;

																																						
	if(!m_p_oEditor->m_pSelectedSPO) return;	
	if (m_p_oDevice3D->GetDevice(0) == NULL) return;

// compute the bitmap filename
	strcpy( szVignetteName, (char*)(LPCTSTR) M_GetMainApp()->m_csEditorDataPath );
	strcat( szVignetteName, "\\" );
	p_szCurDir = szVignetteName + strlen( szVignetteName );
	if(m_p_oEditor->m_pSelectedSPO->GetObject()->GetOwner())
	{
		obj = m_p_oEditor->m_pSelectedSPO->GetObject()->GetOwner();
		if(!obj) return;
		sprintf(p_szCurDir, "objects\\actors\\models\\actors\\%s.bmp", obj->GetName());
		ModelList = "Actor_Model";
	}
	else
	{
		obj = ((EditorIPO*)m_p_oEditor->m_pSelectedSPO->GetObject())->GetPhysicalObject();
		if(!obj->GetOwner()) return;
		fn_bGetPOModelVignetteName(obj, p_szCurDir);
		ModelList = "Physical";
	}
	icon = p_szCurDir;
	p_szCurDir = strchr( p_szCurDir, '\\' );
	while (p_szCurDir)
	{
		*p_szCurDir = 0;
		if (_access( szVignetteName, 0) != 0)
		{
			CreateDirectory( szVignetteName, NULL );
			if (_access( szVignetteName, 0) != 0)
				return;
		}
		*p_szCurDir = '\\';
		p_szCurDir = strchr( p_szCurDir + 1, '\\' );
	}

	p_oWnd = (CWnd *) m_p_oDevice3D->GetDevice(0)->GetViewPort(); 
	p_oWnd->GetClientRect( &stRect );
	p_oDC = p_oWnd->GetWindowDC();

	oDCCopy.CreateCompatibleDC( p_oDC ); 
 
	/* Create a compatible bitmap . */ 
	oBitmap.CreateCompatibleBitmap( p_oDC, stRect.right, stRect.bottom ); 
	//oBitmap.CreateCompatibleBitmap( p_oDC, 100, 100 ); 
	oBitmap.GetBitmap( &stBitmap );

	/* Select the bitmaps into the compatible DC. */ 
 	oDCCopy.SelectObject( &oBitmap );

  /* 
   * Copy color data for the entire display into a bitmap that is selected into a compatible DC. 
   */ 
  oDCCopy.BitBlt( 0, 0, stBitmap.bmWidth, stBitmap.bmHeight, p_oDC, 0,0, SRCCOPY);
	d_stPixel = (COLORREF *) malloc ( 4 * stBitmap.bmWidth * stBitmap.bmHeight );
	p_stPixel = d_stPixel;

	for (iY = 0; iY < stBitmap.bmHeight; iY ++)
		for (iX = 0; iX < stBitmap.bmWidth; iX ++)
			*p_stPixel++ = oDCCopy.GetPixel( iX, iY );

	fn_vSaveBitmap( szVignetteName , stBitmap.bmWidth, stBitmap.bmHeight, d_stPixel );
	m_p_oEditor->GetInterface()->GetBitmapList()->m_fn_vReload( szVignetteName );

	EDT_ModelsByType *pListModels = m_p_oEditor->GetInterface()->GetModelsWithTypeName(ModelList);
	if(!pListModels) return;
	EDT_Model *pModel = pListModels->GetModelWithName(obj->GetName());
	if(!pModel) return;
	EDT_Model *pNewModel = new EDT_Model (pModel->m_pObjectDLL, pModel->m_csTypeName, pModel->m_csName, icon, m_p_oEditor->GetInterface());

	// Remove it from the list
	pListModels->fn_vRemoveAModel(pModel);
	// remove it from global list
	m_p_oEditor->GetInterface()->GetListAllModels()->fn_vRemoveAModel(pModel);
	// tell owner DLL
	pModel->m_pObjectDLL->fn_vOnAddModelToList(pListModels->GetDLL(), ModelList, pModel->m_csName, C_EDTUndo);
	// destroy it
	m_p_oEditor->GetInterface()->SetCurrentModel(NULL);
	delete pModel;
	m_p_oEditor->GetInterface()->fn_bAddANewModel(pNewModel, ModelList, pModel->m_pObjectDLL);
//	pNewModel->m_pObjectDLL->fn_vSaveListModels(pListModels->GetTypeName(), &pListModels->m_stListModels);

	fn_vRefreshModelList();
	ReleaseDC( p_oDC);
	oDCCopy.DeleteDC();
	oBitmap.DeleteObject();
	free (d_stPixel);
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_BUTTON_CREATEVIGNETTE
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnButtonCreatevignette() 
{
	int			iX, iY;
	RECT		stRect;
	COLORREF	*p_stPixel;
	COLORREF	*d_stPixel;
	CWnd		*p_oWnd;
	CDC			*p_oDC, oDCCopy;
	CBitmap		oBitmap;
	BITMAP		stBitmap;
	char		szVignetteName[ _MAX_PATH ];

	if (m_p_oDevice3D->GetDevice(0) == NULL)
		return;

	if ( !fn_bGetCurrentPOModelVignetteName( szVignetteName ) )
		return;

	p_oWnd = (CWnd *) m_p_oDevice3D->GetDevice(0)->GetViewPort(); 
	p_oWnd->GetClientRect( &stRect );
	p_oDC = p_oWnd->GetWindowDC();

	oDCCopy.CreateCompatibleDC( p_oDC ); 
 
	/* Create a compatible bitmap . */ 
	oBitmap.CreateCompatibleBitmap( p_oDC, stRect.right, stRect.bottom ); 
	//oBitmap.CreateCompatibleBitmap( p_oDC, 100, 100 ); 
	oBitmap.GetBitmap( &stBitmap );

	/* Select the bitmaps into the compatible DC. */ 
 
	oDCCopy.SelectObject( &oBitmap );

    /* 
     * Copy color data for the entire display into a bitmap that is selected into a compatible DC. 
     */ 
    oDCCopy.BitBlt( 0, 0, stBitmap.bmWidth, stBitmap.bmHeight, p_oDC, 0,0, SRCCOPY);
	//oDCCopy.StretchBlt( 0, 0, stBitmap.bmWidth, stBitmap.bmHeight, p_oDC, 0, 0, stRect.right, stRect.bottom, SRCCOPY);
	d_stPixel = (COLORREF *) malloc ( 4 * stBitmap.bmWidth * stBitmap.bmHeight );
	p_stPixel = d_stPixel;

	for (iY = 0; iY < stBitmap.bmHeight; iY ++)
		for (iX = 0; iX < stBitmap.bmWidth; iX ++)
			*p_stPixel++ = oDCCopy.GetPixel( iX, iY );

	fn_vSaveBitmap( szVignetteName , stBitmap.bmWidth, stBitmap.bmHeight, d_stPixel );
	m_p_oEditor->GetInterface()->GetBitmapList()->m_fn_vReload( szVignetteName );
	fn_vRefreshModelList();

	ReleaseDC( p_oDC);
	oDCCopy.DeleteDC();
	oBitmap.DeleteObject();
	free (d_stPixel);
}


/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_CHECK_3DVIEW
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::OnCheck3dview() 
{
//ANNECY Shaitan Correction (15/04/98) {
	BOOL b3DView;

	b3DView = ((CButton *) GetDlgItem(IDC_CHECK_3DVIEW))->GetCheck() == 1;

	m_p_oEditor->fn_vSet3DView(b3DView);

	GetDlgItem(IDC_BUTTON_CREATEVIGNETTE)->ShowWindow(b3DView);
	GetDlgItem(IDC_MODELICON)->ShowWindow(b3DView );
	GetDlgItem( IDC_STATIC_3DVIEW )->ShowWindow(!b3DView);
// CPA2 ASilvescu 98-04-09
		GetDlgItem(IDC_MODELICON)->ShowWindow(b3DView );
// End CPA2 ASilvescu 98-04-09
//ENDANNECY Shaitan Correction }
}

/*
 =======================================================================================
	CPhysicalObjectClassDialog specific functions
 =======================================================================================
 */


/*
 ----------------------------------------------------------------------------------------
  Description   : called by editor when closing
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::fn_vOnClose( void )
{
//ANNECY Shaitan Correction (15/04/98) {
/*
	if (m_p_oMainDevice && m_bMainDeviceWasSwapped)
	{
		if ( !m_p_oMainDevice->IsFullScreen() )
			m_p_oMainDevice->SwapFullScreen();
	}
*/
//ENDANNECY Shaitan Correction }
}

/*
 ----------------------------------------------------------------------------------------
  Description   : called by editor when opening
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::fn_vOnOpen( void )
{
//ANNECY Shaitan Correction (15/04/98) {
	BOOL b3DView;
/*	
	if (m_p_oMainDevice == NULL)
	{
		DEV_MultiDevice *p_oMultiDevice;
		p_oMultiDevice = ((DEV_MultiDevice*)g_oFrameGest.ma_p_oWinArray[2][2]->GetActiveView());
		if (p_oMultiDevice)
			m_p_oMainDevice = p_oMultiDevice->GetDevice(0);
	}
*/
	b3DView = m_p_oEditor->fn_bIs3DView();
	GetDlgItem( IDC_BUTTON_CREATEVIGNETTE )->ShowWindow( b3DView );
	GetDlgItem( IDC_STATIC_3DVIEW )->ShowWindow( !b3DView );
	m_p_oFrameForDevice3D->ShowWindow( b3DView );
	((CButton *) GetDlgItem( IDC_CHECK_3DVIEW ))->SetCheck( b3DView ? 1 : 0 );
	GetDlgItem(IDC_MODELICON)->ShowWindow(b3DView );
//ENDANNECY Shaitan Correction }

// CPA2 ASilvescu 98-04-09
		GetDlgItem(IDC_MODELICON)->ShowWindow(b3DView );
// End CPA2 ASilvescu 98-04-09
}



/*
 ----------------------------------------------------------------------------------------
  Description   : init device 3D
  Returns (void )
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::fn_vInitDevice()
{
	RECT				stRect = {0, 0, 50,50 };
    POS_tdstCompletePosition stMatrix;
    MTH3D_tdstVector	st3DX, st3DY, st3DZ;
	CPA_CameraDLLBase	*p_oCamDLL;
	CPA_SuperObject		*p_oCameraSO;
	CPA_CameraCoords	oCoords;
		

	POS_fn_vSetIdentityMatrix(&stMatrix);

	if (m_p_oFrameForDevice3D == NULL)
	{
		/*
		 * Create Frame
		 */
		m_p_oFrameForDevice3D = new CFrameWnd();
		m_p_oFrameForDevice3D->Create( NULL, "Frame for device",WS_CHILD | WS_VISIBLE | WS_BORDER, stRect, this);
//ANNECY Shaitan Correction (15/04/98) {
		m_p_oFrameForDevice3D->ShowWindow(m_p_oEditor->fn_bIs3DView() );
//ENDANNECY Shaitan Correction }
	}

	if (m_p_oDevice3D == NULL)
	{
		/*
		 * Create View
		 */
		CCreateContext	oContext;
		oContext.m_pNewViewClass = RUNTIME_CLASS( DEV_MultiDevice3D );
		oContext.m_pCurrentDoc = M_GetMainWnd()->GetActiveView()->GetDocument();
		m_p_oDevice3D = (DEV_MultiDevice3D *) m_p_oFrameForDevice3D->CreateView( &oContext );
		m_p_oDevice3D->SetFlags(m_p_oDevice3D->GetFlags() | MD_ONEDEVICE);
		m_p_oDevice3D->ShowWindow( TRUE );
		(m_p_oFrameForDevice3D)->SetActiveView(m_p_oDevice3D, TRUE);

		/*
		 * create and associate CPA_Contact
		 */
		m_p_oContact = new CPA_Contact();
		m_p_oDevice3D->SetContact( m_p_oContact );
		
		/*
		 * Create and associate Root Super object
	     */
		m_p_oRootSO = m_p_oEditor->GetInterface()->GetNewSuperObject( E_ss_NoSave );
		HIE_fn_vSetSuperObjectMatrix(m_p_oRootSO->GetStruct(), GEO_fn_hCreateMatrix());
		POS_fn_vSetIdentityMatrix (HIE_fn_hGetSuperObjectMatrix (m_p_oRootSO->GetStruct()));
		m_p_oRootSO->GetStruct()->hGlobalMatrix =  GEO_fn_hCreateMatrix();
		POS_fn_vSetIdentityMatrix (HIE_fn_hGetSuperObjectGlobalMatrix (m_p_oRootSO->GetStruct ()));
		m_p_oRootSO->SetObject( NULL );
		/*
		 * Associate with Evt_Editor
		 */
		m_p_oWorld = new CPA_World( m_p_oEditor->GetMainWorld(), TRUE );
		m_p_oWorld->fn_vSetRoot( m_p_oRootSO );
		m_p_oDevice3D->SetWorld( m_p_oWorld );
		m_p_oDevice3D->SetInterface( m_p_oEditor->GetInterface() );

		/*
		 * create camera for main device (if there is none!)
		 */
		oCoords.SetAxisSystem(CameraCoordinates);
		oCoords.SetXYZ(0,0,-10);
		p_oCamDLL = (CPA_CameraDLLBase*) m_p_oEditor->GetMainWorld()->GetObjectDLLWithName(C_szDLLCameraName);
		ASSERT(p_oCamDLL != NULL);
			
		p_oCameraSO = p_oCamDLL->CreateDefaultCamera();
		ASSERT(p_oCameraSO != NULL);
			
		p_oCamDLL->Translate(p_oCameraSO->GetObject(),&oCoords);
		((DEV_ViewPort3D*)(m_p_oDevice3D->GetDevice(0)->GetViewPort()))->ChangeCamera(p_oCameraSO);

		/*
		 *	Create Light
		 */
		/* ==> light up */
		GLI_xCreateLight( &m_hLightUp, GLI_C_lParallelLight );
		GLI_xSetLightState( m_hLightUp, 1 );
		/* ==> light Down */
		GLI_xCreateLight( &m_hLightDown, GLI_C_lParallelLight );
		GLI_xSetLightState( m_hLightDown, 1 );
		GLI_xGetLightMatrix( m_hLightDown, &stMatrix );
		POS_fn_vGetRotationMatrix( &stMatrix, &st3DX, &st3DY, &st3DZ );
		MTH3D_M_vMulScalarVector(&st3DZ, MTH_C_MinusONE, &st3DZ);
		POS_fn_vSetRotationMatrix( &stMatrix, &st3DX, &st3DY, &st3DZ );
		POS_fn_vNormalizeMatrix( &stMatrix );
		GLI_xSetLightMatrix( m_hLightDown, &stMatrix );
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : initialize list of class displayed in IDC_LIST_POMODEL
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::fn_vInitClassList( void )
{
	CPA_BaseObjectList  *p_oPOClassList;
	CPA_BaseObject		*p_oClass;
	long				lIndex;
	long				lIndexLB;
	CListBox			*p_oLB = (CListBox *) GetDlgItem( IDC_LIST_POCLASS );

	/*
	 * list of class initialisation
	 */
	p_oLB->ResetContent();
	p_oPOClassList = m_p_oEditor->GetMainWorld()->fn_p_oGetOriginalObjectList( C_szPhysicalObjectClassTypeName );
	if (p_oPOClassList != NULL)
	{
		for ( lIndex = 0; lIndex < p_oPOClassList->GetCount(); lIndex ++)
		{
			p_oClass = p_oPOClassList->GetObjectWithIndex( lIndex, E_lo_Alpha );
			//CPA2 Stegaru Cristian 98-04
			//we need the "it" class but we don't want this anymore to be showed in the list
			if ("it" == p_oClass->GetName ())
				continue;
			//End CPA2 Stegaru Cristian 98-04

			lIndexLB = p_oLB->AddString( p_oClass->GetName() );
			if (lIndexLB != LB_ERR)
				p_oLB->SetItemData( lIndexLB, (DWORD) p_oClass );
		}
	}
	((CButton *) GetDlgItem( IDC_RADIO_ALLCLASSES    ) )->SetCheck( 1 );
	((CButton *) GetDlgItem( IDC_RADIO_SELECTEDCLASS ) )->SetCheck( 0 );
	((CButton *) GetDlgItem( IDC_RADIO_ALLMODELS     ) )->SetCheck( 0 );
	((CButton *) GetDlgItem( IDC_RADIO_LOADEDMODELS  ) )->SetCheck( 1 );

	fn_vRefreshModelList();
	fn_vInitDevice();
}


/*
 ----------------------------------------------------------------------------------------
  Description   : refresh IDC_LIST_POMODEL list with model contained in given class
  p_oClass            -> class of PO
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::fn_vRefreshModelList( CPA_BaseObject *p_oClass )
{
	CPA_List<CPA_BaseObject>	oPOList;
	CPA_BaseObject				*p_oPO;
	long						lCount;
	long						lIndexLB;
	CListBox					*p_oLB = (CListBox *) GetDlgItem( IDC_LIST_POMODEL );
	POSITION					xPos;

	p_oLB->ResetContent();
	lCount = m_p_oEditor->GetMainWorld()->fn_lFindObjects( &oPOList, "", C_szPhysicalObjectTypeName, p_oClass );
	if (lCount)
	{
		for ( p_oPO = oPOList.GetHeadElement(xPos); xPos; p_oPO = oPOList.GetNextElement(xPos) )
		{
			lIndexLB = p_oLB->AddString( p_oPO->GetName() );
			if (lIndexLB != LB_ERR)
				p_oLB->SetItemData( lIndexLB, (DWORD) p_oPO );
		}
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : refresh model list in model view
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::fn_vRefreshModelList( void ) 
{
	CListBox					*pLB = (CListBox *) GetDlgItem( IDC_LIST_POCLASS );
	long						lFirstClass, lLastClass, lClass;
	CPA_BaseObject				*p_oClass;
	CPA_BaseObject				*p_oPO;
	CPA_List<CPA_BaseObject>	oPOList;
	POSITION					xPos;
	long						lCount;
	BOOL						bAllModel;
	EDT_Model					*p_oModel;
	char						*p_szVignetteName;
	char						szVignetteName[ _MAX_PATH ];
	char						szVignetteClassName[ _MAX_PATH ];

	/*
	 * destroy all model of "Physical" type
	 */
	EDT_ModelsByType *p_oList;
	p_oList = m_p_oEditor->GetInterface()->GetModelsWithTypeName( C_szPhysicalObjectTypeName );
	//p_oList->fn_vReinit();
	//temp
	while (p_oModel = p_oList->m_stListModels.GetHeadElement() )
	{
		m_p_oEditor->GetInterface()->fn_bRemoveAModel( p_oModel->m_csName, C_szPhysicalObjectTypeName );
	}
	
	//Fin temps

	/*
	 * Get class used to fill model
	 */
	if ( ( (CButton *) GetDlgItem(IDC_RADIO_ALLCLASSES) )->GetCheck() == 1)
	{
		lFirstClass = 0;
		lLastClass = pLB->GetCount() - 1;
	}
	else
	{
		lFirstClass = lLastClass = pLB->GetCurSel();
	}

	if (lLastClass == LB_ERR)
		return;

	bAllModel = ( ( (CButton *) GetDlgItem( IDC_RADIO_ALLMODELS ))->GetCheck() == 1 );

	/*
	 * fill model list
	 */
	//CPA2 Stegaru Cristian 98-04
	CPtrList lstOfEPOClasses;
	for (lClass = lFirstClass; lClass <= lLastClass; lClass ++ )
	{
		p_oClass = (CPA_BaseObject *) pLB->GetItemData( lClass );
		ASSERT (p_oClass);
		lstOfEPOClasses.AddTail (p_oClass);
	}
	lstOfEPOClasses.AddTail (m_p_oEditor->mfn_pGetITClass ());

	POSITION pos = lstOfEPOClasses.GetHeadPosition ();
	while (pos)
	{
		p_oClass = (CPA_BaseObject *)lstOfEPOClasses.GetNext (pos);
		oPOList.RemoveAll ();
		if ("it" == p_oClass->GetName ())
		{
			//add the IT objects from the previous sessions
			CStringList &rlstOfInitialITObjects = m_p_oEditor->GetInitialITObjectsList ();
			POSITION pos = rlstOfInitialITObjects.GetHeadPosition ();
			while (pos)
			{
				CString csITObjectName = rlstOfInitialITObjects.GetNext (pos);
				CPA_BaseObject *pPO = m_p_oEditor->GetMainWorld()->fn_p_oFindObject(csITObjectName, C_szPhysicalObjectTypeName);
				if (pPO)
				{
					if (!pPO->GetOwner ())
						pPO->fn_bSetOwner ((CPA_BaseObject *)m_p_oEditor->mfn_pGetITClass ());

					oPOList.AddHead (pPO);
				}
			}
		}
	//End CPA2 Stegaru Cristian 98-04
		lCount = m_p_oEditor->GetMainWorld()->fn_lFindObjects( &oPOList, "", C_szPhysicalObjectTypeName, p_oClass );
		
		if (lCount)
		{
			fn_bGetClassVignetteName( p_oClass, szVignetteClassName );

			for ( p_oPO = oPOList.GetHeadElement(xPos); xPos; p_oPO = oPOList.GetNextElement(xPos) )
			{
				if ( bAllModel || p_oPO->fn_bIsAvailable() )
				{
					if (!fn_bGetPOModelVignetteName( p_oPO, szVignetteName ))
						p_szVignetteName = szVignetteClassName;
					else
						p_szVignetteName = szVignetteName;

					EDT_Model* pNewModel = new EDT_Model ( m_p_oEditor, C_szPhysicalObjectTypeName, p_oPO->GetName(), p_szVignetteName, m_p_oEditor->GetInterface() );
					m_p_oEditor->GetInterface()->fn_bAddANewModel (pNewModel, C_szPhysicalObjectTypeName, m_p_oEditor);
				}
			}
		}
	//CPA2 Stegaru Cristian 98-04
	}
	//End CPA2 Stegaru Cristian 98-04

}


/*
 ----------------------------------------------------------------------------------------
  Description   : saving a bitmap file
  szFileName          -> filename of file were data will be saved
  iWidth              -> width of bitmap
  iHeight             -> height of bitmap
  a_stPixel           -> bitmap pixels 
 ----------------------------------------------------------------------------------------
 */
void CPhysicalObjectClassDialog::fn_vSaveBitmap( char *szFileName, int iWidth, int iHeight, COLORREF *a_stPixel)
{
	BITMAPFILEHEADER	stBmpHeader;
	BITMAPINFOHEADER	stBmpInfo;
	int					iTrueWidthInByte, iWidthInByte;
	FILE				*hpFile;
	int					iX, iY;
	COLORREF			*p_stCurPixel;

	hpFile = fopen( szFileName, "wb" );
	if (hpFile == NULL)
		return;

	iWidthInByte = iWidth * 3;
	iTrueWidthInByte = 4;
	while ( iTrueWidthInByte < iWidthInByte )
		iTrueWidthInByte += 4;
	iWidthInByte = iTrueWidthInByte - iWidthInByte;


	stBmpHeader.bfType			= 0x4D42;
	stBmpHeader.bfOffBits		= sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER );
	stBmpHeader.bfSize			= stBmpHeader.bfOffBits + iTrueWidthInByte * iHeight;
	stBmpHeader.bfReserved1		= 0;
	stBmpHeader.bfReserved2		= 0;

	stBmpInfo.biSize			= sizeof( BITMAPINFOHEADER ); 
	stBmpInfo.biWidth			= iWidth; 
	stBmpInfo.biHeight			= iHeight; 
	stBmpInfo.biPlanes			= 1; 
	stBmpInfo.biBitCount		= 24;
	stBmpInfo.biCompression		= BI_RGB; 
	stBmpInfo.biSizeImage		= iTrueWidthInByte * iHeight; 
	stBmpInfo.biXPelsPerMeter	= 0; 
	stBmpInfo.biYPelsPerMeter	= 0; 
	stBmpInfo.biClrUsed			= 0; 
	stBmpInfo.biClrImportant	= 0;

	fwrite( &stBmpHeader, sizeof( BITMAPFILEHEADER ), 1, hpFile );
	fwrite( &stBmpInfo,	  sizeof( BITMAPINFOHEADER ), 1, hpFile );

	for ( iY = 0; iY < iHeight; iY ++)
	{
		p_stCurPixel = &a_stPixel[ (iHeight - 1 - iY) * iWidth ];
		for (iX = 0; iX < iWidth; iX ++)
		{
			fputc( GetBValue( *p_stCurPixel  ), hpFile );
			fputc( GetGValue( *p_stCurPixel  ), hpFile );
			fputc( GetRValue( *p_stCurPixel  ), hpFile );
			p_stCurPixel  ++;
		}
		for (iX = 0; iX < iWidthInByte; iX ++)
			fputc( 0, hpFile );
	}
	fclose( hpFile );
}


/*
 ----------------------------------------------------------------------------------------
  Description   : copy in a string name of vignette for a class
  p_oClass            -> pointer on editor class object
  szVignetteName      -> string to fill
  return (BOOL) true if vignette exist
 ----------------------------------------------------------------------------------------
 */
BOOL CPhysicalObjectClassDialog::fn_bGetClassVignetteName( CPA_BaseObject *p_oClass, char *szVignetteName )
{
	char szTestVignette[ _MAX_PATH ];
	sprintf( szVignetteName, "objects\\oit\\%s\\%s.bmp", p_oClass->GetName(), p_oClass->GetName() );
	sprintf( szTestVignette, "%s\\%s", (char*)(LPCTSTR) M_GetMainApp()->m_csEditorDataPath, szVignetteName );
	return (_access( szTestVignette, 0) == 0 );
}


/*
 ----------------------------------------------------------------------------------------
  Description   :  copy in a string name of vignette for a PO
  p_oPO               -> editor po object
  szVignetteName      -> string to fill
  return (BOOL) true if vignette exist
 ----------------------------------------------------------------------------------------
 */
BOOL CPhysicalObjectClassDialog::fn_bGetPOModelVignetteName( CPA_BaseObject *p_oPO, char *szVignetteName )
{
	char szTestVignette[ _MAX_PATH ];
	sprintf( szVignetteName, "objects\\oit\\%s\\%s.bmp", p_oPO->GetOwner()->GetName(), p_oPO->GetName() );
	sprintf( szTestVignette, "%s\\%s", (char*)(LPCTSTR) M_GetMainApp()->m_csEditorDataPath, szVignetteName );
	return (_access( szTestVignette, 0) == 0 );
}


/*
 ----------------------------------------------------------------------------------------
  Description   : return handle to bitmap file were vignette will be saved
  Returns (FILE )
 ----------------------------------------------------------------------------------------
 */
BOOL CPhysicalObjectClassDialog::fn_bGetCurrentPOModelVignetteName( char *szVignetteName )
{
	EditorPO	*p_oPO;
	CListBox	*pLB;
	long		lIndex;
	char		*p_szCurDir;

	pLB = (CListBox *) GetDlgItem( IDC_LIST_POMODEL );

	if ( (lIndex = pLB->GetCurSel()) == LB_ERR )
		return FALSE;

	p_oPO = (EditorPO *) pLB->GetItemData( lIndex );
	
	strcpy( szVignetteName, (char*)(LPCTSTR) M_GetMainApp()->m_csEditorDataPath );
	strcat( szVignetteName, "\\" );
	p_szCurDir = szVignetteName + strlen( szVignetteName );
	fn_bGetPOModelVignetteName( p_oPO, p_szCurDir );
	p_szCurDir = strchr( p_szCurDir, '\\' );

	while (p_szCurDir)
	{
		*p_szCurDir = 0;
		if (_access( szVignetteName, 0) != 0)
		{
			CreateDirectory( szVignetteName, NULL );
			if (_access( szVignetteName, 0) != 0)
				return FALSE;
		}
		*p_szCurDir = '\\';
		p_szCurDir = strchr( p_szCurDir + 1, '\\' );
	}
	return TRUE;
}

/*
 =======================================================================================
 =======================================================================================
	CIPOEditionDialog
 =======================================================================================
 =======================================================================================
 */

IMPLEMENT_DYNCREATE(CIPOEditionDialog, CFormView)

/*
 ----------------------------------------------------------------------------------------
  Description   :  Constructor
 ----------------------------------------------------------------------------------------
 */
CIPOEditionDialog::CIPOEditionDialog() : CFormView(CIPOEditionDialog::IDD)
{
	//{{AFX_DATA_INIT(CIPOEditionDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_xSwapLod = -1;
	m_p_oEditor = NULL;
}

/*
 ----------------------------------------------------------------------------------------
  Description   :  Destructor
 ----------------------------------------------------------------------------------------
 */
CIPOEditionDialog::~CIPOEditionDialog()
{
}

/*
 ----------------------------------------------------------------------------------------
  Description   :  MessageMap
 ----------------------------------------------------------------------------------------
 */
BEGIN_MESSAGE_MAP(CIPOEditionDialog, CFormView)
	//{{AFX_MSG_MAP(CIPOEditionDialog)
	ON_WM_DRAWITEM()
	ON_BN_CLICKED(IDC_BUTTON_EDITPO, OnButtonEditpo)
	ON_BN_CLICKED(IDC_BUTTON_LOD, OnButtonLod)
	ON_BN_DOUBLECLICKED(IDC_BUTTON_LOD, OnDoubleclickedButtonLod)
	ON_WM_SIZE()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/*
 ----------------------------------------------------------------------------------------
  Description   : Creation of FormView, register window and control
 ----------------------------------------------------------------------------------------
 */
BOOL CIPOEditionDialog::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	if ( !CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext) )
		return FALSE;

	TUT_M_vGetTutDll();
	TUT_M_vRegisterControl( GetSafeHwnd(),		"OIT_IPOEditionDialog",		TUT_e_Window ); 
	TUT_M_vRegisterControlID( IDC_BUTTON_APPLY,	"OIT_Ipo_ApplyModifButton",	TUT_e_Button );
	TUT_M_vRegisterControlID( IDC_BUTTON_EDITPO,"OIT_Ipo_EditPOButton",		TUT_e_Button );
	TUT_M_vRegisterControlID( IDC_BUTTON_LOD,	"OIT_Ipo_ThresholdBar",		TUT_e_Button );
	TUT_M_vRegisterControlID( IDC_EDIT_VALUE,	"OIT_Ipo_EditValue",		TUT_e_TextEdit );
	TUT_M_vRegisterControlID( IDC_LIST_VERTEX,	"OIT_Ipo_VerticesList",		TUT_e_ListBox );
	TUT_M_vRegisterControlID( IDC_SPIN_VALUE,	"OIT_Ipo_SpinValue",		TUT_e_Spin );

	return TRUE;
}

/*
 ----------------------------------------------------------------------------------------
  Description   : Unregister control from TUT
 ----------------------------------------------------------------------------------------
 */
void CIPOEditionDialog::OnDestroy() 
{
	TUT_M_vGetTutDll();
	TUT_M_vUnregisterControl( GetSafeHwnd() ); 
	TUT_M_vUnregisterControlID( IDC_BUTTON_APPLY );
	TUT_M_vUnregisterControlID( IDC_BUTTON_EDITPO );
	TUT_M_vUnregisterControlID( IDC_BUTTON_LOD );
	TUT_M_vUnregisterControlID( IDC_EDIT_VALUE );
	TUT_M_vUnregisterControlID( IDC_LIST_VERTEX );
	TUT_M_vUnregisterControlID( IDC_SPIN_VALUE );

	CFormView::OnDestroy();
}


/*
 =======================================================================================
	CIPOEditionDialog diagnostics
 =======================================================================================
 */
#ifdef _DEBUG
void CIPOEditionDialog::AssertValid() const
{
	CFormView::AssertValid();
}

void CIPOEditionDialog::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/*
 =======================================================================================
	CIPOEditionDialog message handlers
 =======================================================================================
 */

/*
 ----------------------------------------------------------------------------------------
  Description   : WM_DRAWITEM
 ----------------------------------------------------------------------------------------
 */
void CIPOEditionDialog::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDIS) 
{
	if (nIDCtl == IDC_BUTTON_LOD)
	{
		if (m_hVisualSet != 0)
		{
			// draw treshold bar
			RECT			rcLod;
			long			lHeight;
			long			lLodHeight;
			long			lCurrentY;
			ACP_tdxIndex	xLod;
			
			CBrush			oWhiteBrush(RGB(255,255,255));
			CBrush			oGrayBrush(RGB(128,128,128));
			CBrush			oBlackBrush(RGB(0,0,0));
			CPen			oRedPen( PS_SOLID, 1, RGB(255,0,0));
			CPen			oBlackPen( PS_SOLID, 1, RGB(0,0,0));
			CPen			oWhitePen( PS_SOLID, 1, RGB(255,255,255));
			CDC				*pDC = CDC::FromHandle( lpDIS->hDC );

			lHeight = lpDIS->rcItem.bottom - lpDIS->rcItem.top;
			lLodHeight = (lHeight - 9) / m_xNbLods;
			lCurrentY = lpDIS->rcItem.top + (( lHeight - (lLodHeight * m_xNbLods) ) / 2);

			rcLod.left = lpDIS->rcItem.left + 5;
			rcLod.right = rcLod.left + 8;
			
			for ( xLod = 0; xLod < m_xNbLods; xLod ++)
			{
				rcLod.top = lCurrentY;
				rcLod.bottom = rcLod.top + lLodHeight - 1;

				pDC->SelectObject( (xLod == m_xCurrentLod)?oRedPen:(xLod & 1)?oBlackPen:oWhitePen );
				pDC->SelectObject( (xLod == m_xSwapLod) ? oGrayBrush : ((xLod & 1) ? oBlackBrush : oWhiteBrush) );

				pDC->Rectangle( &rcLod );

				lCurrentY += lLodHeight;
			}

			oWhiteBrush.DeleteObject();
			oBlackBrush.DeleteObject();
			oWhitePen.DeleteObject();
			oBlackPen.DeleteObject();
			oRedPen.DeleteObject();
		}
	}
	
	CFormView::OnDrawItem(nIDCtl, lpDIS);
}


/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_BUTTON_EDITPO
 ----------------------------------------------------------------------------------------
 */
void CIPOEditionDialog::OnButtonEditpo() 
{
	CPA_EditorBase				*p_oPODLL;
	CPA_List<CPA_BaseObject>	oList;

	p_oPODLL = m_p_oEditor->GetMainWorld()->GetObjectDLLWithName( C_szDLLPhysicalObjectName );
	if (p_oPODLL)
	{
		oList.AddTail( m_p_oIPO->GetPhysicalObject() );
		m_p_oEditor->GetMainWorld()->fn_bActivateEditor( p_oPODLL, &oList );
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_BUTTON_LOD
 ----------------------------------------------------------------------------------------
 */
void CIPOEditionDialog::OnButtonLod()
{
	ACP_tdxIndex	xLod;
	
	if ( (xLod = m_fn_xGetClickedLodIndex()) != -1)
	{
		m_xCurrentLod = xLod;
		m_fn_vShowCurrentLod();
	}
	
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_DBLCLICKED on IDC_BUTTON_TRESHOLD
 ----------------------------------------------------------------------------------------
 */
void CIPOEditionDialog::OnDoubleclickedButtonLod() 
{
	ACP_tdxIndex			xLod;

	if ( (xLod = m_fn_xGetClickedLodIndex()) != -1)
	{
		m_fn_vSwapLOD( xLod, (xLod == m_xSwapLod), TRUE );
		GetDlgItem( IDC_BUTTON_LOD )->Invalidate();
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : handle for WM_SIZE message
  nType               -> sizing type
  cx                  -> width
  cy                  -> height
 ----------------------------------------------------------------------------------------
 */
void CIPOEditionDialog::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);

	if (GetDlgItem( IDC_STATIC_NOIPO ) == NULL)
		return;

	if ( (cx > 150) && (cy > 100) )
	{
		GetDlgItem( IDC_LIST_VERTEX		)->SetWindowPos( NULL, 30, 5, cx - 35, cy - 55, SWP_NOZORDER  );
		GetDlgItem( IDC_EDIT_VALUE		)->SetWindowPos( NULL, 30, cy - 50, cx - 95, 20, SWP_NOZORDER  );
		GetDlgItem( IDC_SPIN_VALUE		)->SetWindowPos( NULL, cx - 65, cy - 50, 20, 20, SWP_NOZORDER  );
		GetDlgItem( IDC_BUTTON_APPLY	)->SetWindowPos( NULL, cx - 40, cy - 50, 35, 20, SWP_NOZORDER  );
		GetDlgItem( IDC_BUTTON_EDITPO   )->SetWindowPos( NULL, 30, cy - 25, cx - 35, 20, SWP_NOZORDER  );
		GetDlgItem( IDC_BUTTON_LOD      )->SetWindowPos( NULL, 5, 0, 20, cy, SWP_NOZORDER  );
		GetDlgItem( IDC_STATIC_NOIPO	)->SetWindowPos( NULL, 5, 5, cx - 10, 20, SWP_NOZORDER  );
	}
}

/*
 =======================================================================================
	CIPOEditionDialog specific functions 
 =======================================================================================
 */

/*
 ----------------------------------------------------------------------------------------
  Description   : set new selected IPO
 ----------------------------------------------------------------------------------------
 */
void CIPOEditionDialog::fn_vUpdateIPO( EditorIPO *_p_oIPO )
{
	BOOL bShow;

	m_p_oIPO = _p_oIPO;

	bShow = (m_p_oIPO != NULL);
	GetDlgItem( IDC_BUTTON_EDITPO	)->ShowWindow( bShow );
	GetDlgItem( IDC_BUTTON_LOD		)->ShowWindow( bShow );
	GetDlgItem( IDC_LIST_VERTEX		)->ShowWindow( bShow );
	GetDlgItem( IDC_EDIT_VALUE		)->ShowWindow( bShow );
	GetDlgItem( IDC_SPIN_VALUE		)->ShowWindow( bShow );
	GetDlgItem( IDC_BUTTON_APPLY	)->ShowWindow( bShow );
	GetDlgItem( IDC_STATIC_NOIPO	)->ShowWindow( !bShow );
	
	if ( m_p_oIPO == NULL)
	{
		m_hVisualSet = 0;
	}
	else
	{
		PO_tdxHandleToPhysicalObject hPO;

		EditorPO *p_oPO = _p_oIPO->GetPhysicalObject();
		hPO = p_oPO->GetStruct();
		m_hVisualSet = PO_fn_hGetVisualSet( hPO );
		m_xNbLods = (short)GLI_lGetVisualSetNumbertOfLOD( m_hVisualSet );
		m_xCurrentLod = 0;
		m_fn_vShowCurrentLod();
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : display info about current lod
 ----------------------------------------------------------------------------------------
 */
void CIPOEditionDialog::m_fn_vShowCurrentLod( void )
{
	MTH_tdxReal		xThreshold;
	CListBox		*p_oLB = (CListBox *) GetDlgItem( IDC_LIST_VERTEX );
	char			szFormat[5] = {'%', '0', ' ', 'd', 0 };
	char			szString[10];
	ACP_tdxIndex	xDeltaRLI, xNbPoints;
	char			cPuissance;

	/* 
	 * get geometric associated with current LOD
	 */
	GLI_vGetVisualSetLOD( m_hVisualSet, m_xCurrentLod, &xThreshold, &m_xCurrentGeom );
	m_xNumberOfPoints = GEO_xGetGeometricObjectNumberOfPoints( m_xCurrentGeom );

	cPuissance = 1;
	xNbPoints = 10;
	while ( (xNbPoints < m_xNumberOfPoints ) && (cPuissance < 9) )
	{
		xNbPoints *= 10;
		cPuissance ++;
	}
	szFormat[2] = '0' + cPuissance;

	p_oLB->ResetContent();
	for (xDeltaRLI = 0; xDeltaRLI <= m_xNumberOfPoints; xDeltaRLI++ )
	{
		sprintf( szString, szFormat, xDeltaRLI ); 
		p_oLB->AddString( szString );
	}

	GetDlgItem( IDC_BUTTON_LOD )->Invalidate();
}

/*
 ----------------------------------------------------------------------------------------
  Description   : Get index of clicked lod in sheme drawn in IDC_BUTTON_TRESHOLD button
 ----------------------------------------------------------------------------------------
 */
ACP_tdxIndex CIPOEditionDialog::m_fn_xGetClickedLodIndex( void ) 
{
	CButton			*p_oBt = (CButton *) GetDlgItem( IDC_BUTTON_LOD );
	POINT			stPoint;
	RECT			stRect;
	long			lHeight, lLodHeight, lY;
	ACP_tdxIndex	xLod = -1;
	
	GetCursorPos( &stPoint );
	p_oBt->ScreenToClient( &stPoint );

	if ( (stPoint.x >= 5) && (stPoint.x <= 12) )
	{
		p_oBt->GetClientRect( &stRect );
		lHeight = stRect.bottom - stRect.top;
		lLodHeight = (lHeight - 9) / m_xNbLods;
		lY = ( lHeight - (lLodHeight * m_xNbLods) ) / 2;
		while (lY < stPoint.y)
		{
			if (++xLod == m_xNbLods)
			{
				xLod = -1;
				break;
			}
			lY += lLodHeight;
		}
	}
	return xLod;
}

/*
 ----------------------------------------------------------------------------------------
  Description   : swap/restore swap of LOD to force drawing of a given LOD
  _xLod               -> index of lod to force
  _bRestore           -> TRUE to restore initial Visual Set
  _bRedraw			  -> TRUE if world must be redrawn
 ----------------------------------------------------------------------------------------
 */
void CIPOEditionDialog::m_fn_vSwapLOD( ACP_tdxIndex _xLod, BOOL _bRestore, BOOL _bRedraw )
{
	ACP_tdxHandleOfObject	xGeometric0, xGeometric;
	MTH_tdxReal				xTreshold0, xTreshold;

	if (_bRestore)
	{
		if (m_xSwapLod != -1)
		{
			((struct GEO_tdstVisualSet_ *) m_hVisualSet)->xNbLodDefinitions = m_xNbLods;
			GLI_vGetVisualSetLOD( m_hVisualSet, 0, &xTreshold0, &xGeometric0 );
			GLI_vGetVisualSetLOD( m_hVisualSet, m_xSwapLod, &xTreshold, &xGeometric );
			GLI_vSetVisualSetLOD( m_hVisualSet, m_xSwapLod, xTreshold, xGeometric0 );
			GLI_vSetVisualSetLOD( m_hVisualSet, 0, xTreshold0, xGeometric );
			m_xSwapLod = -1;
		}
	}
	else
	{
		if (m_xSwapLod != -1)
			m_fn_vSwapLOD( 0, TRUE, FALSE );

		GLI_vGetVisualSetLOD( m_hVisualSet, 0, &xTreshold0, &xGeometric0 );
		GLI_vGetVisualSetLOD( m_hVisualSet, _xLod, &xTreshold, &xGeometric );
		GLI_vSetVisualSetLOD( m_hVisualSet, _xLod, xTreshold, xGeometric0 );
		GLI_vSetVisualSetLOD( m_hVisualSet, 0, xTreshold0, xGeometric );
		((GEO_tdstVisualSet *) m_hVisualSet)->xNbLodDefinitions = 1;
		m_xSwapLod = _xLod;
	}

	if (_bRedraw)
		(((DEV_MultiDevice*)g_oFrameGest.ma_p_oWinArray[2][2]->GetActiveView())->DrawObject());
}

