// DlgActTr.cpp : implementation file
//

#include "stdafx.h"
#include "DlgActTr.hpp"

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

/*=============================================================================
 MACROS
=============================================================================*/
#define M_BT( nID )     ( (CButton*) GetDlgItem( nID ) )
#define M_Tree()        ( (CTreeCtrl*) GetDlgItem( IDC_TREE_ACT ) )


#define M_GetCurrentFamily()            (m_p_oParentDLL -> mfn_p_oGetCurrentFamily())
#define M_GetCurrentAction()            (m_p_oParentDLL -> mfn_p_oGetCurrentAction())

#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

/////////////////////////////////////////////////////////////////////////////
// CDlgActionTree

IMPLEMENT_DYNCREATE(CDlgActionTree, CFormView)

CDlgActionTree::CDlgActionTree()
	: CFormView(CDlgActionTree::IDD)
{
	//{{AFX_DATA_INIT(CDlgActionTree)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
    //
    m_oBmpDefaultAction . LoadBitmap();
    m_oBmpAction . LoadBitmap();
    m_oBmpDefaultState . LoadBitmap();
    m_oBmpState . LoadBitmap();
    //
    m_stImageList.Create(16, 16, FALSE, 0, 4);
    m_stImageList.Add(&m_oBmpDefaultAction,(CBitmap*)NULL);
    m_stImageList.Add(&m_oBmpAction,(CBitmap*)NULL);
    m_stImageList.Add(&m_oBmpDefaultState,(CBitmap*)NULL);
    m_stImageList.Add(&m_oBmpState,(CBitmap*)NULL);
}

CDlgActionTree::~CDlgActionTree()
{
    m_oBmpDefaultAction . DeleteObject();
    m_oBmpAction . DeleteObject();
    m_oBmpDefaultState . DeleteObject();
    m_oBmpState . DeleteObject();
}

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


BEGIN_MESSAGE_MAP(CDlgActionTree, CFormView)
	//{{AFX_MSG_MAP(CDlgActionTree)
	ON_BN_CLICKED(IDC_BT_COPYACTION, OnBtCopyaction)
	ON_BN_CLICKED(IDC_BT_DELACTION, OnBtDelaction)
	ON_BN_CLICKED(IDC_BT_EDITZA, OnBtEditza)
	ON_BN_CLICKED(IDC_BT_NEWACTION, OnBtNewaction)
	ON_BN_CLICKED(IDC_BT_RENAMEACTION, OnBtRenameaction)
	ON_NOTIFY(NM_RCLICK, IDC_TREE_ACT, OnRclickTreeAct)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_ACT, OnSelchangedTreeAct)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgActionTree diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CDlgActionTree message handlers

void CDlgActionTree::OnBtCopyaction() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgActionTree::OnBtDelaction() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgActionTree::OnBtEditza() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgActionTree::OnBtNewaction() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgActionTree::OnBtRenameaction() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgActionTree::OnRclickTreeAct(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

void CDlgActionTree::OnSelchangedTreeAct(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

void CDlgActionTree::mfn_vSetCurrentFamily(CPA_Family * _p_oFamily)
{
    mfn_vFillTree( _p_oFamily );
    mfn_vSetCurrentAction( NULL );
}

void CDlgActionTree::mfn_vSetCurrentAction(CPA_Action * _p_oAction)
{
    CTreeCtrl* pTree = M_Tree();

    if( _p_oAction )
    {
    }
    mfn_vSetCurrentState( NULL );
}

void CDlgActionTree::mfn_vSetCurrentState(CPA_State * _p_oState)
{

}

void CDlgActionTree::mfn_vFillTree(CPA_Family * _p_oFamily)
{
    CTreeCtrl* pTree = M_Tree();

    pTree -> DeleteAllItems();

    if( _p_oFamily )
    {
        CPA_Action *p_oDefaultAction = _p_oFamily -> mfn_p_oGetDefaultAction();

        POSITION xPos = _p_oFamily -> m_oListOfActions . GetHeadPosition();
        while( xPos )
        {
            CPA_Action *p_oAction = _p_oFamily -> m_oListOfActions . GetNext( xPos );
            // add action
            HTREEITEM hTIAction = pTree -> InsertItem( p_oAction -> GetName(),
                                                       ( p_oDefaultAction == p_oAction ? 0 : 1 ),
                                                       ( p_oDefaultAction == p_oAction ? 0 : 1 ),
                                                       TVI_ROOT, TVI_LAST );
            if( hTIAction )
                pTree -> SetItemData( hTIAction, (DWORD)(CPA_ReachableObject*) p_oAction );
            CPA_State *p_oDefaulState = p_oAction -> mfn_p_oGetDefaultState();
            POSITION xPosSt = p_oAction -> m_oListOfStates . GetHeadPosition();
            while( xPosSt )
            {
                CPA_State *p_oState = p_oAction -> m_oListOfStates . GetNext( xPosSt );
                // add state
                HTREEITEM hTIState = pTree -> InsertItem( p_oState -> GetName(),
                                                          ( p_oDefaultState == p_oState ? 2 : 3 ),
                                                          ( p_oDefaultState == p_oState ? 2 : 3 ),
                                                          hTIAction, TVI_LAST );
                if( hTIState )
                    pTree -> SetItemData( hTIState, (DWORD)(CPA_ReachableObject*)p_oState );
            }
        }
    }
}
