Click here to Skip to main content
15,896,153 members
Articles / Desktop Programming / WTL

ScriptRunner Application

Rate me:
Please Sign up or sign in to vote.
4.97/5 (18 votes)
25 Mar 2007CPOL4 min read 88.6K   1.9K   90  
An Introduction to ScriptRunner. A scripting tool for user interface Unit Testing.
// MainFrm.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_MAINFRM_H__8514104D_FE3D_4BAE_A7F8_D1007E63AF4D__INCLUDED_)
#define AFX_MAINFRM_H__8514104D_FE3D_4BAE_A7F8_D1007E63AF4D__INCLUDED_

#include <atlsplit.h>
#include <atlctrlx.h>
#include "atlctrlxp.h"
#include "atlctrlxp2.h"
#include "aboutdlg.h"
#include "OutputView.h"
#include "ScriptRunnerView.h"
#include "ConfigDialog.h"
#include "WndPlacement.h"

#define REG_KEY_WINDOW		_T("Software\\CodeAsm\\ScriptRunner\\Window")
#define REG_KEY_LASTPOS		_T("Position")
#define SCRIPTRUNNER_FRAME	_T("ScriptRunner_Frame")

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

const LPCTSTR s_lpszScrFilter = _T("Script Files (*.js)\0*.js\0All files (*.*)\0*.*\0");

