 #include "StdAfx.h"

#include "WndManager.h"
#include "NetSDKDLL.h"
#include "NetSDKDemo.h"
#include "NetSDKDemoDlg.h"


#define MONITOR_CENTER     0x0001        // center rect to monitor
#define MONITOR_CLIP     0x0000        // clip rect to monitor
#define MONITOR_WORKAREA 0x0002        // use monitor work area
#define MONITOR_AREA     0x0000        // use monitor entire area

CWndManager::CWndManager(void)
{
 	for (int i=0; i<MAX_CHANNEL; i++)
 	{
 		m_pWndCtrl[i] = new CWndCtrl(i, this);
 	}

	m_iCurSelWndIndex = 0;
	m_iRow = 3;
	m_iCol = 3;
}

CWndManager::~CWndManager(void)
{
	for (int i=0; i<MAX_CHANNEL; i++)
	{
		if (m_pWndCtrl[i])
		{
			delete m_pWndCtrl[i];
			m_pWndCtrl[i] = NULL;
		}
	}
	DestroyWindow();
}

BEGIN_MESSAGE_MAP(CWndManager, CWnd)
ON_WM_SIZE()
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
ON_WM_PAINT()
ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()

void CWndManager::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	ReLayoutWnd();
	
}

int CWndManager::ReLayoutWnd()
{
	for (int i=0; i<MAX_CHANNEL; i++)
	{
		if (m_pWndCtrl[i])
			m_pWndCtrl[i]->ShowWindow(SW_HIDE);
	}
	CRect rcClient;
	GetClientRect(rcClient);

	int i_W = max(0, rcClient.Width()/m_iRow);
	int i_H = max(0, rcClient.Height()/m_iCol);

	int iMW = rcClient.Width()%m_iRow;
	int iMH = rcClient.Height()%m_iCol;

	int iIndex = 0;
	for (int i=0; i<m_iRow; i++)
	{
		for (int j=0; j<m_iCol; j++)
		{
			if (m_pWndCtrl[iIndex])
			{
				CRect rcDraw;
				if (i == m_iRow-1 && j == m_iCol-1)
				{
					rcDraw = CRect(i*i_W, j*i_H, i*i_W + i_W + iMW, j*i_H + i_H + iMH);
				}
				else if (i == m_iRow-1)
				{
					rcDraw = CRect(i*i_W, j*i_H, i*i_W + i_W + iMW, j*i_H + i_H);
				}
				else if (j == m_iCol-1)
				{
					rcDraw = CRect(i*i_W, j*i_H, i*i_W + i_W, j*i_H + i_H + iMH);
				}
				else
				{
					rcDraw = CRect(i*i_W, j*i_H, i*i_W + i_W, j*i_H + i_H);
				}
				m_pWndCtrl[iIndex++]->SetWindowPos(
					NULL,
					rcDraw.left,
					rcDraw.top,
					rcDraw.Width(),
					rcDraw.Height(),
					SWP_SHOWWINDOW);
			}
		}
	}
	return 0;
}
int CWndManager::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	for (int i=0; i < MAX_CHANNEL; i++)
	{
		m_pWndCtrl[i]->Create(
			NULL, 
			NULL, 
			WS_CHILD, 
			CRect(0, 0, 0, 0), 
			this, 
			NULL);
	}
	return 0;
}


void CWndManager::SetWndSize(int iW, int iH)
{
	m_iWidth = iW;
	m_iHeight = iH;
}

void CWndManager::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CWnd::OnLButtonDown(nFlags, point);
}


HWND CWndManager::GetCurSelWnd()
{
	return m_pWndCtrl[m_iCurSelWndIndex]->GetPlayWnd();
}


BOOL CWndManager::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message == WM_KEYDOWN)
	{
		if(pMsg->wParam == 13)
			return TRUE;
		if (pMsg->wParam == 27)
		{
			::PostMessage(GetParent()->m_hWnd, WM_KEYDOWN, 27, 0);
			return TRUE;
		}
	}
	return CWnd::PreTranslateMessage(pMsg);
}


void CWndManager::OnPaint()
{

	CPaintDC dc(this); // device context for painting
	// TODO: 在此处添加消息处理程序代码
	// 不为绘图消息调用 CWnd::OnPaint()
}

void CWndManager::SetWndUser(HWND hWnd, LPCTSTR strKey)
{
	for (int i=0; i<MAX_CHANNEL; i++)
	{
		if (m_pWndCtrl[i])
		{
			if (m_pWndCtrl[i]->GetPlayWnd() == hWnd)
			{
				m_pWndCtrl[i]->SetWndUser(strKey);
				if (strKey == NULL)
				{
					m_pWndCtrl[i]->Invalidate();
				}
				((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pMenuDlg->SetBtnVoiceStatus();
				break;
			}
		}
	}
}

CString CWndManager::GetWndUser(HWND hWnd)
{
	for (int i=0; i<MAX_CHANNEL; i++)
	{
		if (m_pWndCtrl[i])
		{
			if (m_pWndCtrl[i]->GetPlayWnd() == hWnd)
			{
				return m_pWndCtrl[i]->GetWndUser();
			}
		}
	}
	return NULL;
}

void CWndManager::ClearWndUser()
{
	for (int i=0; i<MAX_CHANNEL; i++)
	{
		if (m_pWndCtrl[i])
		{
			m_pWndCtrl[i]->SetWndUser(NULL);
			m_pWndCtrl[i]->Invalidate();
		}
	}
}

void CWndManager::SetViewWnd(int x, int y)
{
	if ((x*y) > MAX_CHANNEL) return ;
	m_iRow = x; 
	m_iCol = y;
	ReLayoutWnd();
}


void CWndManager::FullScreen()
{
	CRect rcFull(0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
	//CRect rcFull(0,0,1280*2,1024);

	GetWindowPlacement(&m_wndpl);
	WINDOWPLACEMENT wndpl;
	wndpl.length=sizeof(WINDOWPLACEMENT); 
	wndpl.flags=0; 
	wndpl.showCmd=SW_SHOWNORMAL; 
	wndpl.rcNormalPosition=rcFull; 
	SetWindowPlacement(&wndpl);

	ReLayoutWnd();
}

void CWndManager::EndFullScreen()
{
	SetWindowPlacement(&m_wndpl);
	ReLayoutWnd();
}

void CWndManager::SetCurSelWnd(int iIndex)
{
	if (m_pWndCtrl[m_iCurSelWndIndex])
		m_pWndCtrl[m_iCurSelWndIndex]->SetWindowFocus(FALSE);
	if (m_pWndCtrl[iIndex])
		m_pWndCtrl[iIndex]->SetWindowFocus(TRUE);
	m_iCurSelWndIndex = iIndex;
}

long CWndManager::GetCurWndUser(CString& csName)
{
	csName.Empty();

	HWND hwnd = GetCurSelWnd();
	if (NULL == hwnd)
	{
		return -1;
	}

	csName = GetWndUser(hwnd);
	if (csName.IsEmpty())
	{
		return -2;
	}

	return 0;
}

void CWndManager::OpenDigitZoom(BOOL bOpen, int lPlayID)
{
	if (m_pWndCtrl[m_iCurSelWndIndex])
	{
		m_pWndCtrl[m_iCurSelWndIndex]->OpenDigitZoom(bOpen, lPlayID);
	}
}