// imp_configwindow.cpp : implementation file
//

#include "stdafx.h"
#include "bloadcamera.h"
#include "imp_configwindow.h"
#include "imp_config.h"
#include "imp_block.h"
#include <Vfw.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)
{
	int nOldCameraIndex, nNewCameraIndex;
	EnterCriticalSection(&m_pImpBlock->m_cs);

	nOldCameraIndex = m_pImpBlock->m_nCameraIndex;

	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_REVVID, m_pImpBlock->m_dwRevSignalIdVideo);
	DDX_Text(pDX, IDC_REVAID, m_pImpBlock->m_dwRevSignalIdAudio);
	DDX_CBIndex(pDX, IDC_COMBOCAPTUREDRIVERS, m_pImpBlock->m_nCameraIndex);

	nNewCameraIndex = m_pImpBlock->m_nCameraIndex;

	LeaveCriticalSection(&m_pImpBlock->m_cs);

	if ( pDX->m_bSaveAndValidate && nOldCameraIndex != nNewCameraIndex )
		m_pImpBlock->ConnectCamera(nOldCameraIndex != 0);
	OnRefreshButtons(0, 0);

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


BEGIN_MESSAGE_MAP(CImpBlockConfigWindow, CDialog)
	ON_BN_CLICKED(IDC_APPLY, &CImpBlockConfigWindow::OnBnClickedApply)
	ON_BN_CLICKED(IDC_BUTTON1, &CImpBlockConfigWindow::OnBnClickedConffmt)
	ON_BN_CLICKED(IDC_CONFSOURCE, &CImpBlockConfigWindow::OnBnClickedConfsource)
	ON_MESSAGE(WM_REFRESHBUTTONS, &CImpBlockConfigWindow::OnRefreshButtons)
	ON_BN_CLICKED(IDC_BUTTONSTARENGINE, &CImpBlockConfigWindow::OnBnClickedButtonstarengine)
	ON_BN_CLICKED(IDC_CONFDISP, &CImpBlockConfigWindow::OnBnClickedConfdisp)
	ON_BN_CLICKED(IDC_PREVIEW, &CImpBlockConfigWindow::OnBnClickedPreview)
END_MESSAGE_MAP()


// CImpBlockConfigWindow message handlers

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

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

void CImpBlockConfigWindow::OnBnClickedApply()
{
	CDataExchange dx(this, TRUE);
	DoDataExchange(&dx);
}
/*
void CImpBlockConfigWindow::OnBnClickedBrowse()
{
	CFileDialog fDlg(TRUE);
	if ( IDOK == fDlg.DoModal() )
	{
		CEdit* pEdit = (CEdit*)GetDlgItem(IDC_NAME);
		pEdit->SetWindowText(fDlg.GetPathName());
	}
}
*/
BOOL CImpBlockConfigWindow::OnInitDialog()
{
	CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_COMBOCAPTUREDRIVERS);
	pCombo->InsertString(0, L"0: None");
	int nIndex = 1;
	std::deque<CString>::iterator it = m_pImpBlock->m_dqDriverNames.begin();
	while ( it != m_pImpBlock->m_dqDriverNames.end() )
	{
		pCombo->InsertString(nIndex, (*it));
		nIndex++;
		it++;
	}



	CDialog::OnInitDialog();

	OnRefreshButtons(0, 0);

/*	m_hWndC = capCreateCaptureWindow(L"Capture Window", WS_CHILD | WS_VISIBLE, 276, 40, 160, 120, m_hWnd, IDC_CAPTUREWINDOW);
	if ( wIndex )
	{
		capDriverConnect(m_hWndC, wIndex - 1);
		capPreviewRate(m_hWndC, 66);
		capPreviewScale(m_hWndC, TRUE);

		BITMAPINFO bi;
		ZeroMemory(&bi, sizeof(bi));
		bi.bmiHeader.biBitCount = 24;
		bi.bmiHeader.biHeight = 120;
		bi.bmiHeader.biWidth = 160;
		bi.bmiHeader.biPlanes = 1;
		BOOL r = capSetVideoFormat(m_hWndC, &bi, sizeof(bi));

		capPreview(m_hWndC, TRUE);
	}*/

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

void CImpBlockConfigWindow::OnBnClickedConffmt()
{	
	CAPDRIVERCAPS CapDrvCaps;

	capDriverGetCaps(m_pImpBlock->m_hWndC, &CapDrvCaps, sizeof (CAPDRIVERCAPS)); 
	
	if ( CapDrvCaps.fHasDlgVideoFormat )
	{
		capDlgVideoFormat(m_pImpBlock->m_hWndC);

		EnterCriticalSection(&m_pImpBlock->m_csCam);
		capGetVideoFormat(m_pImpBlock->m_hWndC, &m_pImpBlock->m_bitmapInfo, sizeof(BITMAPINFO));
		LeaveCriticalSection(&m_pImpBlock->m_csCam);
	}
}

