/*
 *  Arcanum Editor - ROM area editor
 *  Copyright (C) 1999  Lucas Wall <kthulhu@usa.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *  If you make changes to the source and want to make the public you can
 *  mail the diferences to me <kthulhu@usa.net> and I'll add them to the
 *  main distribution. Of course your name will be added to the program with
 *  information about the changes you made.
 *
 *
 *  Packed on Thu Aug 19 03:02:27 1999
 *
 */


/*
 * File: arcaed.cpp
 *
 * Changes:
 *
 * 19/08/99 Lucas Wall <kthulhu@usa.net>
 *          First source release. Its quite messy and has no
 *          comments. I never planed to release the source code... :-)
 *
 */


// arcaed.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "arcaed.h"

#include "MainFrm.h"
#include "ChildFrm.h"
#include "arcaedDoc.h"
#include "arcaedView.h"
#include "fread.h"
#include "Splash.h"

#include <locale.h>

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

/////////////////////////////////////////////////////////////////////////////
// CArcaedApp

BEGIN_MESSAGE_MAP(CArcaedApp, CWinApp)
	//{{AFX_MSG_MAP(CArcaedApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CArcaedApp construction

CArcaedApp::CArcaedApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CArcaedApp object

CArcaedApp theApp;
CTables tables;

/////////////////////////////////////////////////////////////////////////////
// CArcaedApp initialization

BOOL CArcaedApp::InitInstance()
{
	// CG: The following block was added by the Splash Screen component.
	{
		CCommandLineInfo cmdInfo;
		ParseCommandLine(cmdInfo);

		CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
	}
	int line, num;
	char buf[1000];
	CString cfgname;
	FILE *cfgfile;

	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

	AfxOleInit();

	// Change the registry key under which our settings are stored.
	// You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Kadath Systems"));

	LoadStdProfileSettings(5);  // 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.

	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(
		IDR_ARCAEDTYPE,
		RUNTIME_CLASS(CArcaedDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CArcaedView));
	AddDocTemplate(pDocTemplate);

	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;

	// Enable drag/drop open
	m_pMainWnd->DragAcceptFiles();

	// Enable DDE Execute open
	EnableShellOpen();
	RegisterShellFileTypes(TRUE);

	init_buffers();

	cfgname = "arcaed.cfg";
	if ( ( cfgfile = fopen( cfgname.GetBuffer( 0 ), "rt" ) ) == NULL )
	{
		GetModuleFileName( NULL, buf, 1000 );
		cfgname = buf;
		if ( ( num = cfgname.ReverseFind( '\\' ) ) != -1 )
			cfgname = cfgname.Left( num + 1 );
		else
			cfgname = "";
		cfgname += "arcaed.cfg";
	}
	else
		fclose( cfgfile );

	line = 0;
	if ( tables.load_data( cfgname.GetBuffer( 0 ), &line ) )
	{
		sprintf( buf, "Error leyendo arcaed.cfg! (%i)", line );
		MessageBox( NULL, buf, "Error", MB_ICONERROR );
		return FALSE;
	}

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

	if ( cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew )
		cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;

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

	// The main window has been initialized, so show and update it.
	pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
	pMainFrame->UpdateWindow();

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CBitmap *m_bcthulhu;
	int count;
	CAboutDlg();
	~CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CButton	m_Cthulhu;
	CString	m_Text;
	CString	m_Version;
	//}}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 void OnCthulhu();
	virtual BOOL OnInitDialog();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	m_Text = _T("");
	m_Version = _T("");
	//}}AFX_DATA_INIT
	m_bcthulhu = new CBitmap();
	m_bcthulhu->LoadBitmap( IDB_CTHULHU );
}

CAboutDlg::~CAboutDlg()
{
	delete m_bcthulhu;
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Control(pDX, IDC_CTHULHU, m_Cthulhu);
	DDX_Text(pDX, IDC_ABOUT_TEXT, m_Text);
	DDX_Text(pDX, IDC_ABOUT_VERSION, m_Version);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	ON_BN_CLICKED(IDC_CTHULHU, OnCthulhu)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CArcaedApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.m_Text.LoadString( IDS_ABOUT_TEXT );
	aboutDlg.DoModal();
}

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	DWORD vers, size, *bver;
	UINT len;
	char buf[1000];
	void *verinfo;
	VS_FIXEDFILEINFO *vinfo;

	m_Cthulhu.SetBitmap( *m_bcthulhu );
	count = 0;
	
	GetModuleFileName( NULL, buf, 1000 );
	size = GetFileVersionInfoSize( buf, &vers );
	bver = (DWORD *) malloc( size );
	GetFileVersionInfo( buf, 0, size, bver );

	size = VerQueryValue( bver, "\\", &verinfo, &len );
	vinfo = (VS_FIXEDFILEINFO *) verinfo;
	sprintf( buf, "%u.%02u", HIWORD(vinfo->dwProductVersionMS),
		LOWORD(vinfo->dwProductVersionMS) * 10 + HIWORD(vinfo->dwProductVersionLS) );
	m_Version = buf;

	UpdateData( FALSE );
	free( bver );

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CAboutDlg::OnCthulhu() 
{
	if ( ++count == 1 )
	{
		m_Text.LoadString( IDS_HPLTEXT );
		UpdateData( FALSE );
	}

	if ( count >= 5 )
		PlaySound( MAKEINTRESOURCE(IDR_CTHULHUWAVE), AfxGetResourceHandle(), SND_ASYNC | SND_RESOURCE );
}

/////////////////////////////////////////////////////////////////////////////
// CArcaedApp commands

int CArcaedApp::ExitInstance() 
{
	free_buffers();
	
	return CWinApp::ExitInstance();
}

BOOL CArcaedApp::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following lines were added by the Splash Screen component.
	if (CSplashWnd::PreTranslateAppMessage(pMsg))
		return TRUE;

	return CWinApp::PreTranslateMessage(pMsg);
}