// ICubeSDKViewModeDlg.cpp: Implementierungsdatei
//

#include "stdafx.h"
#include "ICubeSDKSample.h"
#include "ICubeSDKViewModeDlg.h"
#include "NET_ICube_API.h"

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

extern int g_iCurrentCam;

extern int g_iBinSkipParam;
extern int g_iBinSkipMode;


extern ROI_PROPERTY roi_prop;

ROI_RANGE_PROPERTY	roi_range_prop;

static struct symbolic_list BinSkiplist[] = {
	{ "OFF" },
	{ "2x" },
	{ "4x" },
	{ NULL }
};


/////////////////////////////////////////////////////////////////////////////
// Dialogfeld CICubeSDKViewModeDlg 


CICubeSDKViewModeDlg::CICubeSDKViewModeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CICubeSDKViewModeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CICubeSDKViewModeDlg)
		// HINWEIS: Der Klassen-Assistent fügt hier Elementinitialisierung ein
	//}}AFX_DATA_INIT

	m_pParent = pParent;

}


void CICubeSDKViewModeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CICubeSDKViewModeDlg)
	DDX_Control(pDX, IDC_COMBO_SKIP, m_ComboSkip);
	DDX_Control(pDX, IDC_COMBO_BIN, m_ComboBin);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CICubeSDKViewModeDlg, CDialog)
	//{{AFX_MSG_MAP(CICubeSDKViewModeDlg)
	ON_BN_CLICKED(IDC_BUTTON_SET_BIN, OnButtonSetBin)
	ON_BN_CLICKED(IDC_BUTTON_SET_SKIP, OnButtonSetSkip)
	ON_BN_CLICKED(IDC_BUTTON_SET_ROI, OnButtonSetRoi)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_ROI, OnCheckEnableRoi)
	ON_WM_HSCROLL()
	ON_WM_CLOSE()
	ON_WM_DESTROY()
	ON_WM_VSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Behandlungsroutinen für Nachrichten CICubeSDKViewModeDlg 

BOOL CICubeSDKViewModeDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	int nStat=InitBinSkip(); 
	if (nStat!= IC_SUCCESS )
		AfxMessageBox("Error InitBinSkip");

	nStat=InitRoi(); 
	if (nStat!= IC_SUCCESS )
		AfxMessageBox("Error InitRoi");

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX-Eigenschaftenseiten sollten FALSE zurückgeben
}
//---------------------------------------------------------------------

int CICubeSDKViewModeDlg::InitBinSkip(void) 
{
	int i;
    int nLength = 0;	// Number of items
    int nList[3];
	int nCurrentMode;	// current bin skip mode

	// Init Binning

    if (ICubeSDK_GetBinSkipList(g_iCurrentCam,MODE_BIN,&nLength,&nList[0])!= IC_SUCCESS )
		return IC_ERROR;
   	
	for(i=0;i<nLength;i++)		// Binning 
	{
		m_ComboBin.InsertString(i,BinSkiplist[nList[i]].name);
		m_ComboBin.SetItemData(i,nList[i]);
	}  
	
	if (ICubeSDK_GetBinSkip(g_iCurrentCam,&nCurrentMode,MODE_BIN)!= IC_SUCCESS )
		return IC_ERROR;
   	
	m_ComboBin.SetCurSel(nCurrentMode);

	// Init Skipping

	if (ICubeSDK_GetBinSkipList(g_iCurrentCam,MODE_SKIP,&nLength,&nList[0])!= IC_SUCCESS )
		return IC_ERROR;


	for(i=0;i<nLength;i++)		// Skipping 
	{
		m_ComboSkip.InsertString(i,BinSkiplist[nList[i]].name);
		m_ComboSkip.SetItemData(i,nList[i]);
	} 

	if (ICubeSDK_GetBinSkip(g_iCurrentCam,&nCurrentMode,MODE_SKIP)!= IC_SUCCESS )
		return IC_ERROR;

	m_ComboSkip.SetCurSel(nCurrentMode);

	return IC_SUCCESS;
}
//---------------------------------------------------------------------

int CICubeSDKViewModeDlg::InitRoi(void) 
{
CSliderCtrl *pSlider;
CButton		*pCheckBox;


	if (ICubeSDK_GetResolutionRange(g_iCurrentCam,&roi_range_prop)!= IC_SUCCESS )
		return IC_ERROR;

	if (ICubeSDK_GetResolution(g_iCurrentCam,&roi_prop)!= IC_SUCCESS )
		return IC_ERROR;

	pSlider = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_HOR_POS);		// XPos
	pSlider->SetRange(0,roi_range_prop.nXMax-roi_prop.nXRes);
	pSlider->SetPos(roi_prop.nXPos);
	SetDlgItemInt(IDC_EDIT_HOR_POS,roi_prop.nXPos,FALSE);

	pSlider = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_VER_POS);		// YPos
	pSlider->SetRange(0,roi_range_prop.nYMax-roi_prop.nYRes);
	pSlider->SetPos(roi_prop.nYPos);
	SetDlgItemInt(IDC_EDIT_VER_POS,roi_prop.nYPos,FALSE);

	pSlider = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_HOR_SIZE);	// XRes
	pSlider->SetRange(roi_range_prop.nXMin/ROI_STEP_SIZE,roi_range_prop.nXMax/ROI_STEP_SIZE);
	pSlider->SetPos(roi_prop.nXRes/ROI_STEP_SIZE);
	SetDlgItemInt(IDC_EDIT_HOR_SIZE,roi_prop.nXRes,FALSE);

	pSlider = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_VER_SIZE);	// YRes
	pSlider->SetRange(roi_range_prop.nYMin/ROI_STEP_SIZE,roi_range_prop.nYMax/ROI_STEP_SIZE);
	pSlider->SetPos(roi_prop.nYRes/ROI_STEP_SIZE);
	SetDlgItemInt(IDC_EDIT_VER_SIZE,roi_prop.nYRes,FALSE);

	pCheckBox = (CButton *)GetDlgItem(IDC_CHECK_ENABLE_ROI);	// Enabled
	pCheckBox->SetCheck(roi_prop.bEnabled);

	return IC_SUCCESS;
}
//---------------------------------------------------------------------

