///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//  Implementation of Watch Dialog and List                                  //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"

#define ACTIVE_EDITOR
#define CPA_WANTS_IMPORT
#include "ACP_base.h"
#include "IncItf.h"
#undef ACTIVE_EDITOR
#undef CPA_WANTS_IMPORT

#include "WAW_Main.hpp"
#include "WAW_Data.hpp"
#include "WAW_User.hpp"
#include "WAW_Strg.hpp"
#include "ErO.h"

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

extern HMODULE WAW_g_hModule;
extern WAW_MainDialog * WAW_g_pclWatchWindow;


/////////////////////////////////////////////////////////////////////////////
// WAW_MainDialog dialog


WAW_MainDialog::WAW_MainDialog(CWnd* pParent /*=NULL*/)
	: CDialog(WAW_MainDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(WAW_MainDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	HINSTANCE hOldInstance = AfxGetResourceHandle();
	AfxSetResourceHandle( WAW_g_hModule );
	
	BOOL bValue = CDialog::Create(WAW_MainDialog::IDD, &g_oBaseFrame);
//		pParent);

	m_pri_iPosX  = 50;
	m_pri_iPosY  = 50;
	m_pri_iPosCx = 200;
	m_pri_iPosCy = 200;
	
	AfxSetResourceHandle( hOldInstance );
}


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


BEGIN_MESSAGE_MAP(WAW_MainDialog, CDialog)
	//{{AFX_MSG_MAP(WAW_MainDialog)
	ON_NOTIFY(TVN_KEYDOWN, IDC_TREE_WAW, OnKeydownTreeWaw)
	ON_NOTIFY(TVN_DELETEITEM, IDC_TREE_WAW, OnDeleteitemTreeWaw)
	ON_WM_SIZE()
	ON_WM_CLOSE()
	ON_WM_MOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// WAW_MainDialog message handlers

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

	char * pcPath = new char[MAX_PATH];
	GetModuleFileName( WAW_g_hModule, pcPath, MAX_PATH );
	m_pri_csInitFileName = pcPath;
	delete[] pcPath;
	int i = m_pri_csInitFileName.ReverseFind( '\\' );
	m_pri_csInitFileName = m_pri_csInitFileName.Left( i+1 ) + "WAWInit.ini";

	m_pri_fn_bLoadInitFile();
	//Always visible at the beginning
	SetWindowPos( &CWnd::wndTop/*Most*/, m_pri_iPosX, m_pri_iPosY, m_pri_iPosCx, m_pri_iPosCy, SWP_HIDEWINDOW);

	CRect *pclRect = new CRect;
	GetWindowRect( pclRect );
	ScreenToClient( pclRect );
	pclRect->DeflateRect( 10, 30, 10, 10 );

	m_pub_fn_pclGetTreeCtrl()->MoveWindow( pclRect );

	delete pclRect;

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


void WAW_MainDialog::OnKeydownTreeWaw(NMHDR* pNMHDR, LRESULT* pResult) 
{
	TV_KEYDOWN* pTVKeyDown = (TV_KEYDOWN*)pNMHDR;
	if ( pTVKeyDown->wVKey == 46 )	//"Suppr" was pressed
		{
		ERROR_PREPARE_M( WAW_g_C_csModuleNameForError,
									 "Delete Data in Watch Window",
									 "WAW_MainDialog::OnKeydownTreeWaw",
									 E_ERROR_GRAVITY_INSTABLE,
									 "The pointer to CTreeCtrl is NULL" );
		CTreeCtrl * pclTreeCtrl = m_pub_fn_pclGetTreeCtrl();
		ERROR_ASSERT( pclTreeCtrl != NULL );
		if( pclTreeCtrl == NULL ) return;
		
		HTREEITEM hItem = pclTreeCtrl->GetSelectedItem();
		if (hItem == NULL)
			return;

		WAW_Data * pclData = (WAW_Data*)pclTreeCtrl->GetItemData( hItem );
		WAW_fn_bDeleteData( pclData );
		}
	
	*pResult = 0;
}


void WAW_MainDialog::OnDeleteitemTreeWaw(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	
	HTREEITEM hItem = pNMTreeView->itemOld.hItem;

	ERROR_PREPARE_M( WAW_g_C_csModuleNameForError,
									 "Send a message for deleted Data in Watch Window",
									 "WAW_MainDialog::OnDeleteitemTreeWaw",
									 E_ERROR_GRAVITY_INSTABLE,
									 "The pointer to CTreeCtrl is NULL" );
	CTreeCtrl * pclTreeCtrl = m_pub_fn_pclGetTreeCtrl();
	ERROR_ASSERT( pclTreeCtrl != NULL );

	WAW_Data * pclData = (WAW_Data*)pclTreeCtrl->GetItemData( hItem );
	if( pclData->m_pri_pstUserInfo->m_pfnCallBack!= NULL )
		{
		if( pclData->m_pri_bIsData )
			pclData->m_pri_pstUserInfo->m_pfnCallBack( WAW_eDeletedData, (void*)pclData );
//		else
//			pclData->m_pri_pstUserInfo->m_pfnCallBack( WAW_eDeletedNode, (void*)pclData );
		}

	
	*pResult = 0;
}


void WAW_MainDialog::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	CTreeCtrl * pclTreeCtrl = m_pub_fn_pclGetTreeCtrl();
	if( pclTreeCtrl == NULL ) return;

	CRect *pclRect = new CRect;
	GetWindowRect( pclRect );
	ScreenToClient( pclRect );
	pclRect->DeflateRect( 10, 30, 10, 10 );

	pclTreeCtrl->MoveWindow( pclRect );

	delete pclRect;

	RECT * pstRect = new RECT;
	GetWindowRect( pstRect );
	m_pri_iPosX = pstRect->left;
	m_pri_iPosY = pstRect->top;
	m_pri_iPosCx = pstRect->right - pstRect->left;
	m_pri_iPosCy = pstRect->bottom - pstRect->top;

	m_pri_fn_bSaveInitFile();	
}


