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

#include "stdafx.h"
#include "AsCfgBuilder.h"

#include "MainFrm.h"
#include "ProjectTreeView.h"
#include "ProjectItemView.h"
#include "ProjectDoc.h"
#include "TreePropSheet.h"
#include "PropPage_CppSettings.h"
#include "PropPage_CSharpSettings.h" 
#include "PropPage_AtlSettings.h"
#include "XmlProjectSettings_Cpp.h"
#include "XmlProjectSettings_Atl.h"
#include "XmlProjectSettings_VC7.h"
#include "Win32Error.h"
#include "GenerationReportDlg.h"
#include "MainDialogSettings.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_COMMAND(ID_CODEGENERATION_OPTIONS, OnCodeGenerationOptions)
	ON_COMMAND(ID_CODEGENERATION_GO, OnCodeGenerationGo)
	ON_COMMAND(ID_VIEW_ITEM_ID, OnViewItemId)
	ON_UPDATE_COMMAND_UI(ID_VIEW_ITEM_ID, OnUpdateViewItemId)
	ON_COMMAND(ID_CODEGENERATION_MAINDIALOGPROPERTIES, OnCodeGenerationMainDialogProperties)
	//}}AFX_MSG_MAP
	ON_UPDATE_COMMAND_UI_RANGE(AFX_ID_VIEW_MINIMUM, AFX_ID_VIEW_MAXIMUM, OnUpdateViewStyles)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
cmString CMainFrame::appPath_;

CMainFrame::CMainFrame():
	displayItemId_(false)
{
	TCHAR buf[MAX_PATH];
	
	::GetCurrentDirectory(MAX_PATH, buf);

	appPath_ = buf;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}
	if (!m_wndDlgBar.Create(this, IDR_MAINFRAME, 
		CBRS_ALIGN_TOP, AFX_IDW_DIALOGBAR))
	{
		TRACE0("Failed to create dialogbar\n");
		return -1;		// fail to create
	}

	if (!m_wndReBar.Create(this) ||
		!m_wndReBar.AddBar(&m_wndToolBar) ||!m_wndReBar.AddBar(&m_wndDlgBar))
	{
		TRACE0("Failed to create rebar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Remove this if you don't want tool tips
	m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
		CBRS_TOOLTIPS | CBRS_FLYBY);
	
	CreateDirectory(GetAppPath(), NULL);

	return 0;
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
	CCreateContext* pContext)
{
	// create splitter window
	if (!m_wndSplitter.CreateStatic(this, 1, 2))
		return FALSE;

	if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CProjectTreeView), CSize(200, 100), pContext) ||
		!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CProjectItemView), CSize(100, 100), pContext))
	{
		m_wndSplitter.DestroyWindow();
		return FALSE;
	}

	return TRUE;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	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

CProjectItemView* CMainFrame::GetRightPane()
{
	CWnd* pWnd = m_wndSplitter.GetPane(0, 1);
	CProjectItemView* pView = DYNAMIC_DOWNCAST(CProjectItemView, pWnd);
	return pView;
}

void CMainFrame::OnUpdateViewStyles(CCmdUI* pCmdUI)
{
	// TODO: customize or extend this code to handle choices on the
	// View menu.

	CProjectItemView* pView = GetRightPane(); 

	// if the right-hand pane hasn't been created or isn't a view,
	// disable commands in our range

	if (pView == NULL)
		pCmdUI->Enable(FALSE);
	else
	{
		DWORD dwStyle = pView->GetStyle() & LVS_TYPEMASK;

		// if the command is ID_VIEW_LINEUP, only enable command
		// when we're in LVS_ICON or LVS_SMALLICON mode

		if (pCmdUI->m_nID == ID_VIEW_LINEUP)
		{
			if (dwStyle == LVS_ICON || dwStyle == LVS_SMALLICON)
				pCmdUI->Enable();
			else
				pCmdUI->Enable(FALSE);
		}
		else
		{
			// otherwise, use dots to reflect the style of the view
			pCmdUI->Enable();
			BOOL bChecked = FALSE;

			switch (pCmdUI->m_nID)
			{
			case ID_VIEW_DETAILS:
				bChecked = (dwStyle == LVS_REPORT);
				break;

			case ID_VIEW_SMALLICON:
				bChecked = (dwStyle == LVS_SMALLICON);
				break;

			case ID_VIEW_LARGEICON:
				bChecked = (dwStyle == LVS_ICON);
				break;

			case ID_VIEW_LIST:
				bChecked = (dwStyle == LVS_LIST);
				break;

			default:
				bChecked = FALSE;
				break;
			}

			pCmdUI->SetRadio(bChecked ? 1 : 0);
		}
	}
}




