//
//  setup_offsets.cpp - setup offsets dialog class implementation
//
//  $Id: setup_offsets_dlg.cpp 1 2007-04-15 22:52:37Z brian $ 
//
//  Copyright (c) 1999-2007 by Precision Micro Control Corp. All rights reserved.
//
#include "stdafx.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

int iOffsetNumber = FIRST_OFFSET;

/////////////////////////////////////////////////////////////////////////////
// CSetupOffsetsDlg dialog


CSetupOffsetsDlg::CSetupOffsetsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSetupOffsetsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetupOffsetsDlg)
	m_DGeometryOffset = "";
	m_DWearOffset = "";
	m_HGeometryOffset = "";
	m_HWearOffset = "";
	m_OffsetNumber = "";
	m_DWearFactor = 0;
	m_HWearFactor = 0;
	//}}AFX_DATA_INIT
}

void CSetupOffsetsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetupOffsetsDlg)
	DDX_Control(pDX, IDC_OFFSET_NUMBER_SCROLL, m_OffsetNumber_Scrol);
	DDX_Text(pDX, IDC_DGEOMETRYOFFSET, m_DGeometryOffset);
	DDX_Text(pDX, IDC_DWEAROFFSET, m_DWearOffset);
	DDX_Text(pDX, IDC_HGEOMETRYOFFSET, m_HGeometryOffset);
	DDX_Text(pDX, IDC_HWEAROFFSET, m_HWearOffset);
	DDX_Text(pDX, IDC_OFFSET_NUMBER, m_OffsetNumber);
	DDX_Text(pDX, IDC_DWEARFACTOR, m_DWearFactor);
	DDX_Text(pDX, IDC_HWEARFACTOR, m_HWearFactor);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSetupOffsetsDlg, CDialog)
	//{{AFX_MSG_MAP(CSetupOffsetsDlg)
	ON_WM_VSCROLL()
	ON_EN_KILLFOCUS(IDC_OFFSET_NUMBER, OnKillfocusOffsetNumber)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CSetupOffsetsDlg message handlers

void CSetupOffsetsDlg::OnKillfocusOffsetNumber()
{
	char szOffsetNumber[INTSTRINGLENGTH];
	
	// Get current dialog data into member variables
	UpdateData(TRUE);
	
	// Copy member variables to temporary offset table
	RetrieveVariables(iOffsetNumber);

	// Get new offset number		
	iOffsetNumber = atoi((const char *)m_OffsetNumber);

	// Limit check for valid offset number	
	iOffsetNumber = __min(LAST_OFFSET, iOffsetNumber);
	iOffsetNumber = __max(FIRST_OFFSET, iOffsetNumber);
	
	// Update offset number in member variable
	sprintf(szOffsetNumber,"%d",iOffsetNumber);
	m_OffsetNumber = szOffsetNumber;
	
	// Update member variables with values from the temporary offset
	//  table
	UpdateVariables(iOffsetNumber);
	
	// Move member variables to dialog
	UpdateData(FALSE);
}


void CSetupOffsetsDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	char szOffsetNumber[INTSTRINGLENGTH];
	
	// Check for "spin control" scroll messages	
	if ((pScrollBar!=NULL) && (pScrollBar->GetDlgCtrlID()==IDC_OFFSET_NUMBER_SCROLL))
	{
		if ((nSBCode == SB_LINEUP) || (nSBCode == SB_LINEDOWN))
		{
			// Get current dialog data into member variables
			UpdateData(TRUE);
				
			// Copy member variables to temporary offset table
			RetrieveVariables(iOffsetNumber);
			
			// Adjust Work coordinate number up or down
			if (nSBCode == SB_LINEUP)
				iOffsetNumber = __min(LAST_OFFSET, iOffsetNumber+1);
			else
				iOffsetNumber = __max(FIRST_OFFSET, iOffsetNumber-1);
				
			// Update offset number in dialog box
			sprintf(szOffsetNumber,"%d",iOffsetNumber);
			m_OffsetNumber = szOffsetNumber;
			
			// Update member variables with values from the temporary offset
			//  table
			UpdateVariables(iOffsetNumber);
			
			// Move member variables to dialog
			UpdateData(FALSE);
		}
	}
}

void CSetupOffsetsDlg::RetrieveVariables(int iOffsetNumber)
{
	// Retrieve the dialog data
	DOffsetTable.Geometry[iOffsetNumber].SetS((const char *)m_DGeometryOffset);
	DOffsetTable.Wear[iOffsetNumber].SetS((const char *)m_DWearOffset);
	DOffsetTable.WearFactor[iOffsetNumber] = m_DWearFactor;
	HOffsetTable.Geometry[iOffsetNumber].SetS((const char *)m_HGeometryOffset);
	HOffsetTable.Wear[iOffsetNumber].SetS((const char *)m_HWearOffset);
	HOffsetTable.WearFactor[iOffsetNumber] = m_HWearFactor;
}

		
void CSetupOffsetsDlg::UpdateVariables(int iOffsetNumber)
{
	char szDimStr[VALUESTRINGLENGTH];
	int iSUPrec;
	CMachine *pTheMachine;
	
	pTheMachine = theApp.GetMachine();
	iSUPrec = pTheMachine->GetSetupPrecision();
	
	// Update the dialog data
	m_DGeometryOffset = DOffsetTable.Geometry[iOffsetNumber].GetS(szDimStr, iSUPrec);
	m_DWearOffset = DOffsetTable.Wear[iOffsetNumber].GetS(szDimStr, iSUPrec);
	m_DWearFactor = (double)DOffsetTable.WearFactor[iOffsetNumber];
	m_HGeometryOffset = HOffsetTable.Geometry[iOffsetNumber].GetS(szDimStr, iSUPrec);
	m_HWearOffset = HOffsetTable.Wear[iOffsetNumber].GetS(szDimStr, iSUPrec);
	m_HWearFactor = (double)HOffsetTable.WearFactor[iOffsetNumber];
}
		