class CMainFrame : public CFrameWindowImpl<CMainFrame>,
		public CUpdateUI<CMainFrame>,
		public CMessageFilter,
		public CIdleHandler
{
public:
	DECLARE_FRAME_WND_CLASS(SCRIPTRUNNER_FRAME, IDR_MAINFRAME)

	CHorSplitterWindow  m_wndHorzSplit;
	CPaneContainer		m_wndBottomPane;
	CScriptRunnerView	m_view;
	COutputView			m_OutView;
	CMultiPaneStatusBarXPCtrl m_StatusBar;
	CCommandBarXPCtrl	m_CmdBar;

	virtual BOOL PreTranslateMessage(MSG* pMsg)
	{
		if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
			return TRUE;

		return m_view.PreTranslateMessage(pMsg);
	}

	virtual BOOL OnIdle()
	{
		UIEnable(ID_EDIT_UNDO, m_view.CanUndo());
		UIEnable(ID_EDIT_REDO, m_view.CanRedo());
		UIEnable(ID_EDIT_CUT, m_view.CanCut());
		UIEnable(ID_EDIT_COPY, m_view.CanCopy());
		UIEnable(ID_EDIT_PASTE, m_view.CanPaste());
		UIEnable(ID_TOOLS_STARTTRACE, !m_OutView.IsRunning());
		UIEnable(ID_TOOLS_STOPTRACE, m_OutView.IsRunning());
		UIUpdateToolBar();
		return FALSE;
	}

	BEGIN_UPDATE_UI_MAP(CMainFrame)
		UPDATE_ELEMENT(ID_EDIT_UNDO, UPDUI_MENUPOPUP)
		UPDATE_ELEMENT(ID_EDIT_REDO, UPDUI_MENUPOPUP)
		UPDATE_ELEMENT(ID_EDIT_CUT, UPDUI_MENUPOPUP|UPDUI_TOOLBAR)
		UPDATE_ELEMENT(ID_EDIT_COPY, UPDUI_MENUPOPUP|UPDUI_TOOLBAR)
		UPDATE_ELEMENT(ID_EDIT_PASTE, UPDUI_MENUPOPUP|UPDUI_TOOLBAR)
		UPDATE_ELEMENT(ID_TOOLS_STARTTRACE, UPDUI_MENUPOPUP|UPDUI_TOOLBAR)
		UPDATE_ELEMENT(ID_TOOLS_STOPTRACE, UPDUI_MENUPOPUP|UPDUI_TOOLBAR)
		UPDATE_ELEMENT(ID_TOOLS_COMPARE, UPDUI_MENUPOPUP|UPDUI_TOOLBAR)
		UPDATE_ELEMENT(ID_VIEW_TOOLBAR, UPDUI_MENUPOPUP)
		UPDATE_ELEMENT(ID_VIEW_STATUS_BAR, UPDUI_MENUPOPUP)
		UPDATE_ELEMENT(ID_VIEW_OUTPUTBAR, UPDUI_MENUPOPUP)
		UPDATE_ELEMENT(ID_TOOLS_RUNSCRIPT, UPDUI_MENUPOPUP|UPDUI_TOOLBAR)
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP(CMainFrame)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_CLOSE, OnClose)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
		COMMAND_ID_HANDLER(ID_APP_EXIT, OnFileExit)
		COMMAND_ID_HANDLER(ID_FILE_NEW, OnFileNew)
		COMMAND_ID_HANDLER(ID_FILE_OPEN, OnFileOpen)
		COMMAND_ID_HANDLER(ID_FILE_SAVE, OnFileSave)
		COMMAND_ID_HANDLER(ID_FILE_SAVE_AS, OnFileSaveAs)
		COMMAND_ID_HANDLER(ID_FILE_PRINT, OnFilePrint)
		COMMAND_ID_HANDLER(ID_VIEW_TOOLBAR, OnViewToolBar)
		COMMAND_ID_HANDLER(ID_VIEW_STATUS_BAR, OnViewStatusBar)
		COMMAND_ID_HANDLER(ID_VIEW_OUTPUTBAR, OnViewOutputBar)
		COMMAND_ID_HANDLER(ID_TOOLS_STARTTRACE, OnToolsStartTrace)
		COMMAND_ID_HANDLER(ID_TOOLS_STOPTRACE, OnToolsStopTrace)
		COMMAND_ID_HANDLER(ID_TOOLS_CONFIG, OnToolsConfig)
		COMMAND_ID_HANDLER(ID_TOOLS_COMPARE, OnToolsCompare)
		COMMAND_ID_HANDLER(ID_TOOLS_RUNSCRIPT, OnToolsRunScript)
		COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
		NOTIFY_CODE_HANDLER(NMBC_CARETPOSITION, OnEditCaretPosition)
		CHAIN_MSG_MAP_MEMBER(m_view)
		CHAIN_MSG_MAP_MEMBER(m_OutView)
		CHAIN_MSG_MAP(CUpdateUI<CMainFrame>)
		CHAIN_MSG_MAP(CFrameWindowImpl<CMainFrame>)
        REFLECT_NOTIFICATIONS()
	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		SetProp(m_hWnd, SCRIPTRUNNER_FRAME, (HANDLE)this);

		// create command bar window
		HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
		// attach menu
		m_CmdBar.AttachMenu(GetMenu());
		// load command bar images
		m_CmdBar.LoadImages(IDR_MAINFRAME);
		m_CmdBar.AddBitmap(IDB_EDIT_FIND, ID_EDIT_FIND);
		m_CmdBar.AddBitmap(IDB_EDIT_UNDO, ID_EDIT_UNDO);
		m_CmdBar.AddBitmap(IDB_EDIT_REDO, ID_EDIT_REDO);

		// remove old menu
		SetMenu(NULL);

		HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);

		CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
		AddSimpleReBarBand(hWndCmdBar);
		AddSimpleReBarBand(hWndToolBar, NULL, TRUE);

		m_CmdBar.AddToolbar(hWndToolBar);
		m_CmdBar.Prepare();

		// Create StatusBar
		//CreateSimpleStatusBar();
		m_hWndStatusBar = m_StatusBar.Create(m_hWnd);
		int aPanes[] = { ID_DEFAULT_PANE, IDS_LINENUM };
		m_StatusBar.SetPanes(aPanes, 2);
		m_StatusBar.SetPaneWidth(IDS_LINENUM, 150);

		const DWORD dwSplitStyle = WS_CHILD | WS_VISIBLE |
					WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
					dwSplitExStyle = WS_EX_CLIENTEDGE;
 
		m_wndHorzSplit.Create ( *this, rcDefault, NULL,	dwSplitStyle );
 
		m_wndBottomPane.Create( m_wndHorzSplit );
		m_wndBottomPane.SetPaneContainerExtendedStyle(PANECNT_NOCLOSEBUTTON);
		m_wndBottomPane.SetTitle( _T("Output") );

		m_view.Create(m_wndHorzSplit, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE | ES_NOHIDESEL, WS_EX_CLIENTEDGE);
		m_OutView.Create(m_wndBottomPane, rcDefault, NULL, WS_CHILD|WS_VISIBLE|LVS_REPORT);//|LVS_SINGLESEL
		//m_OutView.Create(m_wndBottomPane, rcDefault, NULL, WS_CHILD|WS_VISIBLE|WS_BORDER|TVS_SHOWSELALWAYS|TVS_FULLROWSELECT);
		m_OutView.SetCommandBar( &m_CmdBar );
		m_wndBottomPane.SetClient ( m_OutView );

		// Set the splitter as the client area window, and resize
		// the splitter to match the frame size.
		m_hWndClient = m_wndHorzSplit;
		m_wndHorzSplit.SetSplitterPos ( 400 );
		m_wndHorzSplit.m_cxyMin = 150;
		m_wndHorzSplit.SetSplitterExtendedStyle ( SPLIT_BOTTOMALIGNED );
		m_wndHorzSplit.SetSplitterPanes ( m_view , m_wndBottomPane );
		UpdateLayout();

		UIAddToolBar(hWndToolBar);
		UISetCheck(ID_VIEW_TOOLBAR, 1);
		UISetCheck(ID_VIEW_STATUS_BAR, 1);
		UISetCheck(ID_VIEW_OUTPUTBAR, 1);
		UISetCheck(ID_TOOLS_COMPARE, m_OutView.GetCompareMode());

		// register object for message filtering and idle updates
		CMessageLoop* pLoop = _Module.GetMessageLoop();
		ATLASSERT(pLoop != NULL);
		pLoop->AddMessageFilter(this);
		pLoop->AddIdleHandler(this);

		// Get last position if exists
		CRegKey regKey;
		CWindowPlacement wndPlacement;
		if (ERROR_SUCCESS == regKey.Open(HKEY_CURRENT_USER, REG_KEY_WINDOW))
		{
			if (wndPlacement.GetPosData(regKey, REG_KEY_LASTPOS))
			{
				wndPlacement.showCmd = SW_SHOW;
				wndPlacement.SetPosData( m_hWnd );
			}
			regKey.Close();
		}

		// Create empty document...
		BOOL bHandled = TRUE;
		OnFileNew(0, 0, 0, bHandled);

		return 0;
	}

	LRESULT OnClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
	{
		DestroyWindow();
		bHandled = FALSE;
		return 0;
	}

	LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		// Save last position...
		CRegKey regKey;
		CWindowPlacement wndPlacement( m_hWnd );
		if (ERROR_SUCCESS == regKey.Create(HKEY_CURRENT_USER, REG_KEY_WINDOW))
		{
			wndPlacement.SetPosData(regKey, REG_KEY_LASTPOS);
			regKey.Close();
		}

		RemoveProp(m_hWnd, SCRIPTRUNNER_FRAME);

		// unregister message filtering and idle updates
		CMessageLoop* pLoop = _Module.GetMessageLoop();
		ATLASSERT(pLoop != NULL);
		pLoop->RemoveMessageFilter(this);
		pLoop->RemoveIdleHandler(this);
		PostQuitMessage(0);
		return 0;
	}

	LRESULT OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		PostMessage(WM_CLOSE);
		return 0;
	}

	LRESULT OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		// TODO: add code to initialize document
		/*if (m_view.IsModified())
		{
			if (IDYES == MessageBox(_T("Do you want to save your changes?"), _T("ScriptRunner"), MB_YESNO))
			{
				SendMessage(WM_COMMAND, ID_FILE_SAVE, 0L);
			}
		}
		m_view.SetText(FALSE, TEXT(""));
		m_view.ExecuteCommand(CID_CARET_HOME);
		*/

		m_view.ResetContent();
		m_view.SetFileName( _T("Untitled.js") );

		return 0;
	}

	LRESULT OnFileOpen(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CFileDialog dlg(TRUE, _T("*.js"));
		dlg.m_ofn.lpstrFilter = s_lpszScrFilter;
		if (dlg.DoModal() == IDOK)
			m_view.LoadFile(TRUE, dlg.m_szFileName);

		return 0;
	}

	LRESULT OnFileSave(WORD wNotifyCode, WORD /*wID*/, HWND hWndCtl, BOOL& bHandled)
	{
		FILEINFO finfo = { 0 };
		finfo.dwSize = sizeof(finfo);
		m_view.GetFileInfo(&finfo);
		if ( _tcscmp(_T("Untitled.js"), finfo.lpszFileName) != 0 )
			m_view.SaveFile( finfo.lpszFileName );
		else
			return OnFileSaveAs(wNotifyCode, ID_FILE_SAVE_AS, hWndCtl, bHandled);
		return 0;
	}
	LRESULT OnFileSaveAs(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CFileDialog dlg(FALSE, _T("*.js"));
		dlg.m_ofn.lpstrFilter = s_lpszScrFilter;
		if (dlg.DoModal() == IDOK)
			m_view.SaveFile(dlg.m_szFileName);

		return 0;
	}

	LRESULT OnFilePrint(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		m_view.Print();

		return 0;
	}

	LRESULT OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		static BOOL bVisible = TRUE;	// initially visible
		bVisible = !bVisible;
		CReBarCtrl rebar = m_hWndToolBar;
		int nBandIndex = rebar.IdToIndex(ATL_IDW_BAND_FIRST + 1);	// toolbar is 2nd added band
		rebar.ShowBand(nBandIndex, bVisible);
		UISetCheck(ID_VIEW_TOOLBAR, bVisible);
		UpdateLayout();
		return 0;
	}

	LRESULT OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
		::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
		UISetCheck(ID_VIEW_STATUS_BAR, bVisible);
		UpdateLayout();
		return 0;
	}

	LRESULT OnViewOutputBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		BOOL bVisible = !::IsWindowVisible(m_wndBottomPane);
		if (bVisible)
			m_wndHorzSplit.SetSinglePaneMode(SPLIT_PANE_NONE);
		else
			m_wndHorzSplit.SetSinglePaneMode(SPLIT_PANE_TOP);

		UISetCheck(ID_VIEW_OUTPUTBAR, bVisible);
		UpdateLayout();
		return 0;
	}
	LRESULT OnToolsStartTrace(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		if (!m_OutView.IsRunning())
			m_OutView.StartServer();
		return 0;
	}
	LRESULT OnToolsStopTrace(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		if (m_OutView.IsRunning())
			m_OutView.StopServer();
		return 0;
	}
	LRESULT OnToolsConfig(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CConfigDialog dlg;
		dlg.SetDepth( m_OutView.GetDepth() );
		dlg.SetLevel( m_OutView.GetLevel() );
		CRulesArray		arRules;
		m_OutView.GetRules( arRules );
		dlg.SetRules( arRules );
		if (dlg.DoModal() == IDOK)
		{
			m_OutView.SetDepth( dlg.GetDepth() );
			m_OutView.SetLevel( dlg.GetLevel() );
			dlg.GetRules( arRules );
			m_OutView.SetRules( arRules );
		}
		return 0;
	}

	LRESULT OnToolsCompare(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		BOOL bCompare = m_OutView.GetCompareMode();
		m_OutView.SetCompareMode( !bCompare );
		UISetCheck(ID_TOOLS_COMPARE, !bCompare);
		return 0;
	}
	LRESULT OnToolsRunScript(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		m_OutView.ClearStatus();
		m_view.RunScript();
		return 0;
	}
	LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CAboutDlg dlg;
		dlg.DoModal();
		return 0;
	}

	LRESULT OnEditCaretPosition(int code, LPNMHDR lpNMHDR, BOOL& bHandled)
	{
		LPNMCARETPOSITION lpCaretPos = (LPNMCARETPOSITION) lpNMHDR;
		CString strLine;
		strLine.Format(_T("Line %d, Col %d"), lpCaretPos->ptCaretPos.y, lpCaretPos->ptCaretPos.x );
		m_StatusBar.SetPaneText(IDS_LINENUM, strLine);
		bHandled = TRUE;
		return 0;
	}

};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MAINFRM_H__8514104D_FE3D_4BAE_A7F8_D1007E63AF4D__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Ernest is a multi-discipline software engineer.
Skilled at software design and development for all Windows platforms.
-
MCSD (C#, .NET)
Interests: User Interface, GDI/GDI+, Scripting, Android, iOS, Windows Mobile.
Programming Skills: C/C++, C#, Java (Android), VB and ASP.NET.

I hope you will enjoy my contributions.

Comments and Discussions