// DlgZA.cpp : implementation file
//

#include "stdafx.h"

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

#include "TAC.h"
#include "OAc.h"

#include "ACInterf.hpp"

#include "DlgZA.hpp"
#include "DLGZALst.hpp"
#include "DlgInput.hpp"
#include "ZAModif.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

///////////////////////////////////////////////////////////////////////////////
// MACROS
///////////////////////////////////////////////////////////////////////////////
#define M_Control( CType, nID)	    ( ( CType*) GetDlgItem( nID ) )
#define M_LB( nID )				    M_Control( CListBox, nID )
#define M_BT( nID )				    M_Control( CButton, nID )

#define M_Enable( nID, bEnable )    GetDlgItem( nID ) -> EnableWindow( bEnable )
#define M_bEnabled( nID )           GetDlgItem( nID ) -> IsWindowEnabled()

#define M_LBZA()                M_LB( IDC_LIST_ZA )
#define M_LBZonesList()         M_LB( IDC_LIST_ZONES )
//#define M_BTAction()            M_BT( IDC_BT_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 )

// Popup Menu ID
#define C_uiPMDefault       2000
#define C_uiPMAdd	        (C_uiPMDefault + 1)
#define C_uiPMCopy          (C_uiPMDefault + 2)
#define C_uiPMDel           (C_uiPMDefault + 3)
#define C_uiPMRename        (C_uiPMDefault + 4)

#define C_iBorderSize      2

/////////////////////////////////////////////////////////////////////////////
// CDlgZonesActivation

IMPLEMENT_DYNCREATE(CDlgZonesActivation, CFormView)

CDlgZonesActivation::CDlgZonesActivation()
	: CFormView(CDlgZonesActivation::IDD)
{
	//{{AFX_DATA_INIT(CDlgZonesActivation)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
    m_oCrossDC.CreateCompatibleDC( NULL );
	m_oCrossBitmap.LoadBitmap( IDB_BITMAP_ALLOWED );
	m_oCrossDC.SelectObject(m_oCrossBitmap);
    // create italic font
    PLOGFONT plfItalic = (PLOGFONT) LocalAlloc(LPTR, sizeof(LOGFONT));
    lstrcpy(plfItalic->lfFaceName, "MS Sans Serif");
    plfItalic->lfItalic = TRUE;
    plfItalic->lfWeight = FW_NORMAL ;
    plfItalic->lfHeight = 8;
    plfItalic->lfEscapement = 0;
    m_oItalicFont.CreateFontIndirect(plfItalic);
    // create bold font
    PLOGFONT plf = (PLOGFONT) LocalAlloc(LPTR, sizeof(LOGFONT));
    lstrcpy(plf->lfFaceName, "MS Sans Serif");
    plf->lfWeight = FW_BOLD ;
    plf->lfHeight = 8;
    plf->lfEscapement = 0;
    m_oBoldFont.CreateFontIndirect(plf);
    //
    m_p_oDLL                = NULL;
    m_p_oDlgZAList          = NULL;
    m_p_oCurrentNamesList   = NULL;
    m_p_oCurrentZA          = NULL;
    //
    m_bFirstShow            = TRUE;

}

CDlgZonesActivation::~CDlgZonesActivation()
{
    m_oCrossDC.DeleteDC();
    m_oItalicFont.DeleteObject();
    m_oBoldFont.DeleteObject();
}

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

BOOL CDlgZonesActivation::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_LIST_ZA, "TAC_ZAM_ZA_LIST", TUT_e_ListBox );
    TUT_M_vRegisterControlID( IDC_LIST_ZONES, "TAC_ZAM_ZA_LISTZONES", TUT_e_ListBox );
    TUT_M_vRegisterControlID( IDC_BT_NEW, "TAC_ZAM_ZA_BTNEW", TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_RENAME, "TAC_ZAM_ZA_BTRENAME", TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_COPY, "TAC_ZAM_ZA_BTCOPY", TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_DELETE, "TAC_ZAM_ZA_BTDELETE", TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_DEFAULT, "TAC_ZAM_ZA_BTDEFAULT", TUT_e_Button );
    //
    return bResult;
}

