//
//  setup_tool_dlg.cpp - setup tool dialog class implementation
//
//  $Id: setup_tool_dlg.cpp 39 2014-02-06 19:42:20Z 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 iToolSetup = FIRST_TOOL;

//
// CSetupToolDlg dialog
//
CSetupToolDlg::CSetupToolDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSetupToolDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetupToolDlg)
	m_Tool = "";
	m_ToolOrientation = 0;
	m_ToolRadius = "";
	m_XToolOffset = "";
	m_YToolOffset = "";
	m_ZToolOffset = "";
	m_ToolDescription = "";
	m_ToolWear = "";
	m_ToolWearFactor = 0;
	//}}AFX_DATA_INIT
}

void CSetupToolDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetupToolDlg)
	DDX_Control(pDX, IDC_TOOL_SCROLL, m_ToolScroll);
	DDX_Text(pDX, IDC_TOOL, m_Tool);
	DDV_MaxChars(pDX, m_Tool, 3);
	DDX_Text(pDX, IDC_TOOLORIENTATION, m_ToolOrientation);
	DDX_Text(pDX, IDC_TOOLRADIUS, m_ToolRadius);
	DDX_Text(pDX, IDC_XTOOLOFFSET, m_XToolOffset);
	DDX_Text(pDX, IDC_YTOOLOFFSET, m_YToolOffset);
	DDX_Text(pDX, IDC_ZTOOLOFFSET, m_ZToolOffset);
	DDX_Text(pDX, IDC_TOOLDESCRIPTION, m_ToolDescription);
	DDX_Text(pDX, IDC_TOOLWEAR, m_ToolWear);
	DDX_Text(pDX, IDC_TOOLWEARFACTOR, m_ToolWearFactor);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSetupToolDlg, CDialog)
	//{{AFX_MSG_MAP(CSetupToolDlg)
	ON_WM_VSCROLL()
	ON_EN_KILLFOCUS(IDC_TOOL, OnKillfocusTool)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CSetupToolDlg message handlers

void CSetupToolDlg::OnKillfocusTool()
{
	char szToolSetup[INTSTRINGLENGTH];
	
	// Get current dialog data into member variables
	UpdateData(TRUE);
	
	// Copy member variables to temporary tool table
	RetrieveVariables(iToolSetup);

	// Get new tool number		
	iToolSetup = atoi((const char *)m_Tool);

	// Limit check for valid tool number	
	iToolSetup = __min(LAST_TOOL, iToolSetup);
	iToolSetup = __max(FIRST_TOOL, iToolSetup);
	
	// Update tool number in member variable
	sprintf(szToolSetup,"%d",iToolSetup);
	m_Tool = szToolSetup;
	
	// Update member variables with values from the temporary tool
	//  table
	UpdateVariables(iToolSetup);
	
	// Move member variables to dialog
	UpdateData(FALSE);
}


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


void CSetupToolDlg::RetrieveVariables(int id)
{
	// Convert tool number to array index
	int index = Tool::ConvertToIndex(id);
	
	// Retrieve the dialog data
	m_tools[index].Description = m_ToolDescription;
	m_tools[index].X.SetS((const char *)m_XToolOffset);
	m_tools[index].Y.SetS((const char *)m_YToolOffset);
	m_tools[index].Z.SetS((const char *)m_ZToolOffset);
	m_tools[index].R.SetS((const char *)m_ToolRadius);
	m_tools[index].W.SetS((const char *)m_ToolWear);
	m_tools[index].F = m_ToolWearFactor;
	m_tools[index].Q = m_ToolOrientation;
}

		
void CSetupToolDlg::UpdateVariables(int id)
{
	// Convert tool number to array index
	int index = Tool::ConvertToIndex(id);
	
	// Update the dialog data
	char dimension[VALUESTRINGLENGTH];
	int precision = theApp.GetMachine()->GetSetupPrecision();

	m_ToolDescription = m_tools[index].Description;
	m_XToolOffset = m_tools[index].X.GetS(dimension, precision);
	m_YToolOffset = m_tools[index].Y.GetS(dimension, precision);
	m_ZToolOffset = m_tools[index].Z.GetS(dimension, precision);
	m_ToolRadius = m_tools[index].R.GetS(dimension, precision);
	m_ToolWear = m_tools[index].W.GetS(dimension, precision);
	m_ToolWearFactor = (double)m_tools[index].F;
	m_ToolOrientation = (int)m_tools[index].Q;
}
		
