// imp_configwindow.cpp : implementation file
//

#include "stdafx.h"
#include "beditgplot.h"
#include "imp_configwindow.h"
#include "imp_block.h"


// CImpBlockConfigWindow dialog

IMPLEMENT_DYNAMIC(CImpBlockConfigWindow, CDialog)

CImpBlockConfigWindow::CImpBlockConfigWindow(CWnd* pParent /*=NULL*/, IUnknown* pUnkOuter)
	: CDialog(CImpBlockConfigWindow::IDD, pParent)
{
	m_pImpBlock = (CImpBlock*)pUnkOuter;
}

CImpBlockConfigWindow::~CImpBlockConfigWindow()
{
}

void CImpBlockConfigWindow::DoDataExchange(CDataExchange* pDX)
{
	EnterCriticalSection(&m_pImpBlock->m_cs);

	CDialog::DoDataExchange(pDX);

	DDX_Text(pDX, IDC_EDITGRAPHNAME, m_pImpBlock->m_strGraphName);
	DDX_Text(pDX, IDC_EDITXLABEL, m_pImpBlock->m_strXLabel);
	DDX_Text(pDX, IDC_EDITYLABEL, m_pImpBlock->m_strYLabel);
	DDX_Check(pDX, IDC_CHECKX, m_pImpBlock->m_bXRange);
	DDX_Check(pDX, IDC_CHECKY, m_pImpBlock->m_bYRange);
	DDX_Text(pDX, IDC_EDITXMIN, m_pImpBlock->m_dMinX);
	DDX_Text(pDX, IDC_EDITXMAX, m_pImpBlock->m_dMaxX);
	DDX_Text(pDX, IDC_EDITYMIN, m_pImpBlock->m_dMinY);
	DDX_Text(pDX, IDC_EDITYMAX, m_pImpBlock->m_dMaxY);


	if ( pDX->m_bSaveAndValidate )
	{
		m_pImpBlock->SetModifiedFlag(TRUE);
	}
	else
	{
		CButton* pButton = (CButton*)GetDlgItem(IDC_CHECKX);
		BOOL bChecked = (m_pImpBlock->m_bXRange ? BST_CHECKED : BST_UNCHECKED);
		pButton->SetCheck(bChecked);
		
		CEdit* pEdit1 = (CEdit*)GetDlgItem(IDC_EDITXMIN);
		CEdit* pEdit2 = (CEdit*)GetDlgItem(IDC_EDITXMAX);

		pEdit1->EnableWindow(bChecked);
		pEdit2->EnableWindow(bChecked);


		pButton = (CButton*)GetDlgItem(IDC_CHECKY);
		bChecked = (m_pImpBlock->m_bYRange ? BST_CHECKED : BST_UNCHECKED);
		pButton->SetCheck(bChecked);
		
		pEdit1 = (CEdit*)GetDlgItem(IDC_EDITYMIN);
		pEdit2 = (CEdit*)GetDlgItem(IDC_EDITYMAX);

		pEdit1->EnableWindow(bChecked);
		pEdit2->EnableWindow(bChecked);

	}

	LeaveCriticalSection(&m_pImpBlock->m_cs);
}


BEGIN_MESSAGE_MAP(CImpBlockConfigWindow, CDialog)
	ON_BN_CLICKED(IDC_APPLY, &CImpBlockConfigWindow::OnBnClickedApply)
	ON_BN_CLICKED(IDC_CHECKX, &CImpBlockConfigWindow::OnBnClickedCheckx)
	ON_BN_CLICKED(IDC_CHECKY, &CImpBlockConfigWindow::OnBnClickedChecky)
END_MESSAGE_MAP()


// CImpBlockConfigWindow message handlers

void CImpBlockConfigWindow::OnCancel()
{
	CDialog::OnCancel();
	DestroyWindow();
}

void CImpBlockConfigWindow::OnOK()
{
	CDialog::OnOK();
	DestroyWindow();
}

BOOL CImpBlockConfigWindow::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here

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

void CImpBlockConfigWindow::OnBnClickedApply()
{
	CDataExchange dx(this, TRUE);
	DoDataExchange(&dx);
}
void CImpBlockConfigWindow::OnBnClickedCheckx()
{
	CButton* pButton = (CButton*)GetDlgItem(IDC_CHECKX);
	BOOL bChecked = pButton->GetCheck() == BST_CHECKED;

	EnterCriticalSection(&m_pImpBlock->m_cs);
	m_pImpBlock->m_bXRange = bChecked;
	LeaveCriticalSection(&m_pImpBlock->m_cs);
	
	CEdit* pEdit1 = (CEdit*)GetDlgItem(IDC_EDITXMIN);
	CEdit* pEdit2 = (CEdit*)GetDlgItem(IDC_EDITXMAX);

	pEdit1->EnableWindow(bChecked);
	pEdit2->EnableWindow(bChecked);
}

void CImpBlockConfigWindow::OnBnClickedChecky()
{
	CButton* pButton = (CButton*)GetDlgItem(IDC_CHECKY);
	BOOL bChecked = pButton->GetCheck() == BST_CHECKED;

	EnterCriticalSection(&m_pImpBlock->m_cs);
	m_pImpBlock->m_bYRange = bChecked;
	LeaveCriticalSection(&m_pImpBlock->m_cs);
	
	CEdit* pEdit1 = (CEdit*)GetDlgItem(IDC_EDITYMIN);
	CEdit* pEdit2 = (CEdit*)GetDlgItem(IDC_EDITYMAX);

	pEdit1->EnableWindow(bChecked);
	pEdit2->EnableWindow(bChecked);
}
