/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/
// UIAPane.cpp : implementation file
//

#include "stdafx.h"
#include "scts3devman.h"
#include "UIAPane.h"

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

/////////////////////////////////////////////////////////////////////////////
// CUIAPane dialog


CUIAPane::CUIAPane(SctsDMCommand **_command,CWnd* pParent /*=NULL*/)
	: CDialog(CUIAPane::IDD, pParent),command(_command)
{
	//{{AFX_DATA_INIT(CUIAPane)
	m_type = 0;
	m_dr = _T("");
	m_display = _T("");
	m_etsel = 0;
	m_itsel = 0;
	m_maxdt = _T("");
	m_maxlen = _T("");
	m_mindt = _T("");
	//}}AFX_DATA_INIT
	*command=0;
}


void CUIAPane::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUIAPane)
	DDX_Control(pDX, IDC_UIAIT, m_it);
	DDX_Control(pDX, IDC_UIAET, m_et);
	DDX_Control(pDX, IDC_UIACHOICES, m_choices);
	DDX_CBIndex(pDX, IDC_UIATYPE, m_type);
	DDX_Text(pDX, IDC_UIADR, m_dr);
	DDX_Text(pDX, IDC_UIADISPLAY, m_display);
	DDX_CBIndex(pDX, IDC_UIAET, m_etsel);
	DDX_CBIndex(pDX, IDC_UIAIT, m_itsel);
	DDX_Text(pDX, IDC_UIAMAXDT, m_maxdt);
	DDX_Text(pDX, IDC_UIAMAXLEN, m_maxlen);
	DDX_Text(pDX, IDC_UIAMINDT, m_mindt);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CUIAPane, CDialog)
	//{{AFX_MSG_MAP(CUIAPane)
	ON_CBN_SELCHANGE(IDC_UIATYPE, OnSelchangeUiatype)
	ON_NOTIFY(UDN_DELTAPOS, IDC_UIAMAXDTSPIN, OnDeltaposUiamaxdtspin)
	ON_NOTIFY(UDN_DELTAPOS, IDC_UIAMINDTSPIN, OnDeltaposUiamindtspin)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUIAPane message handlers

void CUIAPane::OnSelchangeUiatype() 
{
	UpdateData(TRUE);
	GetDlgItem(IDC_UIAMAXLENSPIN)->EnableWindow(m_type==2);
	GetDlgItem(IDC_UIAET)->EnableWindow(m_type==2);
	GetDlgItem(IDC_UIAIT)->EnableWindow(m_type==2);
	GetDlgItem(IDC_UIADR)->EnableWindow(m_type!=0);
	GetDlgItem(IDC_UIACHOICES)->EnableWindow(m_type==3);
}

BOOL CUIAPane::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	((CSpinButtonCtrl *)GetDlgItem(IDC_UIAMAXDTSPIN))->SetRange32(0,1000);
	((CSpinButtonCtrl *)GetDlgItem(IDC_UIAMAXDTSPIN))->SetBuddy(GetDlgItem(IDC_UIAMAXDT));
	((CSpinButtonCtrl *)GetDlgItem(IDC_UIAMAXDTSPIN))->SetPos(10);

	((CSpinButtonCtrl *)GetDlgItem(IDC_UIAMINDTSPIN))->SetRange32(0,1000);
	((CSpinButtonCtrl *)GetDlgItem(IDC_UIAMINDTSPIN))->SetBuddy(GetDlgItem(IDC_UIAMINDT));
	((CSpinButtonCtrl *)GetDlgItem(IDC_UIAMINDTSPIN))->SetPos(10);

	((CSpinButtonCtrl *)GetDlgItem(IDC_UIAMAXLENSPIN))->SetRange32(1,1000);
	((CSpinButtonCtrl *)GetDlgItem(IDC_UIAMAXLENSPIN))->SetBuddy(GetDlgItem(IDC_UIAMAXLEN));
	((CSpinButtonCtrl *)GetDlgItem(IDC_UIAMAXLENSPIN))->SetPos(10);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CUIAPane::OnDeltaposUiamaxdtspin(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
	((CSpinButtonCtrl *)GetDlgItem(IDC_UIAMINDTSPIN))->SetRange32(0,pNMUpDown->iPos+pNMUpDown->iDelta);
	*pResult = 0;
}

void CUIAPane::OnDeltaposUiamindtspin(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
	((CSpinButtonCtrl *)GetDlgItem(IDC_UIAMAXDTSPIN))->SetRange32(pNMUpDown->iPos+pNMUpDown->iDelta,1000);
	*pResult = 0;
}

void CUIAPane::OnOK() 
{
	CString options="";
	CString temp;
	int i;
	char **data;
	UpdateData(TRUE);
	/* Generate options string */
	options+="MINDT=";
	options+=m_mindt;
	options+="&MAXDT=";
	options+=m_maxdt;
	if (m_type!=0 && !m_dr.IsEmpty())
	{
		options+="&DR=";
		options+=m_dr;
	}
	if (m_type==2)
	{
		options+="&ET=";
		m_et.GetLBText(m_etsel,temp);
		options+=temp.Left(1);
		options+="&IT=";
		m_it.GetLBText(m_itsel,temp);
		options+=temp.Left(1);
	}
	/* Create the Alert */
	*command=new SctsDMCommand(SCTS_ALERT);
	(*command)->size=3+(m_type==3?m_choices.GetCount():0);
	data=(char **)sctsMalloc(sizeof(char *)*(*command)->size);
	(*command)->data=data;
	data[0]=(char *)(1100+m_type);
	data[1]=sctsStrdup((LPCTSTR)options);
	data[2]=sctsStrdup((LPCTSTR)m_display);
	if (m_type==3)
	{
		for (i=0;i<m_choices.GetCount();++i)
		{
			m_choices.GetLBText(i,temp);
			data[i+3]=sctsStrdup((LPCTSTR)temp);
		}
	}
	CDialog::OnOK();
}

BOOL CUIAPane::PreTranslateMessage(MSG* pMsg) 
{
	/* Disable ESC  and Enter */
	if (pMsg->message==WM_KEYDOWN)
	{
		if (pMsg->wParam==VK_ESCAPE) return TRUE;
		if (pMsg->wParam==VK_RETURN)
		{
			if (::IsChild(m_choices.m_hWnd,pMsg->hwnd))
			{
				CString s;
				m_choices.GetWindowText(s);
				m_choices.AddString(s);
				m_choices.SetWindowText("");
			}
			return TRUE;
		}
	}

	return CDialog::PreTranslateMessage(pMsg);
}
