// BemStreamView.cpp : implementation of the CBemStreamView class
//

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

#include "BemStreamDoc.h"
#include "zoomview.h"
#include "BemStreamView.h"
#include "streamwell.h"

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

#define STR_POINT 1
#define STR_WELL  2

/////////////////////////////////////////////////////////////////////////////
// CBemStreamView

IMPLEMENT_DYNCREATE(CBemStreamView, CZoomView)

BEGIN_MESSAGE_MAP(CBemStreamView, CZoomView)
	//{{AFX_MSG_MAP(CBemStreamView)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_ZOOM_IN, OnZoomIn)
	ON_COMMAND(ID_ZOOM_OUT, OnZoomOut)
	ON_COMMAND(ID_ZOOM_FULL, OnZoomFull)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_IN, OnUpdateZoomIn)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_OUT, OnUpdateZoomOut)
	ON_COMMAND(ID_STR_POINT, OnStrPoint)
	ON_UPDATE_COMMAND_UI(ID_STR_POINT, OnUpdateStrPoint)
	ON_COMMAND(ID_STR_WELL, OnStrWell)
	ON_UPDATE_COMMAND_UI(ID_STR_WELL, OnUpdateStrWell)
	ON_COMMAND(ID_TIME, OnTime)
	ON_COMMAND(ID_VIEW_TIME, OnViewTime)
	ON_UPDATE_COMMAND_UI(ID_VIEW_TIME, OnUpdateViewTime)
	ON_COMMAND(ID_OPEN_STREAM, OnOpenStream)
	ON_COMMAND(ID_VIEW_STAMP, OnViewStamp)
	ON_UPDATE_COMMAND_UI(ID_VIEW_STAMP, OnUpdateViewStamp)
	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()

/////////////////////////////////////////////////////////////////////////////
// CBemStreamView construction/destruction

CBemStreamView::CBemStreamView()
{
	LBut=0;nstw=4;iscz=iscc=0;
}

CBemStreamView::~CBemStreamView()
{
}

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

	return CZoomView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CBemStreamView drawing

void CBemStreamView::OnDraw(CDC* pDC)
{
	CBemStreamDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->PlotNod (pDC,3,0);
	pDoc->PlotElem (pDC,0);
	pDoc->PlotIntPt (pDC,3,0);
	pDoc->PlotWells (pDC,3);
	pDoc->PlotStream (pDC,iscz,iscc,3);
}

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

/////////////////////////////////////////////////////////////////////////////
// CBemStreamView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CBemStreamView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CBemStreamView message handlers

void CBemStreamView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CStream* str;
	CString msg;
	CZoomView::OnLButtonDown(nFlags, point);
	CBemStreamDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	ViewDPtoLP (&point,1);
	CClientDC dc(this);
	OnPrepareDC (&dc);
	switch (LBut)
	{
	case STR_POINT:
		str=pDoc->AddStream (point,-1);
		pDoc->Stream(dc,str,1.0f);
		break;
	case  STR_WELL:
		pDoc->AddStreamWell(dc,point,nstw);
		break;
	}
	Invalidate();
}

void CBemStreamView::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 CBemStreamView::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 CBemStreamView::OnZoomFull() 
{
	DoZoomFull();	
}

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

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

void CBemStreamView::OnStrPoint() 
{
	LBut=STR_POINT;	
}

void CBemStreamView::OnUpdateStrPoint(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(LBut==STR_POINT);	
}

void CBemStreamView::OnStrWell() 
{
	CStreamWell stw;
	stw.m_nostr=nstw;
	if (stw.DoModal()==IDOK)
	{
		LBut=STR_WELL;
		nstw=stw.m_nostr;
	}
}

void CBemStreamView::OnUpdateStrWell(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(LBut==STR_WELL);	
}

void CBemStreamView::OnTime() 
{
	CBemStreamDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->InpStamp();
	iscz=1;
	Invalidate();
}

void CBemStreamView::OnViewTime() 
{
	if (iscc) iscc=0;
	else iscc=1;
	Invalidate();
}

void CBemStreamView::OnUpdateViewTime(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(iscc);	
}

void CBemStreamView::OnOpenStream() 
{
	char szFile[256];
	UINT  cbString;
	int i;
	char  chReplace;    /* string separator for szFilter */
	char  szFilter[256];
	CString namef;
	CFileDialog fd(TRUE);    
	CFile hf;
	float x,y;

    strcpy(szFilter,"Streamlines (*.STR)|*.str|All files (*.*)|*.*|");
    cbString=strlen(szFilter);
	chReplace = szFilter[cbString - 1]; /* retrieve wildcard */
    szFile[0]=0;
    
    for (i = 0; szFilter[i] != '\0'; i++) 
	{
    	if (szFilter[i] == chReplace) szFilter[i] = '\0';
	}

/* Set all structure members to zero. */

	fd.m_ofn.lpstrFilter = szFilter;
	fd.m_ofn.nFilterIndex = 1;
	fd.m_ofn.lpstrFile= szFile;
	fd.m_ofn.nMaxFile = sizeof(szFile);
	fd.m_ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ENABLEHOOK | OFN_EXPLORER;


	if (fd.DoModal()==IDOK) 
	{
		CBemStreamDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);
		namef=fd.GetPathName();
		hf.Open(namef,CFile::typeBinary | CFile::modeRead);
		pDoc->ReadStamps(&hf);
		Invalidate();
		hf.Close();
	}
}

void CBemStreamView::OnViewStamp() 
{
	if (iscz) iscz=0;
	else iscz=1;
	Invalidate();
}

void CBemStreamView::OnUpdateViewStamp(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(iscz);	
}

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