/////////////////////////////////////////////////////////////////////////////
//! \file CConnectDlg.cpp
//! \brief Implementation CConnectDlg
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RClient.h"
#include "CConnectDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
CConnectDlg::CConnectDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConnectDlg::IDD, pParent)
/////////////////////////////////////////////////////////////////////////////
//! \brief Constructor CConnectDlg dialog.
/////////////////////////////////////////////////////////////////////////////
{
	//{{AFX_DATA_INIT(CConnectDlg)
	m_iPortAddress = 0;
	//}}AFX_DATA_INIT
}

/////////////////////////////////////////////////////////////////////////////
void CConnectDlg::DoDataExchange(CDataExchange* pDX)
/////////////////////////////////////////////////////////////////////////////
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConnectDlg)
	DDX_Control(pDX, IDC_IPADDRESS, m_CIPCtrl);
	DDX_Text(pDX, IDC_PORT, m_iPortAddress);
	DDV_MinMaxUInt(pDX, m_iPortAddress, 0, 65535);
	DDX_Check(pDX, IDC_AUTOCON, m_bAutoCon);
	DDX_Check(pDX, IDC_WRITEXML, m_bXmlWrite);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CConnectDlg, CDialog)
	//{{AFX_MSG_MAP(CConnectDlg)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_READDIR, &CConnectDlg::OnBnClickedReaddir)
	ON_BN_CLICKED(IDC_WRITEDIR, &CConnectDlg::OnBnClickedWritedir)
	ON_BN_CLICKED(IDC_BMPDIR, &CConnectDlg::OnBnClickedBmpdir)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
//! \brief CConnectDlg message handlers
/////////////////////////////////////////////////////////////////////////////
BOOL CConnectDlg::OnInitDialog()
{
	CWnd* pStatus;

	CDialog::OnInitDialog();
	pStatus = GetDlgItem(IDC_TREADDIR);
	pStatus->SetWindowTextA(m_ReadDir);
	pStatus = GetDlgItem(IDC_TWRITEDIR);
	pStatus->SetWindowTextA(m_WriteDir);
	pStatus = GetDlgItem(IDC_TBMPDIR);
	pStatus->SetWindowTextA(m_BMPDir);
	m_CIPCtrl.SetAddress(m_iIPAddress);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

/////////////////////////////////////////////////////////////////////////////
//! \brief OK clicked.
//!
//! Accecpt new values.
/////////////////////////////////////////////////////////////////////////////
void CConnectDlg::OnOK() 
{
	CDialog::OnOK();
	m_CIPCtrl.GetAddress(m_iIPAddress);
}

/////////////////////////////////////////////////////////////////////////////
//! \brief Selects new 'Read directory'.
/////////////////////////////////////////////////////////////////////////////
void CConnectDlg::OnBnClickedReaddir()
{
	CWnd* pStatus;
	SelectDir("Select Read Directory", m_ReadDir);
	pStatus = GetDlgItem(IDC_TREADDIR);
	pStatus->SetWindowTextA(m_ReadDir);
}

/////////////////////////////////////////////////////////////////////////////
//! \brief Selects new 'Write directory'.
/////////////////////////////////////////////////////////////////////////////
void CConnectDlg::OnBnClickedWritedir()
{
	CWnd* pStatus;
	SelectDir("Select Write Directory", m_WriteDir);
	pStatus = GetDlgItem(IDC_TWRITEDIR);
	pStatus->SetWindowTextA(m_WriteDir);
}

/////////////////////////////////////////////////////////////////////////////
//! \brief Selects new 'BMP directory'.
/////////////////////////////////////////////////////////////////////////////
void CConnectDlg::OnBnClickedBmpdir()
{
	CWnd* pStatus;
	SelectDir("Select BMP Directory", m_BMPDir);
	pStatus = GetDlgItem(IDC_TBMPDIR);
	pStatus->SetWindowTextA(m_BMPDir);
}

/////////////////////////////////////////////////////////////////////////////
//! \brief Selects new directory.
//! \param pText Title of window
//! \param pDir New selected directory
/////////////////////////////////////////////////////////////////////////////
void CConnectDlg::SelectDir(char *pText, char *pDir)
{
	DWORD Err;
	OPENFILENAME ofn;
	CString sExt;
	char szFile[260];
	char Drive[_MAX_DRIVE];
	char Dir[_MAX_DIR];
	char FName[_MAX_FNAME];
	char Ext[_MAX_EXT];
	
	strcpy_s(szFile, 260, "default");	
	ZeroMemory(&ofn, sizeof(OPENFILENAME));
	ofn.lStructSize     = sizeof(OPENFILENAME);
	ofn.hwndOwner       = m_hWnd;
	ofn.lpstrFile       = szFile;
	ofn.nMaxFile        = 260;
	ofn.lpstrFilter     = "*.*\0";
	ofn.nFilterIndex    = 1;
	ofn.lpstrFileTitle  = NULL;
	ofn.nMaxFileTitle   = NULL;
	ofn.lpstrInitialDir = pDir;
	ofn.lpstrDefExt     = "";
	ofn.lpstrTitle      = pText;
	ofn.Flags           = OFN_EXPLORER | OFN_HIDEREADONLY;

	if (GetOpenFileName(&ofn) == FALSE) 
		Err = CommDlgExtendedError();
	else
	{
		_splitpath_s(ofn.lpstrFile, Drive, _MAX_DRIVE, Dir, _MAX_DIR, FName, _MAX_FNAME, Ext, _MAX_EXT);
		sprintf_s(pDir, _MAX_DRIVE+_MAX_DIR, "%s%s", Drive, Dir);
	}
}

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