summaryrefslogtreecommitdiff
path: root/NSCmpts/ProgressDlg.cpp
blob: 9f1ae5ca096e55132f4a7b7572b7ed3de0d76bdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// ProgressDlg.cpp : Implementation of CProgressDlg
#include "stdafx.h"
#include "ProgressDlg.h"

/////////////////////////////////////////////////////////////////////////////
// CProgressDlg

CActionProgressDlg::CActionProgressDlg(IUnknown* pSite, UINT nIDD) 
	: CDialogImplEx(nIDD)
{
	m_pUnk = pSite;
	m_bCancel = false;
}

CActionProgressDlg::~CActionProgressDlg() 
{
	if(IsWindow())
		DestroyWindow();

	// Release any pointers we're holding on to
	if(m_pUnk)
		m_pUnk->Release();
	if(m_pErrorLog)
		m_pErrorLog->Release();
}

LRESULT CActionProgressDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	// Used later
	m_ctlProgress = GetDlgItem(IDC_PROGRESS);

	CenterWindow(GetDesktopWindow());

	// This sets up the Event Mechanism
	if(m_pUnk != NULL)
		DispEventAdvise(m_pUnk);	// Pointer from IObjectWithSite

	return 1;  // Let the system set the focus
}

LRESULT CActionProgressDlg::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{

	// Release any pointers we're holding on to
	if(m_pUnk)
	{
		// Drop the Event Stuff
		DispEventUnadvise(m_pUnk);	// Pointer from IObjectWithSite

		m_pUnk->Release();
	}
	if(m_pErrorLog)
		m_pErrorLog->Release();

	return 0;
}

// Update Message from Action Engine
LRESULT CActionProgressDlg::OnUpdateFile(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	NS_UPDATE_DATA* pData = (NS_UPDATE_DATA*)lParam;
	
	// If there 0 in the total then clear
	if(pData->lTot == 0)
	{
		m_ctlProgress.SetRange(0, 1);
		m_ctlProgress.SetPos(0);
	}
	else
	{
		// If total != -1 then change range
		if(pData->lTot > 0)
			m_ctlProgress.SetRange(0, pData->lTot);
		
		// If Current changed (!= -1) then set state
		if(pData->lCur >= 0)
			m_ctlProgress.SetPos(pData->lCur);
	}

	// If there's a message display it
	if(pData->szMessage1)
		::SetWindowText(GetDlgItem(IDC_MESSAGE1), pData->szMessage1);
	if(pData->szMessage2)
		::SetWindowText(GetDlgItem(IDC_MESSAGE2), pData->szMessage2);

	// If m_bCancel is set then this cancels the CActionEngine
	return m_bCancel ? 0 : 1;
}

// Called in response to the button being pressed, window being closed, or
// event being fired
LRESULT CActionProgressDlg::OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	// This gets checked when the Engine sends and Update (OnUpdateFile)
	m_bCancel = true;

	// We're working on cancelling so reflect that in UI
	::SetWindowText(GetDlgItem(IDCANCEL), _T("Cancelling..."));
	::EnableWindow(GetDlgItem(IDCANCEL), FALSE);
	return 0;
}

// Called when event Fired
STDMETHODIMP CActionProgressDlg::OnCancelEv()
{ 
	// Just call normal cancel event
	BOOL bHandled = TRUE;
	OnCancel(0, 0, NULL, bHandled); 

	return S_OK; 
};

// Error Message from CActionEngine
LRESULT CActionProgressDlg::OnError(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	// See if we can get an INightSecErrorLog pointer that IObjectWithSite
	// gave us
	if(m_pErrorLog == NULL)
		m_pErrorLog = m_pUnk;

	if(m_pErrorLog == NULL)
		return 0;

	// Create a new NightSecError object
	CComObject<CActionErrorImpl>* pError = NULL;
	HRESULT hr = CComObject<CActionErrorImpl>::CreateInstance(&pError);

	if(FAILED(hr))
		return 0;

	// Set info on the Error Object
	NS_ERROR_DATA* pData = (NS_ERROR_DATA*)lParam;
	pError->SetError(pData->szDesc, pData->hRes);
	pError->Attach((CAction*)pData->lParam);

	// Send it to the INightSecErrorLog
	m_pErrorLog->AddError(pError->GetUnknown());

	return 0;
}

// Starts the AVI file playing from resources
bool CActionProgressDlg::PlayAmination(UINT nID)
{
	ASSERT(IsWindow());
	ASSERT(GetDlgItem(IDC_ANIMATION));

	// Dialog box should have AutoPlay enabled
	return ((BOOL)::SendMessage(GetDlgItem(IDC_ANIMATION), ACM_OPEN, 0, (LPARAM)MAKEINTRESOURCE(nID))) ? true : false;
}