// imp_configwindow.cpp : implementation file
//

#include "stdafx.h"
#include "brgb2gray.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);
	int red = (int)(m_pImpBlock->m_fRed*1000);
	int green = (int)(m_pImpBlock->m_fGreen*1000);
	int blue = (int)(m_pImpBlock->m_fBlue*1000);
	LeaveCriticalSection(&m_pImpBlock->m_cs);
	DDX_Slider(pDX, IDC_SLIDER_RED, red);
	DDX_Slider(pDX, IDC_SLIDER_GREEN, green);
	DDX_Slider(pDX, IDC_SLIDER_BLUE, blue);

	CString str;
	str.Format(L"%.3f", red / 1000.0f);
	((CEdit*)GetDlgItem(IDC_EDITRED))->SetWindowText(str);
	str.Format(L"%.3f", green / 1000.0f);
	((CEdit*)GetDlgItem(IDC_EDITGREEN))->SetWindowText(str);
	str.Format(L"%.3f", blue / 1000.0f);
	((CEdit*)GetDlgItem(IDC_EDITBLUE))->SetWindowText(str);

	if ( pDX->m_bSaveAndValidate )
		m_pImpBlock->SetModifiedFlag(TRUE);
}


BEGIN_MESSAGE_MAP(CImpBlockConfigWindow, CDialog)
	ON_NOTIFY(TRBN_THUMBPOSCHANGING, IDC_SLIDER_RED, &CImpBlockConfigWindow::OnTRBNThumbPosChangingSliderRed)
	ON_NOTIFY(TRBN_THUMBPOSCHANGING, IDC_SLIDER_GREEN, &CImpBlockConfigWindow::OnTRBNThumbPosChangingSliderGreen)
	ON_NOTIFY(TRBN_THUMBPOSCHANGING, IDC_SLIDER_BLUE, &CImpBlockConfigWindow::OnTRBNThumbPosChangingSliderBlue)
END_MESSAGE_MAP()


// CImpBlockConfigWindow message handlers

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

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

BOOL CImpBlockConfigWindow::OnInitDialog()
{

	// TODO:  Add extra initialization here
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_RED))->SetRange(0, 1000);
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_GREEN))->SetRange(0, 1000);
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_BLUE))->SetRange(0, 1000);

	CDialog::OnInitDialog();

//	CDataExchange dx(this, TRUE);
//	DoDataExchange(&dx);

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

void CImpBlockConfigWindow::OnTRBNThumbPosChangingSliderRed(NMHDR *pNMHDR, LRESULT *pResult)
{
	// This feature requires Windows Vista or greater.
	// The symbol _WIN32_WINNT must be >= 0x0600.
	NMTRBTHUMBPOSCHANGING *pNMTPC = reinterpret_cast<NMTRBTHUMBPOSCHANGING *>(pNMHDR);

	*pResult = 0;

	int nPos = pNMTPC->dwPos;

	EnterCriticalSection(&m_pImpBlock->m_cs);
	if ( pNMTPC->dwPos / 1000.0f + m_pImpBlock->m_fGreen + m_pImpBlock->m_fBlue <= 1 )
		m_pImpBlock->m_fRed = pNMTPC->dwPos / 1000.0f;
	else
	{
		m_pImpBlock->m_fRed = 1.0f - m_pImpBlock->m_fGreen - m_pImpBlock->m_fBlue;
		((CSliderCtrl*)GetDlgItem(IDC_SLIDER_RED))->SetPos(nPos = (DWORD)(m_pImpBlock->m_fRed * 1000));
		*pResult = 1;
	}
	LeaveCriticalSection(&m_pImpBlock->m_cs);

	CString str;
	str.Format(L"%.3f", nPos / 1000.0f);
	((CEdit*)GetDlgItem(IDC_EDITRED))->SetWindowText(str);

	m_pImpBlock->SetModifiedFlag(TRUE);
	// TODO: Add your control notification handler code here
	
}

void CImpBlockConfigWindow::OnTRBNThumbPosChangingSliderGreen(NMHDR *pNMHDR, LRESULT *pResult)
{
	// This feature requires Windows Vista or greater.
	// The symbol _WIN32_WINNT must be >= 0x0600.
	NMTRBTHUMBPOSCHANGING *pNMTPC = reinterpret_cast<NMTRBTHUMBPOSCHANGING *>(pNMHDR);

	*pResult = 0;

	int nPos = pNMTPC->dwPos;

	EnterCriticalSection(&m_pImpBlock->m_cs);
	if ( m_pImpBlock->m_fRed + pNMTPC->dwPos / 1000.0f + m_pImpBlock->m_fBlue <= 1 )
		m_pImpBlock->m_fGreen = pNMTPC->dwPos / 1000.0f;
	else
	{
		m_pImpBlock->m_fGreen = 1.0f - m_pImpBlock->m_fRed - m_pImpBlock->m_fBlue;
		((CSliderCtrl*)GetDlgItem(IDC_SLIDER_GREEN))->SetPos(nPos = (DWORD)(m_pImpBlock->m_fGreen * 1000));
		*pResult = 1;
	}
	LeaveCriticalSection(&m_pImpBlock->m_cs);

	CString str;
	str.Format(L"%.3f", nPos / 1000.0f);
	((CEdit*)GetDlgItem(IDC_EDITGREEN))->SetWindowText(str);

	m_pImpBlock->SetModifiedFlag(TRUE);
	// TODO: Add your control notification handler code here
}

void CImpBlockConfigWindow::OnTRBNThumbPosChangingSliderBlue(NMHDR *pNMHDR, LRESULT *pResult)
{
	// This feature requires Windows Vista or greater.
	// The symbol _WIN32_WINNT must be >= 0x0600.
	NMTRBTHUMBPOSCHANGING *pNMTPC = reinterpret_cast<NMTRBTHUMBPOSCHANGING *>(pNMHDR);

	*pResult = 0;

	int nPos = pNMTPC->dwPos;

	EnterCriticalSection(&m_pImpBlock->m_cs);
	if ( m_pImpBlock->m_fRed + m_pImpBlock->m_fGreen + pNMTPC->dwPos / 1000.0f <= 1 )
		m_pImpBlock->m_fBlue = pNMTPC->dwPos / 1000.0f;
	else
	{
		m_pImpBlock->m_fBlue = 1.0f - m_pImpBlock->m_fRed - m_pImpBlock->m_fGreen;
		((CSliderCtrl*)GetDlgItem(IDC_SLIDER_BLUE))->SetPos(nPos = (DWORD)(m_pImpBlock->m_fBlue * 1000));
		*pResult = 1;
	}
	LeaveCriticalSection(&m_pImpBlock->m_cs);

	CString str;
	str.Format(L"%.3f", nPos / 1000.0f);
	((CEdit*)GetDlgItem(IDC_EDITBLUE))->SetWindowText(str);

	m_pImpBlock->SetModifiedFlag(TRUE);

	// TODO: Add your control notification handler code here
	
}
