
// ChildView.cpp : CChildView 类的实现
//

#include "stdafx.h"
#include "CombineSubjectDrawTest.h"
#include "ChildView.h"

#include "MenuTitleSetting_Dlg.h"
#include "MenuImageManager_Dlg.h"
#include "MenuZoomSetting_Dlg.h"
#include "MenuFullScreenSetting_Dlg.h"
#include "MenuImageAdjust.h"
#include "MenuImageInfoSetting_Dlg.h"
#include "MenuThumbnail_Dlg.h"
#include "MenuMarkModeSetting_Dlg.h"
#include "MenuImageMeasure_Dlg.h"
#include "MenuStatusSetting_Dlg.h"


#include "ControlSelect_Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include "PublicObj.h"
// CChildView

CChildView::CChildView()
{
//	m_pDrawControl = NULL;
	m_nM = 0;
	m_lNextMarkID = 1;
	 
}

CChildView::~CChildView()
{
}


BEGIN_MESSAGE_MAP(CChildView, CWnd)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_COMMAND(ID_COLOR_BK, &CChildView::OnColorBk)
	ON_COMMAND(ID_INFO, &CChildView::OnInfo)
	 

	ON_COMMAND(ID_IMAGE_MANAGER, &CChildView::OnImageManager)
	ON_COMMAND(ID_FULLSCREEN, &CChildView::OnFullscreen)
	
	ON_COMMAND(ID_MENU_ZOOM, &CChildView::OnMenuZoom)
	ON_COMMAND(ID_IMAGE_ADJ, &CChildView::OnImageAdj)
	ON_COMMAND(ID_IMAGE_INFO, &CChildView::OnImageInfo)
	ON_COMMAND(ID_THUM_MENU, &CChildView::OnThumMenu)
	ON_COMMAND(ID_MARK_MODE, &CChildView::OnMarkMode)

	ON_COMMAND_RANGE(ID_MARK_32807, ID_MARK_32817, &CChildView::OnMarkNew)
	ON_COMMAND(ID_MEASURE, &CChildView::OnMeasure)
	ON_COMMAND(ID_STATUS, &CChildView::OnStatus)
END_MESSAGE_MAP()

void  CChildView::OnMarkNew(UINT unID)
{

}

// CChildView 消息处理程序

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), NULL);

	return TRUE;
}

void CChildView::OnPaint() 
{
	CPaintDC dc(this); // 用于绘制的设备上下文
	
	// TODO: 在此处添加消息处理程序代码
	
	// 不要为绘制消息而调用 CWnd::OnPaint()
}



int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	TCHAR szModuleFileName[MAX_PATH];
	::GetModuleFileName(NULL, szModuleFileName, MAX_PATH);

	TCHAR _szDriver[MAX_PATH];
	TCHAR _szDir[MAX_PATH];
	TCHAR _szAppname[MAX_PATH];
#ifdef _UNICODE
	::_wsplitpath_s(szModuleFileName, _szDriver, MAX_PATH, _szDir, MAX_PATH, _szAppname, MAX_PATH, NULL, 0);
#else
	::_splitpath_s(szModuleFileName, _szDriver, MAX_PATH, _szDir, MAX_PATH, _szAppname, MAX_PATH, NULL, 0);
#endif

	CString strIniFile;
	strIniFile.Format(_T("%s%s\\channel.ini"), _szDriver, _szDir);

	g_nChannelCount = GetPrivateProfileInt(_T("CHANNEL"), _T("VALUE"), 7, strIniFile);

 
	// TODO:  在此添加您专用的创建代码
	for (int i = 0; i < g_nChannelCount; i++)
	{
		IAdvImageShowControl* pObj = NULL;
		CreateImageControlInstance(&pObj);
		CRect rc(0,0,10,10);
		pObj->Create(this->GetSafeHwnd(), 0, 0, rc.Width(), rc.Height(), this,i);
		m_pDCList.Add(pObj);
	}
	return 0;
}
void CChildView::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);
	int nLine = m_pDCList.GetSize() / 3;
	int nRow = 3;
	if (nLine == 0)
	{
		nLine = 1;
		if(m_pDCList.GetSize()< 3)
		   nRow = m_pDCList.GetSize();
	}
	else
	{
		if (m_pDCList.GetSize() % 3 != 0)
			nLine += 1;
	}

	int nWidth = cx / nRow;
	int nHeight = cy / nLine;


	int nLeft = 0;
	int nTop = 0;
	
	for (int i = 0; i < m_pDCList.GetSize(); i++)
	{
		nLeft = (i % 3)*nWidth;
		nTop = (i / 3)*nHeight;
		m_pDCList[i]->SetControlPos(nLeft, nTop, nWidth, nHeight);
	}
}
void CChildView::OnColorBk()
{
	COLORREF crBack;
	m_pDCList[0]->GetControlInfoBackColor(crBack);
 
	CColorDialog dlg(crBack);
	if (dlg.DoModal() != IDOK)
		return;

	crBack = dlg.GetColor();
	for (int i = 0; i < m_pDCList.GetSize(); i++)
	{
		m_pDCList[i]->SetControlBackColor(crBack);
	}
}


