PROGRESS.CPP

//----------------------------------------------------------------------------- 
// Microsoft OLE DB TABLECOPY Sample
// Copyright (C) 1996 By Microsoft Corporation.
//
// @doc
//
// @module PROGRESS.CPP
//
//-----------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////
// Includes
//
////////////////////////////////////////////////////////////////////
#include "progress.h"
#include "common.h"



////////////////////////////////////////////////////////////////////
// CProgress::CProgress
//
////////////////////////////////////////////////////////////////////
CProgress::CProgress(HWND hWnd, HINSTANCE hInst)
: CDialog(hWnd, hInst)
{
}

////////////////////////////////////////////////////////////////////
// CProgress::~CProgress
//
////////////////////////////////////////////////////////////////////
CProgress::~CProgress()
{
StopProgress();
}


////////////////////////////////////////////////////////////////////
// BOOL WINAPI CProgress::DlgProc
//
////////////////////////////////////////////////////////////////////
BOOL WINAPI CProgress::DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
{
//Save the this pointer, since this is a static method
CProgress* pThis = (CProgress*)lParam;
SetWindowLong(hWnd, GWL_USERDATA, (LONG)pThis);

//On INIT we know we have a valid hWnd to store
pThis->m_hWnd = hWnd;

CenterDialog(hWnd);
return TRUE;
}
break;

case WM_COMMAND:
{
// All buttons are handled the same way
//Restore instance pointer, since this is a static function
CProgress* pThis = (CProgress*)GetWindowLong(hWnd, GWL_USERDATA);

switch(GET_WM_COMMAND_ID(wParam, lParam))
{
case IDCANCEL:
pThis->m_fCancel = TRUE;
return TRUE;
}
return FALSE;
}
break;

default:
return FALSE;
}
}


////////////////////////////////////////////////////////////////////
// ULONG CProgress::Display
//
////////////////////////////////////////////////////////////////////
ULONG CProgress::Display()
{
m_fCancel = FALSE;

//Create a ModeLess Dialog Box
m_hWnd = CreateDialogParam(m_hInst, MAKEINTRESOURCE(IDD_PROGRESS), NULL, DlgProc, (LPARAM)this);
return TRUE;
}


////////////////////////////////////////////////////////////////////
// BOOL CProgress::StopProgress
//
////////////////////////////////////////////////////////////////////
BOOL CProgress::StopProgress()
{
if(m_hWnd)
{
DestroyWindow(m_hWnd);
m_hWnd = NULL;
}
return TRUE;
}


////////////////////////////////////////////////////////////////////
// BOOL CProgress::SetWorkingItem
//
////////////////////////////////////////////////////////////////////
BOOL CProgress::SetWorkingItem(WCHAR* pwszWorking)
{
ASSERT(pwszWorking);

wSetDlgItemText(m_hWnd, IDT_STATUS, pwszWorking);
return TRUE;
}


////////////////////////////////////////////////////////////////////
// BOOL CProgress::SetProgressText
//
////////////////////////////////////////////////////////////////////
BOOL CProgress::SetProgressText(WCHAR* pwszProgress)
{
ASSERT(pwszProgress);

wSetDlgItemText(m_hWnd, IDT_STAT_INFO, pwszProgress);
return TRUE;
}


////////////////////////////////////////////////////////////////////
// BOOL CProgress::Cancel
//
////////////////////////////////////////////////////////////////////
BOOL CProgress::Cancel()
{
MSGmsg;

//Must have an existing window to cancel
if(m_hWnd==NULL)
return FALSE;

while(PeekMessage(&msg, m_hWnd, 0, 0, PM_REMOVE))
{
if(!IsDialogMessage(m_hWnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return m_fCancel;
}


////////////////////////////////////////////////////////////////////
// BOOL CProgress::ShowCancel
//
////////////////////////////////////////////////////////////////////
BOOL CProgress::ShowCancel(BOOL fShow)
{
ShowWindow(GetDlgItem(m_hWnd, IDCANCEL), (fShow) ? SW_SHOW: SW_HIDE );
return TRUE;
}


////////////////////////////////////////////////////////////////////
// BOOL CProgress::RestartProgress
//
////////////////////////////////////////////////////////////////////
BOOL CProgress::RestartProgress()
{
m_fCancel = FALSE;
return TRUE;
}


////////////////////////////////////////////////////////////////////
// BOOL CProgress::UpdateProgress
//
////////////////////////////////////////////////////////////////////
BOOL CProgress::UpdateProgress(WCHAR* pwszItem, WCHAR* pwszText)
{
ASSERT(pwszItem && pwszText);

//Update the progress text
SetProgressText(pwszText);

// Now check for a cancel from the user, by checking
// the messages from the user
if(Cancel())
{
SetWorkingItem(pwszItem);
if(IDYES == wMessageBox(NULL, MB_ICONEXCLAMATION | MB_YESNO, wsz_CANCEL, wsz_CANCEL_OP))
{
//Indicate the users wishes to stop
return FALSE;
}
else
{
SetWorkingItem(pwszItem);
RestartProgress();
}
}

//Indicate the user wishes to continue
return TRUE;
}