forked from derandark/DungeonViewerAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugDialog.cpp
173 lines (132 loc) · 3.72 KB
/
DebugDialog.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
#include "StdAfx.h"
#include "DebugDialog.h"
#include "Preferences.h"
HWND DebugDialog::m_hWnd = 0;
void DebugDialog::Create(HWND hParent)
{
if (m_hWnd)
return;
// Create the window.
m_hWnd = CreateDialog(Globals::GetAppInstance(), (LPCTSTR)IDD_DEBUG, hParent, (DLGPROC)DlgProc);
if (!m_hWnd)
{
// Failure.
MessageBox(hParent, "Failed creating debug dialog!", "Error", MB_ICONHAND);
}
}
void DebugDialog::OutputDirect(const char* Text)
{
HWND hOutput = GetDlgItem(m_hWnd, IDC_OUTPUT);
if (hOutput)
{
DWORD SelStart, SelEnd;
DWORD TextLength = SendMessage(hOutput, WM_GETTEXTLENGTH, 0, 0);
SendMessage(hOutput, EM_GETSEL, (WPARAM)&SelStart, (LPARAM)&SelEnd);
SendMessage(hOutput, EM_SETSEL, TextLength, TextLength);
SendMessage(hOutput, EM_REPLACESEL, FALSE, (LPARAM)Text);
SendMessage(hOutput, EM_SETSEL, SelStart, SelEnd);
}
}
void DebugDialog::Output(const char* Format, ...)
{
if (!m_hWnd)
return;
va_list ArgPtr;
va_start(ArgPtr, Format);
int TempSize = _vscprintf(Format, ArgPtr) + 1;
char *TempBuffer = new char[ TempSize ];
int WriteCount = _vsnprintf(TempBuffer, TempSize, Format, ArgPtr);
if (WriteCount > 0)
OutputDirect(TempBuffer);
// Free temporary buffer.
delete [] TempBuffer;
va_end(ArgPtr);
}
void DebugDialog::Clear()
{
HWND hOutput = GetDlgItem(m_hWnd, IDC_OUTPUT);
if (hOutput)
{
SendMessage(hOutput, WM_SETTEXT, 0, (LPARAM)"");
}
}
void DebugDialog::SaveToFile()
{
// Ask where the file is to be saved to.
char szFile[ MAX_PATH ];
szFile[0] = '\0';
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = m_hWnd;
ofn.lpstrFilter = "Text File\0*.txt\0\0";
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrInitialDir = Preferences::GetApplicationPath();
ofn.lpstrTitle = "Save Debug File As..";
ofn.lpstrDefExt = "txt";
ofn.Flags = OFN_OVERWRITEPROMPT;
if (!GetSaveFileName(&ofn))
return;
HWND hOutput = GetDlgItem(m_hWnd, IDC_OUTPUT);
if (hOutput)
{
DWORD Length = (DWORD)SendMessage(hOutput, WM_GETTEXTLENGTH, 0, 0);
DWORD AllocLength = Length + 1;
char *TempBuffer = new char[ AllocLength ];
if (TempBuffer)
{
// For safety.
ZeroMemory(TempBuffer, AllocLength);
SendMessage(hOutput, WM_GETTEXT, (WPARAM)AllocLength, (LPARAM)TempBuffer);
FILE *fp = fopen(szFile, "wt");
if (fp)
{
fprintf(fp, "%s", TempBuffer);
fclose(fp);
}
else
MessageBox(m_hWnd, "Failed opening text file for output.", "Error", MB_ICONHAND);
delete [] TempBuffer;
}
}
}
INT_PTR DebugDialog::DlgProc(HWND hWnd, UINT MsgID, WPARAM wParam, LPARAM lParam)
{
switch(MsgID)
{
case WM_INITDIALOG:
{
m_hWnd = hWnd;
Output("Debug window opened.\r\n");
return TRUE;
}
case WM_CLOSE:
{
DestroyWindow(hWnd);
return TRUE;
}
case WM_DESTROY:
{
m_hWnd = 0;
return TRUE;
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDOK:
DestroyWindow(hWnd);
break;
case IDC_SAVETOFILE:
SaveToFile();
break;
case IDC_CLEAR:
Clear();
break;
}
return TRUE;
}
}
return FALSE;
}