/************************************
  REVISION LOG ENTRY
  Revision By: Mihai Filimon ( mfl@exontrol.com )
  Revised on 08/01/2009 3:26:00 PM

  This code may be used in compiled form in any way you desire. This
  file may not be redistributed modified or unmodified without the authors
  written consent.

 ************************************/

#include "stdafx.h"
#include "CIsoView.h"

#include "IsoDoc.h"
#include "IsoView.h"
#include "items.h"
#include "chart.h"

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

/////////////////////////////////////////////////////////////////////////////
// CIsoView

IMPLEMENT_DYNCREATE(CIsoView, CFormView)

BEGIN_MESSAGE_MAP(CIsoView, CFormView)
	//{{AFX_MSG_MAP(CIsoView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, OnGanttPrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, OnGanttPrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnGanttPrint/*CFormView::OnFilePrintPreview*/)
  ON_WM_SIZE()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIsoView construction/destruction

long CIsoView::ms_nCount = 0;

CIsoView::CIsoView()
	: CFormView(CIsoView::IDD)
{
	//{{AFX_DATA_INIT(CIsoView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

	ms_nCount++;
}

CIsoView::~CIsoView()
{
	ms_nCount--;

	if ( ms_nCount == 1 )
	{
		CWnd* pFirst = AfxGetMainWnd()->GetWindow( GW_CHILD );
		if ( pFirst != NULL )
		{
			pFirst = pFirst->GetWindow( GW_CHILD );
			if ( pFirst != NULL )
				pFirst->ShowWindow( SW_SHOWMAXIMIZED );
		}
			
	}
}

void CIsoView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIsoView)
	DDX_Control(pDX, IDC_G2ANTT1, m_g2antt);
	//}}AFX_DATA_MAP
}

BOOL CIsoView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

static CString LoadStringFromResource( UINT nID, LPCTSTR szResource )
{
	CString strText;
	HMODULE hModule = AfxGetResourceHandle();
	if ( HRSRC hSRC = FindResource( hModule, MAKEINTRESOURCE( nID ), szResource ) )
		if ( HGLOBAL h = LoadResource( hModule, hSRC ) )
		{
			if ( void* prgnTemplate = LockResource( h ) )
			{
				int n = SizeofResource( hModule , hSRC );
#ifdef _UNICODE
				USES_CONVERSION;
				BYTE* p = new BYTE[ n + 1];
				ZeroMemory( p, n + 1 );
				memcpy( p, prgnTemplate, n );
				strText = A2T(LPSTR(p));
				delete[] p;
#else
				strText = CString( (TCHAR*)prgnTemplate, n );
#endif //_UNICODE
				UnlockResource( h );
			}
			FreeResource( h );
		}
	return strText;
}


void CIsoView::OnInitialUpdate()
{

	CFormView::OnInitialUpdate();
	ResizeParentToFit();

	m_g2antt.SetTemplate( LoadStringFromResource( IDR_TEMPLATE1, _T("Template") ) );

	if ( ms_nCount == 1 )
	{
		if ( CWnd* pParent = GetParent() )
			pParent->ShowWindow( SW_SHOWMAXIMIZED );
	}
	else
		AfxGetMainWnd()->PostMessage( WM_COMMAND, MAKEWPARAM( ID_WINDOW_TILE_HORZ, 0 ), NULL);

	if (!m_print.Create ("", WS_CHILD, CRect(), this, 106))
	{
		AfxMessageBox (_T("Unable to create print control"));
		return;
	}

}

void CIsoView::OnSize (UINT nType, int cx, int cy)
{
	CFormView::OnSize (nType, cx, cy);

	if (::IsWindow (m_g2antt.m_hWnd))
	{
		CRect rect;
		GetClientRect (rect);
		m_g2antt.SetWindowPos (NULL,
							   rect.left, rect.top,
							   rect.Width (), rect.Height (),
							   SWP_NOACTIVATE | SWP_NOZORDER);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CIsoView printing

void CIsoView::OnGanttPrint()
{
	m_print.SetCaption( _T("IsoView") );
	m_print.SetPrintExt (m_g2antt.GetControlUnknown ());
	m_print.Preview ();
}

/////////////////////////////////////////////////////////////////////////////
// CIsoView diagnostics

#ifdef _DEBUG
void CIsoView::AssertValid() const
{
	CFormView::AssertValid();
}

void CIsoView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CIsoViewDoc* CIsoView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CIsoViewDoc)));
	return (CIsoViewDoc*)m_pDocument;
}
#endif //_DEBUG

