// ExportOptions.cpp : implementation file
//

#include "stdafx.h"
#include "TCTb.h"
#include "ExportOptions.h"

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




/////////////////////////////////////////////////////////////////////////////
// CExportOptions dialog


CExportOptions::CExportOptions(CWnd* pParent /*=NULL*/)
: CDialog(CExportOptions::IDD, pParent)
{
	//{{AFX_DATA_INIT(CExportOptions)
	m_dxtext = 0.0f;
	m_dytext = 0.0f;
	m_showexport = FALSE;
	m_voff = 0.0f;
	m_xstext = 0.0f;
	m_zstext = 0.0f;
	m_dztext = 0.0f;
	//}}AFX_DATA_INIT
	NoUpdate=0;
}


void CExportOptions::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExportOptions)
	DDX_Control(pDX, IDC_ZSTEXT, m_zstctrl);
	DDX_Control(pDX, IDC_XSTEXT, m_xstctrl);
	DDX_Control(pDX, IDC_DZTEXT, m_dztctrl);
	DDX_Control(pDX, IDC_DYTEXT, m_dytctrl);
	DDX_Control(pDX, IDC_DXTEXT, m_dxtctrl);
	DDX_Control(pDX, IDC_ZSSLI, m_zssli);
	DDX_Control(pDX, IDC_XSSLI, m_xssli);
	DDX_Control(pDX, IDC_DZSLI, m_dzsli);
	DDX_Control(pDX, IDC_DYSLI, m_dysli);
	DDX_Control(pDX, IDC_DXSLI, m_dxsli);
	DDX_Text(pDX, IDC_DXTEXT, m_dxtext);
	DDX_Text(pDX, IDC_DYTEXT, m_dytext);
	DDX_Check(pDX, IDC_SHOWEXPORT, m_showexport);
	DDX_Text(pDX, IDC_VOFFTEXT, m_voff);
	DDX_Text(pDX, IDC_XSTEXT, m_xstext);
	DDX_Text(pDX, IDC_ZSTEXT, m_zstext);
	DDX_Text(pDX, IDC_DZTEXT, m_dztext);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CExportOptions, CDialog)
//{{AFX_MSG_MAP(CExportOptions)
ON_WM_HSCROLL()
ON_BN_CLICKED(IDC_FREESTYLE, OnFreestyle)
ON_BN_CLICKED(IDC_GRIDSIZE, OnGridsize)
ON_BN_CLICKED(IDC_SETCONVERSION, OnSetconversion)
ON_EN_CHANGE(IDC_ZSTEXT, OnChangeZstext)
ON_EN_CHANGE(IDC_XSTEXT, OnChangeXstext)
ON_EN_CHANGE(IDC_DZTEXT, OnChangeDztext)
ON_EN_CHANGE(IDC_DYTEXT, OnChangeDytext)
ON_EN_CHANGE(IDC_DXTEXT, OnChangeDxtext)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExportOptions message handlers

BOOL CExportOptions::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	
	m_dxsli.SetRange(1,50);
	m_dysli.SetRange(0,40);
	m_dzsli.SetRange(1,50);
	
	m_xssli.SetRange(1,50);
	m_zssli.SetRange(1,50);
	
	NoUpdate++;
	m_dxsli.SetPos((int)m_dxtext);
	m_dysli.SetPos((int)m_dxtext);
	m_dzsli.SetPos((int)m_dxtext);
	m_xssli.SetPos((int)m_xstext);
	m_zssli.SetPos((int)m_zstext);
	NoUpdate--;
	UpdateControls();
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CExportOptions::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	if(NoUpdate) return;
	
	CSliderCtrl *ctrl=(CSliderCtrl *)pScrollBar;
	if(ctrl==&m_dxsli)
	{
		m_dxtext=(float)m_dxsli.GetPos();
	}
	
	if(ctrl==&m_dysli)
	{
		m_dytext=((float)m_dysli.GetPos())*0.25f;
	}
	
	if(ctrl==&m_dzsli)
	{
		m_dztext=(float)m_dzsli.GetPos();
	}
	
	if(ctrl==&m_xssli)
	{
		m_xstext=(float)m_xssli.GetPos();
	}
	
	if(ctrl==&m_zssli)
	{
		m_zstext=(float)m_zssli.GetPos();
	}
	UpdateData(FALSE);
	UpdateTextFields();
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CExportOptions::OnFreestyle()     { EInfo.BType=ExportInfo::BT_FREESTYLE; UpdateControls(); }
void CExportOptions::OnGridsize()	   { EInfo.BType=ExportInfo::BT_GSIZE;	 UpdateControls(); }
void CExportOptions::OnSetconversion() { EInfo.BType=ExportInfo::BT_SETCON; UpdateControls(); }

