/*=============================================================================
 *
 *  Filename:           DlgActLs.cpp : implementation file
 *  Version:            1.0
 *  Date:				02/01/97
 *  Author:             M.T.
 *
 *  Description:		implementation of a FormView Dialog class for action list
 *						on STATE MODE
 *===========================================================================*/

#include "stdafx.h"

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

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

#include "DlgActLs.hpp"
#include "DlgInput.hpp"
#include "ACModif.hpp"
#include "ACInterf.hpp"
#include "ACModif.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

// Popup Menu ID
#define C_uiPMNew           2000
#define C_uiPMRename        (C_uiPMNew + 1)
#define C_uiPMCopy          (C_uiPMNew + 2)
#define C_uiPMDel           (C_uiPMNew + 3)
#define C_uiPMDelAll        (C_uiPMNew + 4)
#define C_uiPMInit          (C_uiPMNew + 5)

/*=============================================================================
 MACROS
=============================================================================*/
#define M_GetCurrentFamily()            (m_p_oParentDLL -> mfn_p_oGetCurrentFamily())
#define M_GetCurrentAction()            (m_p_oParentDLL -> mfn_p_oGetCurrentAction())

#define M_Enable( nID, bEnable )        GetDlgItem( nID ) -> EnableWindow( (bEnable) )
#define M_bEnabled( nID )               GetDlgItem( nID ) -> IsWindowEnabled()
#define M_EnableMenu( bEnable )         ( (bEnable) ? MF_ENABLED : MF_GRAYED )

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

/*=============================================================================
 CONSTANTS
=============================================================================*/
#define C_iBorderSize   2

/*=============================================================================
* CDlgActionList class
=============================================================================*/
IMPLEMENT_DYNCREATE(CDlgActionList, CFormView)

/*-----------------------------------------------------------------------------
 *  Description : constructor
 *---------------------------------------------------------------------------*/
CDlgActionList::CDlgActionList() : CFormView(CDlgActionList::IDD)
{
	//{{AFX_DATA_INIT(CDlgActionList)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
    // create bold font
    PLOGFONT plfBold = (PLOGFONT) LocalAlloc(LPTR, sizeof(LOGFONT));
    lstrcpy(plfBold->lfFaceName, "MS Sans Serif");
    plfBold->lfWeight = FW_BOLD ;
    plfBold->lfHeight = 8;
    plfBold->lfEscapement = 0;
    m_oBoldFont.CreateFontIndirect(plfBold);
}

/*-----------------------------------------------------------------------------
 *  Description : destructor
 *---------------------------------------------------------------------------*/
CDlgActionList::~CDlgActionList()
{
    // delete bold font
    m_oBoldFont.DeleteObject();
}

/*-----------------------------------------------------------------------------
 *  Description : data exchange
 *---------------------------------------------------------------------------*/
void CDlgActionList::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgActionList)
	//DDX_Control(pDX, IDC_LIST_ACTION, m_CTRL_ListAction);
	//}}AFX_DATA_MAP
}

/*-----------------------------------------------------------------------------
 *  Description : create & register controls for TUT module
 *---------------------------------------------------------------------------*/
BOOL CDlgActionList::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_ACTION,      "TAC_STM_ACTIONSLIST_LIST",         TUT_e_ListBox );
    TUT_M_vRegisterControlID( IDC_BT_NEWACTION,     "TAC_STM_ACTIONSLIST_BTNEW",        TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_INITACTION,    "TAC_STM_ACTIONSLIST_BTDEFAULT",    TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_DELACTION,     "TAC_STM_ACTIONSLIST_BTDELETE",     TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_DELALLACTION,  "TAC_STM_ACTIONSLIST_BTDELALL",     TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_COPYACTION,    "TAC_STM_ACTIONSLIST_BTCOPY",       TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_RENAMEACTION,  "TAC_STM_ACTIONSLIST_BTRENAME",     TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_EDITZA,        "TAC_STM_ACTIONSLIST_BTSWAPMODE",   TUT_e_Button );
    //
    return bResult;
}

/*-----------------------------------------------------------------------------
 *  Description : message map
 *---------------------------------------------------------------------------*/
