// DlgCompass.cpp : implementation file
//

#include "stdafx.h"
#include "6046dryend.h"
#include "6046DryEndDlg.h"
#include "DlgCompass.h"
#include <math.h>


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgCompass dialog


CDlgCompass::CDlgCompass(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgCompass::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgCompass)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	radVal = 180/PI;
}


void CDlgCompass::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgCompass)
	DDX_Control(pDX, IDC_STATIC_REALHEADING, m_ctrlRealHeading);
	DDX_Control(pDX, IDC_YAWRATE, m_ctrlYawRate);
	DDX_Control(pDX, IDC_COMPASS, m_ctrlCompass);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgCompass, CDialog)
	//{{AFX_MSG_MAP(CDlgCompass)
	ON_WM_LBUTTONDOWN()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgCompass message handlers

BOOL CDlgCompass::OnInitDialog() 
{
	CDialog::OnInitDialog();

	HICON icon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	SetIcon(icon, FALSE);

	SetWindowText("Heading Display");

	m_pConfig = (CSystemConfig*)&((CMy6046DryEndApp*)AfxGetApp())->m_SystemConfig;
	LoadSettings();
	
	m_ctrlYawRate.SetBkColor(RGB(0,0,0));
	m_ctrlYawRate.SetTextColor(RGB(0,235,55));

	m_ctrlYawRate.SetWindowText("0 deg/s");

	m_ctrlCompass.SetTicAlign( COMPASS_TIC_BOTH );
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CDlgCompass::OnLButtonDown(UINT nFlags, CPoint point) 
{
	//MAKE WINDOWS THINK WE CLICKED ON THE CAPTION BAR,
	//SO THE DIALOG CAN BE MOVED BY CLICKING ANYWHERE ON IT.
    PostMessage( WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( point.x, point.y));

	CDialog::OnLButtonDown(nFlags, point);
}

void CDlgCompass::AddGroupData( NBlueFin7kMessages::PBLUEFINRECORDTYPEHEADER	 pRecHdr,
  							    NBlueFin7kMessages::PBLUEFINNAVIGATIONMESSAGE    pNavFrame )
{
	m_ctrlCompass.SetPos((int)floor(pNavFrame->m_fYaw*radVal +.5));
	CString temp;
	temp.Format("%.2f°/s", pNavFrame->m_fYawRate * 180/PI);
	m_ctrlYawRate.SetWindowText(temp);

	temp.Format("%.2f°", pNavFrame->m_fYaw*radVal);
	m_ctrlRealHeading.SetWindowText(temp);
}

void CDlgCompass::LoadSettings()
{
	int x,y;

	if(m_pConfig->Open(CSystemConfig::accessRead))
	{		
		m_pConfig->Read();
		m_pConfig->Close();

		x = m_pConfig->HEDWindowX;
		y = m_pConfig->HEDWindowY;

		SetWindowPos(NULL,x,y,0,0,SWP_NOSIZE);
	}
}

void CDlgCompass::SaveSettings()
{
	if(m_pConfig->Open(CSystemConfig::accessWrite))
	{		
		m_pConfig->Save();
		m_pConfig->Close();
	}
}

BOOL CDlgCompass::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
	{
		return TRUE ;
	}	
	return CDialog::PreTranslateMessage(pMsg);
}

LRESULT CDlgCompass::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	if(message == WM_EXITSIZEMOVE)
	{
		CRect rect;
		GetWindowRect(&rect);
		m_pConfig->HEDWindowX	= rect.left;
		m_pConfig->HEDWindowY	= rect.top;
	}
	
	return CDialog::WindowProc(message, wParam, lParam);
}

void CDlgCompass::OnClose() 
{
	((CMy6046DryEndDlg*)AfxGetMainWnd())->ForceClose(DSPCOMPASS);
	
	
	CDialog::OnClose();
}