void WAW_MainDialog::OnMove(int x, int y) 
{
	CDialog::OnMove(x, y);

	RECT * pstRect = new RECT;
	GetWindowRect( pstRect );
	m_pri_iPosX = pstRect->left;
	m_pri_iPosY = pstRect->top;
	m_pri_iPosCx = pstRect->right - pstRect->left;
	m_pri_iPosCy = pstRect->bottom - pstRect->top;

	m_pri_fn_bSaveInitFile();
	
}


void WAW_MainDialog::OnClose() 
{
	WAW_fn_vSendMessage( WAW_eHideWindow, NULL );
	
	CDialog::OnClose();
}



//*****************************************************************************
//*** Specific Members ********************************************************
//*****************************************************************************


CListCtrl * WAW_MainDialog::m_pub_fn_pclGetListCtrl()
{
	return (CListCtrl*)GetDlgItem( IDC_LIST_WAW );
}

CTreeCtrl * WAW_MainDialog::m_pub_fn_pclGetTreeCtrl()
{
	return (CTreeCtrl*)GetDlgItem( IDC_TREE_WAW );
}


BOOL WAW_MainDialog::m_pri_fn_bLoadInitFile()
{
	CFile file;
	if ( !file.Open(m_pri_csInitFileName, CFile::modeRead) ) return FALSE;
	
	file.Close();

	m_pri_iPosX = GetPrivateProfileInt(WAW_g_C_csSectionPosition,	
																		 WAW_g_C_csEntryX,
																		 m_pri_iPosX,
																		 LPCTSTR(m_pri_csInitFileName));
	
	m_pri_iPosY = GetPrivateProfileInt(WAW_g_C_csSectionPosition,	
																		 WAW_g_C_csEntryY,
																		 m_pri_iPosY,
																		 LPCTSTR(m_pri_csInitFileName));

	m_pri_iPosCx = GetPrivateProfileInt(WAW_g_C_csSectionPosition,	
																		 WAW_g_C_csEntryCx,
																		 m_pri_iPosCx,
																		 LPCTSTR(m_pri_csInitFileName));

	m_pri_iPosCy = GetPrivateProfileInt(WAW_g_C_csSectionPosition,	
																		 WAW_g_C_csEntryCy,
																		 m_pri_iPosCy,
																		 LPCTSTR(m_pri_csInitFileName));
	return TRUE;
}


BOOL WAW_MainDialog::m_pri_fn_bSaveInitFile()
{
	CString csWriteString;
	BOOL bReturn = TRUE;

	RECT * pstRect = new RECT;
	GetWindowRect( pstRect );
	//ScreenToClient( pstRect );

	if ( bReturn )
	{
		csWriteString.Format( "%ld", m_pri_iPosX/*pstRect->left*/ );
		bReturn = WritePrivateProfileString(WAW_g_C_csSectionPosition,	
																				WAW_g_C_csEntryX,
																				LPCTSTR(csWriteString),
																				LPCTSTR(m_pri_csInitFileName));
	}

	if ( bReturn )
	{
		csWriteString.Format( "%ld", m_pri_iPosY/*pstRect->top*/ );
		bReturn = WritePrivateProfileString(WAW_g_C_csSectionPosition,	
																				WAW_g_C_csEntryY,
																				LPCTSTR(csWriteString),
																				LPCTSTR(m_pri_csInitFileName));
	}

	if ( bReturn )
	{
		csWriteString.Format( "%ld", m_pri_iPosCx/*pstRect->right - pstRect->left*/ );
		bReturn = WritePrivateProfileString(WAW_g_C_csSectionPosition,	
																				WAW_g_C_csEntryCx,
																				LPCTSTR(csWriteString),
																				LPCTSTR(m_pri_csInitFileName));
	}

	if ( bReturn )
	{
		csWriteString.Format( "%ld", m_pri_iPosCy/*pstRect->bottom - pstRect->top*/ );
		bReturn = WritePrivateProfileString(WAW_g_C_csSectionPosition,	
																				WAW_g_C_csEntryCy,
																				LPCTSTR(csWriteString),
																				LPCTSTR(m_pri_csInitFileName));
	}

	return bReturn;
}
