/*=============================================================================
 *
 *  Filename:           DlgAct.cpp : implementation file
 *  Version:            1.0
 *  Date:				02/01/97
 *  Author:             M.T.
 *
 *  Description:        implementation of a FormView Dialog class for actions
 *                      list in ZAM mode
 *
 *===========================================================================*/

//#undef _DEBUG
#include "stdafx.h"
//#define _DEBUG

#include "ACP_Base.h"
#include "ITF.h"

#include "TAC.h"
#include "TFA.h"

#include "DlgAct.hpp"
#include "DlgZALst.hpp"

#include "ACInterf.hpp"
#include "FAModif.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_LBActions()   ( (CListBox*) GetDlgItem( IDC_LIST_ACTION ) )

#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_MakeDo( p_oModif )               m_p_oDLL -> M_GetEditManager() -> AskFor( p_oModif )

#define C_iBorderSize      2
/////////////////////////////////////////////////////////////////////////////
// CDlgAction

IMPLEMENT_DYNCREATE(CDlgAction, CFormView)

CDlgAction::CDlgAction()
	: CFormView(CDlgAction::IDD)
{
	//{{AFX_DATA_INIT(CDlgAction)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
    m_p_oDLL = NULL;
    m_p_oDlgZAList = NULL;
    m_a_iNewSelection = NULL;
    m_a_iLastSelection = NULL;
    m_lNewNbSelected = 0;
    m_lLastNbSelected = 0;
}

CDlgAction::~CDlgAction()
{
    if( m_a_iLastSelection )
        free( m_a_iLastSelection );
    if( m_a_iNewSelection )
        free( m_a_iNewSelection );
}

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

BOOL CDlgAction::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
	DWORD dwRequestedStyle, const RECT& rect, CWnd* pParentWnd, UINT nID,
	CCreateContext* pContext)
{
    BOOL bResult = CFormView::Create( lpszClassName, lpszWindowName, dwRequestedStyle, rect, pParentWnd, nID, pContext );
    // register controls
    TUT_M_vGetTutDll();
    TUT_M_vRegisterControlID( IDC_BT_SELECTALL, "TAC_ZAM_ACTIONSLIST_BTALL", TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_LIST_ACTION,  "TAC_ZAM_ACTIONSLIST_LIST",  TUT_e_ListBox );
    //
    return bResult;
}

BEGIN_MESSAGE_MAP(CDlgAction, CFormView)
	//{{AFX_MSG_MAP(CDlgAction)
    ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BT_SELECTALL, OnBtSelectall)
	ON_LBN_SELCHANGE(IDC_LIST_ACTION, OnSelchangeListAction)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgAction diagnostics

#ifdef _DEBUG
void CDlgAction::AssertValid() const
{
	CFormView::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// CDlgAction message handlers

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgAction::OnDestroy()
{
    TUT_M_vGetTutDll();
    TUT_M_vUnregisterControlID( IDC_BT_SELECTALL );
    TUT_M_vUnregisterControlID( IDC_LIST_ACTION );
    //
    CFormView::OnDestroy();
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgAction::OnBtSelectall()
{
    CListBox *pLB = M_LBActions();
    int iNbString = pLB -> GetCount();

    if( iNbString <= 1 )
        pLB -> SetSel( 0, TRUE );
    else
        pLB -> SelItemRange( TRUE, 0, iNbString-1 );

    OnSelchangeListAction();
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgAction::OnSelchangeListAction()
{
    CListBox *pLB = M_LBActions();
    int iNbSelected = pLB -> GetSelCount();

    if( iNbSelected != LB_ERR )
    {
        int *a_iNewSelection = (int*) malloc( iNbSelected * sizeof( int ) );

        pLB -> GetSelItems( iNbSelected, a_iNewSelection );

        Family_StateChange *p_oActionList = new Family_StateChange(m_p_oDLL, C_eActionList,
                                                                   a_iNewSelection, m_a_iLastSelection,
                                                                   iNbSelected, m_lLastNbSelected);
        M_MakeDo( p_oActionList );
        free( a_iNewSelection );
    }
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgAction::mfn_vSetCurrentFamily(CPA_Family * _p_oFamily)
{
    // fill LB Actions
    mfn_vFillLBActions( _p_oFamily );
    // select all actions
    // OnBtSelectall();
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgAction::mfn_vSetCurrentActionList( int *_a_ItemList, long _lNumberOfItems )
{
    CListBox *pLB = M_LBActions();

    // unselect all actions
    int iNbString = pLB -> GetCount();

    if( iNbString <= 1 )
        pLB -> SetSel( 0, FALSE );
    else
        pLB -> SelItemRange( FALSE, 0, iNbString-1 );

    // unselect states
    m_p_oDlgZAList -> mfn_vAddAction( NULL, TRUE );

    if( m_a_iLastSelection )
        free( m_a_iLastSelection );
    m_a_iLastSelection = (int*) malloc( _lNumberOfItems * sizeof( int ) );
    m_lLastNbSelected = _lNumberOfItems;
    memcpy( m_a_iLastSelection, _a_ItemList, _lNumberOfItems * sizeof( int ) );

    // select actions
    for( int i=0; i<m_lLastNbSelected ; i++ )
    {
        // select action
        pLB -> SetSel( m_a_iLastSelection[i], TRUE );
        // add states
        CPA_Action *p_oAction = (CPA_Action*) pLB -> GetItemData( m_a_iLastSelection[i] );
        m_p_oDlgZAList -> mfn_vAddAction( p_oAction, FALSE );
    }
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgAction::mfn_vFillLBActions(CPA_Family * _p_oFamily)
{
    CListBox *pLB = M_LBActions();

    pLB -> ResetContent();

    // fill LB
    if( _p_oFamily )
    {
        POSITION stPos = _p_oFamily -> m_oListOfActions . GetHeadPosition();
        while( stPos )
        {
            CPA_Action *p_oAction = _p_oFamily -> m_oListOfActions . GetNext( stPos );
            int iIndex = pLB -> AddString( p_oAction -> GetName() );
            if( iIndex != LB_ERR )
                pLB -> SetItemData( iIndex, (DWORD) p_oAction);
        }
    }
    // unselect all
    int iNbString = pLB -> GetCount();

    if( iNbString <= 1 )
        pLB -> SetSel( 0, FALSE );
    else
        pLB -> SelItemRange( FALSE, 0, iNbString-1 );
    m_p_oDlgZAList -> mfn_vAddAction( NULL, TRUE );
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgAction::OnSize(UINT nType, int cx, int cy)
{
    if( M_LBActions() )
    {
        CRect oRectDlg, oRectBT, oRectLB;

        M_GetClientRect( this, &oRectDlg );
        M_GetClientRectId( IDC_BT_SELECTALL, &oRectBT );
        M_GetClientRectId( IDC_LIST_ACTION, &oRectLB );

        // minimum size
        CSize oSize = oRectDlg . Size();

        // place BT & LB
        oRectLB . right = oRectBT . right = max( oRectBT . left, oRectDlg . right - C_iBorderSize);
        oRectLB . bottom = max( oRectLB . top, oRectDlg . bottom - C_iBorderSize);

        M_SetClientRectId( IDC_BT_SELECTALL, &oRectBT );
        M_SetClientRectId( IDC_LIST_ACTION, &oRectLB );
    }
}