void CExportOptions::SetData(const ExportInfo &einf)
{
	EInfo = einf;
	
	EInfo.GetSizeKM(&m_xstext,&m_zstext);

	EInfo.GetScale_Offset(&m_dxtext, &m_dytext, &m_dztext, &m_voff);
	
	m_showexport=EInfo.Display;
}

void CExportOptions::UpdateControls()
{
	
	m_dxsli.EnableWindow(TRUE);
	m_dxtctrl.EnableWindow(TRUE);
	m_dysli.EnableWindow(TRUE);
	m_dytctrl.EnableWindow(TRUE);
	m_dzsli.EnableWindow(TRUE);
	m_dztctrl.EnableWindow(TRUE);
	
	m_zssli.EnableWindow(TRUE);
	m_xssli.EnableWindow(TRUE);
	m_zstctrl.EnableWindow(TRUE);
	m_xstctrl.EnableWindow(TRUE);
	
	
	CheckDlgButton(IDC_SETCONVERSION,FALSE);
	CheckDlgButton(IDC_GRIDSIZE,FALSE);
	CheckDlgButton(IDC_FREESTYLE,FALSE);
	
	switch(EInfo.BType)
	{
	case ExportInfo::BT_SETCON:		CheckDlgButton(IDC_SETCONVERSION,TRUE);		break;
	case ExportInfo::BT_GSIZE:		CheckDlgButton(IDC_GRIDSIZE,TRUE);			break;
	case ExportInfo::BT_FREESTYLE:	CheckDlgButton(IDC_FREESTYLE,TRUE);			break;
	}
	
	
	if(IsDlgButtonChecked(IDC_SETCONVERSION))
	{
		m_dxsli.EnableWindow(FALSE);
		m_dzsli.EnableWindow(FALSE);
		m_dxtctrl.EnableWindow(FALSE);
		m_dztctrl.EnableWindow(FALSE);
	}
	
	if(IsDlgButtonChecked(IDC_GRIDSIZE))
	{
		m_zssli.EnableWindow(FALSE);
		m_xssli.EnableWindow(FALSE);
		m_zstctrl.EnableWindow(FALSE);
		m_xstctrl.EnableWindow(FALSE);
	}
	
}

void CExportOptions::GetData(ExportInfo *einf)
{
	einf->BType=EInfo.BType;
	einf->Display=m_showexport;
	
	einf->SetScale_Offset(m_dxtext, m_dytext, m_dztext, m_voff);
	einf->SetSizeKM(m_xstext,m_zstext);
}

void CExportOptions::OnChangeZstext() 
{
	if(NoUpdate) return;
	NoUpdate++;
	UpdateData(TRUE);
	m_zssli.SetPos((int)m_zstext);
	
	UpdateTextFields();
	NoUpdate--;
}	


void CExportOptions::OnChangeXstext() 
{
	if(NoUpdate) return;
	NoUpdate++;
	UpdateData(TRUE);
	m_xssli.SetPos((int)m_xstext);
	
	UpdateTextFields();
	NoUpdate--;
}

void CExportOptions::OnChangeDztext() 
{
	if(NoUpdate) return;
	NoUpdate++;
	UpdateData(TRUE);
	m_dzsli.SetPos((int)m_dztext);
	
	
	UpdateTextFields();
	NoUpdate--;
}

void CExportOptions::OnChangeDytext() 
{
	if(NoUpdate) return;
	NoUpdate++;
	UpdateData(TRUE);
	m_dysli.SetPos((int)(m_dytext*4));
	
	UpdateTextFields();
	NoUpdate--;
}

void CExportOptions::OnChangeDxtext() 
{
	if(NoUpdate) return;
	NoUpdate++;
	UpdateData(TRUE);
	m_dxsli.SetPos((int)(m_dxtext));
	
	
	UpdateTextFields();
	NoUpdate--;
}

void CExportOptions::UpdateTextFields()
{
	NoUpdate++;
	UpdateData(TRUE);
	
	EInfo.SetScale_Offset(m_dxtext, m_dytext, m_dztext, m_voff);
	
	EInfo.SetSizeKM(m_xstext,m_zstext);
	
	EInfo.GetSizeKM(&m_xstext,&m_zstext);

	EInfo.GetScale_Offset(&m_dxtext, &m_dytext, &m_dztext, &m_voff);

	UpdateData(FALSE);
	m_dxsli.SetPos((int)m_dxtext);
	m_dysli.SetPos((int)m_dytext);
	m_dzsli.SetPos((int)m_dztext);
	m_xssli.SetPos((int)m_xstext);
	m_zssli.SetPos((int)m_zstext);
	NoUpdate--;
}