BEGIN_MESSAGE_MAP(CDlgZonesActivation, CFormView)
	//{{AFX_MSG_MAP(CDlgZonesActivation)
    ON_WM_DESTROY()
	ON_WM_DRAWITEM()
	ON_WM_SHOWWINDOW()
	ON_LBN_SELCHANGE(IDC_LIST_ZA, OnSelchangeZAList)
	ON_BN_CLICKED(IDC_BT_NEW, OnBtAdd)
	ON_BN_CLICKED(IDC_BT_RENAME, OnBtRename)
	ON_BN_CLICKED(IDC_BT_COPY, OnBtCopy)
	ON_BN_CLICKED(IDC_BT_DELETE, OnBtDelete)
    ON_BN_CLICKED(IDC_BT_DEFAULT, OnBtDefault)
	ON_WM_SIZE()
	ON_LBN_DBLCLK(IDC_LIST_ZA, OnDblclkListZa)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgZonesActivation diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CDlgZonesActivation message handlers

void CDlgZonesActivation::OnDestroy()
{
    // register controls
    TUT_M_vGetTutDll();
    TUT_M_vUnregisterControlID( IDC_LIST_ZA );
    TUT_M_vUnregisterControlID( IDC_LIST_ZONES );	
    TUT_M_vUnregisterControlID( IDC_BT_NEW );
    TUT_M_vUnregisterControlID( IDC_BT_RENAME );
    TUT_M_vUnregisterControlID( IDC_BT_COPY );
    TUT_M_vUnregisterControlID( IDC_BT_DELETE );
    TUT_M_vUnregisterControlID( IDC_BT_DEFAULT );
    //
    CFormView::OnDestroy();
}
// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
BOOL CDlgZonesActivation::PreTranslateMessage(MSG* pMsg)
{
    BOOL bResult = FALSE;

    if ( (pMsg->hwnd == GetDlgItem(IDC_LIST_ZONES)->m_hWnd) &&  // mouse under Zones List
         (m_p_oCurrentZA) )
    {
        if(pMsg->message == WM_LBUTTONDOWN)
            bResult = mfn_bOnLButtonDownLBZones ( pMsg ) ;
        else if(pMsg->message == WM_KEYDOWN)
            bResult = mfn_bOnKeyDownLBZones( pMsg );
        else
            bResult = CFormView::PreTranslateMessage(pMsg);
    }
    if ( (pMsg->hwnd == GetDlgItem(IDC_LIST_ZA)->m_hWnd) )  // mouse under ZA List
    {
        if(pMsg->message == WM_RBUTTONDOWN)
            bResult = mfn_bOnRButtonDownLBZA ( pMsg ) ;
        else
            bResult = CFormView::PreTranslateMessage(pMsg);
    }
    else
        bResult = CFormView::PreTranslateMessage(pMsg);

	return bResult;
}

// -----------------------------------------------------------------------------
// Description : intercept the popup menu entry
// ---------------------------------------------------------------------------*/
BOOL CDlgZonesActivation::OnCommand(WPARAM wParam, LPARAM lParam)
{
	switch(LOWORD(wParam))
    {
    case C_uiPMAdd:     OnBtAdd();      break;
    case C_uiPMCopy:    OnBtCopy();     break;
    case C_uiPMDel:     OnBtDelete();   break;
    case C_uiPMRename:  OnBtRename();   break;
    case C_uiPMDefault: OnBtDefault();  break;
    default :
    	return CFormView::OnCommand(wParam, lParam);
    }

    return TRUE;
}

// ----------------------------------------------------------------------------
// Description : handle function for message WM_DRAWITEM
// ----------------------------------------------------------------------------
void CDlgZonesActivation::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDIS)
{
    if ( (nIDCtl == IDC_LIST_ZONES) &&
         (M_LBZonesList()->IsWindowEnabled() ) &&
         (lpDIS->itemID != LB_ERR) )
    {
        OnDrawZonesListItem( lpDIS );
    }
    else if ( (nIDCtl == IDC_LIST_ZA) &&
              (M_LBZA()->IsWindowEnabled() ) &&
              (lpDIS->itemID != LB_ERR) )
    {
        OnDrawZAListItem( lpDIS );
    }
    else
        CFormView::OnDrawItem(nIDCtl, lpDIS);
}

