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

#include "stdafx.h"
#include "AG_MDA.h"

#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame
CToolBar    m_AGToolBar;
static CMainFrame *m_pMainFrame;
extern ShapeElement *m_pRootShape ;
void AddToLog( CString str )
{
	if( m_pMainFrame ) m_pMainFrame->AppendLog( str ) ;
}
void ClearUpLog()
{
	if( m_pMainFrame ) m_pMainFrame->ClearLog();
}
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

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

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	m_pMainFrame = this;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_AGToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_AGToolBar.LoadToolBar(IDR_TOOLBAR_AG))
	{
		TRACE0("Failed to create toolbar\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
	}

	if( !m_TreeBar.Create( "ä¯ÀÀ" , this , CSize(250,250) , TRUE , 0 ) )
	{
		TRACE0("Failed to create Tree bar\n");
		return -1;
	}
	else
	{
		m_TreeDlg.Create( TreeDlg::IDD , &m_TreeBar );
		m_TreeDlg.ShowWindow( SW_SHOW );
		m_TreeBar.SetBarStyle(m_TreeBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
		m_TreeBar.EnableDocking(CBRS_ALIGN_ANY);
	}
	if( !m_MsgBar.Create( "ÏûÏ¢" , this , CSize(250,200) , TRUE , 0 ) )
	{
		TRACE0("Failed to create msg bar\n");
		return -1;
	}
	else
	{
		m_MsgTab.Create( WS_CHILD | WS_VISIBLE , CRect( 0 , 0 , 0 , 0 ), &m_MsgBar , 0);
		m_MsgLog.Create( MsgLogDlg::IDD , &m_MsgTab );
		m_MsgLog.ShowWindow( SW_SHOW );
		m_MsgTab.AddPage ( "Log" , &m_MsgLog  );
		m_MsgBar.SetBarStyle(m_MsgBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
		m_MsgBar.EnableDocking(CBRS_ALIGN_ANY);
	}
	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);
	DockControlBar(&m_TreeBar,AFX_IDW_DOCKBAR_LEFT);
	DockControlBar(&m_MsgBar,AFX_IDW_DOCKBAR_BOTTOM);

	return 0;
}

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


void CMainFrame::AppendLog(CString str)
{
	m_MsgLog.AppendLog( str );
}

void CMainFrame::ClearLog()
{
	m_MsgLog.ClearLog();
}

void CMainFrame::OnFileSave() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(false ,"xml","*.xml",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"XML File(*.xml)|*.xml||");
	if( dlg.DoModal() == IDOK )
	{
		ShapeNamespace *pSN = (ShapeNamespace*)m_pRootShape;
		CFile file;
		if( file.Open( dlg.GetPathName() , CFile::modeCreate | CFile::modeWrite ) )
		{
			int tabnum = 0;
			pSN->Save( file , tabnum );
			file.Close();
		}
	}
}
