// ZoomDialog.cpp : implementation file
//

#include "stdafx.h"
#include "gosview.h"
#include "ZoomDialog.h"
#include "GosTracePane.h"
#include "math.h"

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

const int numPow10 = 13;	// number of powers of ten fully supported
const int startPow10 = 1;	// index into DivStrings of first supported power
//const int subPow10 = 8;		// number of tics in slider per power of 10
const int subPow10 = 32;		// number of tics in slider per power of 10
const double visZoom = 2.3;	// pixels/dev range = [ 10^visZoom-1, 10^visZoom )

const char *DivStrings[] =
{
	"1nS / div",			// 10^-9
	"10nS / div",			// 10^-8
	"100nS / div",
	"1us / div",
	"10us / div",
	"100us / div",
	"1mS / div",
	"10mS / div",
	"100mS / div",
	"1S / div",
	"10S / div",
	"100S / div",
	"1000S / div",
	"10,000S / div",
	"100,000S / div"
};


/////////////////////////////////////////////////////////////////////////////
// CZoomDialog dialog


CZoomDialog::CZoomDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CZoomDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CZoomDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	m_fReady = false;
	m_pTracePane = NULL;
}


void CZoomDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CZoomDialog)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CZoomDialog, CDialog)
	//{{AFX_MSG_MAP(CZoomDialog)
	ON_WM_HSCROLL()
	ON_CBN_SELCHANGE(IDC_ZOOM_UNITS, OnSelchangeZoomUnits)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CZoomDialog message handlers

//const int numPow10 = 13;	// number of powers of ten fully supported
//const int startPow10 = 1;	// index into DivStrings of first supported power
//const int subPow10 = 8;		// number of tics in slider per power of 10

void CZoomDialog::ReadZoomSlider()
{
	CSliderCtrl *pZoomSlider = (CSliderCtrl *)(GetDlgItem(IDC_ZOOM_SLIDER));
	int zoomNum = pZoomSlider->GetPos();
	int sliderOff = numPow10*subPow10-zoomNum;

	// double m_rSecPerPixel;	// 10^-9 ..10^+3
	// double m_rPixPerDiv;	// 10 .. 75
	// double m_rSecPerDiv;	// 10^-8 .. 10^+4

	double powSecPerPixel = sliderOff/(double)subPow10-10.375+(double)startPow10;	// -9.375, -9.25,-9.125, -9, -8.875, -8.75, ..
	m_rSecPerPixel = pow( 10.0, powSecPerPixel );
	double secDivPow = floor( powSecPerPixel + 20.0 + visZoom ) - 20.0;	// the +/-20 stunt avoids problems with floor(-ve)
	m_rSecPerDiv = pow( 10.0, secDivPow );	// the visZoom shifts the point where divisions split
	m_rPixelPerDiv = m_rSecPerDiv / m_rSecPerPixel;

	int selNum = int(9.1+secDivPow) - startPow10;
	CComboBox *pZoomUnits = (CComboBox *)(GetDlgItem(IDC_ZOOM_UNITS));
	pZoomUnits->SetCurSel(selNum);
	m_fReady = true;
	if( m_pTracePane )
		m_pTracePane->OnUpdate(NULL,0,NULL);
}

void CZoomDialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	ReadZoomSlider();
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CZoomDialog::OnOK()
{
	return;
}

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

	CComboBox *pZoomUnits = (CComboBox *)(GetDlgItem(IDC_ZOOM_UNITS));
	CSliderCtrl *pZoomSlider = (CSliderCtrl *)(GetDlgItem(IDC_ZOOM_SLIDER));

	int i;
	for(i=startPow10; i<=numPow10+startPow10; i++ )
		pZoomUnits->AddString(DivStrings[i]);

	pZoomSlider->SetRange(0,numPow10*subPow10);
	pZoomSlider->SetTicFreq(subPow10);
	pZoomSlider->SetPos((numPow10*subPow10)>>1);

	ReadZoomSlider();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CZoomDialog::OnSelchangeZoomUnits() 
{
	CComboBox *pZoomUnits = (CComboBox *)(GetDlgItem(IDC_ZOOM_UNITS));
	CSliderCtrl *pZoomSlider = (CSliderCtrl *)(GetDlgItem(IDC_ZOOM_SLIDER));
	pZoomSlider->SetPos( (numPow10 - pZoomUnits->GetCurSel()) * subPow10 );
	ReadZoomSlider();
}

void CZoomDialog::ZoomToSecPerPixel( double newSecPerPixel )
{
	CSliderCtrl *pZoomSlider = (CSliderCtrl *)(GetDlgItem(IDC_ZOOM_SLIDER));
	pZoomSlider->SetPos( numPow10*subPow10-(int)((log10(newSecPerPixel)+10.375-(double)startPow10) * subPow10) );
	// read it back to click to the nearest appropriate setting
	ReadZoomSlider();

}





