// ChannelNameSet.cpp : implementation file
//

#include "stdafx.h"
#include "clientdemo5.h"
#include "ChannelNameSet.h"
#include "ClientDemo5Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChannelNameSet dialog


CChannelNameSet::CChannelNameSet(CWnd* pParent /*=NULL*/)
	: CDialog(CChannelNameSet::IDD, pParent)
{

	//{{AFX_DATA_INIT(CChannelNameSet)
	//}}AFX_DATA_INIT
	m_nChannelTotal=SDK_NET_MAX_CHANNUM;
 	for (int i = 0; i < SDK_NET_MAX_CHANNUM; ++i)
 	{
 		m_edit[i] = NULL;
 		m_static[i] = NULL;
 	}
}


void CChannelNameSet::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);

}


BEGIN_MESSAGE_MAP(CChannelNameSet, CDialog)
	//{{AFX_MSG_MAP(CChannelNameSet)
	ON_BN_CLICKED(IDC_BUTTON_APPLY, OnButtonApply)
	ON_BN_CLICKED(IDC_BUTTON_READALL, OnButtonReadall)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChannelNameSet message handlers

BOOL CChannelNameSet::OnInitDialog() 
{
	CDialog::OnInitDialog();
	_CWndCS(this);

	// 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 CChannelNameSet::CleanAll()
{

	int i = 0;
	for ( i = 0; i < m_nChannelTotal; ++i )
	{
 		if ( m_edit[i] && m_static[i])
 		{
			delete m_edit[i];
			delete m_static[i];
			m_edit[i] = NULL;
			m_static[i] = NULL;
		}
	}
	//OutputDebugString("\ntest\n");
}
void CChannelNameSet::InitDlgInfo(NET_SDK_ChannelNameConfigAll *pChannelName,int nbyChanNum )
{

	m_nChannelTotal = nbyChanNum;
	if ( pChannelName != NULL)
	{
		memcpy( &m_ChannelName ,pChannelName ,sizeof(m_ChannelName));
	}else
	{
		memset( &m_ChannelName ,0 ,sizeof(m_ChannelName));
	}

	BOOL value=InitWindow();
	if (value)
	{
		for (int i = 0; i < m_nChannelTotal; i++)
		{
			
			m_edit[i]->SetWindowText(pChannelName->channelTitle[i]);
		}
	}

	

	
}
BOOL CChannelNameSet::InitWindow()
{
	CRect rcStatic;
	CRect rcEdit;
	CString Num;
#define MAX_CHANNEL_PER_ROW 3
	int nSect = m_nChannelTotal / MAX_CHANNEL_PER_ROW;
	int nOffset = m_nChannelTotal % MAX_CHANNEL_PER_ROW;
	GetDlgItem(IDC_STATIC1)->GetWindowRect(rcStatic);
	ScreenToClient(rcStatic);
	GetDlgItem(IDC_STATIC1)->MoveWindow(&rcStatic);
	
	GetDlgItem(IDC_EDIT_C1)->GetWindowRect(rcEdit);
	ScreenToClient(rcEdit);
	GetDlgItem(IDC_EDIT_C1)->MoveWindow(&rcEdit);
	
	GetDlgItem(IDC_EDIT_C1)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_STATIC1)->ShowWindow(SW_HIDE);
	rcStatic.left += 10;
	rcStatic.right += 40;
	rcEdit.left += 40;
	rcEdit.right += 100;
	for (int i = 0; i < nSect; i++)
	{
		CRect orgrcStatic = rcStatic;
		CRect orgrcEdit = rcEdit;
		//	char *Num;
		for (int j = 0; j < MAX_CHANNEL_PER_ROW; j++)
		{

			Num.Format("%s%d",_CS("ConfigManage.CAM"),1+j+i*MAX_CHANNEL_PER_ROW);
			if (m_edit[j+i*MAX_CHANNEL_PER_ROW]==NULL&&m_static[j+i*MAX_CHANNEL_PER_ROW] == NULL)
			{
		
			m_edit[j+i*MAX_CHANNEL_PER_ROW] = new CEdit();
			m_static[j+i*MAX_CHANNEL_PER_ROW] = new CStatic();
			m_edit[j+i*MAX_CHANNEL_PER_ROW]->Create(SS_BLACKFRAME|WS_BORDER|WS_CHILD|WS_VISIBLE,orgrcEdit,this,IDC_EDIT_CNS+j+i*MAX_CHANNEL_PER_ROW);
			m_static[j+i*MAX_CHANNEL_PER_ROW]->Create(Num,WS_CHILD|WS_VISIBLE,orgrcStatic,this,IDC_STATIC_CNS+j+i*MAX_CHANNEL_PER_ROW);
			orgrcEdit.left += 140;
			orgrcEdit.right += 140;
			orgrcStatic.left += 140;
			orgrcStatic.right += 140;
			m_edit[j+i*MAX_CHANNEL_PER_ROW]->LimitText(8);
			}
		}
		if (i < nSect)
		{
			rcStatic.top += 30;
			rcStatic.bottom += 30;
			rcEdit.top += 30;
			rcEdit.bottom += 30;
		}
	}
	if (nOffset>0)
	{
		CRect orgrcStatic = rcStatic;
		CRect orgrcEdit = rcEdit;
	
		for (int i = 0; i < nOffset; i++)
		{
			Num.Format("%s%d",_CS("ConfigManage.CAM"),1+i+nSect*MAX_CHANNEL_PER_ROW);
			if (m_edit[i+nSect*MAX_CHANNEL_PER_ROW] == NULL&&m_static[i+nSect*MAX_CHANNEL_PER_ROW] == NULL)
			{
		
			m_edit[i+nSect*MAX_CHANNEL_PER_ROW] = new CEdit();
			m_static[i+nSect*MAX_CHANNEL_PER_ROW] = new CStatic();
			m_edit[i+nSect*MAX_CHANNEL_PER_ROW]->Create(SS_BLACKFRAME|WS_BORDER|WS_CHILD|WS_VISIBLE,orgrcEdit,this,IDC_EDIT_CNS+i+nSect*MAX_CHANNEL_PER_ROW);
			m_static[i+nSect*MAX_CHANNEL_PER_ROW]->Create(Num,WS_CHILD|WS_VISIBLE,orgrcStatic,this,IDC_STATIC_CNS+i+nSect*MAX_CHANNEL_PER_ROW);
			
			orgrcEdit.left += 140;
			orgrcEdit.right += 140;
			orgrcStatic.left += 140;
			orgrcStatic.right += 140;
			m_edit[i+nSect*MAX_CHANNEL_PER_ROW]->LimitText(8);
			}
		}
	}
	return TRUE;
}

void CChannelNameSet::SaveChannelNS()
{
	CString ChannelName;
	for (int i = 0; i < m_nChannelTotal; i++)
	{
		if (m_edit[i] != NULL)
		{
			m_edit[i]->GetWindowText(ChannelName);
			strcpy( m_ChannelName.channelTitle[i], ChannelName.GetBuffer(0) );
		}
	}
}

void CChannelNameSet::OnButtonApply() 
{
 	SaveChannelNS();
 	((CClientDemo5Dlg*)AfxGetMainWnd())->setChannelNS(&m_ChannelName);
}

void CChannelNameSet::OnButtonReadall() 
{
	// TODO: Add your control notification handler code here
	CleanAll();
	((CClientDemo5Dlg*)AfxGetMainWnd())->GetChannelNS();
}

void CChannelNameSet::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	CleanAll();
}
