// DlgImagery.cpp : implementation file
//

#include "stdafx.h"
#include "6046dryend.h"
#include "6046DryEndDlg.h"
#include "DlgImagery.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgImagery dialog


CDlgImagery::CDlgImagery(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgImagery::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgImagery)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	m_bInitDone = FALSE;
}


void CDlgImagery::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgImagery)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgImagery, CDialog)
	//{{AFX_MSG_MAP(CDlgImagery)
	ON_WM_DESTROY()
	ON_WM_GETMINMAXINFO()
	ON_WM_SIZE()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgImagery message handlers

void CDlgImagery::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

BOOL CDlgImagery::OnInitDialog() 
{
	CDialog::OnInitDialog();

	HICON icon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	SetIcon(icon, FALSE);
	
	pConfig = &((CMy6046DryEndApp*)AfxGetApp())->m_SystemConfig;
	if(pConfig->Open(CSystemConfig::accessRead))
	{
		pConfig->Read();
		pConfig->Close();
	}
	m_clientRect = pConfig->IMAGERYWindowRect;
	
	this->SetWindowPos(NULL,m_clientRect.left,m_clientRect.
							top,m_clientRect.Width(),m_clientRect.Height(),SWP_NOZORDER);
	
	GetClientRect(&m_clientRect);

	if(m_dispWedge.Create(NULL, NULL, WS_CHILD|WS_VISIBLE,m_clientRect,this,1255))
	{
		if(m_dispWedge.CreateGLWindow())
		{
			if(!m_dispWedge.InitGL() || !m_dispWedge.DownloadTexture())
				AfxMessageBox("Error Initializing OpenGL, Imagery Display Disabled");
			m_dispWedge.MoveWindow(&m_clientRect);
			m_dispWedge.ShowWindow(SW_SHOW);

			m_dispWedge.SetLabels(RANGE);
			m_dispWedge.BathyPoints(TRUE);
			m_dispWedge.SetFLS(0);
			m_dispWedge.SetFlip(pConfig->bFlip);
			m_dispWedge.SetGridLines(1);
			m_dispWedge.SetPalette(0);
			m_dispWedge.SetBrightness(0);
			m_dispWedge.SetContrast(0);

			m_bInitDone = TRUE;
		}
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgImagery::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	if(m_bInitDone)
	{
		GetClientRect(&m_clientRect);
		m_dispWedge.MoveWindow(m_clientRect);
		pConfig->IMAGERYWindowRect = m_clientRect;
		
	}
}


void CDlgImagery::OnGetMinMaxInfo(MINMAXINFO *lpMMI)
{
	lpMMI->ptMinTrackSize.x = 200;
	lpMMI->ptMinTrackSize.y = 225;

	CDialog::OnPaint();
	CDialog::OnGetMinMaxInfo(lpMMI);
}

LRESULT CDlgImagery::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	if(message == WM_EXITSIZEMOVE)
	{
		CRect rect;
		GetWindowRect(&rect);
		
		pConfig->IMAGERYWindowRect = rect;

		if(pConfig->Open(CSystemConfig::accessWrite))
		{
			pConfig->Save();
			pConfig->Close();
		}
	}
	
	return CDialog::WindowProc(message, wParam, lParam);
}


void CDlgImagery::OnClose() 
{
	((CMy6046DryEndDlg*)AfxGetMainWnd())->ForceClose(DSPIMAGERY);
	
	CDialog::OnClose();
}
