// PropPage_VbSettings.cpp : implementation file
//

#include "stdafx.h"
#include "AsCfgBuilder.h"
#include "proppage_vbsettings.h"
#include "XmlProjectSettings_Vb.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_VbSettings property page

IMPLEMENT_DYNCREATE(CPropPage_VbSettings, CPropertyPage)

CPropPage_VbSettings::CPropPage_VbSettings() : 
	CPropertyPage(CPropPage_VbSettings::IDD),
	vbSettings_(NULL)
{
	//{{AFX_DATA_INIT(CPropPage_VbSettings)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CPropPage_VbSettings::~CPropPage_VbSettings()
{
}

void CPropPage_VbSettings::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPropPage_VbSettings)
	DDX_Control(pDX, IDC_CHK_ENABLE, chkEnableVb_);
	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_VbSettings, CPropertyPage)
	//{{AFX_MSG_MAP(CPropPage_VbSettings)
	ON_EN_CHANGE(IDC_GENERATION_PATH, OnChangeGenerationPath)
	ON_EN_CHANGE(IDC_CLASS_PREFIX, OnChangeClassPrefix)
	ON_BN_CLICKED(IDC_CHK_DELETE_PREVIOUSLY_GENERATED_FILES, OnChkDeletePreviouslyGeneratedFiles)
	ON_BN_CLICKED(IDC_CHK_GENERATE_GUI_CLASSES, OnChkGenerateGuiClasses)
	ON_BN_CLICKED(IDC_CHK_GENERATE_TEST_PROJECT, OnChkGenerateTestProject)
	ON_BN_CLICKED(IDC_CMD_BROWSE_PATH, OnCmdBrowsePath)
	ON_BN_CLICKED(IDC_CHK_ENABLE, OnChkEnable)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPropPage_VbSettings message handlers

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

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

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

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

void CPropPage_VbSettings::OnChkGenerateGuiClasses() 
{
	SetModified(TRUE);
}

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

void CPropPage_VbSettings::OnCmdBrowsePath() 
{
	CXSBrowseFolder dlg;
	char path[MAX_PATH];

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

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

BOOL CPropPage_VbSettings::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_VbSettings::SetData(CXmlProjectSettings_Vb* vbSettings)
{
	vbSettings_ = vbSettings;

	if (GetSafeHwnd())
		DisplaySettings();
}

void CPropPage_VbSettings::StoreData()
{
	assert(vbSettings_);

	if (vbSettings_) {
		CString tmp;

		vbSettings_->SetEnabled(chkEnableVb_.GetCheck() == TRUE);

		vbSettings_->SetDeleteExistingFiles(chkDeleteOldFiles_.GetCheck() == TRUE);
		vbSettings_->SetGUIGenerationEnabled(chkGenerateGui_.GetCheck() == TRUE);
		vbSettings_->SetTestPrjGenerationEnabled(chkGenerateTestProject_.GetCheck() == TRUE);
	
		txtGenerationPath_.GetWindowText(tmp);
		vbSettings_->SetGenerationPath((const char*)tmp);
		
		txtClassPrefix_.GetWindowText(tmp);
		vbSettings_->SetClassPrefix((const char*) tmp);
	}
}

void CPropPage_VbSettings::DisplaySettings()
{
	if (vbSettings_) {
		chkDeleteOldFiles_.SetCheck(vbSettings_->GetDeleteExistingFiles());
		chkGenerateGui_.SetCheck(vbSettings_->GetGUIGenerationEnabled());
		chkGenerateTestProject_.SetCheck(vbSettings_->GetTestPrjGenerationEnabled());
	
		txtGenerationPath_.SetWindowText(vbSettings_->GetGenerationPath().c_str());
		txtClassPrefix_.SetWindowText(vbSettings_->GetClassPrefix().c_str());

		chkEnableVb_.SetCheck(vbSettings_->GetEnabled());
		UpdateControlsEnable();
	}
}

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

void CPropPage_VbSettings::UpdateControlsEnable()
{
	bool enable;

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

	txtGenerationPath_.EnableWindow(enable);
	cmdBrowsePath_.EnableWindow(enable);
	txtClassPrefix_.EnableWindow(enable);
	chkDeleteOldFiles_.EnableWindow(enable);
	chkGenerateGui_.EnableWindow(enable);
	chkGenerateTestProject_.EnableWindow(enable);
	cmdReset_.EnableWindow(enable);

	GetDlgItem(IDC_LBL_GENERATION_PATH)->EnableWindow(enable);
	GetDlgItem(IDC_LBL_CLASS_PREFIX)->EnableWindow(enable);
}
