////////////////////////////////////////////////////
//      //
// .                                        //
// ---------------------------------------------- //
//                          //
////////////////////////////////////////////////////
//  ,    //
//   ,     //
//   (   -//
//   .                               //
//////////////////////////////////////////////////// 

#include "stdafx.h"
#include "SamplePDX.h"
#include "SamplePDXDlg.h"
#include "signal.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSamplePDXDlg dialog

CSamplePDXDlg::CSamplePDXDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSamplePDXDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSamplePDXDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSamplePDXDlg)
	DDX_Control(pDX, IDC_WRITEDATA, WriteData);
	DDX_Control(pDX, IDC_NUMBEROFFORM, NumberOfForm);
	DDX_Control(pDX, IDC_LIST, list);
	DDX_Control(pDX, IDC_ENABLE, bEnable);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSamplePDXDlg, CDialog)
	//{{AFX_MSG_MAP(CSamplePDXDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_PAGE_PROPERTIES, OnShowProperties)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_READ_DATA, OnReadData)
	ON_BN_CLICKED(IDC_ENABLE, OnEnable)
	ON_BN_CLICKED(IDC_WRITEDATA, OnWritedata)
	ON_BN_CLICKED(IDOK, OnOK)
	ON_CBN_SELCHANGE(IDC_NUMBEROFFORM, OnSelchangeNumberofform)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSamplePDXDlg message handlers

BOOL CSamplePDXDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	//  
	SetIcon(m_hIcon, TRUE);	
	SetIcon(m_hIcon, FALSE);
	
	//  COM
	CoInitialize(NULL);

	HRESULT hRes = fPDX.CreateInstance("AddIn.PDX45");
	if (hRes != S_OK)
	{
		MessageBox(" PDX45  ");
		return FALSE;
	}

	//   = 0
	nFields = 0;
	fPDX->RaiseException = TRUE;
	//   = 0
	NumberOfForm.SetCurSel (0);

	UpdateForm();

	return TRUE;  
}

void CSamplePDXDlg::UpdateForm ()
{
	bEnable.SetCheck ( fPDX->DeviceEnabled );
}

void CSamplePDXDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this);

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}


HCURSOR CSamplePDXDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSamplePDXDlg::OnShowProperties() 
{
	//   
	fPDX->ShowProperties();
}

//    
void CSamplePDXDlg::OnClose()
{	
	if (bEnable.GetState())
		fPDX->DeviceEnabled = FALSE;

	//  
	if (fPDX)
	{
		fPDX.Release();
		fPDX = NULL;
	}
	CoUninitialize();
	
	CDialog::OnClose();
}


//     ""
void CSamplePDXDlg::OnOK() 
{
	if (bEnable.GetState())
		fPDX->DeviceEnabled = FALSE;

	//  
	if (fPDX)
	{
		fPDX.Release();
		fPDX = NULL;
	}
	CoUninitialize();

	CDialog::OnOK();
}

//    
void CSamplePDXDlg::OnReadData() 
{
	int i, j;

	//  ,  
	list.DeleteAllItems();

	fPDX->FormNumber = NumberOfForm.GetCurSel();

	try
	{
		fPDX->BeginReport();
	}
	catch (const _com_error)
	{
		//    
		MessageBox (fPDX->ResultDescription, "...");
		return;
	}

	//    
	if (!nFields)
	{
		for (i = list.GetHeaderCtrl()->GetItemCount() - 1; i>=0; i--)
			list.DeleteColumn(i);

		nFields = fPDX->ReportFieldCount;
		for (i=0; i < nFields; i++)
		{
			fPDX->ReportFieldIndex = i;
			list.InsertColumn (i, fPDX->ReportFieldName, LVCFMT_CENTER, 80, i);
		}
	}
	
	//    
	for (j=0; j<(fPDX->ReportRecordCount); j++)
	{	
		fPDX->GetRecord();
		fPDX->ReportFieldIndex = 0;
		list.InsertItem (j, fPDX->ReportField);
		for (i = 1; i < nFields; i++)
		{
			fPDX->ReportFieldIndex = i;
			list.SetItemText (j, i, fPDX->ReportField);
		}

	}
	
	try
	{
		fPDX->EndReport();
	}
	catch (const _com_error)
	{
		//    
		MessageBox (fPDX->ResultDescription, "...");
	}

	UpdateForm();
}

void CSamplePDXDlg::OnEnable() 
{
	try
	{
		bEnable.GetCheck() ? fPDX->DeviceEnabled = TRUE : fPDX->DeviceEnabled = FALSE;
	}
	catch (const _com_error)
	{
		//    
		MessageBox (fPDX->ResultDescription, "...");
	}

	UpdateForm();
}


void CSamplePDXDlg::OnWritedata() 
{
	char str[100];
	int i, j;

	//      
	//     
	try 
	{
		//      
		fPDX->FormNumber = NumberOfForm.GetCurSel();
		fPDX->BeginAdd ();		
		
		//    5 
		for (i = 0; i < 5; i++)
		{
			for (j = 0; j < 16; j++)
			{
				//  - 				
				fPDX->ReportFieldIndex = j;
				wsprintf (str, " %d  %d", i+1, j+1);
				fPDX->ReportField = str;
			}

			//  
			fPDX->SetRecord();
		}

		//  
		fPDX->EndAdd();
		UpdateForm();
	}
	catch (...)
	{
		MessageBox (fPDX->ResultDescription, "...");		
	}
}

void CSamplePDXDlg::OnSelchangeNumberofform() 
{
	//    ,    
	//   
	nFields = 0;
	UpdateForm();	
}
