// imp_configwindow.cpp : implementation file
//

#include "stdafx.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_strName(_T(""))
	, m_bCheckFile(FALSE)
	, m_bSkipEmptyLayers(FALSE)
	, m_bDisableInput(FALSE)
{
	m_pImpBlock = (CImpBlock*)pUnkOuter;
}

CImpBlockConfigWindow::~CImpBlockConfigWindow()
{
}

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

	CDialog::DoDataExchange(pDX);

	LeaveCriticalSection(&m_pImpBlock->m_cs);

	if ( pDX->m_bSaveAndValidate )
		m_pImpBlock->SetModifiedFlag(TRUE);
	else
		OnUpdateStatus(0, 0);
}


BEGIN_MESSAGE_MAP(CImpBlockConfigWindow, CDialog)
	ON_BN_CLICKED(IDC_BUTTONSET, &CImpBlockConfigWindow::OnBnClickedButtonset)
	ON_MESSAGE(WM_UPDATESTATUS, &CImpBlockConfigWindow::OnUpdateStatus)
END_MESSAGE_MAP()


// CImpBlockConfigWindow message handlers
void CImpBlockConfigWindow::OnBnClickedButtonset()
{
	WCHAR buf[256];
	DWORD dwValue;
	GetDlgItemText(IDC_EDITVALUE, buf, 256);
	if ( 1 == swscanf_s(buf, L"%x", &dwValue) )
	{
		EnterCriticalSection(&m_pImpBlock->m_cs);
		m_pImpBlock->m_dwValueToSet = dwValue;
		m_pImpBlock->m_bSet = TRUE;
		LeaveCriticalSection(&m_pImpBlock->m_cs);
	}
	CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDITVALUE);
	OnUpdateStatus(0, 0);

}

LRESULT CImpBlockConfigWindow::OnUpdateStatus(WPARAM wParam, LPARAM lParam)
{
	DWORD dwValue;
	BOOL bSet;
	EnterCriticalSection(&m_pImpBlock->m_cs);
	dwValue = m_pImpBlock->m_dwValue;
	bSet = m_pImpBlock->m_bSet;
	LeaveCriticalSection(&m_pImpBlock->m_cs);

	CString str;
	str.Format(L"%08x", dwValue);
	if (bSet)
		str = str + L"*";
	
	CEdit* pEdit = (CEdit*)GetDlgItem(IDC_STATICVALUE);
	pEdit->SetWindowText(str);
	return 0;
}