// PropPage_CppSettings.cpp : implementation file
//

#include "stdafx.h"
#include "AsCfgBuilder.h"
#include "PropPage_CppSettings.h"
#include "XmlProjectSettings_Cpp.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_CppSettings property page

IMPLEMENT_DYNCREATE(CPropPage_CppSettings, CPropertyPage)

CPropPage_CppSettings::CPropPage_CppSettings() : 
	CPropertyPage(CPropPage_CppSettings::IDD),
	cppSettings_(NULL)
{
	//{{AFX_DATA_INIT(CPropPage_CppSettings)
	//}}AFX_DATA_INIT
}

CPropPage_CppSettings::~CPropPage_CppSettings()
{
}

void CPropPage_CppSettings::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPropPage_CppSettings)
	DDX_Control(pDX, IDC_LBL_DLL_NAME, lblDllNamePrefix_);
	DDX_Control(pDX, IDC_TXT_DLL_FILE_NAME_PREFIX, txtDllFileNamePrefix_);
	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_CppSettings, CPropertyPage)
	//{{AFX_MSG_MAP(CPropPage_CppSettings)
	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_CppSettings message handlers

BOOL CPropPage_CppSettings::OnApply() 
{
	StoreData();
	
	return CPropertyPage::OnApply();
}

void CPropPage_CppSettings::SetData(CXmlProjectSettings_Cpp* cppSettings)
{
	cppSettings_ = cppSettings;

	if (GetSafeHwnd())
		DisplaySettings();
}

void CPropPage_CppSettings::StoreData()
{
	assert(cppSettings_);

	if (cppSettings_) {
		cmString tmp;

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

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

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

		txtResourceOffset_.GetWindowText(tmp);
		cppSettings_->SetResourcesOffset(tmp);

		txtDllFileNamePrefix_.GetWindowText(tmp);
		cppSettings_->SetDllFileNamePrefix(tmp);
	}
}

BOOL CPropPage_CppSettings::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_CppSettings::OnCmdBrowsePath() 
{
	CXSBrowseFolder dlg;
	wchar_t 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_CppSettings::OnChkGenerateTestProject() 
{
	SetModified(TRUE);
}

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

	SetModified(TRUE);
}

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

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

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

void CPropPage_CppSettings::DisplaySettings()
{
	if (cppSettings_) {
		cmString buf;

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

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

void CPropPage_CppSettings::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());
	txtDllFileNamePrefix_.EnableWindow(enable);
	lblResourceOffset_.EnableWindow(enable && chkGenerateGui_.GetCheck());
	lblDllNamePrefix_.EnableWindow(enable);

	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_CppSettings::OnChkEnable() 
{
	SetModified();
	UpdateControlsEnable();
}

void CPropPage_CppSettings::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 wchar_t*)generationPath);	
	includePath.Format(_T("Header files folder: %s\\include\r\n"), (const wchar_t*)generationPath);
	libPath.Format(_T("Lib/dll files folder: %s\\lib\r\n"), (const wchar_t*)generationPath);
	projectPath.Format(_T("Configuration Dll project folder: %s\\project"), (const wchar_t*)generationPath);

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

	txtFolders_.SetWindowText(description);
}

void CPropPage_CppSettings::OnCmdBrowsePath2() 
{
	CXSBrowseFolder dlg;
	wchar_t 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;
	}	
}
