// UnsDisView.cpp : implementation of the CUnsDisView class
//

#include "stdafx.h"
#include "afxpriv.h"    // For WM_SETMESSAGESTRING
#include "UnsDis.h"

#include "UnsDisDoc.h"
#include "zoomview.h"
#include "UnsDisView.h"
#include "chooselayer.h"
#include "meas.h"
#include "timev.h"

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

/////////////////////////////////////////////////////////////////////////////
// CUnsDisView

IMPLEMENT_DYNCREATE(CUnsDisView, CZoomView)

BEGIN_MESSAGE_MAP(CUnsDisView, CZoomView)
	//{{AFX_MSG_MAP(CUnsDisView)
	ON_COMMAND(ID_LAY_ADD, OnLayAdd)
	ON_COMMAND(ID_LAY_ED, OnLayEd)
	ON_COMMAND(ID_GEN_ELEM, OnGenElem)
	ON_COMMAND(ID_ZOOM_100, OnZoom100)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_100, OnUpdateZoom100)
	ON_COMMAND(ID_ZOOM_200, OnZoom200)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_200, OnUpdateZoom200)
	ON_COMMAND(ID_ZOOM_50, OnZoom50)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_50, OnUpdateZoom50)
	ON_COMMAND(ID_ZOOM_75, OnZoom75)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_75, OnUpdateZoom75)
	ON_COMMAND(ID_INIT_OPEN, OnInitOpen)
	ON_COMMAND(ID_BOUN_CON, OnBounCon)
	ON_COMMAND(ID_SOLVE, OnSolve)
	ON_COMMAND(ID_ZOOM_FULL, OnZoomFull)
	ON_COMMAND(ID_ZOOM_IN, OnZoomIn)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_IN, OnUpdateZoomIn)
	ON_COMMAND(ID_ZOOM_OUT, OnZoomOut)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_OUT, OnUpdateZoomOut)
	ON_COMMAND(ID_FORMAT_AXES, OnFormatAxes)
	ON_COMMAND(ID_FORM_MEAS, OnFormMeas)
	ON_COMMAND(ID_LAY_DEL, OnLayDel)
	ON_COMMAND(ID_FORMAT_FONT, OnFormatFont)
	ON_COMMAND(ID_VIEW_TIME, OnViewTime)
	ON_COMMAND(ID_VIEW_GRAPH, OnViewGraph)
	ON_UPDATE_COMMAND_UI(ID_VIEW_GRAPH, OnUpdateViewGraph)
	ON_COMMAND(ID_VIEW_TAB, OnViewTab)
	ON_UPDATE_COMMAND_UI(ID_VIEW_TAB, OnUpdateViewTab)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CZoomView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CZoomView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CZoomView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUnsDisView construction/destruction

CUnsDisView::CUnsDisView()
{
	mul=div=100;istart=iend=-1;
	IsAxes=FALSE;IsGraph=TRUE;
}

CUnsDisView::~CUnsDisView()
{
}

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

	return CZoomView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CUnsDisView drawing

void CUnsDisView::OnDraw(CDC* pDC)
{
	CUnsDisDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	int tokl,tokp,tokh,tokd;
	CSize sz=pDoc->GetDocSize();
	tokl=(int)(pDoc->lemarg*10);tokp=(int)(pDoc->rimarg*10);
	tokh=(int)(pDoc->topmarg*10);tokd=(int)(pDoc->botmarg*10);
	rokr.left=tokl;rokr.right=sz.cx-tokp;
	rokr.top=tokh;rokr.bottom=sz.cy-tokd;
	pDC->Rectangle(0,0,sz.cx,sz.cy);
	if (IsGraph)
	{
		pDoc->PlotLay(*pDC,rokr);
		pDoc->PlotElems(*pDC,rokr);
		if (IsAxes) pDoc->PlotAxes(*pDC,rokr);
		if (istart>=0 && iend>=istart) pDoc->PlotTime(*pDC,rokr,istart,iend);
	}
	else
	{
		if (istart>=0 && iend>=istart) pDoc->PrintRes(*pDC,rokr,istart,iend);
	}
}