// ----------------------------------------------------------------------------
// Description :handle function for message WM_INITDIALOG
// ----------------------------------------------------------------------------
void CDlgZonesActivation::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CFormView::OnShowWindow(bShow, nStatus);
    /*
    if(bShow && m_bFirstShow)
    {
        mfn_vSetCurrentNamesList( NULL );
    }
    m_bFirstShow = FALSE;
    */
}

// ----------------------------------------------------------------------------
// Description : handle function for message WM_SIZE
// ----------------------------------------------------------------------------
void CDlgZonesActivation::OnSize(UINT nType, int cx, int cy)
{
    //if( cx < 300 ) cx = 300;
    //if( cy < 170 ) cy = 170;
	CFormView::OnSize(nType, cx, cy);

    if( M_LBZA() )
    {
        CRect oRectDlg, oRectLBZA, oRectLBZn, oRectSTZn;
        CSize oSize;

        // get current pos
        M_GetClientRect( this, &oRectDlg );
        M_GetClientRectId( IDC_LIST_ZA, &oRectLBZA );
        M_GetClientRectId( IDC_LIST_ZONES, &oRectLBZn );
        M_GetClientRectId( IDC_ST_ZONES, &oRectSTZn );

        // minimum size
        oSize = oRectDlg . Size();
        if( cx < 350 ) oRectDlg . right = abs( oRectDlg . left ) + 350;
        if( cy < 170 ) oRectDlg . bottom = abs( oRectDlg . top ) + 170;

        // place LBs
        oSize . cx = (oRectDlg . Width() - (C_iBorderSize<<2)) >> 1;

        oRectLBZA . left = oRectDlg . left + C_iBorderSize;
        oRectLBZA . right = oRectLBZA . left + oSize . cx;
        oRectLBZn . right = oRectDlg . right - C_iBorderSize;
        oRectLBZn . left = oRectLBZn . right - oSize . cx;

        oRectLBZA . bottom = oRectLBZn . bottom = oRectDlg . bottom - C_iBorderSize;

        // place Static
        oSize = oRectSTZn . Size();
        oRectSTZn . left = oRectLBZn . left;
        oRectSTZn . right = oRectSTZn . left + oSize . cx;

        // get current pos
        //M_SetClientRect( this, &oRectDlg );
        M_SetClientRectId( IDC_LIST_ZA, &oRectLBZA );
        M_SetClientRectId( IDC_LIST_ZONES, &oRectLBZn );
        M_SetClientRectId( IDC_ST_ZONES, &oRectSTZn );
    }
}

// ----------------------------------------------------------------------------
// Description : handle function for message CBN_SELCHANGE on CB ZAList
// ----------------------------------------------------------------------------
void CDlgZonesActivation::OnSelchangeZAList()
{
    if( !mfn_bVerifyNamesListCoherence( TRUE ) )
        return;

    //
    CListBox *pLB = M_LBZA();
    int iIndex = pLB -> GetCurSel();

    CPA_ZonesActivating *p_oZA = NULL;

    if( iIndex != LB_ERR )
        p_oZA = (CPA_ZonesActivating*) pLB -> GetItemData( iIndex );

    Family_StateChange *p_oZAChange = new Family_StateChange( m_p_oDLL, C_eZA, p_oZA, m_p_oCurrentZA );
    M_MakeDo( p_oZAChange );

    mfn_vUpdateButtons();
}

