// MatlabInterfacingEngineView.cpp : implementation of the CMatlabInterfacingEngineView class
//

#include "stdafx.h"
#include "MatlabInterfacingEngine.h"

#include "MatlabInterfacingEngineDoc.h"
#include "MatlabInterfacingEngineView.h"
#include <BasicClasses\Include\CEditOutputStream.h>
#include <BasicClasses\Include\CStringOut.h>

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

/////////////////////////////////////////////////////////////////////////////
// CMatlabInterfacingEngineView

IMPLEMENT_DYNCREATE(CMatlabInterfacingEngineView, CEditView)

BEGIN_MESSAGE_MAP(CMatlabInterfacingEngineView, CEditView)
	//{{AFX_MSG_MAP(CMatlabInterfacingEngineView)
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	ON_WM_CHAR()
	//}}AFX_MSG_MAP
	//
	ON_MESSAGE(WM_INTERPRET,OnInterpret)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMatlabInterfacingEngineView construction/destruction
//
CMatlabInterfacingEngineView::CMatlabInterfacingEngineView()
{
	previousCtrlDown=false;
}

CMatlabInterfacingEngineView::~CMatlabInterfacingEngineView()
{
}

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

	BOOL bPreCreated = CEditView::PreCreateWindow(cs);
	cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL);	// Enable word-wrapping

	return bPreCreated;
}

/////////////////////////////////////////////////////////////////////////////
// CMatlabInterfacingEngineView drawing

void CMatlabInterfacingEngineView::OnDraw(CDC* pDC)
{
	CMatlabInterfacingEngineDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMatlabInterfacingEngineView diagnostics

#ifdef _DEBUG
void CMatlabInterfacingEngineView::AssertValid() const
{
	CEditView::AssertValid();
}

void CMatlabInterfacingEngineView::Dump(CDumpContext& dc) const
{
	CEditView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CMatlabInterfacingEngineView message handlers

using namespace kaBasicClasses;
using namespace std;

//
void CMatlabInterfacingEngineView::OnInterpret()
{
	CEditOutputStream os(&GetEditCtrl());
	CString statement;
	bool hasPrompt=false;
	{
		CString prompt=promptString();
		CString text;
		GetWindowText(text);
		int pos=0;
		{
			int result;
			while(true)
			{
				result=text.Find(prompt,pos);
				if( result==-1 )
					break;
				else
					hasPrompt=true;
				pos=result+1;
			}
			pos=pos+prompt.GetLength();
		}
		statement=text.Right(text.GetLength()-pos+1);
	}
	if( hasPrompt )
	{
		os.setCursorAtEnd();
		Engine* pEngine=GetDocument()->pEngine;
		if( pEngine==NULL || engEvalString(pEngine,(const char*)statement)!=0 )
		{
			os<<endl<<"Matlab engine is not running."<<endl;
		}
		else
		{
			os<<endl<<GetDocument()->matlabBuffer<<endl;
		}
		os.flush();
		GetDocument()->statementCounter++;
	}
	else
	{
		os<<endl<<"No prompt"<<endl;
		os.flush();
	}
	toPrompt(os);
}

//
void CMatlabInterfacingEngineView::OnInitialUpdate() 
{
	CEditView::OnInitialUpdate();
	CEditOutputStream os(&GetEditCtrl());
	if( GetDocument()->pEngine==NULL )
	{
		os<<"Unable to initialize the Matlab engine"<<endl;
	}
	else
	{
		os<<"Matlab engine was initialized."<<endl<<endl;
		os<<"Press <Ctrl>+<Enter> to evaluate everything between ";
		os<<"the current prompt sign and the end of the editting scope ";
		os<<"as a Matlab language statement."<<endl;
		os<<"If there are several current prompt signs then the last one is considered active."<<endl;
		os<<"Press <Ctrl>+<Enter> to recreate a current prompt sign ";
		os<<"if it was destroyed during editing."<<endl<<endl;
		toPrompt(os);
	}
}

//
void CMatlabInterfacingEngineView::toPrompt(CEditOutputStream& os)
{
	os.flush();
	os.setCursorAtEnd();
	os<<promptString()<<"  ";
	os.setCursorAtEnd();
	os.flush();
}

//
CString CMatlabInterfacingEngineView::promptString()
{
	CString result;
	result.Format("##%d>>",GetDocument()->statementCounter);
	return result;
}

//
void CMatlabInterfacingEngineView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if( previousCtrlDown && nChar==VK_RETURN )
	{
		PostMessage(WM_INTERPRET);
		return;
	}
	if( nChar==VK_CONTROL )
		previousCtrlDown=true;
	else
		previousCtrlDown=false;
	CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
}

//
void CMatlabInterfacingEngineView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if( previousCtrlDown && nChar==VK_RETURN )
	{
		previousCtrlDown=false;
		return;
	}
	previousCtrlDown=false;
	CEditView::OnKeyUp(nChar, nRepCnt, nFlags);
}

//
void CMatlabInterfacingEngineView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if( previousCtrlDown && nChar==10 )
		return;
	
	CEditView::OnChar(nChar, nRepCnt, nFlags);
}


Hosted by www.Geocities.ws

1