testconns/statdesktop/desktop/source/desktop/src/statdesktopdlg.cpp
changeset 4 b8d1455fddc0
equal deleted inserted replaced
2:73b88125830c 4:b8d1455fddc0
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #include "stdafx.h"
       
    23 #include "STATDesktop.h"
       
    24 #include "STATDesktopDlg.h"
       
    25 #include "STATManageConnection.h"
       
    26 #include "ScriptProgressMonitorImp.h"
       
    27 
       
    28 #include <afxdlgs.h>	//required for CFileDialog
       
    29 
       
    30 #include <process.h>
       
    31 
       
    32 #ifdef _DEBUG
       
    33 #define new DEBUG_NEW
       
    34 #undef THIS_FILE
       
    35 static char THIS_FILE[] = __FILE__;
       
    36 #endif
       
    37 
       
    38 //////////////////////////////////////////////////////////////////////////
       
    39 // our thread-safe mechanism
       
    40 CRITICAL_SECTION CriticalSection;
       
    41 
       
    42 //////////////////////////////////////////////////////////////////////////
       
    43 
       
    44 typedef struct _THREADPROC_DATA
       
    45 {
       
    46 	HWND windowHandle;
       
    47 	int handle;
       
    48 	PROC_RUNSCRIPT ptrRunScript;
       
    49 }
       
    50 	THREADPROC_DATA;
       
    51 
       
    52 ////////////////////////////////////////////////
       
    53 // thread entry point
       
    54 
       
    55 UINT WINAPI ThreadProc(LPVOID lpParameter)
       
    56 {
       
    57 	THREADPROC_DATA *data = reinterpret_cast<THREADPROC_DATA*>(lpParameter);
       
    58 
       
    59 	HWND windowHandle = data->windowHandle;
       
    60 	int handle = data->handle;
       
    61 	PROC_RUNSCRIPT iptrRunScript = data->ptrRunScript;
       
    62 
       
    63 	delete data;
       
    64 	data = NULL;
       
    65 
       
    66 	int	err =	GENERAL_FAILURE;
       
    67 
       
    68 	{
       
    69 		// Place this code in scope braces such that the
       
    70 		// object is cleaned up before the end of the 
       
    71 		// function.  The endthread methods will not call
       
    72 		// destructors.
       
    73 		ScriptProgressMonitorImp scriptMonitor( windowHandle );
       
    74 
       
    75 		// We do not use the error but it is useful to get it for debugging
       
    76 		// purposes.
       
    77 		err =	(iptrRunScript)(handle, &scriptMonitor);
       
    78 	}
       
    79 
       
    80 	::_endthreadex(0);	// Use _endthreadex so as not to close thread handle.
       
    81 
       
    82 	// Never get here.
       
    83 
       
    84 	return 0;
       
    85 }
       
    86 
       
    87 /////////////////////////////////////////////////////////////////////////////
       
    88 // CAboutDlg dialog used for App About
       
    89 
       
    90 class CAboutDlg : public CDialog
       
    91 {
       
    92 public:
       
    93 	CAboutDlg(const char* aVersion);
       
    94 
       
    95 // Dialog Data
       
    96 	//{{AFX_DATA(CAboutDlg)
       
    97 	enum { IDD = IDD_ABOUTBOX };
       
    98 	//}}AFX_DATA
       
    99 
       
   100 	// ClassWizard generated virtual function overrides
       
   101 	//{{AFX_VIRTUAL(CAboutDlg)
       
   102 	protected:
       
   103 	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
       
   104 	//}}AFX_VIRTUAL
       
   105 
       
   106 // Implementation
       
   107 protected:
       
   108 	//{{AFX_MSG(CAboutDlg)
       
   109 	virtual BOOL OnInitDialog();
       
   110 	//}}AFX_MSG
       
   111 	DECLARE_MESSAGE_MAP()
       
   112 
       
   113 private:
       
   114 	CString iVersion;
       
   115 
       
   116 
       
   117 };
       
   118 
       
   119 CAboutDlg::CAboutDlg(const char* aVersion) : CDialog(CAboutDlg::IDD)
       
   120 {
       
   121 	iVersion = CString(aVersion);
       
   122 
       
   123 
       
   124 	//{{AFX_DATA_INIT(CAboutDlg)
       
   125 	//}}AFX_DATA_INIT
       
   126 }
       
   127 
       
   128 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
       
   129 {
       
   130 	CDialog::DoDataExchange(pDX);
       
   131 	//{{AFX_DATA_MAP(CAboutDlg)
       
   132 	//}}AFX_DATA_MAP
       
   133 }
       
   134 
       
   135 CAboutDlg::OnInitDialog()
       
   136 {
       
   137 	CDialog::OnInitDialog();
       
   138 
       
   139 	// set About Box details
       
   140 	TCHAR tHeading[25];
       
   141 	_stprintf(tHeading, _T("STAT Version: %s"), iVersion);
       
   142 
       
   143 	GetDlgItem(IDC_ABOUTHEADING)->SetWindowText(tHeading);
       
   144 
       
   145 	return TRUE;
       
   146 }
       
   147 
       
   148 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
       
   149 	//{{AFX_MSG_MAP(CAboutDlg)
       
   150 		// No message handlers
       
   151 	//}}AFX_MSG_MAP
       
   152 END_MESSAGE_MAP()
       
   153 
       
   154 
       
   155 /////////////////////////////////////////////////////////////////////////////
       
   156 // CSTATDesktopDlg dialog
       
   157 
       
   158 CSTATDesktopDlg::CSTATDesktopDlg(CWnd* pParent /*=NULL*/)
       
   159 : CDialog(CSTATDesktopDlg::IDD, pParent),
       
   160 	ihLib(NULL), iHandle(0), hThreadHandle((HANDLE)0), bRunning(false),
       
   161 	iptrVersion(NULL), iptrGetError(NULL), iptrGetErrorText(NULL), iptrSetConnectionLogging(NULL),
       
   162 	iptrCloseConnectionLogging(NULL), iptrConnect(NULL), iptrDisconnect(NULL),
       
   163 	iptrSetCommandDelay(NULL), iptrSetCommandLogging(NULL),
       
   164 	iptrSetImageVerification(NULL), iptrSendRawCommand(NULL),
       
   165 	iptrOpenScriptFile(NULL), iptrRunScript(NULL),
       
   166 	iptrSendCommandFile(NULL), iptrGetCommandCount(NULL), iptrGetCurrentCommandNumber(NULL),
       
   167 	iptrStopProcessing(NULL),
       
   168 	iptrGetSnapShot(NULL), iMessageReporter(NULL)
       
   169 {
       
   170 	//{{AFX_DATA_INIT(CSTATDesktopDlg)
       
   171 	//}}AFX_DATA_INIT
       
   172 	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
       
   173 }
       
   174 
       
   175 //----------------------------------------------------------------------------
       
   176 
       
   177 void CSTATDesktopDlg::DoDataExchange(CDataExchange* pDX)
       
   178 {
       
   179 	CDialog::DoDataExchange(pDX);
       
   180 	//{{AFX_DATA_MAP(CSTATDesktopDlg)
       
   181 	DDX_Control(pDX, IDC_CONNECTION, m_Connection);
       
   182 	DDX_Control(pDX, IDC_VIEW, m_ViewLog);
       
   183 	DDX_Control(pDX, IDC_PROGRESS, m_Progress);
       
   184 	DDX_Control(pDX, IDC_BROWSELOG, m_BrowseLog);
       
   185 	DDX_Control(pDX, IDC_APPEND, m_Append);
       
   186 	DDX_Control(pDX, IDC_BROWSEIMAGE, m_BrowseImage);
       
   187 	DDX_Control(pDX, IDC_TIMEDELAY, m_Delay);
       
   188 	DDX_Control(pDX, IDC_OUTPUT, m_Output);
       
   189 	DDX_Control(pDX, IDC_IMAGEVERIFY, m_VerifyImage);
       
   190 	DDX_Control(pDX, IDC_EDITLOGNAME, m_LogFileName);
       
   191 	DDX_Control(pDX, IDC_DIFFLOGNAME, m_LogToFile);
       
   192 	DDX_Control(pDX, IDC_IMAGEBOX, m_ImageBox);
       
   193 	DDX_Control(pDX, IDC_SCRIPT, m_Script);
       
   194 	DDX_Control(pDX, IDC_LOCATION, m_ImageLocation);
       
   195 	DDX_Control(pDX, IDC_SPEEDSLIDER, m_SliderPos);
       
   196 	DDX_Control(pDX, IDC_PERCENTAGEDIFF, m_PercentageDiff);
       
   197 	//}}AFX_DATA_MAP
       
   198 }
       
   199 
       
   200 BEGIN_MESSAGE_MAP(CSTATDesktopDlg, CDialog)
       
   201 	//{{AFX_MSG_MAP(CSTATDesktopDlg)
       
   202 	ON_WM_SYSCOMMAND()
       
   203 	ON_WM_PAINT()
       
   204 	ON_WM_QUERYDRAGICON()
       
   205 	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
       
   206 	ON_BN_CLICKED(IDEDITSCRIPT, OnEditscript)
       
   207 	ON_BN_CLICKED(IDEXIT, OnExit)
       
   208 	ON_BN_CLICKED(IDRUN, OnRun)
       
   209 	ON_BN_CLICKED(IDC_DIFFLOGNAME, OnLogToFile)
       
   210 	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SPEEDSLIDER, OnReleasedcaptureSpeedslider)
       
   211 	ON_EN_CHANGE(IDC_TIMEDELAY, OnChangeTimedelay)
       
   212 	ON_BN_CLICKED(IDC_BROWSEIMAGE, OnBrowseimage)
       
   213 	ON_BN_CLICKED(IDC_IMAGEVERIFY, OnImageverify)
       
   214 	ON_BN_CLICKED(IDC_BROWSELOG, OnBrowseLog)
       
   215 	ON_BN_CLICKED(IDC_VIEW, OnView)
       
   216 	ON_BN_CLICKED(IDC_MANAGE, OnManage)
       
   217 	ON_WM_DESTROY()
       
   218 	ON_MESSAGE(WM_DONE_COMMAND, OnDoneCommand)
       
   219 	ON_MESSAGE(WM_DONE_SCRIPT, OnDoneScript)
       
   220 	ON_MESSAGE(WM_SHOW_LOG_MESSAGE, OnLogMesage)
       
   221 	//}}AFX_MSG_MAP
       
   222 END_MESSAGE_MAP()
       
   223 
       
   224 /////////////////////////////////////////////////////////////////////////////
       
   225 // CSTATDesktopDlg message handlers
       
   226 
       
   227 BOOL CSTATDesktopDlg::OnInitDialog()
       
   228 {
       
   229 	CDialog::OnInitDialog();
       
   230 
       
   231 	// IDM_ABOUTBOX must be in the system command range.
       
   232 	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
       
   233 	ASSERT(IDM_ABOUTBOX < 0xF000);
       
   234 
       
   235 	CMenu* pSysMenu = GetSystemMenu(FALSE);
       
   236 	if (pSysMenu != NULL)
       
   237 	{
       
   238 		CString strAboutMenu;
       
   239 		strAboutMenu.LoadString(IDS_ABOUTBOX);
       
   240 		if (!strAboutMenu.IsEmpty())
       
   241 		{
       
   242 			pSysMenu->AppendMenu(MF_SEPARATOR);
       
   243 			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
       
   244 		}
       
   245 	}
       
   246 
       
   247 	SetIcon(m_hIcon, TRUE);			// Set big icon
       
   248 	SetIcon(m_hIcon, FALSE);		// Set small icon
       
   249 	
       
   250 	// Load the STAT DLL library.
       
   251 	static char	DLLPath[MAX_PATH];
       
   252 
       
   253 #ifdef _DEBUG
       
   254 	// Load the debug DLL from our current working folder.
       
   255 	sprintf( DLLPath, "statd.dll" );
       
   256 #else // _DEBUG
       
   257 	// Load the current build of STAT DLL from the default 
       
   258 	// STAT folder (probably '\epoc32\tools\STAT').
       
   259 	sprintf( DLLPath, "%s\\%s", DLLFolder, DLLName );
       
   260 #endif // _DEBUG
       
   261 
       
   262 	ihLib =	::LoadLibrary( DLLPath );
       
   263 
       
   264 	if( ihLib  == NULL )
       
   265 	{
       
   266 		::AfxMessageBox( "Failed to load dynamic library.", MB_ICONERROR );
       
   267 		PostMessage( WM_CLOSE, 0, 0 );
       
   268 	}
       
   269 	else
       
   270 	{
       
   271 		iptrVersion =	reinterpret_cast<PROC_VERSION>(::GetProcAddress( ihLib, ProcVersion ));
       
   272 		iptrGetError =	reinterpret_cast<PROC_GETERROR>(::GetProcAddress( ihLib, ProcGetError ));
       
   273 		iptrGetErrorText =	reinterpret_cast<PROC_GETERRORTEXT>(::GetProcAddress( ihLib, ProcGetErrorText ));
       
   274 		iptrSetConnectionLogging =	reinterpret_cast<PROC_SETCONNECTIONLOGGING>(::GetProcAddress( ihLib, ProcSetConnectionLogging ));
       
   275 		iptrCloseConnectionLogging =	reinterpret_cast<PROC_CLOSECONNECTIONLOGGING>(::GetProcAddress( ihLib, ProcCloseConnectionLogging ));
       
   276 		iptrConnect =	reinterpret_cast<PROC_CONNECT>(::GetProcAddress( ihLib, ProcConnect ));
       
   277 		iptrDisconnect =	reinterpret_cast<PROC_DISCONNECT>(::GetProcAddress( ihLib, ProcDisconnect ));
       
   278 		iptrSetCommandDelay =	reinterpret_cast<PROC_SETCOMMANDDELAY>(::GetProcAddress( ihLib, ProcSetCommandDelay ));
       
   279 		iptrSetCommandLogging =	reinterpret_cast<PROC_SETCOMMANDLOGGING>(::GetProcAddress( ihLib, ProcSetCommandLogging ));
       
   280 		iptrSetImageVerification =	reinterpret_cast<PROC_SETIMAGEVERIFICATION>(::GetProcAddress( ihLib, ProcSetImageVerification ));
       
   281 		iptrOpenScriptFile =	reinterpret_cast<PROC_OPENSCRIPTFILE>(::GetProcAddress( ihLib, ProcOpenScriptFile ));
       
   282 		iptrRunScript =	reinterpret_cast<PROC_RUNSCRIPT>(::GetProcAddress( ihLib, ProcRunScript ));
       
   283 		iptrSendRawCommand =	reinterpret_cast<PROC_SENDRAWCOMMAND>(::GetProcAddress( ihLib, ProcSendRawCommand ));
       
   284 		iptrSendCommandFile =	reinterpret_cast<PROC_SENDCOMMANDFILE>(::GetProcAddress( ihLib, ProcSendCommandFile ));
       
   285 		iptrGetCommandCount =	reinterpret_cast<PROC_GETCOMMANDCOUNT>(::GetProcAddress( ihLib, ProcGetCommandCount ));
       
   286 		iptrGetCurrentCommandNumber =	reinterpret_cast<PROC_GETCURRENTCOMMANDNUMBER>(::GetProcAddress( ihLib, ProcGetCurrentCommandNumber ));
       
   287 		iptrStopProcessing =	reinterpret_cast<PROC_STOPPROCESSING>(::GetProcAddress( ihLib, ProcStopProcessing ));
       
   288 		iptrGetSnapShot =	reinterpret_cast<PROC_GETSNAPSHOT>(::GetProcAddress( ihLib, ProcGetSnapShot ));
       
   289 
       
   290 		if( ( NULL == iptrVersion ) ||
       
   291 			( NULL == iptrGetError ) ||
       
   292 			( NULL == iptrGetErrorText ) ||
       
   293 			( NULL == iptrSetConnectionLogging ) ||
       
   294 			( NULL == iptrCloseConnectionLogging ) ||
       
   295 			( NULL == iptrConnect ) ||
       
   296 			( NULL == iptrDisconnect ) ||
       
   297 			( NULL == iptrSetCommandDelay ) ||
       
   298 			( NULL == iptrSetCommandLogging ) ||
       
   299 			( NULL == iptrSetImageVerification ) ||
       
   300 			( NULL == iptrOpenScriptFile ) ||
       
   301 			( NULL == iptrRunScript ) ||
       
   302 			( NULL == iptrSendRawCommand ) ||
       
   303 			( NULL == iptrSendCommandFile ) ||
       
   304 			( NULL == iptrGetCommandCount ) ||
       
   305 			( NULL == iptrGetCurrentCommandNumber ) ||
       
   306 			( NULL == iptrStopProcessing ) )
       
   307 		{
       
   308 			::AfxMessageBox( "Failed to load a function address.", MB_ICONERROR );
       
   309 			PostMessage( WM_CLOSE, 0, 0 );
       
   310 		}
       
   311 	}
       
   312 
       
   313 	iMessageReporter =	new MessageReporterImp( GetSafeHwnd() );
       
   314 
       
   315 	iLastVerify = 0;
       
   316 	iLastLog = 0;
       
   317 	statIniFile.SetIniFileName(STAT_INI_NAME);
       
   318 	// read in previous settings
       
   319 	ReadSettings();
       
   320 
       
   321 	InitializeCriticalSection(&CriticalSection);
       
   322 
       
   323 	return TRUE;  // return TRUE  unless you set the focus to a control
       
   324 }
       
   325 
       
   326 //----------------------------------------------------------------------------
       
   327 void CSTATDesktopDlg::OnSysCommand(UINT nID, LPARAM lParam)
       
   328 {
       
   329 	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
       
   330 	{
       
   331 		CAboutDlg dlgAbout(iptrVersion());
       
   332 		dlgAbout.DoModal();
       
   333 	}
       
   334 	else
       
   335 		CDialog::OnSysCommand(nID, lParam);
       
   336 }
       
   337 
       
   338 //----------------------------------------------------------------------------
       
   339 
       
   340 // If you add a minimize button to your dialog, you will need the code below
       
   341 //  to draw the icon.  For MFC applications using the document/view model,
       
   342 //  this is automatically done for you by the framework.
       
   343 
       
   344 void CSTATDesktopDlg::OnPaint() 
       
   345 {
       
   346 	if (IsIconic())
       
   347 	{
       
   348 		CPaintDC dc(this); // device context for painting
       
   349 
       
   350 		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
       
   351 
       
   352 		// Center icon in client rectangle
       
   353 		int cxIcon = GetSystemMetrics(SM_CXICON);
       
   354 		int cyIcon = GetSystemMetrics(SM_CYICON);
       
   355 		CRect rect;
       
   356 		GetClientRect(&rect);
       
   357 		int x = (rect.Width() - cxIcon + 1) / 2;
       
   358 		int y = (rect.Height() - cyIcon + 1) / 2;
       
   359 
       
   360 		// Draw the icon
       
   361 		dc.DrawIcon(x, y, m_hIcon);
       
   362 	}
       
   363 	else
       
   364 		CDialog::OnPaint();
       
   365 }
       
   366 
       
   367 //----------------------------------------------------------------------------
       
   368 
       
   369 HCURSOR CSTATDesktopDlg::OnQueryDragIcon()
       
   370 {
       
   371 	return (HCURSOR) m_hIcon;
       
   372 }
       
   373 
       
   374 //----------------------------------------------------------------------------
       
   375 
       
   376 void CSTATDesktopDlg::OnBrowse() 
       
   377 {
       
   378 	// use the folder of the current script as a starting point
       
   379 	m_Script.GetWindowText(szBuffer, sizeof(szBuffer));
       
   380 	CString buf = szBuffer;
       
   381 	if (!buf.IsEmpty())
       
   382 	{
       
   383 		int index = buf.ReverseFind('\\');
       
   384 		if (index != -1)
       
   385 		{
       
   386 			CString folder = buf.Left(index);
       
   387 			SetCurrentDirectory(folder.GetBuffer(0));
       
   388 		}
       
   389 	}
       
   390 
       
   391 	CFileDialog dlgStatFileOpen(TRUE, 
       
   392 								NULL, 
       
   393 								NULL, 
       
   394 								OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, 
       
   395 								_T("Script Files (*.txt)|*.txt|All Files (*.*)|*.*||"));
       
   396 
       
   397 	// new script file to use
       
   398 	if (dlgStatFileOpen.DoModal() == IDOK)	//when ok is pressed
       
   399 		m_Script.SetWindowText(dlgStatFileOpen.GetPathName());
       
   400 }
       
   401 
       
   402 //----------------------------------------------------------------------------
       
   403 //Frees memory and releases COM stuff etc...
       
   404 void CSTATDesktopDlg::OnCancel() 
       
   405 {
       
   406 	OnOK();
       
   407 }
       
   408 
       
   409 void CSTATDesktopDlg::OnExit() 
       
   410 {
       
   411 	SaveSettings();
       
   412 	OnOK();
       
   413 }
       
   414 
       
   415 
       
   416 //----------------------------------------------------------------------------
       
   417 //when the Run button is pressed
       
   418 void CSTATDesktopDlg::OnRun()
       
   419 {
       
   420 	if (bRunning)
       
   421 	{
       
   422 		CWaitCursor oWait;
       
   423 
       
   424 		Message("Signalling current script to stop.  Please wait...");
       
   425 		(iptrStopProcessing)(iHandle);
       
   426 	}
       
   427 	else
       
   428 	{
       
   429 		m_Output.ResetContent();
       
   430 		Message("*** STAT LOG ***");
       
   431 		RunIt();
       
   432 	}
       
   433 }
       
   434 
       
   435 
       
   436 //----------------------------------------------------------------------------
       
   437 // main work happens here
       
   438 void CSTATDesktopDlg::RunIt()
       
   439 {
       
   440 	// We should use GetData( true ) here to update all the internal data with the
       
   441 	// text in the dialog controls instead of GetWindowText.
       
   442 
       
   443 	CWaitCursor oWait;
       
   444 	int ret = ITS_OK;
       
   445 	bRunning = true;
       
   446 
       
   447 	// make sure we have something to work with
       
   448 	(*szBuffer) = (TCHAR)0;
       
   449 	m_Script.GetWindowText(szBuffer, sizeof(szBuffer));
       
   450 	if (!(*szBuffer))
       
   451 	{
       
   452 		CleanUp(_T("One or more required fields in the dialog are empty"));
       
   453 		return;
       
   454 	}
       
   455 
       
   456 	// initialise back end
       
   457 	if((ret = ConnectToEngine()) != ITS_OK)
       
   458 	{
       
   459 		CString msg;
       
   460 		if (ret == E_NOCONNECTION)
       
   461 			msg = _T("No connection specified");
       
   462 		else
       
   463 			msg = (iptrGetError)(iHandle);
       
   464 
       
   465 		CleanUp(msg);
       
   466 		return;
       
   467 	}
       
   468 
       
   469 	// turn on logging
       
   470 	if(m_LogToFile.GetCheck())
       
   471 	{
       
   472 		m_LogFileName.GetWindowText(szBuffer, sizeof(szBuffer));
       
   473 		CString cBuffer = szBuffer;
       
   474 
       
   475 		if (!cBuffer.IsEmpty() && (_tcscmp(szBuffer, _T("<date-time>")) != 0))
       
   476 			MessageFormat("Logging to %s Append: %d...", szBuffer, m_Append.GetCheck());
       
   477 		else
       
   478 		{
       
   479 			if (cBuffer.IsEmpty())
       
   480 				m_LogFileName.SetWindowText(_T("<date-time>"));
       
   481 			else
       
   482 				cBuffer.Empty();
       
   483 
       
   484 			MessageFormat("Logging to default file, Append: %d...", m_Append.GetCheck());
       
   485 		}
       
   486 
       
   487 		ret = (iptrSetCommandLogging)(iHandle, cBuffer, iMessageReporter,
       
   488 										EVerbose, (m_Append.GetCheck() > 0), NULL, NULL );
       
   489 		if (ret != LOG_FILE_OK)
       
   490 		{
       
   491 			CleanUp(_T("Logging could not be set"));
       
   492 			return;
       
   493 		}
       
   494 	}
       
   495 	else
       
   496 	{
       
   497 		CString cBuffer;
       
   498 		ret = (iptrSetCommandLogging)(iHandle, NULL, iMessageReporter,
       
   499 										EVerbose, (m_Append.GetCheck() > 0), NULL, NULL );
       
   500 		if (ret != LOG_FILE_OK)
       
   501 		{
       
   502 			CleanUp(_T("Logging could not be set"));
       
   503 			return;
       
   504 		}
       
   505 	}
       
   506 
       
   507 	// set the script execution speed
       
   508 	GetDlgItem(IDC_TIMEDELAY)->GetWindowText(szBuffer, sizeof(szBuffer));
       
   509 	(iptrSetCommandDelay)(iHandle, _ttoi(szBuffer));
       
   510 	MessageFormat("Command delay set to %d...", _ttoi(szBuffer));
       
   511 
       
   512 	// image verification
       
   513 	if(m_VerifyImage.GetCheck() && !SetImageVerification())
       
   514 	{
       
   515 		CleanUp(_T("Image verification could not be set"));
       
   516 		return;
       
   517 	}
       
   518 
       
   519 	// get list of commands in script
       
   520 	m_Script.GetWindowText(szBuffer, sizeof(szBuffer));
       
   521 	int iCount = 0;
       
   522 	ret = (iptrGetCommandCount)(iHandle, szBuffer, &iCount);
       
   523 	if(ret != ITS_OK)	//attempt to open as commands if file cannot be opened
       
   524 	{
       
   525 		CleanUp((iptrGetErrorText)(iHandle, ret));
       
   526 		return;
       
   527 	}
       
   528 
       
   529 	// set the progress bar
       
   530 	m_Progress.SetRange(0, (short)iCount);
       
   531 	m_Progress.SetPos(0);
       
   532 
       
   533 	//open script file
       
   534 	ret = (iptrOpenScriptFile)(iHandle, szBuffer, true);
       
   535 	if(ret != ITS_OK)	//attempt to open as commands if file cannot be opened
       
   536 	{
       
   537 		ret = (iptrOpenScriptFile)(iHandle, szBuffer, false);
       
   538 	}
       
   539 	if(ret != ITS_OK)
       
   540 	{
       
   541 		CleanUp((iptrGetErrorText)(iHandle, ret));
       
   542 		return;
       
   543 	}
       
   544 
       
   545 	EnableFields(FALSE);
       
   546 	oWait.Restore();
       
   547 
       
   548 	// event attributes for the child process - used to kill the thread if required
       
   549 	SECURITY_ATTRIBUTES eventAttr;
       
   550 	eventAttr.nLength = sizeof(eventAttr);
       
   551 	eventAttr.lpSecurityDescriptor = NULL;
       
   552 	eventAttr.bInheritHandle = TRUE;
       
   553 
       
   554 	THREADPROC_DATA *data = new THREADPROC_DATA;
       
   555 	data->windowHandle = GetSafeHwnd( );
       
   556 	data->handle = iHandle;
       
   557 	data->ptrRunScript = iptrRunScript;
       
   558 
       
   559 	// Would be nice to use MFC thread methods here but we want to be specific
       
   560 	// about using _beginthreadex and _endthreadex so we can manage the
       
   561 	// thread handle ourselves.  The MFC will neatly auto-close the handle but
       
   562 	// we do not want that.
       
   563 	// CWinThread *thread = ::AfxBeginThread( ThreadProc, &data );
       
   564 	// hThreadHandle = thread;
       
   565 	UINT dwThreadID = 0;
       
   566 	hThreadHandle = reinterpret_cast<HANDLE>(::_beginthreadex( NULL, 0, ThreadProc, data, 0, &dwThreadID ));
       
   567 
       
   568 	// Give the new thread chance to start.
       
   569 	// This is of no practical value in application operation.
       
   570 	// However, it is very useful in debugging and if we add this code we know
       
   571 	// the new thread will start now.
       
   572 	::Sleep(0);
       
   573 }
       
   574 
       
   575 //----------------------------------------------------------------------------
       
   576 //  Display a message
       
   577 void CSTATDesktopDlg::MessageFormat(const char *message, ...)
       
   578 {
       
   579 	char szText[1024] = {0};
       
   580 	va_list pMsg;
       
   581 
       
   582 	va_start (pMsg, message);
       
   583 	vsprintf (szText, message, pMsg);
       
   584 	va_end (pMsg);
       
   585 
       
   586 	CString temp = szText;
       
   587 	Message(temp);
       
   588 }
       
   589 
       
   590 
       
   591 //-----------------------------------------------------------------------------
       
   592 //  Display a message and (maybe) write to log file
       
   593 void CSTATDesktopDlg::Message(const char* message)
       
   594 {
       
   595 	int nCount = 0;
       
   596 	
       
   597 	m_Output.InsertString(-1, message);	
       
   598 	nCount = m_Output.GetCount();
       
   599 	if (nCount > 0)
       
   600 		m_Output.SetTopIndex(nCount - 1);	//The list view control is scrolled if necessary
       
   601 	
       
   602 	m_Output.UpdateWindow();
       
   603 }
       
   604 
       
   605 //----------------------------------------------------------------------------
       
   606 bool CSTATDesktopDlg::ShowScreenshot(const CString &filename)
       
   607 {
       
   608 	CFile cf;
       
   609 	m_pDib = NULL;
       
   610 	
       
   611 	// Attempt to open the Dib file for reading.
       
   612 	if( !cf.Open(filename, CFile::modeRead ) )
       
   613 		return false;
       
   614 
       
   615 	// Get the size of the file and store
       
   616 	// in a local variable. Subtract the
       
   617 	// size of the BITMAPFILEHEADER structure
       
   618 	// since we won't keep that in memory.
       
   619 	DWORD dwDibSize;
       
   620 	dwDibSize =	cf.GetLength() - sizeof( BITMAPFILEHEADER );
       
   621 
       
   622 	// Attempt to allocate the Dib memory.
       
   623 	unsigned char *pDib;
       
   624 	pDib = new unsigned char [dwDibSize];
       
   625 	if(!pDib)
       
   626 	{
       
   627 		cf.Close();
       
   628 		return false;
       
   629 	}
       
   630 
       
   631 	BITMAPFILEHEADER BFH;
       
   632 
       
   633 	// Read in the Dib header and data.
       
   634 	try
       
   635 	{
       
   636 
       
   637 		// Did we read in the entire BITMAPFILEHEADER?
       
   638 		if( cf.Read( &BFH, sizeof( BITMAPFILEHEADER ) )
       
   639 			!= sizeof( BITMAPFILEHEADER ) ||
       
   640 
       
   641 			// Is the type 'MB'?
       
   642 			BFH.bfType != 'MB' ||
       
   643 
       
   644 			// Did we read in the remaining data?
       
   645 			cf.Read( pDib, dwDibSize ) != dwDibSize )
       
   646 		{
       
   647 
       
   648 			// Delete the memory if we had any
       
   649 			// errors and return FALSE.
       
   650 			delete [] pDib;
       
   651 			cf.Close();
       
   652 			return false;
       
   653 		}
       
   654 	}
       
   655 
       
   656 	// If we catch an exception, delete the
       
   657 	// exception, the temporary Dib memory,
       
   658 	// and return FALSE.
       
   659 	catch( CFileException *e )
       
   660 	{
       
   661 		e->Delete();
       
   662 		delete [] pDib;
       
   663 		cf.Close();
       
   664 		return false;
       
   665 	}
       
   666 	
       
   667 	cf.Close();
       
   668 
       
   669 	// If we got to this point, the Dib has been
       
   670 	// loaded. If a Dib was already loaded into
       
   671 	// this class, we must now delete it.
       
   672 	if( m_pDib)
       
   673 		delete [] m_pDib;
       
   674 
       
   675 	// Store the local Dib data pointer and
       
   676 	// Dib size variables in the class member
       
   677 	// variables.
       
   678 	m_pDib = pDib;
       
   679 	m_dwDibSize = dwDibSize;
       
   680 
       
   681 	// Pointer our BITMAPINFOHEADER and RGBQUAD
       
   682 	// variables to the correct place in the Dib data.
       
   683 	m_pBIH = (BITMAPINFOHEADER *) m_pDib;
       
   684 	m_pPalette =
       
   685 		(RGBQUAD *) &m_pDib[sizeof(BITMAPINFOHEADER)];
       
   686 
       
   687 	// Calculate the number of palette entries.
       
   688 	m_nPaletteEntries = 1 << m_pBIH->biBitCount;
       
   689 	if( m_pBIH->biBitCount > 8 )
       
   690 		m_nPaletteEntries = 0;
       
   691 	else if( m_pBIH->biClrUsed != 0 )
       
   692 		m_nPaletteEntries = m_pBIH->biClrUsed;
       
   693 
       
   694 	// Point m_pDibBits to the actual Dib bits data.
       
   695 	m_pDibBits =
       
   696 		&m_pDib[sizeof(BITMAPINFOHEADER)+
       
   697 			m_nPaletteEntries*sizeof(RGBQUAD)];
       
   698 
       
   699 	// If we have a valid palette, delete it.
       
   700 	if( m_Palette.GetSafeHandle() != NULL )
       
   701 		m_Palette.DeleteObject();
       
   702 
       
   703 	// If there are palette entries, we'll need
       
   704 	// to create a LOGPALETTE then create the
       
   705 	// CPalette palette.
       
   706 	if(m_nPaletteEntries)
       
   707 	{
       
   708 		// Allocate the LOGPALETTE structure.
       
   709 		LOGPALETTE *pLogPal = (LOGPALETTE *) new char
       
   710 				[sizeof(LOGPALETTE) +
       
   711 				 m_nPaletteEntries * sizeof(PALETTEENTRY)];
       
   712 
       
   713 		if(pLogPal)
       
   714 		{
       
   715 			// Set the LOGPALETTE to version 0x300
       
   716 			// and store the number of palette
       
   717 			// entries.
       
   718 			pLogPal->palVersion = 0x300;
       
   719 			pLogPal->palNumEntries = (unsigned short)m_nPaletteEntries;
       
   720 
       
   721 			// Store the RGB values into each
       
   722 			// PALETTEENTRY element.
       
   723 			for( int i=0; i<m_nPaletteEntries; i++ ){
       
   724 				pLogPal->palPalEntry[i].peRed =
       
   725 					m_pPalette[i].rgbRed;
       
   726 				pLogPal->palPalEntry[i].peGreen =
       
   727 					m_pPalette[i].rgbGreen;
       
   728 				pLogPal->palPalEntry[i].peBlue =
       
   729 					m_pPalette[i].rgbBlue;
       
   730 				}
       
   731 
       
   732 			// Create the CPalette object and
       
   733 			// delete the LOGPALETTE memory.
       
   734 			m_Palette.CreatePalette( pLogPal );
       
   735 			delete [] pLogPal;
       
   736 		}
       
   737 		else
       
   738 			return false;
       
   739 	}
       
   740 	
       
   741 	// If we have not data we can't draw.
       
   742 	if(!m_pDib)
       
   743 		return false;
       
   744 	
       
   745 	// set the value that's in the BITMAPINFOHEADER.
       
   746 	
       
   747 	int nWidth = m_pBIH->biWidth;
       
   748 	int nHeight = m_pBIH->biHeight;
       
   749 
       
   750 	//HDC hdc, hImageBoxDC;
       
   751 	//HBITMAP hImageBoxBitmap;
       
   752 	
       
   753 	CDC* pDC = m_ImageBox.GetDC();
       
   754 	CDC ImageBoxDC;
       
   755 
       
   756 	ImageBoxDC.CreateCompatibleDC(pDC);
       
   757 	
       
   758 	CBitmap ImageBoxBitmap;
       
   759 	ImageBoxBitmap.CreateCompatibleBitmap(pDC, nWidth, nHeight);
       
   760 
       
   761 	//select the bitmap
       
   762 	ImageBoxDC.SelectObject(&ImageBoxBitmap);
       
   763 	ImageBoxDC.SetBkColor(OPAQUE);
       
   764 
       
   765 	//set MaskBitmap pixel data to that of the original DDB (on screen)
       
   766 	SetDIBits(ImageBoxDC.m_hDC, (HBITMAP)ImageBoxBitmap.GetSafeHandle(), 0L, nHeight, m_pDibBits, (BITMAPINFO *)m_pBIH, (DWORD)DIB_RGB_COLORS);
       
   767 
       
   768 	RECT Rect;
       
   769 	m_ImageBox.GetClientRect(&Rect);
       
   770 	
       
   771 	StretchDIBits(pDC -> m_hDC, 0, 0,
       
   772 			Rect.right, Rect.bottom,
       
   773 			0, 0,
       
   774 			m_pBIH -> biWidth, m_pBIH -> biHeight,
       
   775 			m_pDibBits,
       
   776 			(BITMAPINFO *) m_pBIH,
       
   777 			BI_RGB, SRCCOPY);
       
   778 
       
   779 	
       
   780 	m_ImageBox.ReleaseDC(pDC);
       
   781 
       
   782 	//release memory originally allocated
       
   783 	if(pDib)
       
   784 		delete [] pDib;
       
   785 
       
   786 	return true;
       
   787 }
       
   788 
       
   789 
       
   790 //////////////////////////////////////////////////////////////////////////////////////
       
   791 //used to adjust speed execution time information
       
   792 void CSTATDesktopDlg::OnReleasedcaptureSpeedslider(NMHDR* pNMHDR, LRESULT* pResult) 
       
   793 {
       
   794 	(void)pNMHDR;
       
   795 	TCHAR buffer[10];
       
   796 	SetDlgItemText(IDC_TIMEDELAY, _itot(m_SliderPos.GetPos(), buffer, 10));
       
   797 	(*pResult) = 0;
       
   798 }
       
   799 
       
   800 
       
   801 //////////////////////////////////////////////////////////////////////////////////////
       
   802 //adjusts slider according 
       
   803 void CSTATDesktopDlg::OnChangeTimedelay() 
       
   804 {
       
   805 	CString usertimer;
       
   806 	GetDlgItemText(IDC_TIMEDELAY, usertimer);
       
   807 	m_SliderPos.SetPos(_ttoi(usertimer));
       
   808 }
       
   809 
       
   810 
       
   811 //////////////////////////////////////////////////////////////////////////////////////
       
   812 // Call Notepad to view/edit script
       
   813 void CSTATDesktopDlg::OnEditscript() 
       
   814 {
       
   815 	CString file;
       
   816 	GetDlgItemText(IDC_SCRIPT, file);
       
   817 
       
   818 	if (!file.IsEmpty())
       
   819 	{
       
   820 		int index = file.ReverseFind('\\');
       
   821 		if (index != -1)
       
   822 		{
       
   823 			OpenFile(file.GetBuffer(0));
       
   824 		}
       
   825 	}
       
   826 
       
   827 }
       
   828 
       
   829 void CSTATDesktopDlg::OnView() 
       
   830 {
       
   831 	CString file;
       
   832 	GetDlgItemText(IDC_EDITLOGNAME, file);
       
   833 
       
   834 	if (file.IsEmpty() || (file.Compare(_T("<date-time>")) == 0))
       
   835 	{
       
   836 
       
   837 		CString setting=STAT_LOGFILEPATH_VALUE;
       
   838 		SetCurrentDirectory(setting);
       
   839 
       
   840 		CFileDialog dlgStatFileOpen(TRUE, 
       
   841 									NULL, 
       
   842 									NULL, 
       
   843 									OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, 
       
   844 									_T("Log Files (*.log)|*.log|All Files (*.*)|*.*||"));
       
   845 
       
   846 		// log file to use
       
   847 		if (dlgStatFileOpen.DoModal() == IDOK)	//when ok is pressed
       
   848 		{
       
   849 			file = dlgStatFileOpen.GetPathName();
       
   850 		}
       
   851 	}
       
   852 
       
   853 	if (!file.IsEmpty() && (file.Compare(_T("<date-time>")) != 0))
       
   854 		OpenFile(file.GetBuffer(0));
       
   855 }
       
   856 
       
   857 
       
   858 //////////////////////////////////////////////////////////////////////////////////////
       
   859 // Open a text file in Notepad
       
   860 void CSTATDesktopDlg::OpenFile(LPTSTR file)
       
   861 {
       
   862 	TCHAR szCurrentDir[MAX_PATH + 1];
       
   863 	if (GetWindowsDirectory(szCurrentDir, sizeof(szCurrentDir)))
       
   864 	{
       
   865 		CString cBuf;
       
   866 		cBuf = _T("\"");
       
   867 		cBuf += szCurrentDir;
       
   868 		cBuf += _T("\\Notepad.exe\" ");
       
   869 		cBuf += file;
       
   870 
       
   871 		if (!CallProcess(NULL, cBuf.GetBuffer(0), NULL))
       
   872 			AfxMessageBox(_T("Could not invoke Notepad.exe to view script file"), MB_OK, NULL);
       
   873 	}
       
   874 	else
       
   875 		AfxMessageBox(_T("Could not locate Windows directory"), MB_OK, NULL);
       
   876 }
       
   877 
       
   878 
       
   879 //////////////////////////////////////////////////////////////////////////////////////
       
   880 // Invoke a process
       
   881 bool CSTATDesktopDlg::CallProcess(LPCTSTR szApplication, LPTSTR szCommandLine, LPCTSTR szDirectory)
       
   882 {
       
   883 	bool valid = false;
       
   884 	STARTUPINFO startInfo = {0};
       
   885 	startInfo.cb = sizeof(STARTUPINFO);
       
   886 
       
   887 	PROCESS_INFORMATION procInfo = {0};
       
   888 
       
   889 	// event attributes for the child process
       
   890 	SECURITY_ATTRIBUTES eventAttr;
       
   891 	eventAttr.nLength = sizeof(eventAttr);
       
   892 	eventAttr.lpSecurityDescriptor = NULL;
       
   893 	eventAttr.bInheritHandle = TRUE;
       
   894 
       
   895 	if (CreateProcess(szApplication, szCommandLine, NULL, NULL, FALSE, NULL, NULL,
       
   896 					  szDirectory, &startInfo, &procInfo))
       
   897 	{
       
   898 		valid = true;
       
   899 		CloseHandle(procInfo.hThread);
       
   900 		CloseHandle(procInfo.hProcess);
       
   901 	}
       
   902 
       
   903 	return valid;
       
   904 }
       
   905 
       
   906 
       
   907 //////////////////////////////////////////////////////////////////////////////////////
       
   908 // Browse for Reference images folder
       
   909 void CSTATDesktopDlg::OnBrowseimage() 
       
   910 {
       
   911 	TCHAR chName[MAX_PATH];
       
   912 
       
   913 	BROWSEINFO bi = {0};
       
   914 	bi.hwndOwner = m_hWnd;
       
   915 	bi.lpszTitle = _T("Please select folder where the reference bitmap files are stored...");
       
   916 
       
   917 	LPITEMIDLIST lpIDList = ::SHBrowseForFolder(&bi); //now display dialog box 
       
   918 
       
   919 	::SHGetPathFromIDList(lpIDList, chName); //Converts an item identifier list to a file system path
       
   920 
       
   921 	m_ImageLocation.SetWindowText(chName);	//pastes location into edit box
       
   922 }
       
   923 
       
   924 
       
   925 //////////////////////////////////////////////////////////////////////////////////////
       
   926 // Read previous STAT settings
       
   927 void CSTATDesktopDlg::ReadSettings()
       
   928 {
       
   929 	//set slider text
       
   930 	m_SliderPos.SetRangeMax(30000, TRUE);	//max delay time
       
   931 	m_SliderPos.SetRangeMin(100, TRUE);		//minimum delay time
       
   932 
       
   933 	if(statIniFile.SectionExists(ST_TEST_KEY) )
       
   934 	{
       
   935 		CString setting;
       
   936 		setting.Empty();
       
   937 		setting=statIniFile.GetKeyValue(ST_DELAY,ST_TEST_KEY);
       
   938 		if(!setting.IsEmpty())
       
   939 		{
       
   940 			int iPos = _ttoi(setting);
       
   941 			if (iPos < 100)
       
   942 				setting= _T("100");
       
   943 
       
   944 			if (iPos > 30000)
       
   945 				setting= _T("30000");
       
   946 
       
   947 			m_Delay.SetWindowText(setting);
       
   948 			m_SliderPos.SetPos(iPos);
       
   949 		}
       
   950 
       
   951 		setting.Empty();
       
   952 		setting=statIniFile.GetKeyValue(ST_CUST_LOGFILE,ST_TEST_KEY);
       
   953 		if(!setting.IsEmpty())
       
   954 			m_LogFileName.SetWindowText(setting);
       
   955 
       
   956 		setting.Empty();
       
   957 		setting=statIniFile.GetKeyValue(ST_FUDGE,ST_TEST_KEY);
       
   958 		if(!setting.IsEmpty())
       
   959 			m_PercentageDiff.SetPos(_ttoi(setting));
       
   960 
       
   961 		setting.Empty();
       
   962 		setting=statIniFile.GetKeyValue(ST_REFDIR,ST_TEST_KEY);
       
   963 		if(!setting.IsEmpty())
       
   964 			m_ImageLocation.SetWindowText(setting);
       
   965 
       
   966 		setting.Empty();
       
   967 		setting=statIniFile.GetKeyValue(ST_SCRIPT,ST_TEST_KEY);
       
   968 		if(!setting.IsEmpty())
       
   969 			m_Script.SetWindowText(setting);
       
   970 
       
   971 		long lCount = 0;
       
   972 		setting.Empty();
       
   973 		setting=statIniFile.GetKeyValue(ST_VERIFYIMAGE,ST_TEST_KEY);
       
   974 		if(!setting.IsEmpty())
       
   975 		{
       
   976 			lCount=_ttol(setting);
       
   977 			if (lCount)
       
   978 			{
       
   979 				m_VerifyImage.SetCheck(lCount);
       
   980 				m_ImageLocation.EnableWindow(TRUE);
       
   981 				m_PercentageDiff.EnableWindow(TRUE);
       
   982 			}
       
   983 			else
       
   984 			{
       
   985 				m_ImageLocation.EnableWindow(FALSE);
       
   986 				m_PercentageDiff.EnableWindow(FALSE);
       
   987 			}
       
   988 		}
       
   989 		setting.Empty();
       
   990 		setting=statIniFile.GetKeyValue(ST_LOGTOFILE,ST_TEST_KEY);
       
   991 		if(!setting.IsEmpty())
       
   992 		{
       
   993 			lCount=_ttol(setting);
       
   994 			if (lCount)
       
   995 			{
       
   996 				m_LogToFile.SetCheck(lCount);
       
   997 				m_LogFileName.EnableWindow(TRUE);
       
   998 			}
       
   999 			else
       
  1000 				m_LogFileName.EnableWindow(FALSE);
       
  1001 		}
       
  1002 
       
  1003 		setting.Empty();
       
  1004 		setting=statIniFile.GetKeyValue(ST_CONNECTION,ST_TEST_KEY);
       
  1005 		if(!setting.IsEmpty())
       
  1006 		{
       
  1007 			m_Connection.SetWindowText(setting);
       
  1008 		}
       
  1009 
       
  1010 		setting.Empty();
       
  1011 		lCount=0;
       
  1012 		setting=statIniFile.GetKeyValue(ST_APPEND,ST_TEST_KEY);
       
  1013 		if(!setting.IsEmpty())
       
  1014 		{
       
  1015 			lCount=_ttol(setting);
       
  1016 		}
       
  1017 		m_Append.SetCheck(lCount);
       
  1018 
       
  1019 		setting.Empty();
       
  1020 		lCount=0;
       
  1021 		setting=statIniFile.GetKeyValue(ST_PLATFORM,ST_TEST_KEY);
       
  1022 		if(!setting.IsEmpty())
       
  1023 		{
       
  1024 			lCount=_ttol(setting);
       
  1025 		}
       
  1026 	}
       
  1027 	OnImageverify();
       
  1028 	OnLogToFile();
       
  1029 }
       
  1030 
       
  1031 
       
  1032 //////////////////////////////////////////////////////////////////////////////////////
       
  1033 // Save STAT settings
       
  1034 void CSTATDesktopDlg::SaveSettings()
       
  1035 {
       
  1036 	// script
       
  1037 	(*szBuffer) = (TCHAR)0;
       
  1038 	m_Script.GetWindowText(szBuffer, sizeof(szBuffer));
       
  1039 	statIniFile.WriteKey(szBuffer,ST_SCRIPT,ST_TEST_KEY);
       
  1040 
       
  1041 	// address
       
  1042 	(*szBuffer) = (char)0;
       
  1043 	m_Connection.GetWindowText(szBuffer, sizeof(szBuffer));
       
  1044 	statIniFile.WriteKey(szBuffer,ST_CONNECTION,ST_TEST_KEY);
       
  1045 
       
  1046 	// delay
       
  1047 	(*szBuffer) = (char)0;
       
  1048 	m_Delay.GetWindowText(szBuffer, sizeof(szBuffer));
       
  1049 	statIniFile.WriteKey(szBuffer,ST_DELAY,ST_TEST_KEY);
       
  1050 
       
  1051 	// percentage
       
  1052 	(*szBuffer) = (char)0;
       
  1053 	_itot(m_PercentageDiff.GetPos(), szBuffer, 10);
       
  1054 	statIniFile.WriteKey(szBuffer,ST_FUDGE,ST_TEST_KEY);
       
  1055 
       
  1056 	// ref dir
       
  1057 	(*szBuffer) = (char)0;
       
  1058 	m_ImageLocation.GetWindowText(szBuffer, sizeof(szBuffer));
       
  1059 	statIniFile.WriteKey(szBuffer,ST_REFDIR,ST_TEST_KEY);
       
  1060 
       
  1061 	(*szBuffer) = (char)0;
       
  1062 	m_LogFileName.GetWindowText(szBuffer, sizeof(szBuffer));
       
  1063 	statIniFile.WriteKey(szBuffer,ST_CUST_LOGFILE,ST_TEST_KEY);
       
  1064 
       
  1065 	_ltot( m_Append.GetCheck(), szBuffer, 10);
       
  1066 	statIniFile.WriteKey(szBuffer,ST_APPEND,ST_TEST_KEY);
       
  1067 
       
  1068 	_ltot(m_VerifyImage.GetCheck(), szBuffer, 10);
       
  1069 	statIniFile.WriteKey(szBuffer,ST_VERIFYIMAGE,ST_TEST_KEY);
       
  1070 
       
  1071 	_ltot(m_LogToFile.GetCheck(), szBuffer, 10);
       
  1072 	statIniFile.WriteKey(szBuffer,ST_LOGTOFILE,ST_TEST_KEY);
       
  1073 
       
  1074   }
       
  1075 
       
  1076 
       
  1077 //////////////////////////////////////////////////////////////////////////////////////
       
  1078 // Actions to perform when image verify checked/unchecked
       
  1079 void CSTATDesktopDlg::OnImageverify()
       
  1080 {
       
  1081 	if (m_VerifyImage.GetCheck())
       
  1082 	{
       
  1083 		m_ImageLocation.EnableWindow(TRUE);
       
  1084 		m_PercentageDiff.EnableWindow(TRUE);
       
  1085 		m_BrowseImage.EnableWindow(TRUE);
       
  1086 	}
       
  1087 	else
       
  1088 	{
       
  1089 		m_ImageLocation.EnableWindow(FALSE);
       
  1090 		m_PercentageDiff.EnableWindow(FALSE);
       
  1091 		m_BrowseImage.EnableWindow(FALSE);
       
  1092 	}
       
  1093 }
       
  1094 
       
  1095 
       
  1096 //////////////////////////////////////////////////////////////////////////////////////
       
  1097 // Actions to perform when log to file checked/unchecked
       
  1098 void CSTATDesktopDlg::OnLogToFile()
       
  1099 {
       
  1100 	if(m_LogToFile.GetCheck())
       
  1101 	{
       
  1102 		m_LogFileName.EnableWindow(TRUE);
       
  1103 		m_Append.EnableWindow(TRUE);
       
  1104 		m_BrowseLog.EnableWindow(TRUE);
       
  1105 		m_ViewLog.EnableWindow(TRUE);
       
  1106 	}
       
  1107 	else
       
  1108 	{
       
  1109 		m_LogFileName.EnableWindow(FALSE);
       
  1110 		m_Append.EnableWindow(FALSE);
       
  1111 		m_BrowseLog.EnableWindow(FALSE);
       
  1112 		m_ViewLog.EnableWindow(FALSE);
       
  1113 	}
       
  1114 }
       
  1115 
       
  1116 
       
  1117 //////////////////////////////////////////////////////////////////////////////////////
       
  1118 // Browse for a log file to write to
       
  1119 void CSTATDesktopDlg::OnBrowseLog() 
       
  1120 {
       
  1121 	CFileDialog dlgStatFileOpen(TRUE, 
       
  1122 								NULL, 
       
  1123 								NULL, 
       
  1124 								OFN_PATHMUSTEXIST, 
       
  1125 								_T("Log Files (*.log)|*.log|Script Files (*.txt)|*.txt|All Files (*.*)|*.*||"));
       
  1126 
       
  1127 	// new script file to use
       
  1128 	if (dlgStatFileOpen.DoModal() == IDOK)	//when ok is pressed
       
  1129 		m_LogFileName.SetWindowText(dlgStatFileOpen.GetPathName());
       
  1130 }
       
  1131 
       
  1132 
       
  1133 //////////////////////////////////////////////////////////////////////////////////////
       
  1134 // Make connection to the back end
       
  1135 int CSTATDesktopDlg::ConnectToEngine() 
       
  1136 {
       
  1137 	int	retCode =	ITS_OK;
       
  1138 
       
  1139 	STATCONNECTTYPE type = SymbianInvalid;
       
  1140 	char szConnection[512] = {0};
       
  1141 
       
  1142 	m_Connection.GetWindowText(szBuffer, sizeof(szBuffer));
       
  1143 	strcpy(szConnection, szBuffer);
       
  1144 
       
  1145 	char *p = strchr(szConnection, ':');
       
  1146 	if (p)
       
  1147 	{
       
  1148 		(*p) = (char)0;
       
  1149 		if (stricmp(szConnection, "SymbianSocket") == 0)
       
  1150 		{
       
  1151 			type = SymbianSocket;
       
  1152 		}
       
  1153 		else if (stricmp(szConnection, "SymbianSerial") == 0)
       
  1154 		{
       
  1155 			type = SymbianSerial;
       
  1156 		}
       
  1157 		else if (stricmp(szConnection, "SymbianInfrared") == 0)
       
  1158 		{
       
  1159 			type = SymbianInfrared;
       
  1160 		}
       
  1161 		else if (stricmp(szConnection, "SymbianBluetooth") == 0)
       
  1162 		{
       
  1163 			type = SymbianBluetooth;
       
  1164 		}
       
  1165 		else if (stricmp(szConnection, "SymbianUsb") == 0)
       
  1166 		{
       
  1167 			type = SymbianUsb;
       
  1168 		}
       
  1169 
       
  1170 		MessageFormat("Connecting over %s to %s...", szConnection, p + 1);
       
  1171 
       
  1172 		// initialise back end
       
  1173 		iHandle =	(iptrConnect)(type, p + 1, NULL, NULL);
       
  1174 
       
  1175 		if( NULL != iHandle )
       
  1176 		{
       
  1177 			retCode =	ITS_OK;
       
  1178 		}
       
  1179 		else
       
  1180 		{
       
  1181 			retCode =	GENERAL_FAILURE;
       
  1182 		}
       
  1183 	}
       
  1184 
       
  1185 	return (retCode);
       
  1186 }
       
  1187 
       
  1188 
       
  1189 //////////////////////////////////////////////////////////////////////////////////////
       
  1190 // Set up image verification
       
  1191 int CSTATDesktopDlg::SetImageVerification()
       
  1192 {
       
  1193 	(*szBuffer) = (char)0;
       
  1194 	m_ImageLocation.GetWindowText(szBuffer, sizeof(szBuffer));
       
  1195 
       
  1196 	int iErrorCode = (iptrSetImageVerification)(iHandle, szBuffer,
       
  1197 						true, m_PercentageDiff.GetPos());
       
  1198 
       
  1199 	// success?
       
  1200 	if(iErrorCode)
       
  1201 	{
       
  1202 		Message("Image verification set");
       
  1203 		return true;
       
  1204 	}
       
  1205 	else
       
  1206 	{
       
  1207 		iErrorCode = GENERAL_FAILURE;
       
  1208 		Message("ERROR: No images available for verification");
       
  1209 	}
       
  1210 
       
  1211 	return false;
       
  1212 }
       
  1213 
       
  1214 //////////////////////////////////////////////////////////////////////////////////////
       
  1215 // Set up image verification
       
  1216 void CSTATDesktopDlg::EnableFields(bool bEnable)
       
  1217 {
       
  1218 	// store
       
  1219 	if (!bEnable)
       
  1220 	{
       
  1221 		iLastVerify = m_VerifyImage.GetCheck();
       
  1222 		iLastLog = m_LogToFile.GetCheck();
       
  1223 
       
  1224 		GetDlgItem(IDRUN)->SetWindowText(_T("Stop"));
       
  1225 	}
       
  1226 
       
  1227 	GetDlgItem(IDC_SCRIPT)->EnableWindow(bEnable);
       
  1228 	GetDlgItem(IDC_BROWSE)->EnableWindow(bEnable);
       
  1229 	GetDlgItem(IDEDITSCRIPT)->EnableWindow(bEnable);
       
  1230 	GetDlgItem(IDC_TIMEDELAY)->EnableWindow(bEnable);
       
  1231 	GetDlgItem(IDC_SPEEDSLIDER)->EnableWindow(bEnable);
       
  1232 	GetDlgItem(IDC_IMAGEVERIFY)->EnableWindow(bEnable);
       
  1233 	GetDlgItem(IDC_LOCATION)->EnableWindow(bEnable);
       
  1234 	GetDlgItem(IDC_BROWSEIMAGE)->EnableWindow(bEnable);
       
  1235 	GetDlgItem(IDC_DIFFLOGNAME)->EnableWindow(bEnable);
       
  1236 	GetDlgItem(IDC_EDITLOGNAME)->EnableWindow(bEnable);
       
  1237 	GetDlgItem(IDC_APPEND)->EnableWindow(bEnable);
       
  1238 	GetDlgItem(IDC_BROWSELOG)->EnableWindow(bEnable);
       
  1239 	GetDlgItem(IDC_EDITLOGNAME)->EnableWindow(bEnable);
       
  1240 	GetDlgItem(IDEXIT)->EnableWindow(bEnable);
       
  1241 	GetDlgItem(IDC_PERCENTAGEDIFF)->EnableWindow(bEnable);
       
  1242 
       
  1243 	// restore
       
  1244 	if (bEnable)
       
  1245 	{
       
  1246 		m_VerifyImage.SetCheck(iLastVerify);
       
  1247 		m_LogToFile.SetCheck(iLastLog);
       
  1248 		OnImageverify();
       
  1249 		OnLogToFile();
       
  1250 		GetDlgItem(IDRUN)->SetWindowText(_T("Run"));
       
  1251 	}
       
  1252 
       
  1253 	UpdateWindow();
       
  1254 }
       
  1255 
       
  1256 
       
  1257 //////////////////////////////////////////////////////////////////////////////////////
       
  1258 // Release resources at finish
       
  1259 void CSTATDesktopDlg::CleanUp(LPCTSTR message, bool bEnableFields)
       
  1260 {
       
  1261 	CString cMsg = message;
       
  1262 	CleanUp(cMsg, bEnableFields);
       
  1263 }
       
  1264 
       
  1265 void CSTATDesktopDlg::CleanUp(const CString &message, bool bEnableFields)
       
  1266 {
       
  1267 	m_Progress.SetPos(0);
       
  1268 
       
  1269 	if (bEnableFields)
       
  1270 		EnableFields(TRUE);
       
  1271 
       
  1272 	if (!message.IsEmpty())
       
  1273 	{
       
  1274 		Message(message);
       
  1275 	}
       
  1276 
       
  1277 	// close the handle to our thread
       
  1278   	if (hThreadHandle)
       
  1279   	{
       
  1280   		CloseHandle(hThreadHandle);
       
  1281   		hThreadHandle = (HANDLE)0;
       
  1282   	}
       
  1283 
       
  1284 	// release the back end
       
  1285 	if( iHandle != 0 )
       
  1286 	{
       
  1287 		(iptrDisconnect)(iHandle);
       
  1288 		iHandle = 0;
       
  1289 	}
       
  1290 
       
  1291 	bRunning = false;
       
  1292 }
       
  1293 
       
  1294 void CSTATDesktopDlg::OnManage() 
       
  1295 {
       
  1296 	STATManageConnection oManage;
       
  1297 	if (IDOK == oManage.DoModal())
       
  1298 	{
       
  1299 		m_Connection.SetWindowText(cConnectionInfo);
       
  1300 	}
       
  1301 }
       
  1302 
       
  1303 void CSTATDesktopDlg::OnDestroy() 
       
  1304 {
       
  1305 	CleanUp(_T(""), false);
       
  1306 
       
  1307 	if( iMessageReporter != NULL )
       
  1308 	{
       
  1309 		delete iMessageReporter;
       
  1310 		iMessageReporter =	NULL;
       
  1311 	}
       
  1312 
       
  1313 	if( ihLib != NULL )
       
  1314 	{
       
  1315 		::FreeLibrary( ihLib );
       
  1316 		ihLib =	NULL;
       
  1317 	}
       
  1318 
       
  1319 	DeleteCriticalSection(&CriticalSection);
       
  1320 
       
  1321 	CDialog::OnDestroy();
       
  1322 }
       
  1323 
       
  1324 LRESULT CSTATDesktopDlg::OnDoneCommand(WPARAM command, LPARAM )
       
  1325 {
       
  1326 	command;
       
  1327 
       
  1328 	int currentCommand = 0;
       
  1329 	if( ITS_OK == (iptrGetCurrentCommandNumber)(iHandle, &currentCommand) )
       
  1330 	{
       
  1331 		m_Progress.SetPos(currentCommand);
       
  1332 	}
       
  1333 
       
  1334 	return (0L );
       
  1335 }
       
  1336 
       
  1337 LRESULT CSTATDesktopDlg::OnDoneScript(WPARAM iScriptExitCode, LPARAM )
       
  1338 {
       
  1339 	CleanUp((iptrGetErrorText)(iHandle, iScriptExitCode));
       
  1340 
       
  1341 	return (0L );
       
  1342 }
       
  1343 
       
  1344 LRESULT CSTATDesktopDlg::OnLogMesage(WPARAM, LPARAM pLogMessage )
       
  1345 {
       
  1346 	LogMessage *logMessage = reinterpret_cast<LogMessage*>(pLogMessage);
       
  1347 
       
  1348 	ASSERT( FALSE == ::IsBadWritePtr( logMessage, sizeof(*logMessage) ) );
       
  1349 
       
  1350 	if( NULL != logMessage )
       
  1351 	{
       
  1352 		if (logMessage->iMessage.GetLength() != 0)
       
  1353 		{
       
  1354 			Message(logMessage->iMessage);
       
  1355 		}
       
  1356 
       
  1357 		if (logMessage->iText.GetLength() != 0)
       
  1358 		{
       
  1359 			Message(logMessage->iText);
       
  1360 		}
       
  1361 
       
  1362 		if (logMessage->iScreenShot)
       
  1363 		{
       
  1364 			ShowScreenshot(logMessage->iText);
       
  1365 		}
       
  1366 
       
  1367 		if (logMessage->iMessageBox)
       
  1368 		{
       
  1369 			::AfxMessageBox(logMessage->iMessage, MB_OK, NULL);
       
  1370 		}
       
  1371 	}
       
  1372 
       
  1373 	return (0L);
       
  1374 }