/*==========================================================================*/
/* Copyright (c) 1999 - Sykes Enterprises Inc. - All rights reserved        */
/*--------------------------------------------------------------------------*/
/* Sykes grants you ("Licensee") a non-exclusive, royalty free, license to  */
/* use, modify and redistribute this software in source and binary code     */
/* form, provided that i) this copyright notice and license appear on all   */
/* copies of the software; and ii) Licensee does not utilize the software   */
/* in a manner which is disparaging to Sykes.                               */
/*                                                                          */
/* This software is provided "AS IS", without a warranty of any kind. ALL   */
/* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING */
/* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR        */
/* PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SYKES AND ITS          */
/* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A  */
/* RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE OR ITS           */
/* DERIVATIVES. IN NO EVENT WILL SYKES OR ITS LICENSORS BE LIABLE FOR ANY   */
/* LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,          */
/* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND        */
/* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR      */
/* INABILITY TO USE SOFTWARE, EVEN IF SYKES HAS BEEN ADVISED OF THE         */
/* POSSIBILITY OF SUCH DAMAGES.                                             */
/*==========================================================================*/

#include "stdafx.h"
#include "Waikoloa.h"
#include "MainFrm.h"
#include "WaikoloaDoc.h"
#include "WaikoloaView.h"

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


/*--------------------------------------------------------------------------*/
// Helper function: Gets the Windows version

short okGetVersion
(
	DWORD*               p_pdwVersion
)
{
	DWORD                dw;


	dw = GetVersion();
	if ( p_pdwVersion != NULL ) *p_pdwVersion = dw;

	if ( dw < 0x80000000 ) return(okVERSION_WINNT);
	if ( LOBYTE(LOWORD(dw)) < 4 ) return(okVERSION_WIN32S);
	return(okVERSION_WIN95);
}


/*--------------------------------------------------------------------------*/
// Helper function: Displays system error

void okDisplaySystemError
(
	const DWORD          p_dwCode
)
{
	LPVOID               lpMsgBuf;
	DWORD                dwCode = p_dwCode;


	// Get the code if parameter set to zero.
	if ( !dwCode ) dwCode = GetLastError();

	// Get the message text
	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
		NULL, dwCode,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &lpMsgBuf, 0, NULL);

	// Display the string.
	AfxMessageBox((LPCSTR)lpMsgBuf);
	
	// Free the buffer.
	LocalFree(lpMsgBuf);
}


/*--------------------------------------------------------------------------*/
// Helper function: Starts an application

BOOL okStart
(
	LPCSTR               p_pszPath
)
{
	STARTUPINFO          SI;
	_PROCESS_INFORMATION PI;
	BOOL                 bRes;
	CString              csCommand;



	memset(&SI, 0, sizeof(STARTUPINFO));
	SI.cb = sizeof(STARTUPINFO);
	SI.wShowWindow = SW_HIDE;
	SI.dwFlags = STARTF_USESHOWWINDOW;
	
   if ( okGetVersion(NULL) == okVERSION_WINNT )
	{
		csCommand.Format("cmd /C \"%s\"", p_pszPath);
	}
	else
	{
		csCommand.Format("start \"%s\"", p_pszPath);
	}

	if ( (bRes = CreateProcess(NULL, (LPTSTR)(const char*)csCommand,
		NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &SI, &PI)) == FALSE )
	{
		okDisplaySystemError(0);
 	}

	return(bRes);
}


/*--------------------------------------------------------------------------*/

BEGIN_MESSAGE_MAP(CWaikoloaApp, CWinApp)
	//{{AFX_MSG_MAP(CWaikoloaApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_HELP_ABOUTSYKES, OnHelpAboutsykes)
	ON_COMMAND(ID_HELP_ABOUTMLT, OnHelpAboutmlt)
	ON_COMMAND(ID_HELP_OPENTAG, OnHelpOpentag)
	ON_COMMAND(ID_HELP_TMX, OnHelpTmx)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()


/*--------------------------------------------------------------------------*/

CWaikoloaApp::CWaikoloaApp()
{
}


CWaikoloaApp theApp;


/*--------------------------------------------------------------------------*/

BOOL CWaikoloaApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings(6);  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CWaikoloaDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CWaikoloaView));
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The one and only window has been initialized, so show and update it.
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

	return TRUE;
}


/*--------------------------------------------------------------------------*/

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};


/*--------------------------------------------------------------------------*/

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}


/*--------------------------------------------------------------------------*/

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}


/*--------------------------------------------------------------------------*/

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/*--------------------------------------------------------------------------*/

void CWaikoloaApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}



/*--------------------------------------------------------------------------*/

void CWaikoloaApp::OnHelpAboutsykes() 
{
	okStart("http://www.sykes.com/");
}


/*--------------------------------------------------------------------------*/

void CWaikoloaApp::OnHelpAboutmlt() 
{
	okStart("http://www.multilingual.com/");
}


/*--------------------------------------------------------------------------*/

void CWaikoloaApp::OnHelpOpentag() 
{
	okStart("http://www.opentag.org/");
}


/*--------------------------------------------------------------------------*/

void CWaikoloaApp::OnHelpTmx() 
{
	okStart("http://www.lisa.org/tmx/");
}