BEGIN_MESSAGE_MAP(CDlgActionList, CFormView)
	//{{AFX_MSG_MAP(CDlgActionList)
    ON_WM_DESTROY()
	ON_LBN_SELCHANGE(IDC_LIST_ACTION, OnSelchangeListAction)
	ON_BN_CLICKED(IDC_BT_COPYACTION, OnBtCopyaction)
	ON_BN_CLICKED(IDC_BT_DELACTION, OnBtDelaction)
	ON_BN_CLICKED(IDC_BT_DELALLACTION, OnBtDelallaction)
	ON_BN_CLICKED(IDC_BT_NEWACTION, OnBtNewaction)
	ON_BN_CLICKED(IDC_BT_RENAMEACTION, OnBtRenameaction)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_BT_EDITZA, OnBtEditza)
	ON_BN_CLICKED(IDC_BT_INITACTION, OnBtInitaction)
	ON_WM_DRAWITEM()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


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

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

/******************************************************************************
* CDlgActionList message handlers
******************************************************************************/

/*-----------------------------------------------------------------------------
 *  Description : unregister control from TUT
 *---------------------------------------------------------------------------*/
void CDlgActionList::OnDestroy()
{
    TUT_M_vGetTutDll();
    TUT_M_vUnregisterControlID( IDC_LIST_ACTION );
    TUT_M_vUnregisterControlID( IDC_BT_COPYACTION );
    TUT_M_vUnregisterControlID( IDC_BT_DELACTION );
    TUT_M_vUnregisterControlID( IDC_BT_DELALLACTION );
    TUT_M_vUnregisterControlID( IDC_BT_NEWACTION );
    TUT_M_vUnregisterControlID( IDC_BT_RENAMEACTION );
    TUT_M_vUnregisterControlID( IDC_BT_INITACTION );
    TUT_M_vUnregisterControlID( IDC_BT_EDITZA );

    CFormView::OnDestroy();
}

/*-----------------------------------------------------------------------------
 *  Description : intercept the Rbutton Click on listbox
 *---------------------------------------------------------------------------*/
BOOL CDlgActionList::PreTranslateMessage(MSG* pMsg)
{
    if( pMsg->hwnd == GetDlgItem(IDC_LIST_ACTION)->m_hWnd )
    {
        if( pMsg->message == WM_RBUTTONDOWN )
        {
            OnRButtonDownActionList( pMsg );
        }
        else if ( pMsg->message == WM_KEYDOWN )
        {
            OnKeyDownActionList( pMsg );
        }
    }
	return CFormView::PreTranslateMessage(pMsg);
}

/*-----------------------------------------------------------------------------
 *  Description : intercept the popup menu entry
 *---------------------------------------------------------------------------*/
BOOL CDlgActionList::OnCommand(WPARAM wParam, LPARAM lParam)
{
	switch(LOWORD(wParam))
    {
    case C_uiPMNew:     OnBtNewaction();        break;
    case C_uiPMRename:  OnBtRenameaction();     break;
    case C_uiPMCopy:    OnBtCopyaction();       break;
    case C_uiPMDel:     OnBtDelaction();        break;
    case C_uiPMDelAll:  OnBtDelallaction();     break;
    case C_uiPMInit:    OnBtInitaction();       break;
    default :
    	return CFormView::OnCommand(wParam, lParam);
    }

    return TRUE;
}

/*-----------------------------------------------------------------------------
 *  Description : handle function for msg WM_DRAWITEM
 *---------------------------------------------------------------------------*/