void CICubeSDKViewModeDlg::OnButtonSetBin() 
{
	g_iBinSkipMode=MODE_BIN;

	g_iBinSkipParam=(int)m_ComboBin.GetItemData(m_ComboBin.GetCurSel());	

	if(ICubeSDK_IsStarted(g_iCurrentCam))
	{
		if (ICubeSDK_Stop(g_iCurrentCam)!= IC_SUCCESS )
			AfxMessageBox("Error Stop");

		m_pParent->PostMessage(WM_VIEWMODE_DLG,WM_DEVMODECHANGE);	// Restart Camera
	}	
}
//---------------------------------------------------------------------

void CICubeSDKViewModeDlg::OnButtonSetSkip() 
{
	g_iBinSkipMode=MODE_SKIP;

	g_iBinSkipParam=(int)m_ComboSkip.GetItemData(m_ComboSkip.GetCurSel());	

	if(ICubeSDK_IsStarted(g_iCurrentCam))
	{
		if (ICubeSDK_Stop(g_iCurrentCam)!= IC_SUCCESS )
			AfxMessageBox("Error Stop");

		m_pParent->PostMessage(WM_VIEWMODE_DLG,WM_DEVMODECHANGE);	// Restart Camera
	}
}
//---------------------------------------------------------------------

void CICubeSDKViewModeDlg::OnButtonSetRoi() 
{
	if(ICubeSDK_IsStarted(g_iCurrentCam))
	{
		int nStat=ICubeSDK_SetResolution(g_iCurrentCam,&roi_prop);
		if ( nStat != IC_SUCCESS )
			AfxMessageBox("Error Set ROI");
	}
}
//---------------------------------------------------------------------

void CICubeSDKViewModeDlg::OnCheckEnableRoi() 
{
	if(IsDlgButtonChecked(IDC_CHECK_ENABLE_ROI))	// En/Disable Roi
	{
		roi_prop.bEnabled=1;
	}
	else
	{
		roi_prop.bEnabled=0;
	}  	
}
//---------------------------------------------------------------------

void CICubeSDKViewModeDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CSliderCtrl *pSlider = (CSliderCtrl *)pScrollBar;

	int nNewVal=pSlider->GetPos();

	switch(pSlider->GetDlgCtrlID())
	{
	case IDC_SLIDER_HOR_POS:
		roi_prop.nXPos=nNewVal;
		SetDlgItemInt(IDC_EDIT_HOR_POS,roi_prop.nXPos,FALSE);
		break;
	case IDC_SLIDER_HOR_SIZE:
		roi_prop.nXRes=nNewVal*ROI_STEP_SIZE;
		SetDlgItemInt(IDC_EDIT_HOR_SIZE,roi_prop.nXRes,FALSE);

		pSlider = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_HOR_POS);		// new XPos-Range
		pSlider->SetRange(0,roi_range_prop.nXMax-roi_prop.nXRes);

		break;
	default:
		break;
	}	

	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
//---------------------------------------------------------------------

void CICubeSDKViewModeDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CSliderCtrl *pSlider = (CSliderCtrl *)pScrollBar;

	int nNewVal=pSlider->GetPos();

	switch(pSlider->GetDlgCtrlID())
	{
	case IDC_SLIDER_VER_POS:
		roi_prop.nYPos=nNewVal;
		SetDlgItemInt(IDC_EDIT_VER_POS,roi_prop.nYPos,FALSE);

		break;
	case IDC_SLIDER_VER_SIZE:
		roi_prop.nYRes=nNewVal*ROI_STEP_SIZE;
		SetDlgItemInt(IDC_EDIT_VER_SIZE,roi_prop.nYRes,FALSE);

		pSlider = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_VER_POS);		// // new YPos-Range
		pSlider->SetRange(0,roi_range_prop.nYMax-roi_prop.nYRes);

		break;
	default:
		break;
	}		

	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}
//---------------------------------------------------------------------

void CICubeSDKViewModeDlg::OnCancel() 
{
	
	m_pParent->PostMessage(WM_VIEWMODE_DLG,WM_CANCELMODE);

	CDialog::OnCancel();
}