void CUnsDisView::OnInitialUpdate()
{
	CZoomView::OnInitialUpdate();
	CUnsDisDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	SetZoomSizes(pDoc->GetDocSize());
	DoZoomFull();
}

/////////////////////////////////////////////////////////////////////////////
// CUnsDisView printing

void CUnsDisView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
	CUnsDisDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	int tokl,tokp,tokh,tokd;
	CSize sz=pDoc->GetDocSize();
	tokl=(int)(pDoc->lemarg*10);tokp=(int)(pDoc->rimarg*10);
	tokh=(int)(pDoc->topmarg*10);tokd=(int)(pDoc->botmarg*10);
	rokr.left=tokl;rokr.right=sz.cx-tokp;
	rokr.top=tokh;rokr.bottom=sz.cy-tokd;

	if (IsGraph)
	{
		pDoc->PlotLay(*pDC,rokr);
		pDoc->PlotElems(*pDC,rokr);
		if (IsAxes) pDoc->PlotAxes(*pDC,rokr);
		if (istart>=0 && iend>=istart) pDoc->PlotTime(*pDC,rokr,istart,iend);
	}
	else
	{
		if (istart>=0 && iend>=istart) pDoc->PrintRes(*pDC,rokr,istart,iend);
	}
}

BOOL CUnsDisView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CUnsDisView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CUnsDisView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CUnsDisView diagnostics

#ifdef _DEBUG
void CUnsDisView::AssertValid() const
{
	CZoomView::AssertValid();
}

