// DlgLoad.cpp : implementation file
//

#include "stdafx.h"
#include <AFXTEMPL.h>
#include "ACP_Base.h"
#include "incITF.h"
#include "dlgload.hpp"

#include "..\Main\Inc\_EditID.h"
#include "TUT.h"

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

#define M_LB()      ( (CListBox*) GetDlgItem(IDC_LB_FAMILIES) )
#define M_BTLoad()  ( (CButton*)  GetDlgItem(IDOK) )

#define C_iBorderSize     15

#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 )

/////////////////////////////////////////////////////////////////////////////
// CDlgLoad dialog


CDlgLoad::CDlgLoad(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgLoad::IDD, pParent)
{
	//CPA2 Stegaru Cristian 98-06
	m_pActorDLL = NULL;
	//End CPA2 Stegaru Cristian 98-06
	//{{AFX_DATA_INIT(CDlgLoad)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDlgLoad::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgLoad)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgLoad, CDialog)
	//{{AFX_MSG_MAP(CDlgLoad)
	ON_WM_SIZE()
	ON_LBN_SELCHANGE(IDC_LB_FAMILIES, OnSelchangeLbFamilies)
	ON_WM_GETMINMAXINFO()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgLoad message handlers

BOOL CDlgLoad::OnInitDialog() 
{
	CDialog::OnInitDialog();

    CListBox *pLB = M_LB();

    pLB -> ResetContent();
    M_BTLoad() -> EnableWindow( FALSE );
    // fill ListBox
    Position xPos = m_p_oListOfFamilies -> GetHeadPosition();
    while( xPos )
    {
        CPA_BaseObject *p_oFamily = m_p_oListOfFamilies -> GetNext( xPos );
        if( ! p_oFamily -> fn_bIsAvailable() )
        {
            int iIndex = pLB -> AddString( p_oFamily -> GetName() );
            if( iIndex != LB_ERR )
                pLB -> SetItemData( iIndex, (DWORD) p_oFamily );
        }
    }
    pLB -> SetCurSel( -1 );

	// register control for TUT module
    TUT_M_vGetTutDll();
    TUT_M_vRegisterControlID( IDC_LB_FAMILIES	, "TFA_LOAD_LIST",	TUT_e_ListBox );
    TUT_M_vRegisterControlID( IDOK				, "TFA_LOAD_OK",	TUT_e_Button );
    TUT_M_vRegisterControlID( IDCANCEL			, "TFA_LOAD_CANCEL",TUT_e_Button );

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgLoad::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);

    if( GetDlgItem( IDOK) )
    {
        CRect oRectDlg, oRectLoad, oRectCancel, oRectLB, oRectText;
        CSize oSize;

        // get positions
        M_GetClientRect( this, &oRectDlg );
        M_GetClientRectId( IDOK, &oRectLoad );
        M_GetClientRectId( IDCANCEL, &oRectCancel );
        M_GetClientRectId( IDC_TEXT, &oRectText );
        M_GetClientRectId( IDC_LB_FAMILIES, &oRectLB );

        // place BT Cancel
        oSize = oRectCancel . Size();
        oRectCancel . bottom = oRectDlg . bottom - C_iBorderSize;
        oRectCancel . top = oRectCancel . bottom - oSize . cy;
        // place BT OK
        oSize = oRectCancel . Size();
        oRectLoad . bottom = oRectCancel . bottom;
        oRectLoad . top = oRectCancel . top;
        oRectLoad . right = oRectDlg . right - C_iBorderSize;
        oRectLoad . left = oRectLoad . right - oSize . cx;
        // place LB
        oRectLB . right = oRectLoad . right;
        oRectLB . bottom = oRectCancel . top - C_iBorderSize;
        // place Text
        oRectText . right = oRectLoad . right;

        // set positions
        M_SetClientRectId( IDOK, &oRectLoad );
        M_SetClientRectId( IDCANCEL, &oRectCancel );
        M_SetClientRectId( IDC_TEXT, &oRectText );
        M_SetClientRectId( IDC_LB_FAMILIES, &oRectLB );
    }
}

void CDlgLoad::OnOK() 
{
    CListBox *pLB = M_LB();
    int iNbSelected = pLB -> GetSelCount();

    m_oListOfLoadedFamilies . RemoveAll();

    if( iNbSelected )
    {
        int *ai_SelIdx = (int*) malloc( iNbSelected * sizeof(int) );
        pLB -> GetSelItems( iNbSelected, ai_SelIdx );
        for( int Idx = 0; Idx < iNbSelected ; Idx++ )
		{
	        //CPA2 Stegaru Cristian 98-06
            //m_oListOfLoadedFamilies . AddTail( (CPA_BaseObject*)pLB -> GetItemData( ai_SelIdx[Idx] ) );
			CPA_BaseObject *pObject = (CPA_BaseObject*)pLB -> GetItemData( ai_SelIdx[Idx] );
            m_oListOfLoadedFamilies . AddTail (pObject);
			if (m_pActorDLL != NULL)
				m_pActorDLL->OnQueryAction(NULL, C_uiActor_LoadFamilyModels, (LPARAM)pObject);
			//End CPA2 Stegaru Cristian 98-06
		}
        free( ai_SelIdx );
    }
	CDialog::OnOK();
}

void CDlgLoad::OnSelchangeLbFamilies() 
{
    CListBox *pLB = M_LB();
    int iNbSelected = pLB -> GetSelCount();

    M_BTLoad() -> EnableWindow( (BOOL)iNbSelected );
}

void CDlgLoad::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
{
    // limit min size
    lpMMI -> ptMinTrackSize . x = 200;
    lpMMI -> ptMinTrackSize . y = 300;
	
	CDialog::OnGetMinMaxInfo(lpMMI);
}

void CDlgLoad::OnDestroy()
{
	// unregister control for TUT module
    TUT_M_vGetTutDll();
    TUT_M_vUnregisterControlID( IDC_LB_FAMILIES	);
    TUT_M_vUnregisterControlID( IDOK			);
    TUT_M_vUnregisterControlID( IDCANCEL		);

	CDialog::OnDestroy();
}
