// PropPage_AtlSettings.cpp : implementation file
//

#include "stdafx.h"
#include "AsCfgBuilder.h"
#include "PropPage_AtlSettings.h"
#include "XmlProjectSettings_Atl.h"
#include "XmlClassInfo.h"
#include "XmlProject.h"
#include "XSBrowseFolder.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_AtlSettings property page

IMPLEMENT_DYNCREATE(CPropPage_AtlSettings, CPropertyPage)

CPropPage_AtlSettings::CPropPage_AtlSettings() : 
	CPropertyPage(CPropPage_AtlSettings::IDD),
	atlSettings_(NULL),
	project_(NULL)
{
	//{{AFX_DATA_INIT(CPropPage_AtlSettings)
	//}}AFX_DATA_INIT
}

CPropPage_AtlSettings::~CPropPage_AtlSettings()
{
}

void CPropPage_AtlSettings::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPropPage_AtlSettings)
	DDX_Control(pDX, IDC_TXT_DLL_FILE_NAME_PREFIX2, txtDllFileNamePrefix_);
	DDX_Control(pDX, IDC_LBL_DLL_NAME_PREFIX, lblDllFileNamePrefix_);
	DDX_Control(pDX, IDC_TYPE_LIBRARY_NAME, txtTypeLibraryName_);
	DDX_Control(pDX, IDC_LBL_TYPE_LIBRARY_NAME, lblTypeLibraryName_);
	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_TXT_POST_BUILD_DLL_PATH, txtPostBuildPath_);
	DDX_Control(pDX, IDC_TXT_FOLDERS, txtFolders_);
	DDX_Control(pDX, IDC_CHK_ENABLE, chkEnableAtl_);
	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_AtlSettings, CPropertyPage)
	//{{AFX_MSG_MAP(CPropPage_AtlSettings)
	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)
	ON_BN_CLICKED(IDC_CMD_RESET_GUID, OnCmdResetGuid)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPropPage_AtlSettings message handlers
/////////////////////////////////////////////////////////////////////////////
// CPropPage_AtlSettings message handlers

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

void CPropPage_AtlSettings::SetData(CXmlProjectSettings_Atl* atlSettings, CXmlProject* project)
{
	atlSettings_ = atlSettings;
	project_ = project;

	if (GetSafeHwnd())
		DisplaySettings();
}

void CPropPage_AtlSettings::StoreData()
{
	assert(atlSettings_);

	if (atlSettings_) {
		cmString tmp;

		atlSettings_->SetEnabled(chkEnableAtl_.GetCheck() == TRUE);

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

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

		txtTypeLibraryName_.GetWindowText(tmp);
		atlSettings_->SetTypeLibraryName(tmp);
		
		txtResourceOffset_.GetWindowText(tmp);
		atlSettings_->SetResourcesOffset((int)tmp);

		txtDllFileNamePrefix_.GetWindowText(tmp);
		atlSettings_->SetDllFileNamePrefix(tmp);

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

		SetModified(FALSE);
	}
}

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

void CPropPage_AtlSettings::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_AtlSettings::OnChkGenerateTestProject() 
{
	SetModified(TRUE);
}

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

	SetModified(TRUE);
}

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

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

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

void CPropPage_AtlSettings::DisplaySettings()
{
	if (atlSettings_) {
		cmString buf;

		chkEnableAtl_.SetCheck(atlSettings_->GetEnabled());
		chkDeleteOldFiles_.SetCheck(atlSettings_->GetDeleteExistingFiles());
		chkGenerateGui_.SetCheck(atlSettings_->GetGUIGenerationEnabled());
		chkGenerateTestProject_.SetCheck(atlSettings_->GetTestPrjGenerationEnabled());
		
		txtGenerationPath_.SetWindowText(atlSettings_->GetGenerationPath());
		txtClassPrefix_.SetWindowText(atlSettings_->GetClassPrefix());
		txtPostBuildPath_.SetWindowText(atlSettings_->GetPostBuildPath());
		txtTypeLibraryName_.SetWindowText(atlSettings_->GetTypeLibraryName());
		txtDllFileNamePrefix_.SetWindowText(atlSettings_->GetDllFileNamePrefix());

		buf.Format(_T("%d"), atlSettings_->GetResourcesOffset());
		txtResourceOffset_.SetWindowText(buf);

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

void CPropPage_AtlSettings::UpdateControlsEnable()
{
	bool enable;

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

	txtGenerationPath_.EnableWindow(enable);
	txtClassPrefix_.EnableWindow(enable);
	txtPostBuildPath_.EnableWindow(enable);
	txtTypeLibraryName_.EnableWindow(enable);
	lblTypeLibraryName_.EnableWindow(enable);
	txtResourceOffset_.EnableWindow(enable && chkGenerateGui_.GetCheck());
	lblResourceOffset_.EnableWindow(enable && chkGenerateGui_.GetCheck());
	txtDllFileNamePrefix_.EnableWindow(enable);
	lblDllFileNamePrefix_.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_AtlSettings::OnChkEnable() 
{
	SetModified();
	UpdateControlsEnable();
}

void CPropPage_AtlSettings::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_AtlSettings::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;
	}	
}

void CPropPage_AtlSettings::OnCmdResetGuid() 
{
	CXmlClassInfo* rootClass;

	rootClass = project_->GetRootElement();
	
	rootClass->ClearAllGuids();
	
	atlSettings_->SetTypeLibraryGuid(_T(""));
	atlSettings_->SetMainDialogClassGuid(_T(""));
	atlSettings_->SetMainDialogInterfaceGuid(_T(""));
}