void CUnsDisView::Dump(CDumpContext& dc) const
{
	CZoomView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CUnsDisView message handlers

void CUnsDisView::OnLayAdd() 
{
	CUnsDisDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->AddLayer();
	Invalidate();
}

void CUnsDisView::OnLayEd() 
{
	CUnsDisDoc* pDoc = GetDocument();
	int nlay;
	CChooseLayer clr;
	clr.pDoc=pDoc;
	if (clr.DoModal()==IDOK)
	{
		nlay=clr.nchos;
		if (nlay>=0)
		{
			pDoc->EditLayer(nlay);
			Invalidate();
		}
	}
}

void CUnsDisView::OnGenElem() 
{
	CUnsDisDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->Generate();
	Invalidate();
}

void CUnsDisView::OnZoom100() 
{
	mul=100;Invalidate();	
}

void CUnsDisView::OnUpdateZoom100(CCmdUI* pCmdUI) 
{
	if (mul==100) pCmdUI->SetCheck(1);
	else pCmdUI->SetCheck(0);
}

void CUnsDisView::OnZoom200() 
{
	mul=200;Invalidate();	
}

void CUnsDisView::OnUpdateZoom200(CCmdUI* pCmdUI) 
{
	if (mul==200) pCmdUI->SetCheck(1);
	else pCmdUI->SetCheck(0);
}

void CUnsDisView::OnZoom50() 
{
	mul=50;Invalidate();	
}

void CUnsDisView::OnUpdateZoom50(CCmdUI* pCmdUI) 
{
	if (mul==50) pCmdUI->SetCheck(1);
	else pCmdUI->SetCheck(0);
}

void CUnsDisView::OnZoom75() 
{
	mul=75;Invalidate();	
}

void CUnsDisView::OnUpdateZoom75(CCmdUI* pCmdUI) 
{
	if (mul==75) pCmdUI->SetCheck(1);
	else pCmdUI->SetCheck(0);
}

void CUnsDisView::OnInitOpen() 
{
	CUnsDisDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->OpenInitCon();
	Invalidate();
}

void CUnsDisView::OnBounCon() 
{
	CUnsDisDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->EditBC();
	Invalidate();
}

void CUnsDisView::OnSolve() 
{
	CUnsDisDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CClientDC dc(this);
	OnPrepareDC (&dc);
	pDoc->PlotLay(dc,rokr);
	pDoc->PlotElems(dc,rokr);
	IsAxes=TRUE;
	pDoc->PlotAxes(dc,rokr);
	pDoc->Solve(dc,rokr);
	istart=0;iend=pDoc->TimeInt.GetSize();
}

void CUnsDisView::OnZoomFull() 
{
	DoZoomFull();	
}

void CUnsDisView::OnZoomIn() 
{
	// Toggle zoomin mode
	CWnd *pWin = ((CWinApp *) AfxGetApp())->m_pMainWnd;
	if (GetZoomMode() == MODE_ZOOMIN) {
		SetZoomMode(MODE_ZOOMOFF);
		// Clear the statusbar
		pWin->SendMessage(WM_SETMESSAGESTRING, 0, 
			(LPARAM)(LPCSTR)"");
	} else {
		SetZoomMode(MODE_ZOOMIN);
		// Give instructions in the statusbar
		pWin->SendMessage(WM_SETMESSAGESTRING, 0, 
			(LPARAM)(LPCSTR)"Click to zoom in on point or drag a zoom box.");
	}
}

void CUnsDisView::OnUpdateZoomIn(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(GetZoomMode() == MODE_ZOOMIN);
}

void CUnsDisView::OnZoomOut() 
{
	CWnd *pWin = ((CWinApp *) AfxGetApp())->m_pMainWnd;
	if (GetZoomMode() == MODE_ZOOMOUT) {
		SetZoomMode(MODE_ZOOMOFF);
		// Clear the statusbar
		pWin->SendMessage(WM_SETMESSAGESTRING, 0, 
			(LPARAM)(LPCSTR)"");
	} else {
		SetZoomMode(MODE_ZOOMOUT);
		// Give instructions in the statusbar
		pWin->SendMessage(WM_SETMESSAGESTRING, 0, 
			(LPARAM)(LPCSTR)"Click to zoom out on point.");
	}
}

void CUnsDisView::OnUpdateZoomOut(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(GetZoomMode() == MODE_ZOOMOUT);
}

void CUnsDisView::OnFormatAxes() 
{
	CUnsDisDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->FormAxes();
	IsAxes=TRUE;
	Invalidate();
}

void CUnsDisView::OnFormMeas() 
{
	CUnsDisDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CMeas ms;
	ms.m_meas=pDoc->meas;
	if (ms.DoModal()==IDOK)
	{
		pDoc->meas=ms.m_meas;
		Invalidate();
	}
}

void CUnsDisView::OnLayDel() 
{
	CUnsDisDoc* pDoc = GetDocument();
	int nlay;
	CChooseLayer clr;
	clr.pDoc=pDoc;
	if (clr.DoModal()==IDOK)
	{
		nlay=clr.nchos;
		if (nlay>=0)
		{
			CString msg;
			msg.Format("Do you really want to delete %d. layer?",nlay+1);
			int i=AfxMessageBox(msg,MB_YESNO);
			if (i==IDYES)
			{
				pDoc->DelLayer(nlay);
				Invalidate();
			}
		}
	}
}

void CUnsDisView::OnFormatFont() 
{
	CUnsDisDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->Font();
	Invalidate();
}

void CUnsDisView::OnViewTime() 
{
	CTimeV tv;
	tv.m_start=istart+1;tv.m_end=iend;
	if (tv.DoModal()==IDOK)
	{
		istart=tv.m_start-1;iend=tv.m_end;
		Invalidate();
	}
}

void CUnsDisView::OnViewGraph() 
{
	IsGraph=TRUE;
	Invalidate();
}

void CUnsDisView::OnUpdateViewGraph(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(IsGraph);	
}

void CUnsDisView::OnViewTab() 
{
	IsGraph=FALSE;
	Invalidate();
}

void CUnsDisView::OnUpdateViewTab(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(!IsGraph);	
}
