/*
 =======================================================================================
  Name        : DlgNwCfg.cpp
 
  Author      :vincent lhullier             Date :24/07/97
 
  Description :implementation file for config file name management dialog box
 =======================================================================================
  Modification -> Author :                  Date :
  Description :
 =======================================================================================
 */

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include "stdafx.h"
#include "DlgNwCfg.h"
#include "MajData.h"
#include "HelpId.h"

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

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

/*
 =======================================================================================
	CNewConfigDialog class
 =======================================================================================
 */

/*
 ----------------------------------------------------------------------------------------
  constructor
 ----------------------------------------------------------------------------------------
 */
CNewConfigDialog::CNewConfigDialog(char _cConfigType, CWnd* pParent /*=NULL*/)	: CDialog(CNewConfigDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNewConfigDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_cConfigType = _cConfigType;

}

/*
 ----------------------------------------------------------------------------------------
  message map
 ----------------------------------------------------------------------------------------
 */
BEGIN_MESSAGE_MAP(CNewConfigDialog, CDialog)
	//{{AFX_MSG_MAP(CNewConfigDialog)
	ON_LBN_SELCHANGE(IDC_LIST_CONFIG, OnSelchangeListConfig)
	ON_BN_CLICKED(IDC_BUTTON_HELP, OnButtonHelp)
	ON_WM_HELPINFO()
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_RENAME, OnButtonRename)
	ON_EN_KILLFOCUS(IDC_EDIT_NAME, OnKillfocusEditName)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/*
 ----------------------------------------------------------------------------------------
  Pre translate message
 ----------------------------------------------------------------------------------------
 */
BOOL CNewConfigDialog::PreTranslateMessage(MSG* pMsg) 
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	CWnd *pWnd = GetDlgItem(IDC_TREE_VSS);
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	
	if ( pMsg->hwnd == GetDlgItem(IDC_EDIT_NAME)->GetSafeHwnd() )
	{
		if (pMsg->message == WM_KEYDOWN)
		{
			if (pMsg->wParam == VK_RETURN )
			{
				GetDlgItem( IDOK )->SetFocus();
				return 1;
			}
		}
	}

	return CDialog::PreTranslateMessage(pMsg);
}


/*
 =======================================================================================
	CNewConfigDialog message handlers
 =======================================================================================
 */

/*
 ----------------------------------------------------------------------------------------
  BN_CLICKED on IDOK button
 ----------------------------------------------------------------------------------------
 */
void CNewConfigDialog::OnOK() 
{
	char		szConfigName[40];

	GetDlgItem( IDC_EDIT_NAME )->GetWindowText( szConfigName, 40 );

	if (*szConfigName == 0)
	{
		if (m_cConfig == -1 )
		{
			MessageBox("Select a config or type a new config name", "Info", MB_OK | MB_ICONINFORMATION );
			return;
		}
	}
	else
	{
		if (m_cConfig != -1)
		{
			if (strcmp( szConfigName, m_szConfigName) != 0)
				m_cConfig = -1;
		}

		if (m_cConfig == -1)
		{
			m_iCurSel = m_p_oListConfig->FindStringExact( 0, szConfigName );
			if (m_iCurSel != LB_ERR )
			{
				m_cConfig = (char) m_p_oListConfig->GetItemData( m_iCurSel );
				m_p_oListConfig->GetText( m_iCurSel, m_szConfigName );
			}
			else
			{
				strcpy( m_szConfigName, szConfigName );
			}
		}
	}
	
	CDialog::OnOK();
}

/*
 ----------------------------------------------------------------------------------------
  WM_INITDIALOG
 ----------------------------------------------------------------------------------------
 */
BOOL CNewConfigDialog::OnInitDialog() 
{
	char		cConfig;
	int			iIndex;
	char		szName[40];

	CDialog::OnInitDialog();

	m_p_oListConfig = (CListBox *) GetDlgItem( IDC_LIST_CONFIG );

	/*
	 * fill list of config
	 */
	for (cConfig = 0; cConfig < 100; cConfig ++)
	{
		if (g_stIniData.a_cAvailableConfig[ cConfig ] )
		{
			if (fn_IniD_cGetConfigType( cConfig ) == m_cConfigType)
			{
				fn_IniD_vGetConfigName(cConfig, szName );
				iIndex = m_p_oListConfig->AddString( szName );
				m_p_oListConfig->SetItemData( iIndex, cConfig );
			}
		}
	}

	/*
	 * set current config and configure display
	 */
	m_iCurSel = -1;
	m_cConfig = -1;
	*m_szConfigName = 0;
	GetDlgItem( IDC_BUTTON_DELETE )->EnableWindow( FALSE );
	GetDlgItem( IDC_BUTTON_RENAME )->EnableWindow( FALSE );
	((CEdit *) GetDlgItem( IDC_EDIT_NAME ))->LimitText(39);
	GetDlgItem( IDC_EDIT_NAME )->SetFocus();
	
	return FALSE; 
}

/*
 ----------------------------------------------------------------------------------------
  LBN_SELCHANGE on IDC_LIST_CONFIG
 ----------------------------------------------------------------------------------------
 */