void CImpBlockConfigWindow::OnBnClickedConfsource()
{
	CAPDRIVERCAPS CapDrvCaps;

	capDriverGetCaps(m_pImpBlock->m_hWndC, &CapDrvCaps, sizeof (CAPDRIVERCAPS)); 
	
	if ( CapDrvCaps.fHasDlgVideoSource )
	{
		capDlgVideoSource(m_pImpBlock->m_hWndC);

		EnterCriticalSection(&m_pImpBlock->m_csCam);
		capGetVideoFormat(m_pImpBlock->m_hWndC, &m_pImpBlock->m_bitmapInfo, sizeof(BITMAPINFO));
		LeaveCriticalSection(&m_pImpBlock->m_csCam);
	}
}

LRESULT CImpBlockConfigWindow::OnRefreshButtons(WPARAM wParam, LPARAM lParam)
{
	CButton* pButton = (CButton*)GetDlgItem(IDC_BUTTONSTARENGINE);
	pButton->SetWindowText(m_pImpBlock->m_dwStatus == CAMSTATUS_RUNNING ? L"Stop engine" : L"Start Engine");

	CAPDRIVERCAPS CapDrvCaps;
	capDriverGetCaps(m_pImpBlock->m_hWndC, &CapDrvCaps, sizeof (CAPDRIVERCAPS)); 

	pButton = (CButton*)GetDlgItem(IDC_CONFDISP);
	pButton->EnableWindow(CapDrvCaps.fHasDlgVideoDisplay && m_pImpBlock->m_dwStatus == CAMSTATUS_STOPPED && m_pImpBlock->m_nCameraIndex);
	pButton = (CButton*)GetDlgItem(IDC_CONFFMT);
	pButton->EnableWindow(CapDrvCaps.fHasDlgVideoFormat && m_pImpBlock->m_dwStatus == CAMSTATUS_STOPPED && m_pImpBlock->m_nCameraIndex);
	pButton = (CButton*)GetDlgItem(IDC_CONFSOURCE);
	pButton->EnableWindow(CapDrvCaps.fHasDlgVideoSource && m_pImpBlock->m_dwStatus == CAMSTATUS_STOPPED && m_pImpBlock->m_nCameraIndex);

	pButton = (CButton*)GetDlgItem(IDC_PREVIEW);
	pButton->EnableWindow(m_pImpBlock->m_dwStatus == CAMSTATUS_STOPPED);

	return 0;
}
void CImpBlockConfigWindow::OnBnClickedButtonstarengine()
{
	switch ( m_pImpBlock->m_dwStatus )
	{
	case CAMSTATUS_RUNNING:
		capCaptureStop(m_pImpBlock->m_hWndC);
		break;
	case CAMSTATUS_STOPPED:
		capGetVideoFormat(m_pImpBlock->m_hWndC, &m_pImpBlock->m_bitmapInfo, sizeof(BITMAPINFO));
		::ShowWindow(m_pImpBlock->m_hWndC, SW_HIDE);
		capCaptureSequenceNoFile(m_pImpBlock->m_hWndC);
		((CButton*)GetDlgItem(IDC_PREVIEW))->SetCheck(FALSE);
		break;
	}
}

void CImpBlockConfigWindow::OnBnClickedConfdisp()
{
	CAPDRIVERCAPS CapDrvCaps;

	capDriverGetCaps(m_pImpBlock->m_hWndC, &CapDrvCaps, sizeof (CAPDRIVERCAPS)); 
	
	if ( CapDrvCaps.fHasDlgVideoDisplay )
	{
		capDlgVideoDisplay(m_pImpBlock->m_hWndC);

		EnterCriticalSection(&m_pImpBlock->m_csCam);
		capGetVideoFormat(m_pImpBlock->m_hWndC, &m_pImpBlock->m_bitmapInfo, sizeof(BITMAPINFO));
		LeaveCriticalSection(&m_pImpBlock->m_csCam);
	}
}


void CImpBlockConfigWindow::OnBnClickedPreview()
{
	CButton* pButton = (CButton*)GetDlgItem(IDC_PREVIEW);
	if ( pButton->GetCheck() )
	{
		capPreview(m_pImpBlock->m_hWndC, TRUE);
		capPreviewRate(m_pImpBlock->m_hWndC, 100);
		capPreviewScale(m_pImpBlock->m_hWndC, TRUE);
		capOverlay(m_pImpBlock->m_hWndC, TRUE);
		::ShowWindow(m_pImpBlock->m_hWndC, SW_SHOW);
	}
	else
	{
		capPreview(m_pImpBlock->m_hWndC, FALSE);
		::ShowWindow(m_pImpBlock->m_hWndC, SW_HIDE);
	}
}
