/*
*********************************************************************
*
*	The Essence of OLE
*
*	Source code sample
*
*	Copyright (c) 1996, Rolling Thunder Computing, Ipswich MA
*
*	e-mail: essence@rollthunder.com
*	CompuServe: 73540,3430
*	Web: http://www.rollthunder.com
*
*********************************************************************
*/

// scriptView.cpp : implementation of the CScriptView class
//

#include "stdafx.h"
#include "script.h"
#include "scriptsite.h"
#include "scriptDoc.h"
#include "scriptView.h"



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

/////////////////////////////////////////////////////////////////////////////
// CScriptView

IMPLEMENT_DYNCREATE(CScriptView, CEditView)

BEGIN_MESSAGE_MAP(CScriptView, CEditView)
	//{{AFX_MSG_MAP(CScriptView)
	ON_COMMAND(ID_SCRIPT_RUN, OnScriptRun)
	ON_UPDATE_COMMAND_UI(ID_SCRIPT_RUN, OnUpdateScriptRun)
	ON_COMMAND(ID_SCRIPT_STOP, OnScriptStop)
	ON_UPDATE_COMMAND_UI(ID_SCRIPT_STOP, OnUpdateScriptStop)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
END_MESSAGE_MAP()


extern CLSID	g_EngineClsid ;
 

/////////////////////////////////////////////////////////////////////////////
// CScriptView construction/destruction

CScriptView::CScriptView()
{
	m_pActiveScriptEngine = NULL ;
	m_pActiveParse = NULL ;
	m_bIsRunning = FALSE ;
}

CScriptView::~CScriptView()
{
}

BOOL CScriptView::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;
}

/////////////////////////////////////////////////////////////////////////////
// CScriptView drawing

void CScriptView::OnDraw(CDC* pDC)
{
	CScriptDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CScriptView printing

BOOL CScriptView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default CEditView preparation
	return CEditView::OnPreparePrinting(pInfo);
}

void CScriptView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
	// Default CEditView begin printing.
	CEditView::OnBeginPrinting(pDC, pInfo);
}

void CScriptView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
	// Default CEditView end printing
	CEditView::OnEndPrinting(pDC, pInfo);
}

/////////////////////////////////////////////////////////////////////////////
// CScriptView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CScriptView message handlers

void CScriptView::OnScriptRun() 
{
	HRESULT hr;

/*
Create the active script engine to be used for the script in this window.
*/

	hr = CoCreateInstance(
		g_EngineClsid, 
		NULL, 
		CLSCTX_INPROC_SERVER, 
		IID_IActiveScript, 
		(void **)&m_pActiveScriptEngine);

	if (hr != S_OK)
    {
		AfxMessageBox ("Couldn't create script engine object") ;
	    return ;
    }

/*
Tell the IActiveScript about the site it lives in.
*/
	
	hr = m_pActiveScriptEngine->SetScriptSite(&m_ActiveSite);

	if (hr != S_OK)
	{
		AfxMessageBox ("Script engine wouldn't take the site") ;
	}

/*
We need the IActiveScriptParse interface to use the script engine.
*/
	
	hr = m_pActiveScriptEngine->QueryInterface (IID_IActiveScriptParse, (void **)&m_pActiveParse);
  
	if (hr != S_OK)
    {
		AfxMessageBox ("Couldn't get activescriptparse") ;
		return ;
    }

/*
Initialize a new parsing script
*/

	hr = m_pActiveParse->InitNew( ) ;

	if (hr != S_OK)
	{
		AfxMessageBox ("InitNew failed") ;
	}

/*
Feed it our text
*/

	USES_CONVERSION

	char TextBuf [4096] ;

	GetEditCtrl( ).GetWindowText (TextBuf, sizeof(TextBuf)) ;

	hr = m_pActiveParse->ParseScriptText ( 
		A2W(TextBuf),	// text to parse, in wide char
		NULL,		// item name
		NULL,		// debugging context
		NULL,		// end delimiter
		0,			// cookie
		0,			// start line number
		0,			// flags
		NULL, 
		NULL) ;

	if  (hr != S_OK) 
	{
		AfxMessageBox ("ParseScriptText failed") ;
	}
/*
Tell the script that the grid control exists.
*/

	hr = m_pActiveScriptEngine->AddNamedItem (L"Control1", 
		SCRIPTITEM_ISSOURCE | SCRIPTITEM_ISVISIBLE | SCRIPTITEM_GLOBALMEMBERS) ;

	if (hr != S_OK)
	{
		AfxMessageBox ("AddNamedItem failed") ;
	}

/*
Tell script engine to run the script.
*/
	hr = m_pActiveScriptEngine->SetScriptState (SCRIPTSTATE_CONNECTED) ;	

	if (hr != S_OK) 
	{
		AfxMessageBox ("Script wouldn't run") ;
	}
	else
	{
		m_bIsRunning = TRUE ;
	}
}

void CScriptView::OnScriptStop() 
{
	HRESULT hr ;

	hr = m_pActiveScriptEngine->SetScriptState (SCRIPTSTATE_DISCONNECTED) ;	
	hr = m_pActiveScriptEngine->Close ( ) ;
	hr = m_pActiveParse->Release ( ) ;

/*
Close old engine and release old pointer if necessary
*/

	hr = m_pActiveScriptEngine->Release( ) ;
	m_pActiveScriptEngine = NULL ;

	m_bIsRunning = FALSE ;
}

void CScriptView::OnUpdateScriptStop(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable (m_bIsRunning != FALSE) ;	
}
void CScriptView::OnUpdateScriptRun(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable (m_bIsRunning == FALSE) ;
}
