// FrmWnd.cpp : implementation file
//

#include "stdafx.h"
#include "vplot.h"
#include "FrmWnd.h"
#include "imp_view.h"
#include "Viewer.h"

// CFrmWnd

IMPLEMENT_DYNCREATE(CFrmWnd, CFrameWnd)

BEGIN_MESSAGE_MAP(CFrmWnd, CFrameWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_UPDATE_COMMAND_UI(ID_REFRESH_NOW, &CFrmWnd::OnUpdateRefreshNow)
	ON_COMMAND_RANGE(0x9000, 0x90ff, CFrmWnd::OnCommandUser)
	ON_COMMAND(ID_LAYER_NEXT, &CFrmWnd::OnLayerNext)
	ON_UPDATE_COMMAND_UI(ID_LAYER_NEXT, &CFrmWnd::OnUpdateLayerNext)
	ON_COMMAND(ID_LAYER_PREVIOUS, &CFrmWnd::OnLayerPrevious)
	ON_UPDATE_COMMAND_UI(ID_LAYER_PREVIOUS, &CFrmWnd::OnUpdateLayerPrevious)
	ON_COMMAND(ID_REFRESH_NOW, &CFrmWnd::OnRefreshNow)
	ON_COMMAND(ID_REFRESH_ONAUTOTRIGGER, &CFrmWnd::OnRefreshOnautotrigger)
//	ON_UPDATE_COMMAND_UI(ID_REFRESH_ONAUTOTRIGGER, &CFrmWnd::OnUpdateRefreshOnautotrigger)
	ON_COMMAND(ID_REFRESH_ONTRIGGER, &CFrmWnd::OnRefreshOntrigger)
//	ON_UPDATE_COMMAND_UI(ID_REFRESH_ONTRIGGER, &CFrmWnd::OnUpdateRefreshOntrigger)
END_MESSAGE_MAP()

CFrmWnd::CFrmWnd() : /*m_pWndDrawArea((CImpView*)CImpView::CreateObject()), */m_pViewerPtr(NULL)
{
	m_dwOldLayerCount = 0;
	m_pWndDrawArea = new CImpView(this);
	m_dwLayer = 0;
	m_pData = NULL;
	m_bUpdateTrigger = TRUE;
	m_bUpdateAutoTrigger = TRUE;
}

CFrmWnd::~CFrmWnd()
{
	delete ((CImpView*)m_pWndDrawArea);
}

void CFrmWnd::PostNcDestroy()
{
	// TODO: Add your specialized code here and/or call the base class
	m_pViewerPtr->m_pWndMain->SendMessage(WM_VIEWERCLOSED, (WPARAM)m_pViewerPtr->m_pDocument, (LPARAM)m_pViewerPtr);
	CWnd::PostNcDestroy();
	//CFrameWnd::PostNcDestroy();
}

int CFrmWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	m_pWndDrawArea->Create(AfxRegisterWndClass(0), NULL, WS_CHILD, CRect(), this, 0);
	m_pWndDrawArea->ShowWindow(SW_SHOW);

	OnCommandUser(0x9000-1);

	m_hAccel = ::LoadAccelerators(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));

	CMenu *pMenu = GetMenu();
	pMenu->CheckMenuItem(ID_REFRESH_ONAUTOTRIGGER, MF_BYCOMMAND | (m_bUpdateAutoTrigger ? MF_CHECKED : MF_UNCHECKED));
	pMenu->CheckMenuItem(ID_REFRESH_ONTRIGGER, MF_BYCOMMAND | (m_bUpdateTrigger ? MF_CHECKED : MF_UNCHECKED));


	return 0;
}

void CFrmWnd::OnSize(UINT nType, int cx, int cy)
{
	CFrameWnd::OnSize(nType, cx, cy);
	m_pWndDrawArea->SetWindowPos(NULL, 0, 0, cx, cy, 0);
	// TODO: Add your message handler code here
}

void CFrmWnd::Update(DWORD dwType)
{
	switch ( dwType )
	{
	case REFRESHTYPE_TRIGGER:
		if ( m_bUpdateTrigger )
		{
			m_pWndDrawArea->UpdateScrollBars();
			m_pWndDrawArea->Invalidate();
		}
		break;
	case REFRESHTYPE_AUTOTRIGGER:
		if ( m_bUpdateAutoTrigger )
		{
			m_pWndDrawArea->UpdateScrollBars();
			m_pWndDrawArea->Invalidate();
		}
		break;
	default:	// vynucene
		m_pWndDrawArea->UpdateScrollBars();
		m_pWndDrawArea->Invalidate();		
		break;
	}

}

