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

#include "stdafx.h"
#include "mpsetup.h"

#include "mainfrm.h"
#include "mpsetdoc.h"
#include "mpsetvw.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMpsetupApp

BEGIN_MESSAGE_MAP(CMpsetupApp, CWinApp)
	//{{AFX_MSG_MAP(CMpsetupApp)
	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)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMpsetupApp construction

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

/////////////////////////////////////////////////////////////////////////////
// The one and only CMpsetupApp object

CMpsetupApp NEAR theApp;

/////////////////////////////////////////////////////////////////////////////
// CMpsetupApp initialization

BOOL CMpsetupApp::InitInstance()
{
	// 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.

	SetDialogBkColor();        // Set dialog background color to gray
	LoadStdProfileSettings();  // 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(CMpsetupDoc),
//		RUNTIME_CLASS(CMainFrame),     // main SDI frame window
//		RUNTIME_CLASS(CMpsetupView));
//	AddDocTemplate(pDocTemplate);

	// create a new (empty) document
//	OnFileNew();

	if (m_lpCmdLine[0] != '\0')
	{
		// TODO: add command line processing here
	}           
	CSetupDlg setupDlg;
	setupDlg.DoModal();


	return TRUE;
}

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

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

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

// Implementation
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//{{AFX_MSG(CAboutDlg)
		// No message handlers
	//}}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)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CMpsetupApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CMpsetupApp commands
/////////////////////////////////////////////////////////////////////////////
// CSetupDlg dialog


CSetupDlg::CSetupDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSetupDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetupDlg)
	//}}AFX_DATA_INIT
//    GetPrivateProfileString("Comm","Type","MicrotekParallel",m_CommType,
//			     sizeof(m_CommType),IniFile); 
    GetPrivateProfileString("Comm","Type","Serial",m_CommType,
			     sizeof(m_CommType),IniFile); 
	if (strcmpi(m_CommType,"serial") == 0) {
       GetPrivateProfileString("serial","comPort","COM2",m_Port,
 			     sizeof(m_Port),IniFile); 
	} else if (strcmpi(m_CommType,"printer") == 0) {
       GetPrivateProfileString("printer","lptPort","lpt1",m_Port,
 			     sizeof(m_Port),IniFile); 
	} else {
	   strcpy(m_Port,m_CommType);
	}
    GetPrivateProfileString("SystemInfo","MicePack","68306",m_MICEpack,
				 sizeof(m_MICEpack),IniFile);
    GetPrivateProfileString("SystemInfo","EPClock","20",m_EPClock,
				 sizeof(m_EPClock),IniFile);
//	GetPrivateProfileString("MicrotekParallel","BasePort","0x200",m_BasePort,
//			     sizeof(m_BasePort),IniFile);                         
	GetPrivateProfileString("Serial","BaudRate","57600",m_BaudRate,
			     sizeof(m_BaudRate),IniFile);                         
}                          

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

BEGIN_MESSAGE_MAP(CSetupDlg, CDialog)
	//{{AFX_MSG_MAP(CSetupDlg)
	ON_BN_CLICKED(IDC_HELP, OnHelp)
	ON_CBN_SELCHANGE(IDC_COMMTYPE, OnSelchangeCommtype)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CSetupDlg message handlers

void CSetupDlg::OnHelp()
{
	::WinHelp(m_hWnd, "mpsetup.hlp", HELP_CONTENTS,0);
}

void CSetupDlg::OnOK()
{
    GetDlgItemText(IDC_MICEPACK, (LPSTR)m_MICEpack, 32);
    GetDlgItemText(IDC_EPCLOCK, (LPSTR)m_EPClock, 32);
    GetDlgItemText(IDC_COMMTYPE, (LPSTR)m_Port, 32);
    GetDlgItemText(IDC_BAUDRATE, (LPSTR)m_BaudRate, 32); 
    WritePrivateProfileString("SystemInfo","MICEpack",m_MICEpack,IniFile);
    WritePrivateProfileString("SystemInfo","EPClock",m_EPClock,IniFile);
    WritePrivateProfileString("Comm","Type","Serial",IniFile);    
    WritePrivateProfileString("Serial","ComPort",m_Port,IniFile);    
    WritePrivateProfileString("Serial","BaudRate",m_BaudRate,IniFile);    
//    GetDlgItemText(IDC_BASEPORT, (LPSTR)m_BasePort, 32); 
//    if (strnicmp(m_Port,"COM",3) == 0) {
//       GetDlgItemText(IDC_BAUDRATE, (LPSTR)m_BaudRate, 32); 
//	   WritePrivateProfileString("Comm","Type","Serial",IniFile);    
//	   WritePrivateProfileString("Serial","ComPort",m_Port,IniFile);    
//  	   WritePrivateProfileString("Serial","BaudRate",m_BaudRate,IniFile);    
//	} else if (strnicmp(m_Port,"LPT",3) == 0) {
//	   WritePrivateProfileString("Comm","Type","Printer",IniFile);    
//	   WritePrivateProfileString("Printer","LptPort",m_Port,IniFile);  
//	} else {
//	   WritePrivateProfileString("Comm","Type",m_Port,IniFile);    
//	}
//    WritePrivateProfileString("SystemInfo","MICEpack",m_MICEpack,IniFile);
//    WritePrivateProfileString("SystemInfo","EPClock",m_EPClock,IniFile);
//	WritePrivateProfileString("MicrotekParallel","basePort",m_BasePort,IniFile);
	
	CDialog::OnOK();
}

BOOL CSetupDlg::OnInitDialog()
{   int initCommType;

	CDialog::OnInitDialog();
	
	::SendDlgItemMessage(m_hWnd,IDC_MICEPACK,CB_SELECTSTRING,NULL,(LPARAM)m_MICEpack);
	::SendDlgItemMessage(m_hWnd,IDC_COMMTYPE,CB_SELECTSTRING,NULL,(LPARAM)m_Port);
//    SetDlgItemText(IDC_BASEPORT, m_BasePort);                     
    SetDlgItemText(IDC_EPCLOCK, m_EPClock);                     
    initCommType = (int)::SendDlgItemMessage(m_hWnd,IDC_COMMTYPE,CB_GETCURSEL,0,0);
//  added for RS232
    ::SendDlgItemMessage(m_hWnd,IDC_BAUDRATE,CB_SELECTSTRING,NULL,(LPARAM)m_BaudRate);
//	if (initCommType != 0) {
//	   ::EnableWindow(GetDlgItem(IDC_STRPORT)->m_hWnd, FALSE);
//	   ::EnableWindow(GetDlgItem(IDC_BASEPORT)->m_hWnd, FALSE);
//	   if ((initCommType >= 1) && (initCommType <= 3)) {
//          ::EnableWindow(GetDlgItem(IDC_STRBAUD)->m_hWnd, FALSE);
//	      ::SendDlgItemMessage(m_hWnd,IDC_BAUDRATE,CB_SETCURSEL,-1,0);
//	      ::EnableWindow(GetDlgItem(IDC_BAUDRATE)->m_hWnd, FALSE);
//	   } else {
//          ::SendDlgItemMessage(m_hWnd,IDC_BAUDRATE,CB_SELECTSTRING,-1,(LPARAM)m_BaudRate);
//	   }
//	} else {
//       ::EnableWindow(GetDlgItem(IDC_STRBAUD)->m_hWnd, FALSE);
//       ::EnableWindow(GetDlgItem(IDC_BAUDRATE)->m_hWnd, FALSE);
//	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSetupDlg::OnSelchangeCommtype()
{
}
