// dlgcaract.cpp : implementation file
//

#ifdef ACTIVE_EDITOR

#include "stdafx.h"
#include "ACP_Base.h"
#include "incITF.h"

#include "dlgcarac.hpp"

#undef CPA_WANTS_IMPORT
#undef CPA_EXPORT
#define CPA_WANTS_EXPORT
	#include "OGD.h"
#undef CPA_WANTS_EXPORT
#define CPA_WANTS_IMPORT

#include "3Dinterf.hpp"

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


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
extern char *g_szElementTypeName[];
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
#define C_iBorderSize						5

#define M_CtrlList()					   ((CListCtrl*) GetDlgItem(IDC_LIST_ELEMENTS))
#define M_GetClientRect( pWnd, poRect )    (pWnd) -> GetWindowRect( poRect );ScreenToClient( poRect )
#define M_GetClientRectId( Id, poRect )    M_GetClientRect( GetDlgItem( Id ), poRect )
#define M_SetClientRect( pWnd, poRect )    (pWnd) -> MoveWindow( poRect )
#define M_SetClientRectId( Id, poRect )    M_SetClientRect( GetDlgItem( Id ), poRect )
#define M_GetElementType(_szName,_Type)    for( (_Type) = 0 ; strcmpi(g_szElementTypeName[_Type],"END") && strcmpi(g_szElementTypeName[_Type],_szName) ; (_Type)++);

#define M_ForEachElement( oList, oElement, Pos )\
            for( oElement = (oList) . GetHeadElement( Pos ) ; \
                 oElement ; \
                 oElement = (oList) . GetNextElement( Pos ) )


/////////////////////////////////////////////////////////////////////////////
// CDlgCaract dialog
IMPLEMENT_DYNCREATE(CDlgCaract,CFormView)

CDlgCaract::CDlgCaract(CWnd* pParent /*=NULL*/)
           :CFormView(CDlgCaract::IDD)
{
    m_p_oParentDLL = NULL;
	m_bFirstInit = TRUE;
	m_p_oCurrentObject = NULL;
	m_p_oSuperObject = NULL;
}


void CDlgCaract::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CDlgCaract, CFormView)
	//{{AFX_MSG_MAP(CDlgCaract)
	ON_WM_SIZE()	
	ON_NOTIFY( LVN_ITEMCHANGED , IDC_LIST_ELEMENTS, OnChangeList )
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgCaract message handlers

void CDlgCaract::fn_vUpdateInfo(CPA_SuperObject *_p_oSprObj, Geometry3D *_p_oObject)
{
	if( m_bFirstInit )
	{
		m_bFirstInit = FALSE;
		fn_vInitList();
	}

    if(_p_oObject && (m_p_oCurrentObject != _p_oObject))
    {
        ACP_tdxHandleOfObject p_tdstGeom = _p_oObject -> GetStruct();

        m_Data_cName.Format("%s",_p_oObject->GetName());
        m_Data_lNbPoints   = (long)GEO_xGetGeometricObjectNumberOfPoints(p_tdstGeom);
        m_Data_lNbEdges    = (long)GEO_xGetGeometricObjectNumberOfEdges(p_tdstGeom);
        m_Data_lNbBdV      = (long)GEO_xGetGeometricObjectNumberOfParallelBox(p_tdstGeom);
		m_Data_lNbElements = (long)GEO_xGetGeometricObjectNumberOfElements(p_tdstGeom);

        // elements
		fn_vUpdateList( _p_oObject );
    }
	else if( ! _p_oObject )
        {
		m_Data_cName = "No Object";
		fn_vUpdateList( NULL );
        }
	m_p_oSuperObject   = _p_oSprObj;
	m_p_oCurrentObject = _p_oObject;
    //
    fn_vShowValue( IDC_STATIC_NBPOINTS, m_Data_lNbPoints );
    fn_vShowValue( IDC_STATIC_NBEDGES,  m_Data_lNbEdges );
    fn_vShowValue( IDC_STATIC_NBBDV,    m_Data_lNbBdV );
    fn_vShowValue( IDC_ELEMENTS,		m_Data_lNbElements );

    if(GetDlgItem(IDC_EDIT_NAME))
        GetDlgItem(IDC_EDIT_NAME)->SetWindowText( m_Data_cName );
    GetDlgItem(IDC_EDIT_NAME)->EnableWindow(m_p_oCurrentObject != NULL);
    //
    UpdateData(FALSE);
}

void CDlgCaract::fn_vShowValue(int _nID, long _lValue)
{
    CString oCst;
    CWnd *p_oWnd = GetDlgItem(_nID) ;
    if(p_oWnd)
    {
        oCst.Format("%ld",_lValue);
        p_oWnd->SetWindowText(oCst);
    }
}

void CDlgCaract::fn_vInitList()
{
    int iTaille = M_CtrlList()->GetStringWidth("m");
	CString csNum;

    M_CtrlList()->InsertColumn(0,"Element",LVCFMT_LEFT,iTaille*15);
    M_CtrlList()->InsertColumn(1,"Type",LVCFMT_CENTER,iTaille*4);
    M_CtrlList()->InsertColumn(2,"Size",LVCFMT_RIGHT,iTaille*4);
    M_CtrlList()->InsertColumn(3,"BdV",LVCFMT_RIGHT,iTaille*4);
}

