// proppage_csharpsettings.cpp : implementation file
//

#include "stdafx.h"
#include "ascfgbuilder.h"
#include "proppage_csharpsettings.h"
#include "XmlProjectSettings_CSharp.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_CSharpSettings property page

IMPLEMENT_DYNCREATE(CPropPage_CSharpSettings, CPropertyPage)

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

CPropPage_CSharpSettings::~CPropPage_CSharpSettings()
{
}

void CPropPage_CSharpSettings::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPropPage_CSharpSettings)
	DDX_Control(pDX, IDC_LBL_GENERATION_PATH, lblGenerationPath_);
	DDX_Control(pDX, IDC_LBL_CLASS_PREFIX, lblClassPrefix_);
	DDX_Control(pDX, IDC_GENERATION_PATH, txtGenerationPath_);
	DDX_Control(pDX, IDC_CMD_BROWSE_PATH, cmdBrowseGenerationPath_);
	DDX_Control(pDX, IDC_CLASS_PREFIX, txtClassPrefix_);
	DDX_Control(pDX, IDC_CHK_ENABLE, chkEnableCSharp_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPropPage_CSharpSettings, CPropertyPage)
	//{{AFX_MSG_MAP(CPropPage_CSharpSettings)
	ON_BN_CLICKED(IDC_CHK_ENABLE, OnChkEnable)
	ON_BN_CLICKED(IDC_CMD_BROWSE_PATH, OnCmdBrowsePath)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPropPage_CSharpSettings message handlers

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

void CPropPage_CSharpSettings::UpdateControlsEnable()
{
	BOOL enable;

	enable = chkEnableCSharp_.GetCheck();

	lblClassPrefix_.EnableWindow(enable);
	txtGenerationPath_.EnableWindow(enable);
	
	lblGenerationPath_.EnableWindow(enable);
	txtClassPrefix_.EnableWindow(enable);

	cmdBrowseGenerationPath_.EnableWindow(enable);
}

void CPropPage_CSharpSettings::SetData(CXmlProjectSettings_CSharp* cSharpSettings)
{
	cSharpSettings_ = cSharpSettings;

	if (GetSafeHwnd())
		DisplaySettings();
}

void CPropPage_CSharpSettings::StoreData()
{
	assert(cSharpSettings_);

	if (!cSharpSettings_)
		return;

	cmString tmp;

	cSharpSettings_->SetEnabled(chkEnableCSharp_.GetCheck() == TRUE);

	txtGenerationPath_.GetWindowText(tmp);
	cSharpSettings_->SetGenerationPath(tmp);
	
	txtClassPrefix_.GetWindowText(tmp);
	cSharpSettings_->SetClassPrefix(tmp);
}

void CPropPage_CSharpSettings::DisplaySettings()
{
	if (!cSharpSettings_)
		return;

	cmString buf;

	chkEnableCSharp_.SetCheck(cSharpSettings_->GetEnabled());
	
	txtGenerationPath_.SetWindowText(cSharpSettings_->GetGenerationPath());
	txtClassPrefix_.SetWindowText(cSharpSettings_->GetClassPrefix());
	
	chkEnableCSharp_.SetCheck(cSharpSettings_->GetEnabled());
	UpdateControlsEnable();
	//UpdateFoldersDescription();
}

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

BOOL CPropPage_CSharpSettings::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_CSharpSettings::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;
	}	
}