void CChildView::OnInfo()
{
	// TODO: 在此添加命令处理程序代码
	CControlSelect_Dlg _selDlg;
	_selDlg.m_nTotalControl = m_pDCList.GetSize();
	if (IDOK != _selDlg.DoModal())
		return;


	CMenuTitleSetting_Dlg dlg;
	dlg.m_pControl = m_pDCList[_selDlg.m_nSelControl];
	dlg.DoModal();
}
 

void CChildView::OnFullscreen()
{
	CControlSelect_Dlg _selDlg;
	_selDlg.m_nTotalControl = m_pDCList.GetSize();
	if (IDOK != _selDlg.DoModal())
		return;

	CMenuFullScreenSetting_Dlg dlg;
	dlg.m_pControl = m_pDCList[_selDlg.m_nSelControl];
	dlg.DoModal();

}

void CChildView::OnImageManager()
{
	MessageBox(_T("此功能及DEMO程序中相关的所有代码已经禁用!\r\n请在图像控件上鼠标右键：设置图片实现此功能!"));
	return;
  /*  CMenuImageManager_Dlg dlg;
	dlg.SetFileParent(this);
	dlg.DoModal();
	*/
}
//void CChildView::AddFile(CImageFileManager::stFileInfo* pFile)
//{
//	if (pFile == NULL)
//		return;
// 	m_nM += 2;
//	stImageBaseInfo _info;
//	_info.lKm = m_nM % 1000;
//	_info.lM = m_nM/1000;
//	_info.nImageIndex = pFile->lImageIdx;
//	CTime _curTime = CTime::GetCurrentTime();
//	_info._time.nYear = _curTime.GetYear();
//	_info._time.nMonth = _curTime.GetMonth();
//	_info._time.nDay = _curTime.GetDay();
//	_info._time.nHour = _curTime.GetHour();
//	_info._time.nMintue = _curTime.GetMinute();
//	_info._time.nSecond = _curTime.GetSecond();
//	_info.strInfoExt = "附加信息 图像类别\r\n 附加信息 测试一下换行";
//	_info.nImageWidth = pFile->nImageWidth;
//	_info.nImageHeight = pFile->nImageHeight;
//	for (int i = 0; i < DRAW_CONTROL_COUNT; i++)
//	{
//		m_pDCList[i]->SetImageBuffer(pFile->pImageBuffer, pFile->lImageBufferLength, _info);
//	}
//}

void CChildView::OnMenuZoom()
{
	CControlSelect_Dlg _selDlg;
	_selDlg.m_nTotalControl = m_pDCList.GetSize();

	if (IDOK != _selDlg.DoModal())
		return;

	CMenuZoomSetting_Dlg dlg;
	dlg.m_pControl = m_pDCList[_selDlg.m_nSelControl];
	dlg.DoModal();
}




void CChildView::OnImageAdj()
{
	CControlSelect_Dlg _selDlg;
	_selDlg.m_nTotalControl = m_pDCList.GetSize();

	if (IDOK != _selDlg.DoModal())
		return;

	// TODO: 在此添加命令处理程序代码
	CMenuImageAdjust dlg;
	dlg.m_pControl = m_pDCList[_selDlg.m_nSelControl];
	dlg.DoModal();

}


void CChildView::OnImageInfo()
{

	CControlSelect_Dlg _selDlg;
	_selDlg.m_nTotalControl = m_pDCList.GetSize();

	if (IDOK != _selDlg.DoModal())
		return;

	CMenuImageInfoSetting_Dlg dlg;
	//dlg.m_pControl = m_pDrawControl;
	dlg.m_pControl = m_pDCList[_selDlg.m_nSelControl];
	dlg.DoModal();
}
//void CChildView::SeekFile(long lFile)
//{
//	CControlSelect_Dlg _selDlg;
//	_selDlg.m_nTotalControl = DRAW_CONTROL_COUNT;
//
//	if (IDOK != _selDlg.DoModal())
//		return;
//
//	m_pDCList[_selDlg.m_nSelControl]->SeekToImage(lFile);
//	 
//}

