// CTMS.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "CTMS.h"

#include "MainFrm.h"
#include "CTMSDoc.h"
#include "CTMSView.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCTMSApp

BEGIN_MESSAGE_MAP(CCTMSApp, CWinApp)
	//{{AFX_MSG_MAP(CCTMSApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_INIFILE, OnInifile)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CCTMSApp construction

/////////////////////////////////////////////////////////////////////////////
// CCTMSApp::CCTMSApp()
// 
// 
/////////////////////////////////////////////////////////////////////////////
CCTMSApp::CCTMSApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
  m_iSendRecv = 0;
  //m_hLib = LoadLibrary( "BlackBox.dll");

  //start using Winsock...
  if (!WSAStartup ( MAKEWORD ( 2,2 ), &wsaData))
  {

    //Tell the user that we could not find a usable WinSock DLL
    if ((LOBYTE( wsaData.wVersion) != 2) || (HIBYTE( wsaData.wVersion) != 2 ))
    {

      WSACleanup ();    
      AfxMessageBox( "Failed to start Winsocket support, exiting application....", MB_OK, 0);
      exit(1);
    }
  }
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CCTMSApp object

CCTMSApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CCTMSApp initialization

BOOL CCTMSApp::InitInstance()
{

//	if (!AfxSocketInit())
//	{
//		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
//		return FALSE;
//	}

  //single instance of this application
  if (!m_oSingleInstance.Create( IDR_MAINFRAME)) return FALSE;

	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

  //initialise the ini file object
  CString sWdir = this->m_pszHelpFilePath;
  sWdir = sWdir.Left( sWdir.GetLength() - 4 - strlen( this->m_pszExeName));

  theINIFile.Create( LPTSTR(this->m_pszExeName), LPTSTR(sWdir.GetBuffer(1)));

  //generate all the first tdi
  //TDI.Add( new CTDI);

  // 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("RLD Automation"));

	LoadStdProfileSettings(0);  // Load standard INI file options (including MRU)

  //get the previous window size, if any
  //m_iX = GetProfileInt( "Settings", "Left", 0);
  //m_iY = GetProfileInt( "Settings", "Top", 0);
  //m_iH = GetProfileInt( "Settings", "Height", 0);
  //m_iW = GetProfileInt( "Settings", "Width", 0);
  //m_iP = GetProfileInt( "Settings", "Pane", 0);

  // 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(CCTMSDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CCTMSView));
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

  //don't display a new document
  cmdInfo.m_nShellCommand = CCommandLineInfo::FileNew;

	// 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;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

/////////////////////////////////////////////////////////////////////////////
// class CAboutDlg : public CDialog
// 
// 
/////////////////////////////////////////////////////////////////////////////
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CStatic	m_cVersion;
	CStatic	m_cComment;
	//}}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)
	virtual BOOL OnInitDialog();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
private:
	CExecImageVersion m_oVersion;
	CHyperLink m_HomePageLink;
};

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
// 
// 
/////////////////////////////////////////////////////////////////////////////
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

/////////////////////////////////////////////////////////////////////////////
// void CAboutDlg::DoDataExchange(CDataExchange* pDX)
// 
// 
/////////////////////////////////////////////////////////////////////////////
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Control(pDX, IDC_VERSION, m_cVersion);
	DDX_Control(pDX, IDC_COMMENT, m_cComment);
	//}}AFX_DATA_MAP
  DDX_Control(pDX, IDC_HOMEPAGELINK, m_HomePageLink);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CCTMSApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CCTMSApp message handlers


/////////////////////////////////////////////////////////////////////////////
// void CCTMSApp::SetTDISubId(int SubId)
// 
// 
/////////////////////////////////////////////////////////////////////////////
void CCTMSApp::SetTMIndex(int SubId)
{

  m_iTDISubId = SubId; // >> 8;

  //TDI.ElementAt( m_iTDISubId)->SetCluster( SubId & 0x00ff);
}

