// VideoWnd.cpp : implementation file
//

#include "stdafx.h"
#include "Capture.h"
#include "VideoWnd.h"
#include "fwxVideo.h"
#include "1394Edit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CVideoWnd

CVideoWnd::CVideoWnd()
{
	bPaletteBased = TRUE;
	nDisplayBPP = 0;
	nVideoMode = 0;
	nModeHeight = 0;
	nModeWidth = 0;

	bInitialized = FALSE;
}

CVideoWnd::~CVideoWnd()
{
	pVideoProcess->fwxDeleteDisplay(this->m_hWnd);
}


BEGIN_MESSAGE_MAP(CVideoWnd, CStatic)
	//{{AFX_MSG_MAP(CVideoWnd)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVideoWnd message handlers

BOOL CVideoWnd::Initialize(UINT nMode)
{
	pVideoProcess->fwxDeleteDisplay(this->m_hWnd);
	bInitialized = FALSE;

	nVideoMode = nMode;

	CRect crect;
	GetClientRect(&crect);
	DisplaySize = crect.Size();

	//make sure the window client size is right
	switch(nVideoMode)
	{
	case 1:
		nModeWidth = 320;
		nModeHeight = 240;
		break;
	case 2:
	case 3:
	case 4:
	case 5:
		nModeWidth = 640;
		nModeHeight = 480;
		break;
	case 0:
	default:
		nModeWidth = 160;
		nModeHeight = 120;
		break;
	}

	int xadj = nModeWidth - DisplaySize.cx;
	int yadj = nModeHeight - DisplaySize.cy;

	CRect wrect;
	GetWindowRect(&wrect);
	wrect.right += xadj;
	wrect.bottom += yadj;

	SetWindowPos(NULL, 0, 0, wrect.right - wrect.left, wrect.bottom - wrect.top, 
		SWP_NOMOVE|SWP_NOREPOSITION|SWP_NOZORDER);

	GetClientRect(&crect);
	DisplaySize = crect.Size();

	//find out how many bpp the display is
	CDC *pDC = GetDC();
	int val = pDC->GetDeviceCaps(BITSPIXEL);
	if (val > 0)
	{
		//only support 8 and 16 bpps modes
		if ((val == 8) || (val == 16))
		{
			nDisplayBPP = val;
		}
		else
		{
			if (val > 16)
			{
				nDisplayBPP = 16;
			}
			else
			{
				nDisplayBPP = 8;
			}
		}
	}
	else
	{
		AfxMessageBox("Unable to determine the display bits/pixel");
		return FALSE;
	}

	if (nDisplayBPP == 8)
	{
		//see if the device supports palettes
		val = pDC->GetDeviceCaps(RASTERCAPS);
		if ((val & RC_PALETTE) == 0)
		{
			bPaletteBased = FALSE;
			AfxMessageBox("Display device does not support palattes.");
			return FALSE;
		}
		else
			bPaletteBased = TRUE;
	}
	else
		bPaletteBased = FALSE;

	ReleaseDC(pDC);

	//call this function last during initialization
	//all video processing is sone in fwxVideo.lib
	if (!pVideoProcess->fwxInitDisplay(nDisplayBPP, nModeWidth, nModeHeight, this->m_hWnd))
	{
		AfxMessageBox("Error initializing the video display.");
		return FALSE;
	}

	bInitialized = TRUE;
	return TRUE;
}

void CVideoWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	pVideoProcess->fwxShowFrame(nVideoMode);
	
	// Do not call CStatic::OnPaint() for painting messages
}
