// BemRawView.cpp : implementation of the CBemRawView class
//

#include "stdafx.h"
#include "afxpriv.h"    // For WM_SETMESSAGESTRING
#include "BemRaw.h"
#include "MainFrm.h"

#include "geom.h"
#include "BemRawDoc.h"
#include "ZoomView.h"
#include "BemRawView.h"
#include "addpoint.h"
#include "viewtime.h"

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

#define ADD_CORN  1
#define MOVE_CORN 2
#define DEL_CORN  3
/////////////////////////////////////////////////////////////////////////////
// CBemRawView

IMPLEMENT_DYNCREATE(CBemRawView, CZoomView)

BEGIN_MESSAGE_MAP(CBemRawView, CZoomView)
	//{{AFX_MSG_MAP(CBemRawView)
	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_ZOOM_FULL, OnZoomFull)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_GEOM_AREA_INP, OnGeomAreaInp)
	ON_COMMAND(ID_GEOM_AREA_MOUSE, OnGeomAreaMouse)
	ON_COMMAND(ID_GEOM_AREA_MOVE, OnGeomAreaMove)
	ON_UPDATE_COMMAND_UI(ID_GEOM_AREA_MOVE, OnUpdateGeomAreaMove)
	ON_COMMAND(ID_GEOM_AREA_DEL, OnGeomAreaDel)
	ON_UPDATE_COMMAND_UI(ID_GEOM_AREA_DEL, OnUpdateGeomAreaDel)
	ON_COMMAND(ID_GEOM_AREA_OPEN, OnGeomAreaOpen)
	ON_COMMAND(ID_GEOM_AREA_SAVE, OnGeomAreaSave)
	ON_UPDATE_COMMAND_UI(ID_GEOM_AREA_MOUSE, OnUpdateGeomAreaMouse)
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_GEOM_PART, OnGeomPart)
	ON_COMMAND(ID_TIME, OnTime)
	ON_COMMAND(ID_VIEW_TIME, OnViewTime)
	ON_COMMAND(ID_PART_OPEN, OnPartOpen)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CBemRawView construction/destruction

CBemRawView::CBemRawView()
{
	LBut=0;IsButDown=FALSE;
	ndnum=-1;
	tstart=tend=-1;
}

CBemRawView::~CBemRawView()
{
}

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

	return CZoomView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CBemRawView drawing

void CBemRawView::OnDraw(CDC* pDC)
{
	CBemRawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->PlotNod (pDC,3,0);
	pDoc->PlotElem (pDC,0);
	pDoc->PlotIntPt (pDC,3,0);
	pDoc->PlotWells (pDC,3);
	pDoc->PlotArea(*pDC);
	pDoc->PlotTime(*pDC,tstart,tend);
}

void CBemRawView::OnInitialUpdate()
{
	CZoomView::OnInitialUpdate();
	CBemRawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CRect rc;
	GetClientRect (&rc);
	pDoc->ScaleNodes(rc);
	SetZoomSizes(pDoc->GetDocSize());
}

/////////////////////////////////////////////////////////////////////////////
// CBemRawView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CBemRawView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CBemRawView message handlers

void CBemRawView::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 CBemRawView::OnUpdateZoomIn(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(GetZoomMode() == MODE_ZOOMIN);
}

void CBemRawView::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 CBemRawView::OnUpdateZoomOut(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(GetZoomMode() == MODE_ZOOMOUT);
}

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

void CBemRawView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CBemRawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	int i;
	CZoomView::OnLButtonDown(nFlags, point);
	ViewDPtoLP (&point,1);
	CClientDC dc(this);
	OnPrepareDC (&dc);
	switch (LBut)
	{
	case ADD_CORN:
		pDoc->AddCorner(point);
		pDoc->PlotArea(dc);
		break;
	case MOVE_CORN:
		i=pDoc->FindCorner(point);
		if (i<0)
		{
			AfxMessageBox ("Corner is not found!");
		}
		else
		{
			ndnum=i;IsButDown=TRUE;
		}
		break;
	case DEL_CORN:
		i=pDoc->FindCorner(point);
		if (i<0)
		{
			AfxMessageBox ("Corner is not found!");
		}
		else
		{
			pDoc->DelCorner(i);
		}
		break;
	}
}

