-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathStatusDlg.cpp
405 lines (340 loc) · 10.3 KB
/
StatusDlg.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// StatusDlg.cpp : implementation file
//
// Allows the user to peep inside the program by allowing
// the programmer to end data to its screen.
// It also will log the data to a file.
//
// (c) 1991-99 Andy Whittaker, Chester, England.
//
#include "StatusDlg.h"
#include "cderr.h" // for CommDlgExtendedError()
#include "strsafe.h"
#include "FreeScan.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStatusDlg dialog
CStatusDlg::CStatusDlg(CWnd* pParent /*=NULL*/)
: CDialog(CStatusDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CStatusDlg)
//}}AFX_DATA_INIT
//Load hide status
CWinApp* pApp = AfxGetApp();
m_hidden = pApp->GetProfileInt(_T("StatusDlg"), _T("Hide Status"), FALSE);
m_csLogFile = pApp->GetProfileString(_T("StatusDlg"), _T("Log Filename"), _T(""));
m_WindowPos.left = 0;
m_WindowPos.right = 0;
m_WindowPos.top = 0;
m_WindowPos.bottom = 0;
}
CStatusDlg::~CStatusDlg()
{
if (m_file.m_hFile != CFile::hFileNull)
{ // close our log file if it's open
m_file.Flush();
m_file.Close(); // close the logging file when we exit.
}
//Save hide status
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileInt(_T("StatusDlg"), _T("Hide Status"), m_hidden);
pApp->WriteProfileString(_T("StatusDlg"), _T("Log Filename"), m_csLogFile);
StartLog(FALSE);// close the logging file when we exit.
}
void CStatusDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStatusDlg)
DDX_Control(pDX, IDC_TIME, m_Time);
DDX_Control(pDX, IDC_STATUS, m_Status);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStatusDlg, CDialog)
//{{AFX_MSG_MAP(CStatusDlg)
ON_WM_MOVE()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStatusDlg access functions
//WriteStatus enters text into the list box at the end of the list
void CStatusDlg::WriteStatus(CString csText)
{ //CListBox
if (m_hidden) // don't write to window when hidden
return;
// TRACE_T(("%s\n"), csText);
if (IsWindow(m_hWnd))
{
PumpMessages();
if(m_Status.GetCount() == 100) // Maximum number of strings we want
{
// TRACE("Listbox > 100 items, deleting first string\n");
m_Status.DeleteString(0);
}
m_Status.AddString(csText); // Add the new string in again.
SendDlgItemMessage(IDC_STATUS, WM_VSCROLL , MAKEWPARAM(SB_LINEDOWN, NULL), NULL );
}
if (m_file.m_pStream != NULL) // i.e. we have a file open
{
csText = csText + _T("\n"); // Line Feed while we log to disk
m_file.WriteString(csText);
}
if (IsWindow(m_hWnd))
UpdateWindow();
}
//WriteStatus enters text into the list box at the end of the list with timestamp
void CStatusDlg::WriteStatusTimeLogged(CString csText)
{
if (m_hidden) // don't write to window when hidden
return;
CString csBuf;
m_now = CTime::GetCurrentTime();
csBuf = m_now.Format("%d %b %y %H:%M:%S - ");
if (IsWindow(m_Time.m_hWnd))
m_Time.SetWindowText(csBuf); // Updates the time string
csBuf = csBuf + csText;
WriteStatus(csBuf);
}
//Writes the contents of the given buffer as a Hex-dump
void CStatusDlg::WriteASCII(const unsigned char* const buffer, int ilength)
{
if (m_hidden) // don't write to window when hidden
return;
CString cs;
int iIndex;
// Hmmm, I'm sure there's a better way of doing this.
for(iIndex=0; iIndex < ilength; iIndex++)
{
CString csTemp;
csTemp.Format(_T("0x%02X "), buffer[iIndex]);
cs = cs + csTemp; // concatenate string
}
// Now write the string.
WriteStatusTimeLogged(cs);
}
//Writes a formatted log entry to the log file
// e.g. WriteLogEntry("Program started");
// WriteLogEntry("Processed %d bytes", 91341);
// WriteLogEntry("%d error(s) found in %d line(s)", 10, 1351);
// WriteLogEntry("Program completed");
void CStatusDlg::WriteLogEntry(LPCTSTR pstrFormat, ...)
{
if (m_hidden) // don't write to window when hidden
return;
if (m_file.m_hFile == CFile::hFileNull)
{ // No log-file open
return;
}
CTime timeWrite;
timeWrite = CTime::GetCurrentTime();
// write the time out
CString str = timeWrite.Format("%d %b %y %H:%M:%S - ");
m_file.Write(str, str.GetLength());
// format and write the data we were given
va_list args;
va_start(args, pstrFormat);
str.FormatV(pstrFormat, args);
m_file.Write(str, str.GetLength());
// put a newline
m_file.Write("\n", 1);
return;
}
// Starts or stops logging to file
BOOL CStatusDlg::StartLog(BOOL bStart)
{
CString csBuf = _T("");
if (!bStart)
{ // we want to close the logging file
if (m_file.m_hFile != CFile::hFileNull)
{
WriteStatusTimeLogged(_T("Log file stopped"));
m_file.Close(); // close the logging file when we exit.
}
else
WriteStatusTimeLogged(_T("Log file is already closed"));
return FALSE;
}
// We now must want to log to a file
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(ofn)); // initialize structure to 0/NULL
TCHAR szFileName[_MAX_PATH] = { 0 }; // contains full path name after return
TCHAR szFileTitle[_MAX_PATH] = { 0 };
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = szFileName;
ofn.nMaxFile = _countof(szFileName);
ofn.lpstrFileTitle = (LPTSTR)szFileTitle;
ofn.nMaxFileTitle = _countof(szFileTitle);
ofn.lpstrTitle = _T("Create/Open Logging File");
ofn.lpstrFilter = _T("log Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0\0");
ofn.lpstrInitialDir = m_csLogFile.GetString();
ofn.lpstrDefExt = _T("txt");
// setup initial file name from the registry
//lstrcpyn(szFileName, m_csLogFile, _countof(szFileName));//unsafe
StringCchCopy(szFileName, _countof(szFileName), m_csLogFile.GetString());
ofn.Flags = OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
int nResult = ::GetSaveFileName(&ofn);
if (nResult == 0) // Handle the error
{
DWORD dwerror = CommDlgExtendedError();
switch (dwerror)
{
case 0 : // no error
csBuf.Format(_T("User cancelled/closed file Dialog"));
break; // cancel or closed was pressed
case FNERR_BUFFERTOOSMALL :
csBuf.Format(_T("Buffer too small"));
AfxMessageBox(csBuf, MB_OK | MB_ICONSTOP );
break;
case FNERR_INVALIDFILENAME :
csBuf.Format(_T("Invalid file name"));
AfxMessageBox(csBuf, MB_OK | MB_ICONSTOP );
break;
case CDERR_MEMALLOCFAILURE :
csBuf.Format(_T("Memory allocation failure"));
AfxMessageBox(csBuf, MB_OK | MB_ICONSTOP );
break;
case CDERR_MEMLOCKFAILURE :
csBuf.Format(_T("Memory lock failure"));
AfxMessageBox(csBuf, MB_OK | MB_ICONSTOP );
break;
default:
csBuf.Format(_T("An unknown error has occured - %ld"), dwerror);
AfxMessageBox(csBuf, MB_OK | MB_ICONSTOP );
}
WriteStatus(csBuf);
return FALSE;
}
// Copy filename from our dialog to the CString member variable
m_csLogFile.Format(_T("%s"),ofn.lpstrFile);
if (!m_file.Open(m_csLogFile, CFile::modeCreate | CFile::modeReadWrite | CFile::typeText))
{
csBuf.Format(_T("Cannot open %s"), m_csLogFile.GetString());
WriteStatus(csBuf);
AfxMessageBox(csBuf, MB_OK | MB_ICONSTOP );
return FALSE;
}
WriteStatusTimeLogged(_T("Log file has started"));
/*
// Construct our File Dialog
CFileDialog Dialog(FALSE, "txt",
m_csLogFile,
OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT,
"log Files (*.txt)|*.txt|All Files (*.*)|*.*||", NULL);
// Change the title
Dialog.m_ofn.lpstrTitle = "Create/Open Logging File";
// Display the dialog box
if (Dialog.DoModal() == IDOK)
{
m_csLogFile = Dialog.GetPathName();
if (!m_file.Open(m_csLogFile, CFile::modeCreate | CFile::modeReadWrite | CFile::typeText))
{
csBuf.Format("Cannot open %s", m_csLogFile);
WriteStatus(csBuf);
AfxMessageBox(csBuf, MB_OK | MB_ICONSTOP );
return FALSE;
}
WriteStatusLogged("Log file has started");
}
else // User pressed cancel
WriteStatus("User cancelled log file");
*/
if (m_file.m_hFile != CFile::hFileNull)
return TRUE;
else
return FALSE;
}
// Hides the status window from view
void CStatusDlg::Hide(BOOL yes)
{
if (yes)
{
m_hidden = TRUE;
if (::IsWindow(m_hWnd))
ShowWindow(SW_HIDE);
}
else
{
m_hidden = FALSE;
if (::IsWindow(m_hWnd))
ShowWindow(SW_SHOW);
}
}
// Hide status of window
BOOL CStatusDlg::IsHidden(void)
{
return m_hidden;
}
// Ensures window receives messages and updates correctly
void CStatusDlg::PumpMessages(void)
{
// Must call Create() before using the dialog
ASSERT(m_hWnd!=NULL);
MSG msg;
// Handle dialog messages
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(!IsDialogMessage(&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
// This is for debugging. Prints the current screen coordinates.
void CStatusDlg::PrintRect(LPRECT lpR)
{
CString csData;
csData.Format(_T("Rect Data:\nleft = %li\ntop = %li\nright = %li\nbottom = %li"),
lpR->left, lpR->top, lpR->right, lpR->bottom);
AfxMessageBox(csData, MB_OK | MB_ICONINFORMATION);
}
/////////////////////////////////////////////////////////////////////////////
// CStatusDlg message handlers
BOOL CStatusDlg::OnInitDialog()
{
//Retrieve the Window Position from the registry
CWinApp* pApp = AfxGetApp();
m_WindowPos.left = pApp->GetProfileInt(_T("StatusDlg"), _T("left pos"), 100);
m_WindowPos.top = pApp->GetProfileInt(_T("StatusDlg"), _T("top pos"), 100);
// Set the window position
SetWindowPos( &wndBottom, m_WindowPos.left, m_WindowPos.top, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);
BOOL bResult = CDialog::OnInitDialog();
// Sets the virtual size of the list box
m_Status.SetHorizontalExtent(2000);
// Allocates system memory to listbox to speed up memory allocations
m_Status.InitStorage(32000, 1048576);
if (m_hidden)
{
if (::IsWindow(m_hWnd))
ShowWindow(SW_HIDE);
}
else
{
if (::IsWindow(m_hWnd))
ShowWindow(SW_SHOW);
}
return bResult;
}
// This is called by the BaseDialog to create this Status Dialog
BOOL CStatusDlg::Create(CWnd* pParentWnd)
{
return CDialog::Create(IDD, pParentWnd);
}
// This tracks the window's position
void CStatusDlg::OnMove(int x, int y)
{
CDialog::OnMove(x, y);
GetWindowRect(&m_WindowPos);
}
void CStatusDlg::OnDestroy()
{
//Save the current window position
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileInt(_T("StatusDlg"), _T("left pos"), m_WindowPos.left);
pApp->WriteProfileInt(_T("StatusDlg"), _T("top pos"), m_WindowPos.top);
CDialog::OnDestroy();
}