void CDlgActionList::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDIS)
{
    if ( (lpDIS->itemID != LB_ERR) &&
         (nIDCtl == IDC_LIST_ACTION) &&
         (GetDlgItem(IDC_LIST_ACTION)->IsWindowEnabled() ) )
	{
		char		szText[100];
        CListBox    *pLB = (CListBox *) GetDlgItem(IDC_LIST_ACTION);;
		CBrush		oBrush;
		COLORREF	xTextColor;
		BOOL		bSelected = (lpDIS->itemState & ODS_SELECTED );
        CFont       *xFntPrev;
        CDC         *pDC = CDC::FromHandle(lpDIS->hDC);
        CString     oCst;
        BOOL        bFontChanged = FALSE;
        CPA_Action  *m_p_oAction = (CPA_Action*) pLB -> GetItemData( lpDIS->itemID );

		oBrush.CreateSolidBrush( GetSysColor( bSelected ? COLOR_HIGHLIGHT : COLOR_WINDOW) );
		xTextColor = GetSysColor( bSelected ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT);
// Shaitan => state list in the level
		CPA_Family * pFamily = (CPA_Family *) m_p_oAction->GetOwner();
		if (!pFamily->m_oListOfUsedActions.IsEmpty() && !pFamily->m_oListOfUsedActions.Find(m_p_oAction))
			xTextColor = RGB(255,0,0);
// End Shaitan => state list in the level

		pDC->FillRect( &lpDIS->rcItem, &oBrush );

        pLB->GetText( lpDIS->itemID, szText );

        // change font
        if ( M_GetCurrentFamily() && (m_p_oAction == M_GetCurrentFamily() -> mfn_p_oGetInitialAction()) )
        {
            xFntPrev = pDC->SelectObject( &m_oBoldFont );
            bFontChanged = TRUE;
            oCst . Format ( "%s" , szText );
        }
        else
            oCst . Format ( "%s" , szText );

		pDC->SetBkMode( TRANSPARENT);
		pDC->SetTextColor( xTextColor );
        lpDIS->rcItem.left += 2;
		pDC->DrawText( oCst, &lpDIS->rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER );
        // restore font
        if ( bFontChanged )
            pDC->SelectObject( xFntPrev );

		oBrush.DeleteObject();
	}
    else
        CFormView::OnDrawItem(nIDCtl, lpDIS);
}

/*-----------------------------------------------------------------------------
 *  Description : handle function for msg WM_SIZE
 *---------------------------------------------------------------------------*/
void CDlgActionList::OnSize(UINT nType, int cx, int cy)
{
    if(GetDlgItem(IDC_LIST_ACTION))
    {
        CRect oRectDlg, oRectList, oRectBTSwap, oRectBTNew, oRectBTCopy, oRectBTDel, oRectBTRen, oRectBTDelAll, oRectBTInit ;
        int iSize;

        // get pos
        M_GetClientRect( this, &oRectDlg );
        M_GetClientRectId( IDC_BT_EDITZA, &oRectBTSwap );
        M_GetClientRectId( IDC_BT_NEWACTION, &oRectBTNew );
        M_GetClientRectId( IDC_BT_COPYACTION, &oRectBTCopy );
        M_GetClientRectId( IDC_BT_DELACTION, &oRectBTDel );
        M_GetClientRectId( IDC_BT_INITACTION, &oRectBTInit );
        M_GetClientRectId( IDC_BT_RENAMEACTION, &oRectBTRen );
        M_GetClientRectId( IDC_BT_DELALLACTION, &oRectBTDelAll );
        M_GetClientRectId( IDC_LIST_ACTION, &oRectList );

        // Left Align
        oRectBTSwap . left = oRectBTNew . left = oRectBTInit . left = oRectList . left = oRectDlg . left + C_iBorderSize;
        // Right Align
        oRectBTSwap . right = oRectBTDel . right = oRectBTDelAll . right = oRectList . right = oRectDlg . right - C_iBorderSize;
        // Bottom Align
        oRectList . bottom = max ( oRectList . top, oRectDlg . bottom - C_iBorderSize);

        // BTs
        iSize = (oRectDlg . Width() - (C_iBorderSize<<2)) / 3;

        oRectBTInit . right  = oRectBTNew . right    = oRectBTNew . left + iSize;
        oRectBTDelAll . left = oRectBTDel . left     = oRectBTDel . right - iSize ;
        oRectBTRen . left    = oRectBTCopy . left    = oRectBTNew . right + C_iBorderSize;
        oRectBTRen . right   = oRectBTCopy . right   = oRectBTDel . left - C_iBorderSize;

        // set pos
        M_SetClientRectId( IDC_BT_EDITZA, &oRectBTSwap );
        M_SetClientRectId( IDC_BT_NEWACTION, &oRectBTNew );
        M_SetClientRectId( IDC_BT_COPYACTION, &oRectBTCopy );
        M_SetClientRectId( IDC_BT_DELACTION, &oRectBTDel );
        M_SetClientRectId( IDC_BT_INITACTION, &oRectBTInit );
        M_SetClientRectId( IDC_BT_RENAMEACTION, &oRectBTRen );
        M_SetClientRectId( IDC_BT_DELALLACTION, &oRectBTDelAll );
        M_SetClientRectId( IDC_LIST_ACTION, &oRectList );
    }
}