void CNewConfigDialog::OnSelchangeListConfig() 
{
	if ( (m_iCurSel = m_p_oListConfig->GetCurSel() ) == LB_ERR)
	{
		m_cConfig = -1;
		*m_szConfigName = 0;
	}
	else
	{
		m_cConfig = (char) m_p_oListConfig->GetItemData( m_iCurSel );
		m_p_oListConfig->GetText( m_iCurSel, m_szConfigName);
	}

	GetDlgItem( IDC_BUTTON_DELETE )->EnableWindow( m_cConfig != -1 );
	//GetDlgItem( IDC_BUTTON_RENAME )->EnableWindow( m_cConfig != -1 );
	GetDlgItem( IDC_EDIT_NAME )->SetWindowText( m_szConfigName );
	OnKillfocusEditName();
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICK on IDC_BUTTON_DELETE
 ----------------------------------------------------------------------------------------
 */
void CNewConfigDialog::OnButtonDelete() 
{
	char		szText[100];

	/*
	 * check if there's a selected sonfig
	 */
	/*
	if (m_cConfig == -1)
	{
		MessageBox("Select a config in list before", "error", MB_OK | MB_ICONERROR );
		return;
	}
	*/

	/*
	 * display confirmation box
	 */
	sprintf( szText, "Are you sure you want to delete %s config ?\r\n!! Operation can't be undone !!", m_szConfigName );
	if ( MessageBox( szText, "Confirm", MB_YESNO | MB_ICONQUESTION ) == IDYES )
	{
		/*
		 * delete config and refresh display
		 */
		fn_IniD_vDeleteFileList( m_cConfig, g_szIniFile );
		m_p_oListConfig->DeleteString( m_iCurSel );

		if (m_iCurSel == m_p_oListConfig->GetCount() )
			m_p_oListConfig->SetCurSel( m_iCurSel - 1 );
		else
			m_p_oListConfig->SetCurSel( m_iCurSel );
		OnSelchangeListConfig();
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICK on IDC_BUTTON_RENAME
 ----------------------------------------------------------------------------------------
 */
void CNewConfigDialog::OnButtonRename() 
{
	char		szText[100];
	char		szNewConfigName[40];

	/*
	if (m_cConfig == -1)
	{
		MessageBox("Select a config in list before\r\nand type a new name in edit box", "Error", MB_OK | MB_ICONERROR );
		return;
	}
	*/

	//m_p_oListConfig->GetText( iIndex, szOldConfigName );
	GetDlgItem( IDC_EDIT_NAME )->GetWindowText( szNewConfigName, 40 );

	/*
	if ( *szConfigName == 0)
	{
		sprintf( szText, "Type a new name in edit box for %s config", szOldConfigName );
		MessageBox(szText, "Error", MB_OK | MB_ICONERROR );
		return;
	}

	if (strcmp( szConfigName, szOldConfigName) == 0)
		return;
	*/

	sprintf( szText, "Are you sure you want to rename %s config with %s ?\r\n!! Operation can't be undone !!", m_szConfigName, szNewConfigName );
	if ( MessageBox( szText, "Confirm", MB_YESNO | MB_ICONQUESTION ) == IDYES )
	{
		fn_IniD_vRenameFileList( m_cConfig, szNewConfigName, g_szIniFile );
		m_p_oListConfig->DeleteString( m_iCurSel );
		m_iCurSel = m_p_oListConfig->AddString( szNewConfigName );
		m_p_oListConfig->SetItemData( m_iCurSel, m_cConfig );
		m_p_oListConfig->SetCurSel( m_iCurSel );
		OnSelchangeListConfig();
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICK on IDC_BUTTON_RENAME
 ----------------------------------------------------------------------------------------
 */
void CNewConfigDialog::OnKillfocusEditName() 
{
	char szName[40];
	BOOL bRename;
	char *p_cCur;

	GetDlgItem( IDC_EDIT_NAME )->GetWindowText( szName, 39 );

	for (p_cCur = szName; *p_cCur != 0; p_cCur++)
		if ( (*p_cCur == ' ') || (*p_cCur == '\t'))
				*p_cCur = '_';

	GetDlgItem( IDC_EDIT_NAME )->SetWindowText( szName );

	bRename = ( (m_cConfig != -1) && (*szName != 0) && (strcmp( m_szConfigName, szName) != 0));
	GetDlgItem( IDC_BUTTON_RENAME )->EnableWindow( bRename );
}

/*
 ----------------------------------------------------------------------------------------
  BN_CLICKED on IDC_BUTTON_HELP
 ----------------------------------------------------------------------------------------
 */
void CNewConfigDialog::OnButtonHelp() 
{
	::WinHelp(this->GetSafeHwnd(), C_szHelpFile, HELP_CONTEXT, IDH_J_CONFIGFILELIST );
}


/*
 ----------------------------------------------------------------------------------------
  Description   : WM_HELPINFO
 ----------------------------------------------------------------------------------------
 */
BOOL CNewConfigDialog::OnHelpInfo(HELPINFO* pHelpInfo) 
{
	::WinHelp(this->GetSafeHwnd(), C_szHelpFile, HELP_CONTEXT, IDH_J_CONFIGFILELIST );
	return TRUE;
}