void CBemRawView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	CZoomView::OnLButtonUp(nFlags, point);
	switch (LBut)
	{
	case MOVE_CORN:
		IsButDown=FALSE;
		break;
	}
}

void CBemRawView::OnMouseMove(UINT nFlags, CPoint point) 
{
	CZoomView::OnMouseMove(nFlags, point);
	CBemRawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CMainFrame* pMain=(CMainFrame*)AfxGetMainWnd();
	float xb,yb;
	CString msg;
	ASSERT_VALID(pDoc);
	ViewDPtoLP (&point,1);
	switch (LBut)
	{
	case ADD_CORN:
		RevTran (pDoc->gcb,point,xb,yb);
		msg.Format("x=%.3f, y=%.3f",xb,yb);
		pMain->m_wndStatusBar.SetPaneText(0,msg);
		break;
	case MOVE_CORN:
		if (IsButDown && ndnum>=0)
		{
			CNode* nd=pDoc->GetArNode(ndnum);
			nd->xi=point.x;nd->yi=point.y;
			nd->RevTran(pDoc->gcb);
			msg.Format("x=%.3f, y=%.3f",nd->xb,nd->yb);
			pMain->m_wndStatusBar.SetPaneText(0,msg);
			Invalidate();
		}
		break;
	}
}

void CBemRawView::OnGeomAreaInp() 
{
	CAddPoint ap;
	CBemRawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	for (;;)
	{
		if (ap.DoModal()==IDCANCEL) break;
		pDoc->AddCorner(ap.m_x,ap.m_y);
		Invalidate();
	}
}

void CBemRawView::OnGeomAreaMouse() 
{
	LBut=ADD_CORN;	
}

void CBemRawView::OnGeomAreaMove() 
{
	LBut=MOVE_CORN;	
}

void CBemRawView::OnUpdateGeomAreaMove(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(LBut==MOVE_CORN);	
}

void CBemRawView::OnGeomAreaDel() 
{
	LBut=DEL_CORN;	
}

void CBemRawView::OnUpdateGeomAreaDel(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(LBut==DEL_CORN);	
}

void CBemRawView::OnGeomAreaOpen() 
{
	CBemRawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->OpenCorner();
	Invalidate();
}

void CBemRawView::OnGeomAreaSave() 
{
	CBemRawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->SaveCorner();
	Invalidate();
}

void CBemRawView::OnUpdateGeomAreaMouse(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(LBut==ADD_CORN);	
}

void CBemRawView::RevTran(float *gcb, CPoint p, float &xb, float &yb)
{
 	xb=((float)p.x-gcb[0])/gcb[1]+gcb[2];
 	yb=((float)p.y-gcb[3])/gcb[4]+gcb[5];
}

void CBemRawView::OnGeomPart() 
{
	CBemRawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CClientDC dc(this);
	OnPrepareDC (&dc);
	pDoc->GenParts(dc);
	tstart=tend=0;
}

void CBemRawView::OnTime() 
{
	CBemRawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CClientDC dc(this);
	OnPrepareDC (&dc);
	pDoc->TimeInter(dc);
	tstart=0;tend=pDoc->ntim;
}

void CBemRawView::OnViewTime() 
{
	CViewTime vt;
	vt.m_start=tstart;vt.m_end=tend;
	if (vt.DoModal()==IDOK)
	{
		tstart=vt.m_start;tend=vt.m_end;
		Invalidate();
	}
}

void CBemRawView::OnPartOpen() 
{
	CBemRawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->OpenParts();
	if (pDoc->ntim>0)
	{
		tstart=0;tend=pDoc->ntim;
	}
	Invalidate();
}