/*-----------------------------------------------------------------------------
 *  Description : selected action change in action list
 *---------------------------------------------------------------------------*/
void CDlgActionList::OnSelchangeListAction()
{
	CListBox	*pLB = (CListBox *) GetDlgItem(IDC_LIST_ACTION);
    CPA_Action  *p_oAction;
	int			iIndex;

	if ((iIndex = pLB->GetCurSel()) == LB_ERR)
	{
		fn_vUpdateButtons( FALSE ) ;
		p_oAction = NULL;
	}
	else
	{
		fn_vUpdateButtons( TRUE ) ;
		p_oAction = (CPA_Action*) pLB->GetItemData(iIndex);
	}

	m_p_oParentDLL->mfn_vChangeAction( p_oAction );

}

/*-----------------------------------------------------------------------------
 *  Description : handle function for message BN_CLICKED on Edit ZA button
 *---------------------------------------------------------------------------*/
void CDlgActionList::OnBtEditza()
{
    Family_StateChange *p_oChangeMode = new Family_StateChange( m_p_oParentDLL, C_eMode, (void*)FALSE, (void*)TRUE );
    M_MakeDo( p_oChangeMode );
}

/*-----------------------------------------------------------------------------
 *  Description : handle function for message BN_CLICKED on CopyAction button
 *---------------------------------------------------------------------------*/
void CDlgActionList::OnBtCopyaction()
{
    Action_Copy *p_oActionCopy = new Action_Copy ( m_p_oParentDLL, M_GetCurrentFamily(), M_GetCurrentAction() ) ;
    M_MakeDo ( p_oActionCopy ) ;
}

/*-----------------------------------------------------------------------------
 *  Description : handle function for message BN_CLICKED on DelAction button
 *---------------------------------------------------------------------------*/
void CDlgActionList::OnBtDelaction()
{
    CPA_Action * p_oAction = M_GetCurrentAction();

    if(p_oAction)
    {
        char szText[100];

        sprintf(szText,"Do you really want to delete action \"%s\" ?",p_oAction -> GetName() );
        if( MessageBox(szText,"WARNING", MB_ICONQUESTION+MB_YESNO) == IDYES )
        {
            Action_Delete *p_oActionDelete = new Action_Delete( m_p_oParentDLL, M_GetCurrentFamily(), p_oAction ) ;
            M_MakeDo ( p_oActionDelete ) ;
        }
    }
}

/*-----------------------------------------------------------------------------
 *  Description : handle function for message BN_CLICKED on DelAllActions button
 *---------------------------------------------------------------------------*/
void CDlgActionList::OnBtDelallaction()
{
    if( (M_GetCurrentFamily()) &&
        (MessageBox("Do you really want to delete all actions ?", "WARNING", MB_ICONQUESTION+MB_YESNO) == IDYES ) )
    {
        Family_DelAllActions *p_oDelAllActions = new Family_DelAllActions ( m_p_oParentDLL, M_GetCurrentFamily() ) ;
        M_MakeDo( p_oDelAllActions ) ;
    }

}

/*-----------------------------------------------------------------------------
 *  Description : handle function for message BN_CLICKED on "New" button
 *---------------------------------------------------------------------------*/
void CDlgActionList::OnBtNewaction()
{
	// get new action name
    HINSTANCE hOldInst = AfxGetResourceHandle();
    AfxSetResourceHandle( m_p_oParentDLL -> GetDLLIdentity() -> hModule );

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

    p_oDlgInput->SetPos( &oPoint );
    p_oDlgInput->SetTitle( "Create a new action" );
    p_oDlgInput->SetMessage( "New Action Name :" );
	p_oDlgInput->SetDefaultValue( C_szActionTypeName );
	p_oDlgInput->SetSelect( TRUE );
    if(p_oDlgInput->DoModal() == IDOK)
	{
		//
		Action_New *p_oActionNew = new Action_New ( m_p_oParentDLL, M_GetCurrentFamily() , p_oDlgInput -> GetInputText() ) ;
		M_MakeDo( p_oActionNew ) ;
	}
	delete p_oDlgInput;
	AfxSetResourceHandle( hOldInst );
}

