// ModeSettingsPage.cpp : implementation file
//

#include "stdafx.h"
#include "dloader.h"
#include "ModeSettingsPage.h"

static _TCHAR g_sz_MODE[] = _T("Mode Settings");
static _TCHAR g_sz_CMD_TO_BOOT[] = _T("BootCmd");
static _TCHAR g_sz_WAIT_TIME_FOR_BOOT[] = _T("Wait Time for boot");
static _TCHAR g_sz_WAIT_TIME_FOR_CHIP[] = _T("Wait Time for chip");

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

/////////////////////////////////////////////////////////////////////////////
// CModeSettingsPage property page

IMPLEMENT_DYNCREATE(CModeSettingsPage, CPropertyPage)

CModeSettingsPage::CModeSettingsPage() : CPropertyPage(CModeSettingsPage::IDD)
{
	//{{AFX_DATA_INIT(CModeSettingsPage)
	m_strWaitTime = _T("");
	m_strWaitTimeForBoot = _T("");
	//}}AFX_DATA_INIT
	m_strIniFile = _T("");
	m_bCmdBoot = FALSE;
}

CModeSettingsPage::~CModeSettingsPage()
{
}

void CModeSettingsPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CModeSettingsPage)
	DDX_Control(pDX, IDC_CHECK_MANUAL_TO_BOOT, m_chkBootCmd);
	DDX_Text(pDX, IDC_WAIT_TIME_EDIT, m_strWaitTime);
	DDV_MaxChars(pDX, m_strWaitTime, 8);
	DDX_Text(pDX, IDC_WAIT_TIME_BOOT_EDIT, m_strWaitTimeForBoot);
	DDV_MaxChars(pDX, m_strWaitTimeForBoot, 8);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CModeSettingsPage, CPropertyPage)
	//{{AFX_MSG_MAP(CModeSettingsPage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CModeSettingsPage message handlers
BOOL CModeSettingsPage::LoadSettings(LPCTSTR pFileName)
{
	ASSERT(NULL != pFileName);
    if(NULL == pFileName)
    {
        return FALSE;
    }   
    m_strIniFile = pFileName;

    m_bCmdBoot = ::GetPrivateProfileInt(g_sz_MODE,g_sz_CMD_TO_BOOT,FALSE,pFileName);
    DWORD dwWaitTime = ::GetPrivateProfileInt( g_sz_MODE, g_sz_WAIT_TIME_FOR_CHIP, 0, pFileName );    
    m_strWaitTime.Format( _T("%d"), dwWaitTime );
    dwWaitTime = ::GetPrivateProfileInt( g_sz_MODE,g_sz_WAIT_TIME_FOR_BOOT, 5000, pFileName);
    m_strWaitTimeForBoot.Format( _T("%d"), dwWaitTime );
    return TRUE;
}

BOOL CModeSettingsPage::SaveSettings(LPCTSTR pFileName)
{
    CString strTemp;
    strTemp.Format(_T("%d"),m_bCmdBoot);
    ::WritePrivateProfileString(g_sz_MODE,g_sz_CMD_TO_BOOT,strTemp,pFileName);
    ::WritePrivateProfileString( g_sz_MODE, g_sz_WAIT_TIME_FOR_CHIP, m_strWaitTime, pFileName );
    ::WritePrivateProfileString( g_sz_MODE, g_sz_WAIT_TIME_FOR_BOOT, m_strWaitTimeForBoot, pFileName );
    
    return TRUE;
}

BOOL CModeSettingsPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
    m_chkBootCmd.SetCheck( m_bCmdBoot );
    
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
void CModeSettingsPage::OnOK() 
{
    m_bCmdBoot = m_chkBootCmd.GetCheck();
    GetDlgItem( IDC_WAIT_TIME_EDIT )->GetWindowText( m_strWaitTime );
    GetDlgItem( IDC_WAIT_TIME_BOOT_EDIT )->GetWindowText( m_strWaitTimeForBoot );
	//±£´æÉèÖÃ
	if(!m_strIniFile.IsEmpty())
	{
		DWORD dwAttr = GetFileAttributes(m_strIniFile);
		if(-1 != dwAttr)
		{
			dwAttr &= ~FILE_ATTRIBUTE_READONLY;
			::SetFileAttributes(m_strIniFile,dwAttr);
		}
		SaveSettings(m_strIniFile);
	}

	CPropertyPage::OnOK();
}
