// FullDisplayWnd.cpp : implementation file
//

#include "stdafx.h"
#include "NetViewX.h"
#include "FullDisplayWnd.h"
#include "NetViewXCtl.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFullDisplayWnd dialog
//extern HCURSOR	g_hCursor;
HHOOK hHook = NULL;

LRESULT CALLBACK GetMessageProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	// Switch the module state for the correct handle to be used.
	AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
	
	
	
	// If this is a keystrokes message, translate it in controls'
	// PreTranslateMessage().
	LPMSG lpMsg = (LPMSG) lParam;
	if( (nCode >= 0) &&
		PM_REMOVE == wParam &&
		(lpMsg->message >= WM_KEYFIRST && lpMsg->message <= WM_KEYLAST) &&
		AfxGetApp()->PreTranslateMessage((LPMSG)lParam) )
	{
		// The value returned from this hookproc is ignored, and it cannot
		// be used to tell Windows the message has been handled. To avoid
		// further processing, convert the message to WM_NULL before
		// returning.
		lpMsg->message = WM_NULL;
		lpMsg->lParam = 0L;
		lpMsg->wParam = 0;
	}
	
	
	
	// Passes the hook information to the next hook procedure in
	// the current hook chain.
	return ::CallNextHookEx(hHook, nCode, wParam, lParam);
}

CFullDisplayWnd::CFullDisplayWnd(CWnd* pParent /*=NULL*/)
	: CDialog(CFullDisplayWnd::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFullDisplayWnd)
	//}}AFX_DATA_INIT
	m_pParent = pParent;
	m_bSendPTZ = FALSE;
	m_bInitFlag = FALSE;
	m_TimerID   = 0;
	m_bKeepProportion = FALSE;
}


void CFullDisplayWnd::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFullDisplayWnd)
	DDX_Control(pDX, IDC_STATIC_VIDEO, m_wndVideo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFullDisplayWnd, CDialog)
	//{{AFX_MSG_MAP(CFullDisplayWnd)
	ON_WM_PAINT()
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	ON_WM_KILLFOCUS()
	ON_WM_TIMER()
	ON_WM_RBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_SHOWWINDOW()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFullDisplayWnd message handlers

BOOL CFullDisplayWnd::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	hHook = ::SetWindowsHookEx(
		WH_GETMESSAGE,
		GetMessageProc,
		AfxGetInstanceHandle(),
		GetCurrentThreadId());
	ASSERT (hHook);
	// 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 CFullDisplayWnd::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
	if(	bShow)
	{
		if(IsWindowVisible()) return;
		MoveWindow(
			0,
			0,
			GetSystemMetrics(SM_CXSCREEN),
			GetSystemMetrics(SM_CYSCREEN),
			TRUE);
		if(m_bKeepProportion == FALSE)
		{
			m_wndVideo.ShowWindow(SW_HIDE);
#if defined(PROTYPE_XISHI) || defined(PROTYPE_LOGO)
			if(((CNetViewXCtrl*)m_pParent)->m_displayhandle)
			{
				VSNET_ClientShowSetWnd(((CNetViewXCtrl*)m_pParent)->m_displayhandle,m_hWnd);
				VSNET_ClientRefreshImageShow(((CNetViewXCtrl*)m_pParent)->m_displayhandle);
			}
#else
			VSNET_ClientSetWnd(((CNetViewXCtrl*)m_pParent)->m_hChHandle,m_hWnd);
			VSNET_ClientRefrenshWnd(((CNetViewXCtrl*)m_pParent)->m_hChHandle);
#endif
		}
		else
		{
#if defined(PROTYPE_XISHI) || defined(PROTYPE_LOGO)
			if(((CNetViewXCtrl*)m_pParent)->m_displayhandle)
			{
				VSNET_ClientShowSetWnd(((CNetViewXCtrl*)m_pParent)->m_displayhandle,m_wndVideo.m_hWnd);
				VSNET_ClientRefreshImageShow(((CNetViewXCtrl*)m_pParent)->m_displayhandle);
			}
#else
			VSNET_ClientSetWnd(((CNetViewXCtrl*)m_pParent)->m_hChHandle,m_wndVideo.m_hWnd);
			VSNET_ClientRefrenshWnd(((CNetViewXCtrl*)m_pParent)->m_hChHandle);
#endif
			CRect rc;
			CalVideoRect(rc);
			m_wndVideo.MoveWindow(&rc);
			m_wndVideo.ShowWindow(SW_SHOW);
		}
		m_bInitFlag = TRUE;
	}
	else
	{
	}
}

void CFullDisplayWnd::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	if(((CNetViewXCtrl*)m_pParent)->m_hChHandle == NULL)
	{
		CRect Rect;
		CBrush tempBrush(UNLINK_COLOR/*FACE_COLOR*/);
		GetClientRect(&Rect);
		dc.FillRect(Rect, &tempBrush);
		return;
	}
	//if(NetClient_RefrenshWnd(((CNetViewXCtrl*)m_pParent)->m_hChHandle) == FALSE)
	{
		CRect Rect;
		CBrush tempBrush(FACE_COLOR);
		GetClientRect(&Rect);
		dc.FillRect(Rect, &tempBrush);
	}