/*-----------------------------------------------------------------------------
 *  Description : handle function for message BN_CLICKED on "Rename" button
 *---------------------------------------------------------------------------*/
void CDlgActionList::OnBtRenameaction()
{
    if(M_GetCurrentAction())
    {
        HINSTANCE hOldInst = AfxGetResourceHandle();
        AfxSetResourceHandle( m_p_oParentDLL -> GetDLLIdentity() -> hModule );

        CDlgInputText *p_oDlgInput = new CDlgInputText();
        char szTitle[200];
        CRect oRect;
        GetDlgItem(IDC_LIST_ACTION)->GetWindowRect(&oRect);
        CPoint oPoint( oRect.left, oRect.top);

        p_oDlgInput->SetPos(&oPoint);
        sprintf(szTitle,"Rename action \"%s\"", M_GetCurrentAction()->fn_p_szGetName() );
        p_oDlgInput->SetTitle(szTitle);
        p_oDlgInput->SetMessage("New Name :");
		p_oDlgInput->SetDefaultValue( M_GetCurrentAction()->fn_p_szGetName() );
		p_oDlgInput->SetSelect( TRUE );
        if(p_oDlgInput->DoModal() == IDOK)
        {
            Action_Rename *p_oActionRename = new Action_Rename ( m_p_oParentDLL, M_GetCurrentAction() , p_oDlgInput->GetInputText() ) ;
            M_MakeDo ( p_oActionRename ) ;
        }
        delete p_oDlgInput;
        AfxSetResourceHandle( hOldInst );
    }
}

/*-----------------------------------------------------------------------------
 *  Description : handle function for message BN_CLICKED on "Set as Initial" button
 *---------------------------------------------------------------------------*/
void CDlgActionList::OnBtInitaction()
{
    Family_ModifInitialAction *p_oActionInit = new Family_ModifInitialAction ( m_p_oParentDLL, M_GetCurrentFamily(), M_GetCurrentAction() ) ;
    M_MakeDo ( p_oActionInit ) ;
}

/******************************************************************************
* CDlgActionList specific functions
******************************************************************************/

/*-----------------------------------------------------------------------------
 *  Description : change action list with the given one
 *---------------------------------------------------------------------------*/
void CDlgActionList::fn_vSetActionList( CPA_List<CPA_Action> *_p_oListOfActions )
{
	CListBox *pLB = (CListBox *) GetDlgItem(IDC_LIST_ACTION);

	// emptied list box
	pLB->ResetContent();

    if(_p_oListOfActions)
    {
	    // for each action in list add it in list box
        POSITION stPos = _p_oListOfActions->GetHeadPosition ( ) ;
        while ( stPos )
        {
            CPA_Action *p_oAction = (CPA_Action *) _p_oListOfActions->GetNext ( stPos ) ;
		    int iIndex = pLB->AddString ( p_oAction->GetName () ) ;
            if( iIndex != LB_ERR )
                pLB->SetItemData( iIndex, (DWORD) p_oAction ) ;
        }
    }
    // select first item
    CPA_Action *p_oAction = NULL;
    int iIndex = -1;
    if( pLB -> GetCount() > 0 )
    {
        iIndex = 0;
        p_oAction = (CPA_Action*) pLB -> GetItemData( iIndex );
    }
    // update selection
    /*
    pLB->SetCurSel( iIndex );
    fn_vUpdateButtons( p_oAction != NULL );
     */
    m_p_oParentDLL -> mfn_vSetCurrentAction( p_oAction );
}

/*-----------------------------------------------------------------------------
 *  Description : select given action
 *---------------------------------------------------------------------------*/
