// PropPage_VC7Settings.cpp : implementation file
//

#include "stdafx.h"
#include "AsCfgBuilder.h"
#include "PropPage_VC7Settings.h"
#include "XmlProjectSettings_VC7.h"
#include "XSBrowseFolder.h"
#include "enums.h"

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

#if !defined (BIF_NEWDIALOGSTYLE)
#define BIF_NEWDIALOGSTYLE 64
#endif

/////////////////////////////////////////////////////////////////////////////
// CPropPage_VC7Settings property page

IMPLEMENT_DYNCREATE(CPropPage_VC7Settings, CPropertyPage)

CPropPage_VC7Settings::CPropPage_VC7Settings() : 
	CPropertyPage(CPropPage_VC7Settings::IDD),
	vc7Settings_(NULL)
{
	//{{AFX_DATA_INIT(CPropPage_VC7Settings)
	//}}AFX_DATA_INIT
}

CPropPage_VC7Settings::~CPropPage_VC7Settings()
{
}

void CPropPage_VC7Settings::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPropPage_VC7Settings)
	DDX_Control(pDX, IDC_LBL_RESOURCE_OFFSET, lblResourceOffset_);
	DDX_Control(pDX, IDC_RESOURCE_OFFSET, txtResourceOffset_);
	DDX_Control(pDX, IDC_CMD_BROWSE_PATH2, cmdBrowsePath2_);
	DDX_Control(pDX, IDC_POST_BUILD_DLL_PATH, txtPostBuildPath_);
	DDX_Control(pDX, IDC_TXT_FOLDERS, txtFolders_);
	DDX_Control(pDX, IDC_CHK_ENABLE, chkEnableCpp_);
	DDX_Control(pDX, IDC_GENERATION_PATH, txtGenerationPath_);
	DDX_Control(pDX, IDC_CMD_RESET_SETTINGS, cmdReset_);
	DDX_Control(pDX, IDC_CMD_BROWSE_PATH, cmdBrowsePath_);
	DDX_Control(pDX, IDC_CLASS_PREFIX, txtClassPrefix_);
	DDX_Control(pDX, IDC_CHK_GENERATE_TEST_PROJECT, chkGenerateTestProject_);
	DDX_Control(pDX, IDC_CHK_GENERATE_GUI_CLASSES, chkGenerateGui_);
	DDX_Control(pDX, IDC_CHK_DELETE_PREVIOUSLY_GENERATED_FILES, chkDeleteOldFiles_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPropPage_VC7Settings, CPropertyPage)
	//{{AFX_MSG_MAP(CPropPage_VC7Settings)
	ON_BN_CLICKED(IDC_CMD_BROWSE_PATH, OnCmdBrowsePath)
	ON_BN_CLICKED(IDC_CHK_GENERATE_TEST_PROJECT, OnChkGenerateTestProject)
	ON_BN_CLICKED(IDC_CHK_GENERATE_GUI_CLASSES, OnChkGenerateGuiClasses)
	ON_BN_CLICKED(IDC_CHK_DELETE_PREVIOUSLY_GENERATED_FILES, OnChkDeletePreviouslyGeneratedFiles)
	ON_EN_CHANGE(IDC_GENERATION_PATH, OnChangeGenerationPath)
	ON_EN_CHANGE(IDC_CLASS_PREFIX, OnChangeClassPrefix)
	ON_BN_CLICKED(IDC_CHK_ENABLE, OnChkEnable)
	ON_BN_CLICKED(IDC_CMD_BROWSE_PATH2, OnCmdBrowsePath2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPropPage_VC7Settings message handlers
BOOL CPropPage_VC7Settings::OnApply() 
{
	StoreData();
	
	return CPropertyPage::OnApply();
}

void CPropPage_VC7Settings::SetData(CXmlProjectSettings_VC7* vc7Settings)
{
	vc7Settings_ = vc7Settings;

	if (GetSafeHwnd())
		DisplaySettings();
}

void CPropPage_VC7Settings::StoreData()
{
	assert(vc7Settings_);

	if (vc7Settings_) {
		cmString tmp;

		vc7Settings_->SetEnabled(chkEnableCpp_.GetCheck() == TRUE);

		vc7Settings_->SetDeleteExistingFiles(chkDeleteOldFiles_.GetCheck() == TRUE);
		vc7Settings_->SetGUIGenerationEnabled(chkGenerateGui_.GetCheck() == TRUE);
		vc7Settings_->SetTestPrjGenerationEnabled(chkGenerateTestProject_.GetCheck() == TRUE);
	
		txtGenerationPath_.GetWindowText(tmp);
		vc7Settings_->SetGenerationPath(tmp);
		
		txtClassPrefix_.GetWindowText(tmp);
		vc7Settings_->SetClassPrefix( tmp);

		txtPostBuildPath_.GetWindowText(tmp);
		vc7Settings_->SetPostBuildPath(tmp);

		txtResourceOffset_.GetWindowText(tmp);
		vc7Settings_->SetResourcesOffset((int)tmp);
	}
}

BOOL CPropPage_VC7Settings::OnInitDialog() 
{
	if (!CDialog::OnInitDialog())
		return FALSE;

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

void CPropPage_VC7Settings::OnCmdBrowsePath() 
{
	CXSBrowseFolder dlg;
	TCHAR path[MAX_PATH];

	// Use the new style
	dlg.ModifyStyle(BIF_NEWDIALOGSTYLE);
	dlg.SetTitle(_T("Select generation path:"));
	
	switch (dlg.Show(GetSafeHwnd(), path)) {
		case CXSBrowseFolder::RET_OK:
			txtGenerationPath_.SetWindowText(path);
			UpdateFoldersDescription();
			break;

		case CXSBrowseFolder::RET_CANCEL:
		case CXSBrowseFolder::RET_NOPATH:
			break;
	}	
}

void CPropPage_VC7Settings::OnChkGenerateTestProject() 
{
	SetModified(TRUE);
}

void CPropPage_VC7Settings::OnChkGenerateGuiClasses() 
{
	lblResourceOffset_.EnableWindow(chkGenerateGui_.GetCheck());
	txtResourceOffset_.EnableWindow(chkGenerateGui_.GetCheck());

	SetModified(TRUE);
}

void CPropPage_VC7Settings::OnChkDeletePreviouslyGeneratedFiles() 
{
	SetModified(TRUE);	
}

void CPropPage_VC7Settings::OnChangeGenerationPath() 
{
	SetModified(TRUE);
}

void CPropPage_VC7Settings::OnChangeClassPrefix() 
{
	SetModified(TRUE);
}

void CPropPage_VC7Settings::DisplaySettings()
{
	if (vc7Settings_) {
		TCHAR buf[100];

		chkEnableCpp_.SetCheck(vc7Settings_->GetEnabled());
		chkDeleteOldFiles_.SetCheck(vc7Settings_->GetDeleteExistingFiles());
		chkGenerateGui_.SetCheck(vc7Settings_->GetGUIGenerationEnabled());
		chkGenerateTestProject_.SetCheck(vc7Settings_->GetTestPrjGenerationEnabled());
	
		txtGenerationPath_.SetWindowText(vc7Settings_->GetGenerationPath());
		txtClassPrefix_.SetWindowText(vc7Settings_->GetClassPrefix());
		txtPostBuildPath_.SetWindowText(vc7Settings_->GetPostBuildPath());
		
		wsprintf(buf, _T("%d"), vc7Settings_->GetResourcesOffset());
		txtResourceOffset_.SetWindowText(buf);

		((CButton*)GetDlgItem(IDC_CHK_ENABLE))->SetCheck(vc7Settings_->GetEnabled());
		UpdateControlsEnable();
		UpdateFoldersDescription();
	}
}

void CPropPage_VC7Settings::UpdateControlsEnable()
{
	bool enable;

	enable = ((CButton*)GetDlgItem(IDC_CHK_ENABLE))->GetCheck() == TRUE;

	txtGenerationPath_.EnableWindow(enable);
	txtClassPrefix_.EnableWindow(enable);
	txtPostBuildPath_.EnableWindow(enable);
	txtResourceOffset_.EnableWindow(enable && chkGenerateGui_.GetCheck());
	lblResourceOffset_.EnableWindow(enable && chkGenerateGui_.GetCheck());

	cmdBrowsePath_.EnableWindow(enable);
	cmdBrowsePath2_.EnableWindow(enable);
	cmdReset_.EnableWindow(enable);

	chkDeleteOldFiles_.EnableWindow(enable);
	chkGenerateGui_.EnableWindow(enable);
	chkGenerateTestProject_.EnableWindow(enable);
	
	GetDlgItem(IDC_LBL_GENERATION_PATH)->EnableWindow(enable);
	GetDlgItem(IDC_LBL_CLASS_PREFIX)->EnableWindow(enable);
	GetDlgItem(IDC_LBL_POST_BUILD_PATH)->EnableWindow(enable);
}

void CPropPage_VC7Settings::OnChkEnable() 
{
	SetModified();
	UpdateControlsEnable();
}

void CPropPage_VC7Settings::UpdateFoldersDescription()
{
	CString generationPath;
	CString description;
	CString sourcePath;
	CString includePath;
	CString libPath;
	CString projectPath;

	txtGenerationPath_.GetWindowText(generationPath);
	
	sourcePath.Format(_T("Source files folder: %s\\src\r\n"), (const TCHAR*)generationPath);	
	includePath.Format(_T("Header files folder: %s\\include\r\n"), (const TCHAR*)generationPath);
	libPath.Format(_T("Lib/dll files folder: %s\\lib\r\n"), (const TCHAR*)generationPath);
	projectPath.Format(_T("Configuration Dll project folder: %s\\project"), (const TCHAR*)generationPath);

	description +=sourcePath;
	description += includePath;
	description += libPath;
	description += projectPath;

	txtFolders_.SetWindowText(description);
}

void CPropPage_VC7Settings::OnCmdBrowsePath2() 
{
	CXSBrowseFolder dlg;
	TCHAR path[MAX_PATH];

	// Use the new style
	dlg.ModifyStyle(BIF_NEWDIALOGSTYLE);
	dlg.SetTitle(_T("Select post build dll destination path:"));
	
	switch (dlg.Show(GetSafeHwnd(), path)) {
		case CXSBrowseFolder::RET_OK:
			txtPostBuildPath_.SetWindowText(path);
			UpdateFoldersDescription();
			break;

		case CXSBrowseFolder::RET_CANCEL:
		case CXSBrowseFolder::RET_NOPATH:
			break;
	}	
}
