/*	Copyright (C) G. Jespersen
	See Licence.txt.*/

// RadioBrowseUpdateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "dslavsplit.h"
#include "RadioBrowseUpdateDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CRadioBrowseUpdateDlg dialog


CRadioBrowseUpdateDlg::CRadioBrowseUpdateDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRadioBrowseUpdateDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRadioBrowseUpdateDlg)
	m_strStatus = _T("");
	//}}AFX_DATA_INIT
	m_pDoc = NULL;
}


void CRadioBrowseUpdateDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRadioBrowseUpdateDlg)
	DDX_Control(pDX, IDC_RB_UPDATE_BTN, m_ctrlUpdateBtn);
	DDX_Control(pDX, IDCANCEL, m_ctrlCanceBtn);
	DDX_Control(pDX, IDOK, m_ctrlOkBtn);
	DDX_Control(pDX, IDC_RB_STATUS_EDIT, m_ctrlStatusEdit);
	DDX_Control(pDX, IDC_RB_PROGRESS, m_ctrlProgress);
	DDX_Text(pDX, IDC_RB_STATUS_EDIT, m_strStatus);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CRadioBrowseUpdateDlg, CDialog)
	//{{AFX_MSG_MAP(CRadioBrowseUpdateDlg)
	ON_BN_CLICKED(IDC_RB_UPDATE_BTN, OnUpdateRadioBrowse)
	//}}AFX_MSG_MAP
	ON_MESSAGE (WM_USER_THREAD_PROGRESS_POS,	OnProgressPos)
	ON_MESSAGE (WM_USER_THREAD_PROGRESS_MAX,	OnProgressMax)
	ON_MESSAGE (WM_USER_THREAD_STATUS_STRING,	OnStatusString)
	ON_MESSAGE (WM_USER_THREAD_DONE,			OnUpdateDone)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRadioBrowseUpdateDlg message handlers

// Update button pressed.
void CRadioBrowseUpdateDlg::OnUpdateRadioBrowse() 
{
	if(!m_pDoc)
		return;
	// Start the update thread.
	m_pDoc->UpdateRadioBrowse(GetSafeHwnd());

	// Disable the update button.
	m_ctrlUpdateBtn.EnableWindow(FALSE);
	// Disable the OK (close) button.
	m_ctrlOkBtn.EnableWindow(FALSE);
	// Enable the Cancel button.
	m_ctrlCanceBtn.EnableWindow(TRUE);	
}

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

	// m_strSever is initialized in CDsLavDoc::OnToolsUpdateRb().
	m_strStatus.Format(_T("Press the Update button to begin.\r\n")
					   _T("Updates are obtained from:\r\n")
					   _T("  %s."), m_strSever);

	// 7/22 adjust progress ctrl range - using RB_UPDATE_PROG_STEPS
	// Note set Smooth style checked for more divisions.
	// See DsLavDoc.h.
	m_ctrlProgress.SetRange32(0, RB_UPDATE_PROG_STEPS);
	m_ctrlProgress.SetPos(0);

	// On start up, cancel is disabled.
	m_ctrlCanceBtn.EnableWindow(FALSE);

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

// Cancel pressed.
// Note escape key also calls here.
void CRadioBrowseUpdateDlg::OnCancel() 
{
	if(m_pDoc)
	{
		TRACE(_T("CRadioBrowseUpdateDlg::OnCancel cancelling update.\n"));
		// Update status msg
		m_strStatus += _T("\r\n");
		m_strStatus += _T("Attempting to cancel update.");
		UpdateData(FALSE);
		m_ctrlStatusEdit.LineScroll(m_ctrlStatusEdit.GetLineCount());
		m_pDoc->CancelUpdate();
	}	
	// No default handling - will close dialog.
	//CDialog::OnCancel();
}

// Ok pressed.void CRadioBrowseUpdateDlg::OnOK() 
void CRadioBrowseUpdateDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

//////////////////////////////////////////////////////////////////////////
// Functions called with Dialog Handle from worker thread.
//
// Update finished, failed or canceled.
LRESULT CRadioBrowseUpdateDlg::OnUpdateDone(WPARAM wParam, LPARAM lParam) // WM_USER_THREAD_DONE
{
	TRACE(_T("Entering CRadioBrowseUpdateDlg::OnUpdateDone"));

	// Thread is complete - update buttons.
	//
	// Enable the update button.
	m_ctrlUpdateBtn.EnableWindow(TRUE);

	// Enable the OK (close) button.
	m_ctrlOkBtn.EnableWindow(TRUE);

	// Disable the Cancel button.
	m_ctrlCanceBtn.EnableWindow(FALSE);	

    return 0;
}

// Progress ctrl position.
LRESULT CRadioBrowseUpdateDlg::OnProgressPos(WPARAM wParam, LPARAM lParam) // WM_USER_THREAD_PROGRESS_POS
{
	TRACE(_T("CRadioBrowseUpdateDlg OnProgressPos Pos = %d\n"), (int)lParam);

	m_ctrlProgress.SetPos((int)lParam);
    return 0;
}

// Progress ctrl maximum.
LRESULT CRadioBrowseUpdateDlg::OnProgressMax(WPARAM wParam, LPARAM lParam) // WM_USER_THREAD_PROGRESS_MAX
{
	TRACE(_T("CRadioBrowseUpdateDlg OnProgressMax Max = %d\n"), (int)lParam);

	m_ctrlProgress.SetRange32(0, (int)lParam);
    return 0;
}

// Status string.
LRESULT CRadioBrowseUpdateDlg::OnStatusString(WPARAM wParam, LPARAM lParam)	// WM_USER_STATUS_STRING
{
	// Get message string from CApp.
	CString str;
	if(	((CDsLavApp*)AfxGetApp())->GetThreadMessage(str) )
	{
		// Append to current.
		m_strStatus += _T("\r\n");
		m_strStatus += str;
		UpdateData(FALSE);
		m_ctrlStatusEdit.LineScroll(m_ctrlStatusEdit.GetLineCount());
	}
    return 0;
}





