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

#include "stdafx.h"
#include "xpeluc01.h"
#include "ViewersDoc.h"
#include "MainFrm.h"
#include "SchemeDoc.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	ON_WM_CREATE()
	ON_COMMAND(ID_VIEW_BLOCKSTOOLBAR, &CMainFrame::OnViewBlockstoolbar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_BLOCKSTOOLBAR, &CMainFrame::OnUpdateViewBlockstoolbar)
	ON_WM_DESTROY()
	ON_MESSAGE(WM_DATAUPDATE, &CMainFrame::OnDataUpdate)
	ON_MESSAGE(WM_VIEWERCLOSED, &CMainFrame::OnViewerClosed)
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
}

CMainFrame::~CMainFrame()
{
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_pDocViewers = new CViewersDoc();
	m_pDocViewers->OnNewDocument();

	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_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_wndBlocksToolBar.Create(CBlocksToolBar::IDD, this) )
	{
		TRACE0("Failed to create blocks bar\n");
		return -1;
	}

	// 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);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::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
{
	CMDIFrameWnd::AssertValid();
}

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

#endif //_DEBUG


// CMainFrame message handlers




void CMainFrame::OnViewBlockstoolbar()
{
	m_wndBlocksToolBar.ShowWindow(!m_wndBlocksToolBar.IsWindowVisible() ? SW_SHOWNA : SW_HIDE);
}

void CMainFrame::OnUpdateViewBlockstoolbar(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_wndBlocksToolBar.IsWindowVisible());
}

void CMainFrame::OnDestroy()
{
	CMDIFrameWnd::OnDestroy();
	delete m_pDocViewers;
	// TODO: Add your message handler code here
}

LPARAM CMainFrame::OnDataUpdate(WPARAM wParam, LPARAM lParam)
{
	CMDIChildWnd *pMdiChild = MDIGetActive();
	CDocument *pDocument = pMdiChild->GetActiveDocument();
	if ( pDocument == (CDocument*)wParam )																// POZOR POZOR
		return ((CSchemeDoc*)wParam)->UpdateViewers();
	return -1;
}

LRESULT CMainFrame::OnViewerClosed(WPARAM wParam, LPARAM lParam)
{
	CSchemeDoc *pDoc = (CSchemeDoc*)wParam;
	return pDoc->DestroyViewer((IViewer*)lParam);
}

BOOL CMainFrame::OnIdle(LONG lCount)
{
	POSITION pos;
	pos = m_olDocList.GetHeadPosition();
	while ( pos )
	{
		CObject* pObj = m_olDocList.GetNext(pos);
		if ( pObj->IsKindOf(RUNTIME_CLASS(CSchemeDoc)) )
		{
			((CSchemeDoc*)pObj)->OnIdleCustom(lCount);
		}
	}
	return TRUE;
}

BOOL CMainFrame::DocumentAdd(CDocument *pDoc)
{
	m_olDocList.AddTail((CObject*)pDoc);
	return TRUE;
}

BOOL CMainFrame::DocumentRemove(CDocument *pDoc)
{
	POSITION pos = m_olDocList.Find((CObject*)pDoc);
	if ( !pos )
		return FALSE;
	m_olDocList.RemoveAt(pos);
	return TRUE;
}