//
//  setup_work_coord_dlg.cpp - setup work coordinates dialog class implementation
//
//  $Id: setup_work_coord_dlg.cpp 40 2014-02-06 21:58:01Z brian $ 
//
//  Copyright (c) 1999-2014 by Precision Micro Control Corp. All rights reserved.
//
#include "stdafx.h"

int iWorkCoorSetup = FIRST_WORKCOOR;

//
// CSetupWorkCoorDlg dialog
//
CSetupWorkCoorDlg::CSetupWorkCoorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSetupWorkCoorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetupWorkCoorDlg)
	m_UWorkOffset = "";
	m_VWorkOffset = "";
	m_WWorkOffset = "";
	m_XWorkOffset = "";
	m_YWorkOffset = "";
	m_ZWorkOffset = "";
	m_WorkCoor = "";
	//}}AFX_DATA_INIT
}

void CSetupWorkCoorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetupWorkCoorDlg)
	DDX_Control(pDX, IDC_WORKCOOR_SCROLL, m_WorkCoor_Scroll);
	DDX_Text(pDX, IDC_UWORKOFFSET, m_UWorkOffset);
	DDX_Text(pDX, IDC_VWORKOFFSET, m_VWorkOffset);
	DDX_Text(pDX, IDC_WWORKOFFSET, m_WWorkOffset);
	DDX_Text(pDX, IDC_XWORKOFFSET, m_XWorkOffset);
	DDX_Text(pDX, IDC_YWORKOFFSET, m_YWorkOffset);
	DDX_Text(pDX, IDC_ZWORKOFFSET, m_ZWorkOffset);
	DDX_Text(pDX, IDC_WORKCOOR, m_WorkCoor);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSetupWorkCoorDlg, CDialog)
	//{{AFX_MSG_MAP(CSetupWorkCoorDlg)
	ON_EN_KILLFOCUS(IDC_WORKCOOR, OnKillfocusWorkcoor)
	ON_WM_VSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CSetupWorkCoorDlg message handlers


void CSetupWorkCoorDlg::OnKillfocusWorkcoor()
{
	char szWorkCoorSetup[INTSTRINGLENGTH];
	
	// Get current dialog data into member variables
	UpdateData(TRUE);
	
	// Copy member variables to temporary work coordinate table
	RetrieveVariables(iWorkCoorSetup);

	// Get new work coordinate number		
	iWorkCoorSetup = atoi((const char *)m_WorkCoor);

	// Limit check for valid work coordinate number	
	iWorkCoorSetup = __min(LAST_WORKCOOR, iWorkCoorSetup);
	iWorkCoorSetup = __max(FIRST_WORKCOOR, iWorkCoorSetup);
	
	// Update work coordinate number in member variable
	sprintf(szWorkCoorSetup,"%d",iWorkCoorSetup);
	m_WorkCoor = szWorkCoorSetup;
	
	// Update member variables with values from the temporary work coordinate
	//  table
	UpdateVariables(iWorkCoorSetup);
	
	// Move member variables to dialog
	UpdateData(FALSE);
}


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

void CSetupWorkCoorDlg::RetrieveVariables(int id)
{
	int index = WorkCoord::ConvertToIndex(id);			// Convert work coordinate number to array index
	
	// Retrieve the dialog data
	WorkCoor[index].X.SetS((const char *)m_XWorkOffset);
	WorkCoor[index].Y.SetS((const char *)m_YWorkOffset);
	WorkCoor[index].Z.SetS((const char *)m_ZWorkOffset);
	WorkCoor[index].U.SetS((const char *)m_UWorkOffset);
	WorkCoor[index].V.SetS((const char *)m_VWorkOffset);
	WorkCoor[index].W.SetS((const char *)m_WWorkOffset);
}
		
void CSetupWorkCoorDlg::UpdateVariables(int id)
{
	char dimension[VALUESTRINGLENGTH];
	int precision = theApp.GetMachine()->GetSetupPrecision();
	int index = WorkCoord::ConvertToIndex(id);			// Convert work coordinate number to array index
	
	// Update the dialog data
	m_XWorkOffset = WorkCoor[index].X.GetS(dimension, precision);
	m_YWorkOffset = WorkCoor[index].Y.GetS(dimension, precision);
	m_ZWorkOffset = WorkCoor[index].Z.GetS(dimension, precision);
	m_UWorkOffset = WorkCoor[index].U.GetS(dimension, precision);
	m_VWorkOffset = WorkCoor[index].V.GetS(dimension, precision);
	m_WWorkOffset = WorkCoor[index].W.GetS(dimension, precision);
}
		