void CDlgActionList::fn_vSetAction(CPA_Action *_p_oAction)
{
	CListBox *pLB = (CListBox *) GetDlgItem(IDC_LIST_ACTION);

    if(_p_oAction)
    {
        int iIndex = pLB -> FindStringExact( -1 , _p_oAction -> GetName() ) ;

        if( iIndex != LB_ERR )
        {
            pLB -> SetCurSel( iIndex );
            fn_vUpdateButtons( TRUE ) ;
        }
        else
        {
            pLB -> SetCurSel( -1 );
            fn_vUpdateButtons( FALSE ) ;
        }
    }
    else
    {
        pLB -> SetCurSel( -1 );
        fn_vUpdateButtons( FALSE ) ;
    }

}

/*-----------------------------------------------------------------------------
 *  Description : enable or disable buttons
 *---------------------------------------------------------------------------*/
void CDlgActionList::fn_vUpdateButtons(BOOL _bEnabled)
{
	CListBox *pLB = (CListBox *) GetDlgItem(IDC_LIST_ACTION);

    M_Enable( IDC_BT_NEWACTION, M_GetCurrentFamily() != NULL );
    M_Enable( IDC_BT_DELALLACTION, pLB && pLB->GetCount() );
    M_Enable( IDC_BT_COPYACTION, _bEnabled );
    M_Enable( IDC_BT_RENAMEACTION, _bEnabled );
    M_Enable( IDC_BT_DELACTION, _bEnabled );
    M_Enable( IDC_BT_INITACTION, _bEnabled && (M_GetCurrentAction() != M_GetCurrentFamily() -> mfn_p_oGetInitialAction() ) );
}

// ----------------------------------------------------------------------------
// Description :    create and display the context popmenu
// ----------------------------------------------------------------------------
void CDlgActionList::OnRButtonDownActionList(MSG *pMsg)
{
    CMenu       oPopMenu;
    POINT       xPos;

    oPopMenu . CreatePopupMenu () ;
    // fill
    oPopMenu . AppendMenu ( MF_STRING + M_EnableMenu( M_bEnabled( IDC_BT_NEWACTION ) ),     C_uiPMNew     , "New Action" ) ;
    oPopMenu . AppendMenu ( MF_STRING + M_EnableMenu( M_bEnabled( IDC_BT_RENAMEACTION ) ),  C_uiPMRename   , "Rename Action" ) ;
    oPopMenu . AppendMenu ( MF_STRING + M_EnableMenu( M_bEnabled( IDC_BT_COPYACTION ) ) ,   C_uiPMCopy    , "Copy Action" ) ;
    oPopMenu . AppendMenu ( MF_STRING + M_EnableMenu( M_bEnabled( IDC_BT_DELACTION ) ) ,    C_uiPMDel     , "Delete Action" ) ;
    oPopMenu . AppendMenu ( MF_STRING + M_EnableMenu( M_bEnabled( IDC_BT_DELALLACTION ) ) , C_uiPMDelAll  , "Delete all Actions" ) ;
    oPopMenu . AppendMenu ( MF_SEPARATOR );
    oPopMenu . AppendMenu ( MF_STRING + M_EnableMenu( M_bEnabled( IDC_BT_INITACTION ) ) ,   C_uiPMInit    , "Set as Initial Action of Family" ) ;
    // display
    xPos.x = LOWORD(pMsg->lParam);
    xPos.y = HIWORD(pMsg->lParam);
    GetDlgItem(IDC_LIST_ACTION)->ClientToScreen(&xPos);
	// register for TUT module
    TUT_M_vGetTutDll    ();
    TUT_M_vRegisterMenu (this -> m_hWnd , oPopMenu . m_hMenu , xPos.x, xPos.y);
    oPopMenu . TrackPopupMenu ( TPM_LEFTALIGN|TPM_RIGHTBUTTON, xPos.x, xPos.y, this);
    oPopMenu . DestroyMenu ();
}

// ----------------------------------------------------------------------------
// Description : test keyboard
// ----------------------------------------------------------------------------
void CDlgActionList::OnKeyDownActionList(MSG *pMsg)
{
    int nVirtKey = (int)pMsg->wParam;

    if( ( nVirtKey == VK_DELETE ) && M_GetCurrentAction() )
        OnBtDelaction();
    else if( nVirtKey == VK_INSERT )
        OnBtNewaction();
}