void CFrmWnd::UpdateMenu()
{
	DWORD dwLayerCount = 0;
	if ( m_pData )
	{
		dwLayerCount = m_pData->dwLayers;
	}
	CMenu* pMenu = (CMenu*)GetMenu()->GetSubMenu(0);

	for ( DWORD i = m_dwOldLayerCount; i < dwLayerCount; i++ )
	{
		CString str;
		str.Format(L"Layer %d", i);
		pMenu->AppendMenu(MF_STRING, 0x9000 + i, str);
	}
	for ( DWORD i = dwLayerCount; i < m_dwOldLayerCount; i++ )
	{
		pMenu->RemoveMenu(0x9000 + i, MF_BYCOMMAND);
	}
	if ( !(m_dwOldLayerCount <= dwLayerCount) )
	{
		OnCommandUser(0x9000 + dwLayerCount - 1);
	}
	else if ( m_dwOldLayerCount == 0 && m_dwOldLayerCount != dwLayerCount )
	{
		OnCommandUser(0x9000 + m_dwLayer);
	}
	m_dwOldLayerCount = dwLayerCount;
}

void CFrmWnd::OnCommandUser(UINT nId)
{
	CMenu* pMenu = (CMenu*)GetMenu();
	MENUITEMINFO mii;
	ZeroMemory(&mii, sizeof(mii));
	mii.cbSize = sizeof(mii);
	mii.fMask = MIIM_FTYPE | MIIM_STATE;
	mii.fType = MFT_RADIOCHECK | MFT_STRING;
	mii.fState = 0;

	// odznaceni stareho prvku
	pMenu->SetMenuItemInfo(0x9000 + m_dwLayer, &mii);


	if ( nId != 0x9000-1 )
	{

		mii.fState = MFS_CHECKED;
		// oznaceni noveho prvku
		pMenu->SetMenuItemInfo(nId, &mii);

		m_dwLayer = nId - 0x9000;

		CString str;
		str.Format(L"View Plot - Layer %d", nId - 0x9000);
		SetWindowText(str);
	}
	else
	{
		CString str;
		str.Format(L"View Plot - No layers");
		SetWindowText(str);
	}
	Update(0);
}
void CFrmWnd::OnLayerNext()
{
	OnCommandUser(0x9000 + m_dwLayer + 1);
}

void CFrmWnd::OnUpdateLayerNext(CCmdUI *pCmdUI)
{
	pCmdUI->Enable( m_dwLayer+1 < m_dwOldLayerCount );
}

void CFrmWnd::OnLayerPrevious()
{
	OnCommandUser(0x9000 + m_dwLayer - 1);
}

void CFrmWnd::OnUpdateLayerPrevious(CCmdUI *pCmdUI)
{
	pCmdUI->Enable( m_dwLayer > 0 );
}

BOOL CFrmWnd::PreTranslateMessage(MSG* pMsg)
{

	return CFrameWnd::PreTranslateMessage(pMsg);
}

BOOL CFrmWnd::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
	// TODO: Add your specialized code here and/or call the base class
	if ( WM_KEYFIRST <= message &&
		message <= WM_KEYLAST )
	{
		HACCEL hAccel = m_hAccel;
		MSG msg;
		msg.hwnd = m_hWnd;
		msg.lParam = lParam;
		msg.message = message;
		msg.pt.x = 0;
		msg.pt.y = 0;
		msg.time = 0;
		msg.wParam = wParam;
		if ( hAccel && ::TranslateAccelerator(m_hWnd, hAccel, &msg) )
			return TRUE;
	}
	return CFrameWnd::OnWndMsg(message, wParam, lParam, pResult);
}

void CFrmWnd::OnRefreshNow()
{
	Update(0);
}

void CFrmWnd::OnUpdateRefreshNow(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_pData && m_dwLayer < m_pData->dwLayers);
}

void CFrmWnd::OnRefreshOnautotrigger()
{
	m_bUpdateAutoTrigger = !m_bUpdateAutoTrigger;
	CMenu *pMenu = GetMenu();
	pMenu->CheckMenuItem(ID_REFRESH_ONAUTOTRIGGER, MF_BYCOMMAND | (m_bUpdateAutoTrigger ? MF_CHECKED : MF_UNCHECKED));
}

//void CFrmWnd::OnUpdateRefreshOnautotrigger(CCmdUI *pCmdUI)
//{
//	// TODO: Add your command update UI handler code here
//}

void CFrmWnd::OnRefreshOntrigger()
{
	m_bUpdateTrigger = !m_bUpdateTrigger;
	CMenu *pMenu = GetMenu();
	pMenu->CheckMenuItem(ID_REFRESH_ONTRIGGER, MF_BYCOMMAND | (m_bUpdateTrigger ? MF_CHECKED : MF_UNCHECKED));
}

//void CFrmWnd::OnUpdateRefreshOntrigger(CCmdUI *pCmdUI)
//{
//	// TODO: Add your command update UI handler code here
//}