void CMainFrame::OnCodeGenerationOptions() 
{
	CPropPage_CppSettings wndCppSettings;
	CPropPage_AtlSettings wndAtlSettings;
	CPropPage_CSharpSettings wndCSharpSettings;

	CProjectDoc* doc;

	doc = GetDocument();
	
	// disable help button
	wndCppSettings.m_psp.dwFlags&= ~PSP_HASHELP;
	wndAtlSettings.m_psp.dwFlags&= ~PSP_HASHELP;
	wndCSharpSettings.m_psp.dwFlags&= ~PSP_HASHELP;

	wndCppSettings.SetData(doc->GetProject()->GetProjectSettings()->GetCppSettings());
	wndAtlSettings.SetData(doc->GetProject()->GetProjectSettings()->GetAtlSettings(), doc->GetProject());
	wndCSharpSettings.SetData(doc->GetProject()->GetProjectSettings()->GetCSharpSettings());
		
	// set images
	CImageList	defaultImages;

	defaultImages.Create(16, 16, ILC_COLOR32 | ILC_MASK, 4, 4);
	defaultImages.Add(AfxGetApp()->LoadIcon(IDI_CLASS));
	defaultImages.Add(AfxGetApp()->LoadIcon(IDI_SETTINGS));
		
	TreePropSheet::CTreePropSheet::SetPageIcon(&wndCppSettings, defaultImages, 1);
	TreePropSheet::CTreePropSheet::SetPageIcon(&wndAtlSettings, defaultImages, 1);
	TreePropSheet::CTreePropSheet::SetPageIcon(&wndCSharpSettings, defaultImages, 1);

	TreePropSheet::CTreePropSheet sht(_T("Project Settings"));
	sht.m_psh.dwFlags&= ~PSH_HASHELP;
	sht.SetTreeWidth(200);
	sht.AddPage(&wndCppSettings);
	sht.AddPage(&wndAtlSettings);
	sht.AddPage(&wndCSharpSettings);

	sht.SetEmptyPageText(_T("Please select a child item of '%s'."));

	sht.SetTreeViewMode(TRUE, TRUE, TRUE);
	sht.SetTreeDefaultImages(&defaultImages);
	sht.SetActivePage(&wndCppSettings);

	sht.DoModal();

	sht.DestroyPageIcon(&wndCppSettings);
	sht.DestroyPageIcon(&wndAtlSettings);
	sht.DestroyPageIcon(&wndCSharpSettings);

}

void CMainFrame::OnCodeGenerationGo() 
{
	CGenerationReportDlg dlg;
	CProjectDoc* doc;

	doc = GetDocument();

	dlg.Init(doc->GetProject(), appPath_);
	dlg.DoModal();
	
}

CProjectDoc* CMainFrame::GetDocument()
{
	POSITION pos;
	CDocTemplate* docTemplate;
	CProjectDoc* ret = NULL;

	pos = AfxGetApp()->GetFirstDocTemplatePosition();

	docTemplate = AfxGetApp()->GetNextDocTemplate(pos);
	if (docTemplate) {
		pos = docTemplate->GetFirstDocPosition();

		ret = dynamic_cast<CProjectDoc*>(docTemplate->GetNextDoc(pos));
	}

	return ret;
}


void CMainFrame::OnViewItemId() 
{
	CProjectTreeView* treeView;

	displayItemId_ = !displayItemId_;

	treeView = dynamic_cast<CProjectTreeView*>(m_wndSplitter.GetPane(0, 0));
	if (treeView)
		treeView->DisplayItemId(displayItemId_);
}

void CMainFrame::OnUpdateViewItemId(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable();
	pCmdUI->SetCheck(displayItemId_);
}

void CMainFrame::OnCodeGenerationMainDialogProperties() 
{
	CMainDialogSettings dlg;
	int ret;

	dlg.SetMainDialogSettings(GetDocument()->GetProject()->GetMainDialog());
	dlg.SetProject(GetDocument()->GetProject());
	ret = dlg.DoModal();

}

cmString CMainFrame::GetAppPath()
{
	return appPath_;
}

cmString CMainFrame::GetTmpPath()
{
	return appPath_ + _T("\\tmp");
}