/////////////////////////////////////////////////////////////////////////////
// CTDI * CCTMSApp::GetTDI()
// 
// 
/////////////////////////////////////////////////////////////////////////////
CTDI * CCTMSApp::GetTDI( int index)
{

  if ((index < 0) || (index >= TDI.GetSize()))
    return NULL; //TDI.ElementAt( m_iTDISubId);

  return TDI.ElementAt( index);
}

/////////////////////////////////////////////////////////////////////////////
// CTDI * CCTMSApp::GetTDI()
// 
// 
/////////////////////////////////////////////////////////////////////////////
CTM * CCTMSApp::GetTM( int index)
{

  if ((index < 0) || (index >= m_TMs.GetSize()))
    return NULL;

  return m_TMs.ElementAt( index);
}


/////////////////////////////////////////////////////////////////////////////
// int CCTMSApp::ExitInstance() 
// 
// 
/////////////////////////////////////////////////////////////////////////////
int CCTMSApp::ExitInstance() 
{
	// TODO: Add your specialized code here and/or call the base class
  int i;

  //delete all the TDI objects
  for (i = 0; i < TDI.GetSize(); i++)
    delete TDI.GetAt( i);

	TDI.RemoveAll();

  //if (m_hLib != NULL) FreeLibrary( m_hLib);

  //now we can dlete the array of pointers
	return CWinApp::ExitInstance();
}

/////////////////////////////////////////////////////////////////////////////
// int CCTMSApp::GetLastTDI()
// 
// retrun the length of the TDI array
/////////////////////////////////////////////////////////////////////////////
int CCTMSApp::GetLastTDI()
{

  return TDI.GetSize();
}

/////////////////////////////////////////////////////////////////////////////
// void CCTMSApp::AddTDI()
// 
// add new TDI at end of array
/////////////////////////////////////////////////////////////////////////////
int CCTMSApp::AddTDI(CString TmId)
{

  //CTDI *tdi = new CTDI;
  //TDI.Add( tdi);
  CTM *tm = new CTM( (char *)((LPCTSTR) TmId));
  m_TMs.Add( tm);
  return (m_TMs.GetSize() - 1);
}

/////////////////////////////////////////////////////////////////////////////
// void CCTMSApp::RemoveTDI(int index)
// 
// 
/////////////////////////////////////////////////////////////////////////////
void CCTMSApp::RemoveTDI(int index)
{


  if (index < TDI.GetSize())
  {

    //delete the object
    delete TDI.GetAt( index);

    //delete the pointer to the object
    TDI.RemoveAt( index);
  }
}

/////////////////////////////////////////////////////////////////////////////
// CString CCTMSApp::GetMainFrameClass()
// 
// 
/////////////////////////////////////////////////////////////////////////////
CString CCTMSApp::GetMainFrameClass()
{

  return m_oSingleInstance.GetClassName();
}

/////////////////////////////////////////////////////////////////////////////
// BOOL CAboutDlg::OnInitDialog() 
// 
// 
/////////////////////////////////////////////////////////////////////////////
BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
  m_cComment.SetWindowText( m_oVersion.GetComments());
  m_cVersion.SetWindowText( m_oVersion.GetInternalName() + " Version " + m_oVersion.GetProductVersion());

	// Set HyperLink for Home Page
  m_HomePageLink.SetWindowText( m_oVersion.GetCompanyName() + " " + m_oVersion.GetCopyright());
  m_HomePageLink.SetURL(_T(IDS_HOMEPAGEADDR));
  m_HomePageLink.SetUnderline(TRUE);
  m_HomePageLink.SetLinkCursor(AfxGetApp()->LoadCursor( IDC_HANDx));
	
  return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

int CCTMSApp::GetTMCount()
{

  return m_TMs.GetSize();
}

void CCTMSApp::OnInifile() 
{
	//Open the ini-file located in the working directory
	CString ini = this->m_pszHelpFilePath;
	ini = ini.Mid(0, ini.GetLength() - 3);
	ini += "ini";
#ifdef _DEBUG
	ini = "tm-sim.ini";
#endif
	ShellExecute(NULL, _T("open"), LPCTSTR(ini), NULL,NULL, SW_SHOWDEFAULT);
}