void CDlgCaract::fn_vUpdateList(Geometry3D *_p_oObject)
{
    M_CtrlList()->DeleteAllItems();

	if( _p_oObject )
	{
		long					iInd;
		POSITION                xPos;
		SectionElement          *p_oElementSection;
		char					szShortSectionName[SCR_CV_ui_Cfg_MaxLenName];

		iInd = 0;
        M_ForEachElement( _p_oObject -> m_oListOfElementSections, p_oElementSection, xPos )
		{
	        ACP_tdxHandleOfObject	p_tdstGeom = _p_oObject -> GetStruct();
			ACP_tdxIndex			xElementType;
			char					*p_cElement, *p_cName;
			CString					oCst;

			strcpy( szShortSectionName, (const char*)p_oElementSection -> GetReferencedSectionName() );
			p_cElement = strstr( szShortSectionName, "Element" ) + strlen("Element");
			p_cName = strchr( p_cElement, ':' );
			*p_cName++ = 0;
			M_GetElementType( p_cElement , xElementType );
			M_CtrlList()->InsertItem(iInd, p_cName,0);
            switch(xElementType)
            {
            case GEO_C_xElementIndexedTriangles:	M_CtrlList()->SetItemText( iInd, 1, "ITr" );		break;
            case GEO_C_xElementFaceMapDescriptors:  M_CtrlList()->SetItemText( iInd, 1, "FMD" );		break;
            case GEO_C_xElementSprites:             M_CtrlList()->SetItemText( iInd, 1, "Spr" );		break;
            case GEO_C_xElementTMeshes:             M_CtrlList()->SetItemText( iInd, 1, "TMs" );		break;
            case GEO_C_xElementPoints:              M_CtrlList()->SetItemText( iInd, 1, "Pts" );		break;
            case GEO_C_xElementLines:               M_CtrlList()->SetItemText( iInd, 1, "Lin" );		break;
            case GEO_C_xElementSpheres:             M_CtrlList()->SetItemText( iInd, 1, "Sph" );		break;
            case GEO_C_xElementAlignedBoxes:        M_CtrlList()->SetItemText( iInd, 1, "Box" );		break;
			case GEO_C_xElementAltimap:				M_CtrlList()->SetItemText( iInd, 1, "Atm" );		break;
            }
			oCst.Format("%d",_p_oObject->fn_xGetNbIndexedElements(iInd));
			M_CtrlList()->SetItemText(iInd,2,oCst);
			if( GEO_xGetParallelBoxIndexOfElement( p_tdstGeom, iInd ) != GEO_C_xNoParallelBox )
				oCst.Format("%d",GEO_xGetParallelBoxIndexOfElement( p_tdstGeom, iInd ));
			else
				oCst = " ";

			M_CtrlList()->SetItemText(iInd,3,oCst);

			iInd++;
		}
	}
}

void CDlgCaract::OnSize(UINT nType, int cx, int cy)
{
    if(GetDlgItem(IDC_EDIT_NAME))
    {
		CRect oRectDlg, oRectList, oRectBorder, oRectName;

	    M_GetClientRect( this, &oRectDlg );
		M_GetClientRectId( IDC_LIST_ELEMENTS, &oRectList );
		M_GetClientRectId( IDC_STATIC_BORDER, &oRectBorder );
		M_GetClientRectId( IDC_EDIT_NAME,     &oRectName );
		
		// recompute pos
		oRectBorder . left  = oRectList . left  = oRectDlg . left  + C_iBorderSize;
		oRectBorder . right = oRectList . right = oRectDlg . right - C_iBorderSize;
		oRectList . bottom = max( oRectList . top + 20 , oRectDlg . bottom - C_iBorderSize );

		oRectName . left  = oRectBorder . left  + C_iBorderSize;
		oRectName . right = oRectBorder . right - C_iBorderSize;

        // set pos
		M_SetClientRectId( IDC_LIST_ELEMENTS, &oRectList );
		M_SetClientRectId( IDC_STATIC_BORDER, &oRectBorder );
		M_SetClientRectId( IDC_EDIT_NAME,     &oRectName );
	}
}

void CDlgCaract::OnChangeList(NMHDR* pNMHDR, LRESULT* pResult)
{
	NM_LISTVIEW* pnmv = (NM_LISTVIEW *)pNMHDR;

	if( (pnmv->uOldState & LVIS_SELECTED) && !(pnmv->uNewState & LVIS_SELECTED) )
	{
		// unselect
		m_p_oParentDLL -> mfn_vShowOrHideElement( FALSE );
	}
	else if( pnmv->uNewState & LVIS_SELECTED )
	{
		// select
		m_p_oParentDLL -> mfn_vShowOrHideElement( TRUE, m_p_oSuperObject, m_p_oCurrentObject, pnmv -> iItem );
	}
    
    *pResult = 0;
}

#endif //ACTIVE_EDITOR