void CChildView::OnThumMenu()
{
	CControlSelect_Dlg _selDlg;
	_selDlg.m_nTotalControl = m_pDCList.GetSize();

	if (IDOK != _selDlg.DoModal())
		return;

	CMenuThumbnail_Dlg dlg;
	dlg.m_pControl = m_pDCList[_selDlg.m_nSelControl];
	dlg.DoModal();
	 
}


void CChildView::OnMarkMode()
{
	CControlSelect_Dlg _selDlg;
	_selDlg.m_nTotalControl = m_pDCList.GetSize();

	if (IDOK != _selDlg.DoModal())
		return;

	CMenuMarkModeSetting_Dlg dlg;
	dlg.m_pControl = m_pDCList[_selDlg.m_nSelControl];
	dlg.DoModal();
}



//通知上层.当前新添加的标记区域 ::上层需要填充:stFaultInfo::nImageIndex值
BOOL CChildView::OnControlInsertMark(UINT nContrlID, stMarkInfo& _newMarkInfo)
{

	CMenu  _menu;
	_menu.LoadMenu(IDR_MENU_MARK);

	CMenu* pSub = _menu.GetSubMenu(0);
	CPoint pt;
	GetCursorPos(&pt);
	UINT nID = pSub->TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTBUTTON | TPM_LEFTALIGN, pt.x, pt.y, this);
	if(nID> 0)
	{
		_newMarkInfo.lFaultIndex = m_lNextMarkID;
		m_lNextMarkID++;
		_newMarkInfo.nFaultTypeID = nID;
		CString strText;
		_menu.GetMenuString(nID, strText, MF_BYCOMMAND);

#ifdef _UNICODE
		std::wstring _unicode_string(strText.GetBuffer());
		strText.ReleaseBuffer();
		CPublicObj::Unicode2Ansi(_unicode_string, _newMarkInfo.strFaultName);
#else
		_newMarkInfo.strFaultName = strText.GetBuffer();
		strText.ReleaseBuffer();
#endif


		return TRUE;
	}
	return FALSE;


 
}
//通知上层，标记区域发生变化；
BOOL CChildView::OnControlModifyMark(UINT nID, stMarkInfo&)
{
	return TRUE;
}
//通知当前图像的缩放比例
void CChildView::OnZoomValueChange(UINT nID, const float& _curValue)
{

}
//virtual ...
void CChildView::OnControlRButtonDown(UINT nCtrlID, long lImageIdx, long lMarkIdx)
{
	CString strMsg;
	strMsg.Format(_T("鼠标右键消息 图像序号:%d   标记区域序号:%d"), lImageIdx, lMarkIdx);
	//AfxMessageBox(strMsg);

	CMenu  _menu;
	_menu.LoadMenu(IDR_MENU_R);

	IAdvImageShowControl* pControl = m_pDCList[nCtrlID];

	CMenu* pSub = _menu.GetSubMenu(0);
	if (pControl->IsFullScreen())
	{
		pSub->CheckMenuItem(ID_32831, MF_BYCOMMAND| MF_CHECKED);
	}
	emImageDirection _di;
	pControl->GetImageDirection(_di);
	if (_di == DIRECTION_0)
	{
		pSub->CheckMenuItem(ID_32823, MF_BYCOMMAND | MF_CHECKED);
	}
	else if (_di == DIRECTION_90)
	{
		pSub->CheckMenuItem(ID_32824, MF_BYCOMMAND | MF_CHECKED);
	}
	else if (_di == DIRECTION_180)
	{
		pSub->CheckMenuItem(ID_32825, MF_BYCOMMAND | MF_CHECKED);
	}
	else if (_di == DIRECTION_270)
	{
		pSub->CheckMenuItem(ID_32826, MF_BYCOMMAND | MF_CHECKED);
	}
	if (lMarkIdx == -1)
	{
		pSub->EnableMenuItem(ID_32834, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
	}

	BOOL bMark = FALSE;
	BOOL bLine = FALSE;
	bMark = pControl->GetMarkMode();
	bLine = pControl->GetMeasureModeStatus();
	if (bMark == FALSE && bLine == FALSE)
	{
		pSub->CheckMenuItem(ID_32828, MF_BYCOMMAND | MF_CHECKED);
	}
	else if (bMark == TRUE)
	{
		pSub->CheckMenuItem(ID_32829, MF_BYCOMMAND | MF_CHECKED);
	}
	else if (bLine == TRUE)
	{
		pSub->CheckMenuItem(ID_32830, MF_BYCOMMAND | MF_CHECKED);
	}

	CPoint pt;
	GetCursorPos(&pt);
	UINT nID = pSub->TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTBUTTON | TPM_LEFTALIGN, pt.x, pt.y, this);
	if (nID> 0)
	{

		switch (nID)
		{
				case ID_32831:
								{
									if (pControl->IsFullScreen())
										pControl->SetFullScreen(FALSE);
									else
										pControl->SetFullScreen(TRUE);
								}
								break;
				case ID_32832:
								{
					pControl->ShowImageAdjustByVisibleRectMode();
								}
								break;
				case ID_32833:
				{
					pControl->ShowImageWidthRealSize();
					break;
				}
				case ID_32823:
				{
					pControl->SetImageDirection(DIRECTION_0);
					break;
				}
				case ID_32824:
				{
					pControl->SetImageDirection(DIRECTION_90);
					break;
				}
				case ID_32825:
				{
					pControl->SetImageDirection(DIRECTION_180);
					break;
				}
				case ID_32826:
				{
					pControl->SetImageDirection(DIRECTION_270);
					break;
				}

				case ID_32828:
				{
					pControl->SetMarkMode(FALSE);
					pControl->SetMeasureModeStatus(FALSE);
					break;
				}
				case ID_32829:
				{
					pControl->SetMarkMode(TRUE);
					break;
				}
				case ID_32830:
				{
					pControl->SetMeasureModeStatus(TRUE);
					break;
				}
				case ID_32834:
				{
					pControl->DeleteMarkInfo(lImageIdx, lMarkIdx);
					break;
				}
				case ID_32835_IMAGE:
				{

					TCHAR szFilter[] = _T("JPG(*.jpg)|*.jpg|PNG(*.png)|*.png|BMP(*.bmp)|*.bmp||");
					CFileDialog fileDlg(TRUE, _T("png"), NULL, 0, szFilter, this);
					CString strFilePath;
					if (fileDlg.DoModal() == IDOK)
					{

						pControl->DeleteAllImage();


						strFilePath = fileDlg.GetPathName();

						CxImage _img;
						if (FALSE == _img.Load(strFilePath.GetBuffer()))
						{
							strFilePath.ReleaseBuffer();
							MessageBox(_T("添加图像失败!"));
							return;
						}

						stImageBaseInfo _info;
						_info.lM = 0;
						_info.lKm = 100;
						_info.nImageHeight = _img.GetHeight();
						_info.nImageIndex = 1;
						_info.nImageWidth = _img.GetWidth();
 
						_info.strInfoExt ="直接显示图像文件";

						CTime _curTime = CTime::GetCurrentTime();
						_info._time.nYear = _curTime.GetYear();
						_info._time.nMonth = _curTime.GetMonth();
						_info._time.nDay = _curTime.GetDay();
						_info._time.nHour = _curTime.GetHour();
						_info._time.nMintue = _curTime.GetMinute();
						_info._time.nSecond = _curTime.GetSecond();

						 
#ifdef _UNICODE
						std::wstring _unicode_string(strFilePath.GetBuffer());
						strFilePath.ReleaseBuffer();
						std::string _fileName;
						CPublicObj::Unicode2Ansi(_unicode_string, _fileName);
						pControl->SetImageFile(_fileName, _info);

#else
						pControl->SetImageFile(strFilePath.GetBuffer(), _info);
#endif
						
						
						}
					}


					break;
				}

		}
		
}

void CChildView::OnMeasure()
{
	// TODO: 在此添加命令处理程序代码
	CControlSelect_Dlg _selDlg;
	_selDlg.m_nTotalControl = m_pDCList.GetSize();

	if (IDOK != _selDlg.DoModal())
		return;

	CMenuImageMeasure_Dlg dlg;
	dlg.m_pControl = m_pDCList[_selDlg.m_nSelControl];
	dlg.DoModal();
}
void CChildView::OnControlMeasure(UINT nID, int nWidth, int nHeight, std::string& _showInfo)
{
	char szInfo[128];
	sprintf(szInfo, "width:%d  height:%d", nWidth, nHeight);
	_showInfo = szInfo;
}



void CChildView::OnStatus()
{
	CControlSelect_Dlg _selDlg;
	_selDlg.m_nTotalControl = m_pDCList.GetSize();

	if (IDOK != _selDlg.DoModal())
		return;
	CMenuStatusSetting_Dlg dlg;
	dlg.m_pControl = m_pDCList[_selDlg.m_nSelControl];
	dlg.DoModal();

}
