// BemIsolView.cpp : implementation of the CBemIsolView class
//

#include "stdafx.h"
#include "afxpriv.h"
#include "BemIsol.h"

#include "BemIsolDoc.h"
#include "ZoomView.h"
#include "BemIsolView.h"

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

#define LABEL 1
/////////////////////////////////////////////////////////////////////////////
// CBemIsolView

IMPLEMENT_DYNCREATE(CBemIsolView, CZoomView)

BEGIN_MESSAGE_MAP(CBemIsolView, CZoomView)
	//{{AFX_MSG_MAP(CBemIsolView)
	ON_COMMAND(ID_NET_DEN, OnNetDen)
	ON_UPDATE_COMMAND_UI(ID_NET_DEN, OnUpdateNetDen)
	ON_COMMAND(ID_NET_NORM, OnNetNorm)
	ON_UPDATE_COMMAND_UI(ID_NET_NORM, OnUpdateNetNorm)
	ON_COMMAND(ID_NET_SC, OnNetSc)
	ON_UPDATE_COMMAND_UI(ID_NET_SC, OnUpdateNetSc)
	ON_COMMAND(ID_ZOOM_IN, OnZoomIn)
	ON_COMMAND(ID_ZOOM_OUT, OnZoomOut)
	ON_COMMAND(ID_ZOOM_FULL, OnZoomFull)
	ON_COMMAND(ID_INT_REG, OnIntReg)
	ON_COMMAND(ID_INT_NON, OnIntNon)
	ON_COMMAND(ID_ISOL_OPEN, OnIsolOpen)
	ON_COMMAND(ID_LABEL, OnLabel)
	ON_UPDATE_COMMAND_UI(ID_LABEL, OnUpdateLabel)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_FORMAT_FONT, OnFormatFont)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CBemIsolView construction/destruction

CBemIsolView::CBemIsolView()
{
	LBut=net=0;
}

CBemIsolView::~CBemIsolView()
{
}

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

	return CZoomView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CBemIsolView drawing

void CBemIsolView::OnDraw(CDC* pDC)
{
	CBemIsolDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->PlotNod (pDC,3,FALSE);
	pDoc->PlotElem (pDC,FALSE);
	pDoc->PlotIntPt (pDC,3,FALSE);
	pDoc->PlotWells (pDC,3);
	pDoc->PlotLines (pDC);
}

void CBemIsolView::OnInitialUpdate()
{
	CZoomView::OnInitialUpdate();
	CZoomView::OnInitialUpdate();
	CBemIsolDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CRect rc;
	GetClientRect (&rc);
	pDoc->ScaleNods(rc);
	SetZoomSizes(pDoc->GetDocSize());
}

/////////////////////////////////////////////////////////////////////////////
// CBemIsolView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CBemIsolView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CBemIsolView message handlers

void CBemIsolView::OnNetDen() 
{
	CBemIsolDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);net=50;
	pDoc->GenerNet(50,50);
	CClientDC dc(this);
	OnPrepareDC(&dc);
	pDoc->Potenc(dc);
}

void CBemIsolView::OnUpdateNetDen(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(net==50);	
}

void CBemIsolView::OnNetNorm() 
{
	CBemIsolDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);net=40;
	pDoc->GenerNet(40,40);
	CClientDC dc(this);
	OnPrepareDC(&dc);
	pDoc->Potenc(dc);
}

void CBemIsolView::OnUpdateNetNorm(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(net==40);	
}

void CBemIsolView::OnNetSc() 
{
	CBemIsolDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);net=30;
	pDoc->GenerNet(30,30);
	CClientDC dc(this);
	OnPrepareDC(&dc);
	pDoc->Potenc(dc);
}

void CBemIsolView::OnUpdateNetSc(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(net==30);	
}

void CBemIsolView::OnZoomIn() 
{
	// Toggle zoomin mode
	LBut=0;
	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 CBemIsolView::OnZoomOut() 
{
	// Toggle zoomout mode
	LBut=0;
	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 CBemIsolView::OnZoomFull() 
{
	DoZoomFull();	
}

void CBemIsolView::OnIntReg() 
{
	CBemIsolDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CClientDC dc(this);
	OnPrepareDC (&dc);
	if (pDoc->InterIsoReg())
	{
		pDoc->InterIso(dc);
		Invalidate();
	}
}

void CBemIsolView::OnIntNon() 
{
	CBemIsolDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CClientDC dc(this);
	OnPrepareDC (&dc);
	if (pDoc->InterIsoNon())
	{
		pDoc->InterIso(dc);
		Invalidate();
	}
}

void CBemIsolView::OnIsolOpen() 
{
	CBemIsolDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->OpenIsol();
	Invalidate();
}

void CBemIsolView::OnLabel() 
{
	if (LBut==LABEL) LBut=0;
	else LBut=LABEL;
}

void CBemIsolView::OnUpdateLabel(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(LBut==LABEL);	
}

void CBemIsolView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CZoomView::OnLButtonDown(nFlags, point);
	CBemIsolDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	ViewDPtoLP (&point,1);
	CClientDC dc(this);
	OnPrepareDC (&dc);
	int il;
	switch (LBut)
	{
	case LABEL:
		il=pDoc->LabLine(point);
		if (il>=0) pDoc->PlotLine(&dc,il);
		break;
	}
	Invalidate();
}

void CBemIsolView::OnFormatFont() 
{
	CBemIsolDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->ChangeFont();
	Invalidate();
}
