// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "SixtyFour.h"

#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	
    CWinApp* pApp = AfxGetApp();
	int 
		nTop = pApp->GetProfileInt(_T("Window"), _T("Top"), -1),
		nBottom = pApp->GetProfileInt(_T("Window"), _T("Bottom"), -1),
		nRight = pApp->GetProfileInt(_T("Window"), _T("Right"), -1),
		nLeft = pApp->GetProfileInt(_T("Window"), _T("Left"), -1);

	// Stop the stupid document - app thing

	cs.style &= ~FWS_ADDTOTITLE & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME;

	cs.x = 100;
	cs.y = 100;

	cs.cx = 500;
	cs.cy = 500;

	if (nTop != -1 && nBottom != -1 && nRight != -1 && nLeft != -1)
    {
        // restore the window's width and height

        cs.cx = nRight - nLeft;
        cs.cy = nBottom - nTop;

        // the following correction is needed when the taskbar is
        // at the left or top and it is not "auto-hidden"

        RECT workArea;
        SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
        nLeft += workArea.left;
        nTop += workArea.top;

        // make sure the window is not completely out of sight

        int max_x = GetSystemMetrics(SM_CXSCREEN) - GetSystemMetrics(SM_CXICON);
        int max_y = GetSystemMetrics(SM_CYSCREEN) - GetSystemMetrics(SM_CYICON);
        cs.x = min(nLeft, max_x);
        cs.y = min(nTop, max_y);
    }

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers



void CMainFrame::OnClose() 
{
    CWinApp* pApp = AfxGetApp();

    WINDOWPLACEMENT wp;
    GetWindowPlacement(&wp);
    pApp->WriteProfileInt(_T("Window"), _T("Top"), wp.rcNormalPosition.top);
    pApp->WriteProfileInt(_T("Window"), _T("Left"), wp.rcNormalPosition.left);
    pApp->WriteProfileInt(_T("Window"), _T("Bottom"), wp.rcNormalPosition.bottom);
    pApp->WriteProfileInt(_T("Window"), _T("Right"), wp.rcNormalPosition.right);
	
	CFrameWnd::OnClose();
}