// ----------------------------------------------------------------------------
// Description : handle function for message LBN_DBLCLK on ListBox ZA
// ----------------------------------------------------------------------------
void CDlgZonesActivation::OnDblclkListZa()
{
    if( m_p_oDlgZAList && m_p_oCurrentZA )
        m_p_oDlgZAList -> mfn_vSetZA( m_p_oCurrentZA, TRUE );
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Owner handle msg functions
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

// ----------------------------------------------------------------------------
// Description : handle function for message WM_DRAWITEM on List box
// ----------------------------------------------------------------------------
void CDlgZonesActivation::OnDrawZonesListItem( LPDRAWITEMSTRUCT lpDIS )
{
    CDC         *pDC = CDC::FromHandle( lpDIS->hDC );
    char        szText[100];
    CListBox    *pLB = M_LBZonesList();
    BOOL        bSelected = (lpDIS->itemState & ODS_SELECTED );
    BOOL        bChecked = (pLB->GetItemData( lpDIS->itemID ) ? TRUE : FALSE);
    CFont       *xFntPrev;

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

    if ( m_p_oCurrentZA )
    {
        if (bChecked)
            pDC->BitBlt( lpDIS->rcItem.left + 2, lpDIS->rcItem.top + 1, 12, 12, &m_oCrossDC, 0, 0, SRCAND );
    }
    else
        xFntPrev = pDC->SelectObject( &m_oItalicFont );

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

    pLB -> GetText(lpDIS->itemID, szText ) ;
    lpDIS->rcItem.left += 16;
    pDC->DrawText( szText, strlen(szText), &lpDIS->rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER );
    lpDIS->rcItem.left -= 1;
    if (bSelected)
        pDC->InvertRect( &lpDIS->rcItem );
    // restore font
    if ( !m_p_oCurrentZA )
        pDC->SelectObject( xFntPrev );

}

// ----------------------------------------------------------------------------
// Description : handle function for message WM_DRAWITEM on List box
// ----------------------------------------------------------------------------
void CDlgZonesActivation::OnDrawZAListItem( LPDRAWITEMSTRUCT lpDIS )
{
    CDC                 *pDC          = CDC::FromHandle( lpDIS->hDC );
    CListBox            *pLB          = M_LBZA();
    BOOL                bSelected     = (lpDIS->itemState & ODS_SELECTED );
    CPA_ZonesActivating *p_oZA        = (CPA_ZonesActivating*) pLB->GetItemData( lpDIS->itemID );
    CPA_BaseObject      *p_oDefaultZA = ( m_p_oCurrentNamesList ? m_p_oCurrentNamesList -> m_fnp_oGetDefaultZA() : NULL );
    CFont               *xFntPrev;
    char                szText[100];

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

    if ( p_oZA == p_oDefaultZA )
        xFntPrev = pDC->SelectObject( &m_oBoldFont );

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

    pLB -> GetText(lpDIS->itemID, szText ) ;
    pDC->DrawText( szText, strlen(szText), &lpDIS->rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER );

    if (bSelected)
        pDC->InvertRect( &lpDIS->rcItem );
    // restore font
    if ( p_oZA == p_oDefaultZA )
        pDC->SelectObject( xFntPrev );

}
// ----------------------------------------------------------------------------
// Description : handle function for message WM_LBUTTONDOWN on ListBox Zones
// ----------------------------------------------------------------------------
BOOL CDlgZonesActivation::mfn_bOnLButtonDownLBZones(MSG *pMsg)
{
    if( !mfn_bVerifyNamesListCoherence( TRUE ) )
        return CFormView::PreTranslateMessage(pMsg);

    //
    CListBox    *pLB = M_LBZonesList();
    POINT       xPos;
    CRect       oRect;
    BOOL        bOutside;
    int         iIndex;
    BOOL        bResult = TRUE;

    xPos.x = LOWORD(pMsg->lParam);
    xPos.y = HIWORD(pMsg->lParam);

    iIndex = pLB -> ItemFromPoint ( xPos, bOutside ) ;
    pLB -> GetItemRect ( iIndex , &oRect ) ;
    if( (oRect . PtInRect ( xPos ) ) && ( xPos.x < 16 )  )
    {
        BOOL bChecked = (pLB->GetItemData( iIndex ) ? TRUE : FALSE);

        if( m_p_oCurrentZA )
        {
	        ZA_Modify *p_oZAModif = new ZA_Modify( m_p_oDLL, m_p_oCurrentZA, iIndex, !bChecked );
            M_MakeDo( p_oZAModif ) ;
            bResult = TRUE;
        }
        else
            bResult = CFormView::PreTranslateMessage(pMsg);
    }
    else
        bResult = CFormView::PreTranslateMessage(pMsg);

    pLB -> Invalidate();
    return bResult;
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
BOOL CDlgZonesActivation::mfn_bOnKeyDownLBZones(MSG *pMsg)
{
    if( !mfn_bVerifyNamesListCoherence( TRUE ) )
        return CFormView::PreTranslateMessage(pMsg);

    //
    int         nVirtKey    = (int) pMsg -> wParam;
    CListBox    *pLB        = M_LBZonesList();
    int         iIndex      = pLB -> GetCurSel();
    BOOL        bResult = TRUE;

    if( ( nVirtKey == VK_SPACE ) && ( iIndex != LB_ERR ) && m_p_oCurrentZA )
    {
        BOOL bChecked = ( pLB->GetItemData( iIndex ) ? TRUE : FALSE );
	    ZA_Modify *p_oZAModif = new ZA_Modify( m_p_oDLL, m_p_oCurrentZA, iIndex, !bChecked );
        M_MakeDo( p_oZAModif ) ;
        bResult = TRUE;
    }
    else
        bResult = CFormView::PreTranslateMessage(pMsg);

    return bResult;
}

// ----------------------------------------------------------------------------
// Description : handle function for message WM_RBUTTONDOWN on ListBox ZA
// ----------------------------------------------------------------------------
BOOL CDlgZonesActivation::mfn_bOnRButtonDownLBZA(MSG *pMsg)
{
    if( !mfn_bVerifyNamesListCoherence( TRUE ) )
        return CFormView::PreTranslateMessage(pMsg);

    //
    // create and display the context popmenu
    CMenu oPopMenu;
    POINT xPos;

    xPos.x = LOWORD(pMsg->lParam);
    xPos.y = HIWORD(pMsg->lParam);
    // create
    oPopMenu . CreatePopupMenu () ;
    // fill
    oPopMenu . AppendMenu( MF_STRING + ( M_bEnabled( IDC_BT_NEW ) ? MF_ENABLED : MF_GRAYED ), C_uiPMAdd, "New");
    oPopMenu . AppendMenu( MF_STRING + ( M_bEnabled( IDC_BT_COPY ) ? MF_ENABLED : MF_GRAYED ), C_uiPMCopy, "Copy");
    oPopMenu . AppendMenu( MF_STRING + ( M_bEnabled( IDC_BT_RENAME ) ? MF_ENABLED : MF_GRAYED ), C_uiPMRename, "Rename");
    oPopMenu . AppendMenu( MF_STRING + ( M_bEnabled( IDC_BT_DELETE ) ? MF_ENABLED : MF_GRAYED ), C_uiPMDel, "Delete");
    oPopMenu . AppendMenu( MF_SEPARATOR );
    oPopMenu . AppendMenu( MF_STRING + ( M_bEnabled( IDC_BT_DEFAULT ) ? MF_ENABLED : MF_GRAYED ), C_uiPMDefault, "Set As Default ZA");

    // display
    M_LBZA() -> ClientToScreen(&xPos);
// ALX
    TUT_M_vGetTutDll    ();
    TUT_M_vRegisterMenu (this -> m_hWnd , oPopMenu . m_hMenu , xPos . x , xPos . y);
// End ALX
    oPopMenu . TrackPopupMenu ( TPM_LEFTALIGN|TPM_RIGHTBUTTON, xPos.x, xPos.y, this);
    oPopMenu . DestroyMenu ();
    return CFormView::PreTranslateMessage(pMsg);
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Owner methods
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////


// ----------------------------------------------------------------------------
// Description : add a new ZA
// ----------------------------------------------------------------------------
void CDlgZonesActivation::OnBtAdd()
{
    if( !mfn_bVerifyNamesListCoherence( TRUE ) )
        return;

    //
    HINSTANCE hOldInst = AfxGetResourceHandle();
    AfxSetResourceHandle( m_p_oDLL -> GetDLLIdentity() -> hModule );

    CDlgInputText *p_oDlgInput = new CDlgInputText();
    CRect oRect;
    M_LBZA()->GetWindowRect(&oRect);
    CPoint oPoint(oRect.left, oRect.top);

    p_oDlgInput->SetTitle( "Create new activation" );
    p_oDlgInput->SetMessage("Activation Name :");
    p_oDlgInput->SetPos(&oPoint);
	p_oDlgInput->SetDefaultValue( C_szZATypeName );
	p_oDlgInput->SetSelect( TRUE );
    if(p_oDlgInput->DoModal() == IDOK)
    {
		ZA_New *p_oZANew = new ZA_New( m_p_oDLL, m_p_oCurrentNamesList, p_oDlgInput->GetInputText() );
		M_MakeDo( p_oZANew ) ;
	}
    delete p_oDlgInput;
    AfxSetResourceHandle( hOldInst );
}

// ----------------------------------------------------------------------------
// Description : rename a ZA
// ----------------------------------------------------------------------------
void CDlgZonesActivation::OnBtRename()
{
    if( !mfn_bVerifyNamesListCoherence( TRUE ) )
        return;

    //
    CListBox *pLB = M_LBZA();
    int iIndex = pLB -> GetCurSel();

    if( iIndex != LB_ERR )
    {
		CPA_ZonesActivating *p_oZA = (CPA_ZonesActivating*)pLB -> GetItemData( iIndex );

        HINSTANCE hOldInst = AfxGetResourceHandle();
        AfxSetResourceHandle( m_p_oDLL -> GetDLLIdentity() -> hModule );

        CDlgInputText *p_oDlgInput = new CDlgInputText();
        CRect oRect;
        char szTitle[200];
		char *szZAName = p_oZA->fn_p_szGetName();

        pLB->GetWindowRect(&oRect);
        CPoint oPoint(oRect.left, oRect.top);
        sprintf(szTitle,"Rename activation \"%s\"",szZAName );
        p_oDlgInput->SetTitle(szTitle);
        p_oDlgInput->SetMessage("New Name :");
        p_oDlgInput->SetPos(&oPoint);
		p_oDlgInput->SetDefaultValue( szZAName );
		p_oDlgInput->SetSelect( TRUE );
        if(p_oDlgInput->DoModal() == IDOK)
        {
	        ZA_Rename *p_oZARename = new ZA_Rename(m_p_oDLL, p_oZA, p_oDlgInput->GetInputText());
	        M_MakeDo( p_oZARename ) ;
        }
        delete p_oDlgInput;
        AfxSetResourceHandle( hOldInst );
    }
}

// ----------------------------------------------------------------------------
// Description : copy current ZA
// ----------------------------------------------------------------------------
void CDlgZonesActivation::OnBtCopy()
{
    if( !mfn_bVerifyNamesListCoherence( TRUE ) )
        return;

    //
    CListBox *pLB = M_LBZA();
    int iIndex = pLB -> GetCurSel();

    if( iIndex != LB_ERR )
    {
	    ZA_Copy *p_oZACopy = new ZA_Copy( m_p_oDLL, (CPA_ZonesActivating*)pLB -> GetItemData( iIndex ) );
	    M_MakeDo( p_oZACopy ) ;
    }
}

// ----------------------------------------------------------------------------
// Description : delete current ZA
// ----------------------------------------------------------------------------
void CDlgZonesActivation::OnBtDelete()
{
    if( !mfn_bVerifyNamesListCoherence( TRUE ) )
        return;

    //
    CListBox *pLB = M_LBZA();
    int iIndex = pLB -> GetCurSel();

    if( iIndex != LB_ERR )
    {
	    ZA_Delete *p_oZADelete = new ZA_Delete(m_p_oDLL, (CPA_ZonesActivating*)pLB -> GetItemData( iIndex ));
	    M_MakeDo( p_oZADelete ) ;
    }
}

// ----------------------------------------------------------------------------
// Description : set current ZA as default ZA of current NameList
// ----------------------------------------------------------------------------
void CDlgZonesActivation::OnBtDefault()
{
    if( !mfn_bVerifyNamesListCoherence( TRUE ) )
        return;

    //
    CListBox *pLB = M_LBZA();
    int iIndex = pLB -> GetCurSel();

    if( iIndex != LB_ERR )
    {
	    ZA_Default *p_oZADefault = new ZA_Default(m_p_oDLL, (CPA_ZonesActivating*)pLB -> GetItemData( iIndex ));
	    M_MakeDo( p_oZADefault ) ;
    }
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZonesActivation::mfn_vSetCurrentNamesList(CPA_tdoNameList *_p_oNamesList)
{
    CListBox *pLB = M_LBZA();
    int iIndex = -1;

    m_p_oCurrentNamesList = _p_oNamesList;
    // fill ListBox
	mfn_vFillLBZA( m_p_oCurrentNamesList );
	mfn_vFillLBZones( m_p_oCurrentNamesList );
    // select first ZA
    m_p_oCurrentZA = NULL;
    if( pLB -> GetCount() > 0 )
    {
        iIndex = 0;
        m_p_oCurrentZA = (CPA_ZonesActivating*)pLB -> GetItemData( iIndex );
    }
    // update according to selection
    /*MM
    pLB -> SetCurSel( iIndex );
    mfn_vUpdateButtons();
    M_LBZA() -> Invalidate();
    M_LBZonesList() -> Invalidate();
    MM*/
    m_p_oDLL -> mfn_vSetCurrentZA( m_p_oCurrentZA );
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZonesActivation::mfn_vUpdateDisplay( CPA_ZonesActivating *_p_oZA, int _iIndex )
{
    CListBox *pLBZn = M_LBZonesList();
    CListBox *pLBZA = M_LBZA();

    if( _p_oZA )
    {
        CPA_tdoNameList *p_oNamesList = (CPA_tdoNameList*) _p_oZA -> GetOwner();
		if( p_oNamesList != m_p_oCurrentNamesList )
			mfn_vSetCurrentNamesList( p_oNamesList );
		else
			mfn_vFillLBZA( m_p_oCurrentNamesList );

        mfn_iSelectStringExact( pLBZA, _p_oZA -> GetName() );
		mfn_vUpdateFlags( _p_oZA );
        m_p_oCurrentZA = _p_oZA;
        //
        if( ( _iIndex >= 0 ) && ( _iIndex < pLBZn -> GetCount() ) )
            pLBZn -> SetCurSel( _iIndex );
        else
            pLBZn -> SetCurSel( -1 );
    }
    else
    {
        pLBZn -> SetCurSel( -1 );
		mfn_vFillLBZones( m_p_oCurrentNamesList );
        m_p_oCurrentZA = _p_oZA;
        pLBZA -> SetCurSel( -1 );
    }
    /*
    if( m_p_oDlgZAList )
        m_p_oDlgZAList -> mfn_vSetZA( m_p_oCurrentZA, FALSE );
    */
    mfn_vUpdateButtons();
	pLBZn -> Invalidate();
	pLBZA -> Invalidate();
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

// ----------------------------------------------------------------------------
// Description : fill CB ZAList
// ----------------------------------------------------------------------------
void CDlgZonesActivation::mfn_vFillLBZA( CPA_tdoNameList *_p_oNamesList )
{
    CListBox *pLB = M_LBZA();

    pLB -> ResetContent();
    if ( _p_oNamesList )
    {
        // get list
        CPA_List<CPA_BaseObject> oList;
        m_p_oDLL -> fn_lFindObjects (&oList, "", C_szZATypeName, (CPA_BaseObject*)_p_oNamesList );
        // fill comboBox
        POSITION xPos = oList . GetHeadPosition();
        while ( xPos )
        {
            CPA_ZonesActivating *p_oZA = (CPA_ZonesActivating*) oList . GetNext( xPos );
            int iIndex = pLB -> AddString( p_oZA -> GetName() );
            if( iIndex != LB_ERR )
                pLB -> SetItemData( iIndex, (DWORD) p_oZA );
        }
    }
}

// ----------------------------------------------------------------------------
// Description : fill LB Zones ( names )
// ----------------------------------------------------------------------------
void CDlgZonesActivation::mfn_vFillLBZones( CPA_tdoNameList *_p_oNamesList )
{
    CListBox *pLB = M_LBZonesList();

    pLB -> ResetContent();

    if ( _p_oNamesList )
    {
        POSITION stPos = _p_oNamesList -> GetHeadPosition();
        while ( stPos )
        {
            tdoObjectName *p_oName  = _p_oNamesList -> GetNext( stPos );

            int iIndex = pLB -> AddString( p_oName -> GetName() );
            if( iIndex != LB_ERR )
                pLB -> SetItemData( iIndex, (DWORD) FALSE );
        }
    }
	pLB -> Invalidate();
}

// ----------------------------------------------------------------------------
// Description : update flags with ZAList
// ----------------------------------------------------------------------------
void CDlgZonesActivation::mfn_vUpdateFlags( CPA_ZonesActivating *_p_oZA )
{
    CListBox            *pLB        = M_LBZonesList();
    int                  iMaxIndex  = pLB -> GetCount();

    // reset flags
    for ( int iIdx = 0 ; iIdx < iMaxIndex ; iIdx++ )
        pLB -> SetItemData( iIdx, (DWORD) FALSE );

    // update flags
    if ( _p_oZA )
    {
        POSITION stPos = _p_oZA -> m_oListOfZDxNameIdx . GetHeadPosition();
        while ( stPos )
        {
            CPA_ZdxIndex *p_oZdxIndex = _p_oZA -> m_oListOfZDxNameIdx . GetNext( stPos ) ;

            int iIndex = (int)p_oZdxIndex -> mfn_usGetIndex();
            if( ( iIndex >= 0 ) && ( iIndex < iMaxIndex ) )
                pLB -> SetItemData( iIndex, (DWORD) TRUE );
        }
    }
    mfn_vUpdateButtons();
	pLB -> Invalidate();
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZonesActivation::mfn_vUpdateButtons(void)
{
	int iIndex = M_LBZA() -> GetCurSel();

    M_Enable(IDC_BT_NEW, (BOOL)m_p_oCurrentNamesList );
    M_Enable(IDC_BT_COPY, (iIndex != LB_ERR) );
    M_Enable(IDC_BT_RENAME, (iIndex != LB_ERR) );
    M_Enable(IDC_BT_DELETE, (iIndex != LB_ERR) );
    if( ( iIndex != LB_ERR ) && (BOOL)m_p_oCurrentNamesList )
    {
        M_Enable(IDC_BT_DEFAULT, ( ((CPA_BaseObject*)M_LBZA()->GetItemData(iIndex)) != m_p_oCurrentNamesList -> m_fnp_oGetDefaultZA() ) );
    }
    else
        M_Enable(IDC_BT_DEFAULT, FALSE );
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
int CDlgZonesActivation::mfn_iSelectStringExact(CListBox *_pLB, CString _csEntry)
{

    int iIndex = _pLB -> FindStringExact( -1, _csEntry );
    if( iIndex != LB_ERR )
        _pLB -> SetCurSel( iIndex );
    return iIndex;
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
BOOL CDlgZonesActivation::mfn_bVerifyNamesListCoherence(BOOL _bUpdateDisplay /*=TRUE*/)
{
    CListBox *pLB = M_LBZA();
    CPA_BaseObject *p_oZAList = m_p_oDLL -> mfn_p_oGetCurrentZAList();

    if ( (p_oZAList) && (pLB -> GetCount() > 0) )
    {
        CPA_BaseObject *p_oZANamesList = ((CPA_BaseObject*) pLB -> GetItemData( 0 ) ) -> GetOwner();
        CPA_BaseObject *p_oZAListNamesList = p_oZAList -> GetOwner();

        if( p_oZANamesList != p_oZAListNamesList )
        {
            if( _bUpdateDisplay )
                mfn_vSetCurrentNamesList( m_p_oDLL -> mfn_p_oGetCurrentNamesList() );
            return FALSE;
        }
    }
    return TRUE;
}