// InputType.cpp : implementation file
// Copyright Sensoray Company 2007-2010

#include "stdafx.h"
#include "app-2255-demo.h"
#include "InputType.h"
#include "s2255f.h"

#define FROM_CONTROLS TRUE
#define TO_CONTROLS FALSE

// CInputType dialog
IMPLEMENT_DYNAMIC(CInputType, CPropertyPage)
CInputType::CInputType()
	: CPropertyPage(CInputType::IDD)
{
    m_bStreaming = FALSE;
    m_index = 0;
	m_channel = 0;
	pDoc = NULL;
	memset(&mode, 0, sizeof(mode));
    m_iX = 0;
    m_iY = 0;
    m_fields = 0;
}

CInputType::~CInputType()
{
}

void CInputType::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_RADIO_COMPOSITE, m_rad1);
    DDX_Control(pDX, IDC_RADIO_SVIDEO, m_rad2);
    DDX_Radio(pDX, IDC_RADIO_COMPOSITE, m_index);
    DDX_Slider(pDX, IDC_SLIDER_XPOS, m_iX);
    DDV_MinMaxInt(pDX, m_iX, -10, 10);
    DDX_Slider(pDX, IDC_SLIDER_YPOS, m_iY);
    DDV_MinMaxInt(pDX, m_iY, -4, 4);
    DDX_Control(pDX, IDC_SLIDER_XPOS, m_sldX);
    DDX_Control(pDX, IDC_SLIDER_YPOS, m_sldY);
    DDX_Text(pDX, IDC_STATIC_XPOS, m_sX);
    DDX_Text(pDX, IDC_STATIC_YPOS, m_sY);
    DDX_Check(pDX, IDC_CHECK_FIELDS, m_fields);
}


BEGIN_MESSAGE_MAP(CInputType, CPropertyPage)
    ON_BN_CLICKED(IDC_RADIO_COMPOSITE, OnBnClickedRadioComposite)
    ON_BN_CLICKED(IDC_RADIO_SVIDEO, OnBnClickedRadioSvideo)
    ON_BN_CLICKED(IDC_CHECK_FIELDS, OnBnClickedCheckFields)
    ON_WM_HSCROLL()
END_MESSAGE_MAP()


// CInputType message handlers
BOOL CInputType::OnInitDialog()
{
	CPropertyPage::OnInitDialog();
    m_sldX.SetRange(-10, 10, TRUE);
    m_sldY.SetRange(-4, 4, TRUE);
    m_sX.Format(TEXT("%d"), m_iX);
    m_sY.Format(TEXT("%d"), m_iY);
    if( m_bStreaming)
    {
        // disable the controls if currently streaming
        //m_rad2.EnableWindow(FALSE);
        //m_rad1.EnableWindow(FALSE);
    }
    UpdateData(TO_CONTROLS);
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX ProperTy Pages should return FALSE
}

void CInputType::OnBnClickedCheckFields()
{
    UpdateData(TRUE);
    if (pDoc == NULL)
        return;

    mode.color &= ~MASK_INPUT_TYPE;
    if (m_index)
        mode.color |= (1 << 16);
    mode.xOffs = m_iX;
    mode.yOffs = m_iY;
    mode.fields = (unsigned int)m_fields;
    S2255_SetMode(pDoc->m_hdev, m_channel, &mode);
}


void CInputType::OnBnClickedRadioComposite()
{
    //SetModified( TRUE);
}

void CInputType::OnBnClickedRadioSvideo()
{
    //SetModified( TRUE);
}


BOOL CInputType::OnApply()
{
    UpdateData(TRUE);
    if( pDoc == NULL)
        return FALSE;

    mode.color &= ~MASK_INPUT_TYPE;
	if (m_index)
		mode.color |= (1 << 16);
    mode.xOffs = m_iX;
    mode.yOffs = m_iY;
    mode.fields = (unsigned int)m_fields;
    S2255_SetMode( pDoc->m_hdev, m_channel, &mode);
    return TRUE;
}


void CInputType::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    UpdateData(FROM_CONTROLS);
	if( pScrollBar) {
        BOOL bmoved = FALSE;
        switch( nSBCode ) {
        case SB_LEFT:
            bmoved = TRUE;
            break;
        case SB_RIGHT:
            bmoved = TRUE;
            break;
        case SB_LINELEFT:
            bmoved = TRUE;
            break;
        case SB_LINERIGHT:
            bmoved = TRUE;
            break;
        case SB_PAGELEFT:
            bmoved = TRUE;
            break;
        case SB_PAGERIGHT:
            bmoved = TRUE;
            break;
        case SB_THUMBPOSITION:
            break;
        case SB_THUMBTRACK:
            bmoved = TRUE;
            break;
        case SB_ENDSCROLL:
            break;
        default:
            break;
        }
        if( bmoved) {
            m_sX.Format(TEXT("%d"), m_iX);
            m_sY.Format(TEXT("%d"), m_iY);
            UpdateData(TO_CONTROLS);
            mode.color &= ~MASK_INPUT_TYPE;
            if (m_index)
                mode.color |= (1 << 16);
            mode.xOffs = m_iX;
            mode.yOffs = m_iY;
            mode.fields = m_fields;
            S2255_SetMode( pDoc->m_hdev, m_channel, &mode);
        }
    }
    CPropertyPage::OnHScroll(nSBCode, nPos, pScrollBar);
    return;
}