#if !defined(PROTYPE_XISHI) && !defined(PROTYPE_LOGO)
	if(((CNetViewXCtrl*)m_pParent)->m_OptString[0] != '\0')
	{
		CDC *pDc = &dc;
		pDc->SetTextColor(((CNetViewXCtrl*)m_pParent)->m_Optcolor);
		pDc->SetBkColor(RGB(10,10,10));
		pDc->DrawText(((CNetViewXCtrl*)m_pParent)->m_OptString,&((CNetViewXCtrl*)m_pParent)->m_OptRect,DT_LEFT);
	}
#endif
	//CDialog::OnCancel();
}

void CFullDisplayWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	if(((CNetViewXCtrl*)m_pParent)->m_hChHandle == NULL
		|| m_bSendPTZ == TRUE) return;
	if(nChar == VK_NUMPAD4 || nChar == VK_LEFT)
	{
		VSNET_ClientPTZCtrl(((CNetViewXCtrl*)m_pParent)->m_hChHandle,0X00,5);
		m_bSendPTZ = TRUE;
	}
	else if(nChar == VK_NUMPAD6 || nChar == VK_RIGHT)
	{
		VSNET_ClientPTZCtrl(((CNetViewXCtrl*)m_pParent)->m_hChHandle,0X01,5);
		m_bSendPTZ = TRUE;
	}
	else if(nChar == VK_NUMPAD8 || nChar == VK_UP)
	{
		VSNET_ClientPTZCtrl(((CNetViewXCtrl*)m_pParent)->m_hChHandle,0X02,5);
		m_bSendPTZ = TRUE;
	}
	else if(nChar == VK_NUMPAD2 || nChar == VK_DOWN)
	{
		VSNET_ClientPTZCtrl(((CNetViewXCtrl*)m_pParent)->m_hChHandle,0X03,5);
		m_bSendPTZ = TRUE;
	}
	else if(nChar == VK_NEXT || nChar == VK_ADD)
	{
		VSNET_ClientPTZCtrl(((CNetViewXCtrl*)m_pParent)->m_hChHandle,0X09,5);
		m_bSendPTZ = TRUE;
	}
	else if(nChar == VK_PRIOR || nChar == VK_SUBTRACT)
	{
		VSNET_ClientPTZCtrl(((CNetViewXCtrl*)m_pParent)->m_hChHandle,0X08,5);
		m_bSendPTZ = TRUE;
	}
	else if (nChar == VK_ESCAPE)
	{
		((CNetViewXCtrl*)m_pParent)->UnFullScreen();
	}
	//CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CFullDisplayWnd::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	if(((CNetViewXCtrl*)m_pParent)->m_hChHandle == NULL
		|| m_bSendPTZ == FALSE) return;
	VSNET_ClientPTZCtrl(((CNetViewXCtrl*)m_pParent)->m_hChHandle,0X0D,5);
	m_bSendPTZ = FALSE;
	//CDialog::OnKeyUp(nChar, nRepCnt, nFlags);
}

void CFullDisplayWnd::OnKillFocus(CWnd* pNewWnd) 
{
	CDialog::OnKillFocus(pNewWnd);
	if(m_bInitFlag)
	{
		m_bInitFlag = FALSE;
		m_TimerID = SetTimer(1,50,0);
	}
	// TODO: Add your message handler code here
	
}

void CFullDisplayWnd::OnTimer(UINT nIDEvent) 
{	
	CDialog::OnTimer(nIDEvent);
	if(m_TimerID)
	{
		KillTimer(m_TimerID);   
		m_TimerID = 0;
//		CDialog::OnOK();
		((CNetViewXCtrl*)m_pParent)->UnFullScreen();
	}
}


void CFullDisplayWnd::OnRButtonUp(UINT nFlags, CPoint point) 
{
	if(m_TimerID)
	{
		KillTimer(m_TimerID);   
		m_TimerID = 0;
	}
//	CDialog::OnOK();
	((CNetViewXCtrl*)m_pParent)->UnFullScreen();
	CDialog::OnRButtonUp(nFlags, point);
}

void CFullDisplayWnd::CalVideoRect(RECT& rect)
{
	CRect rc;
	GetClientRect(&rc);
	DWORD iWidth = 0, iHeight = 0;
	if(!VSNET_ClientGetVideoSize(((CNetViewXCtrl*)m_pParent)->m_hChHandle,&iWidth,&iHeight))
	{
		rect = rc;
		return;
	}
	int iRealHeight = 0 ;
	int iRealWidth  = 0;

	if(iWidth * rc.Height() > iHeight * rc.Width())
	{
		iRealWidth = rc.Width();
		iRealHeight= iWidth * rc.Height() / iHeight;
	}
	else
	{
		iRealWidth  = iHeight * rc.Width() / iWidth;
		iRealHeight = rc.Height();
	}
	rect.left = (rc.Width() - iRealWidth) / 2;
	rect.top  = (rc.Height()-iRealHeight) / 2;
	rect.bottom = rect.top + iRealHeight;
	rect.right  = rect.left+ iRealWidth;
}

void CFullDisplayWnd::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
//	CDialog::OnCancel();
	((CNetViewXCtrl*)m_pParent)->UnFullScreen();
	
	CDialog::OnLButtonDblClk(nFlags, point);
}


void CFullDisplayWnd::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	VERIFY (::UnhookWindowsHookEx (hHook));
}

BOOL CFullDisplayWnd::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (pMsg->message == WM_KEYDOWN)
	{
		OnKeyDown(pMsg->wParam, 0, 0);
	}
	
	return CDialog::PreTranslateMessage(pMsg);
}
