-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathMemoryViewerControlBinding.cpp
More file actions
353 lines (282 loc) · 10.2 KB
/
Copy pathMemoryViewerControlBinding.cpp
File metadata and controls
353 lines (282 loc) · 10.2 KB
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
#include "MemoryViewerControlBinding.hh"
#include "ra_fwd.h"
#include "ra_utility.h"
#include "ui/EditorTheme.hh"
#include "ui/drawing/gdi/GDISurface.hh"
namespace ra {
namespace ui {
namespace win32 {
namespace bindings {
constexpr int MEMVIEW_MARGIN = 4;
constexpr UINT WM_USER_INVALIDATE = WM_USER + 1;
INT_PTR CALLBACK MemoryViewerControlBinding::WndProc(HWND hControl, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
RenderMemViewer();
return 0;
case WM_ERASEBKGND:
// we'll repaint the entire control area in WM_PAINT, so don't need to clear first.
return TRUE;
case WM_MOUSEWHEEL:
if (GET_WHEEL_DELTA_WPARAM(wParam) > 0)
ScrollUp();
else if (GET_WHEEL_DELTA_WPARAM(wParam) < 0)
ScrollDown();
return FALSE;
case WM_LBUTTONUP:
OnClick({ GET_X_LPARAM(lParam) - MEMVIEW_MARGIN, GET_Y_LPARAM(lParam) - MEMVIEW_MARGIN });
return FALSE;
case WM_KEYDOWN:
return (!OnKeyDown(static_cast<UINT>(LOWORD(wParam))));
case WM_CHAR:
return (!OnEditInput(static_cast<UINT>(LOWORD(wParam))));
case WM_SETFOCUS:
OnGotFocus();
return FALSE;
case WM_KILLFOCUS:
OnLostFocus();
return FALSE;
case WM_GETDLGCODE:
return DLGC_WANTCHARS | DLGC_WANTARROWS;
case WM_USER_INVALIDATE:
Invalidate();
return FALSE;
case WM_XBUTTONUP:
if (GET_XBUTTON_WPARAM(wParam) == XBUTTON1)
m_pViewModel.MoveHistoryBackward();
else if (GET_XBUTTON_WPARAM(wParam) == XBUTTON2)
m_pViewModel.MoveHistoryForward();
return FALSE;
}
return ControlBinding::WndProc(hControl, uMsg, wParam, lParam);
}
void MemoryViewerControlBinding::RegisterControlClass() noexcept
{
static bool bClassRegistered = false;
if (!bClassRegistered)
{
WNDCLASSEX wc{ sizeof(WNDCLASSEX) };
wc.style = ra::to_unsigned(CS_PARENTDC | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS);
wc.lpfnWndProc = DefWindowProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = GetStockBrush(WHITE_BRUSH);
wc.lpszClassName = TEXT("MemoryViewerControl");
if (!RegisterClassEx(&wc))
{
// if the class already exists, assume the DLL was shutdown and restarted. Unregister the old one and reregister the new one
bool bSuccess = false;
if (GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
{
if (UnregisterClass(wc.lpszClassName, wc.hInstance))
{
if (RegisterClassEx(&wc))
bSuccess = true;
}
}
if (!bSuccess)
{
MessageBox(nullptr, TEXT("Failed to register memory viewer control"), TEXT("Error"), MB_OK);
return;
}
}
bClassRegistered = true;
}
}
void MemoryViewerControlBinding::SetHWND(DialogBase& pDialog, HWND hControl)
{
ControlBinding::SetHWND(pDialog, hControl);
SubclassWndProc();
RECT rcRect;
GetWindowRect(hControl, &rcRect);
const ra::ui::Size szSize{ rcRect.right - rcRect.left, rcRect.bottom - rcRect.top };
OnSizeChanged(szSize);
}
void MemoryViewerControlBinding::ScrollUp()
{
if (m_pViewModel.IsAddressFixed())
return;
m_pViewModel.SetFirstAddress(m_pViewModel.GetFirstAddress() - 32);
Invalidate();
}
void MemoryViewerControlBinding::ScrollDown()
{
if (m_pViewModel.IsAddressFixed())
return;
m_pViewModel.SetFirstAddress(m_pViewModel.GetFirstAddress() + 32);
Invalidate();
}
bool MemoryViewerControlBinding::OnKeyDown(UINT nChar)
{
bool bHandled = false;
// multiple properties may change while navigating, we'll do a single Invalidate after we're done
m_bSuppressMemoryViewerInvalidate = true;
if (!m_pViewModel.IsAddressFixed())
bHandled = HandleNavigation(nChar);
m_bSuppressMemoryViewerInvalidate = false;
if (bHandled)
Invalidate();
return bHandled;
}
bool MemoryViewerControlBinding::HandleNavigation(UINT nChar)
{
const bool bShiftHeld = (GetKeyState(VK_SHIFT) < 0);
const bool bControlHeld = (GetKeyState(VK_CONTROL) < 0);
switch (nChar)
{
case VK_RIGHT:
if (bShiftHeld || bControlHeld)
m_pViewModel.AdvanceCursorWord();
else
m_pViewModel.AdvanceCursor();
return true;
case VK_LEFT:
if (bShiftHeld || bControlHeld)
m_pViewModel.RetreatCursorWord();
else
m_pViewModel.RetreatCursor();
return true;
case VK_DOWN:
if (bControlHeld)
m_pViewModel.SetFirstAddress(m_pViewModel.GetFirstAddress() + 0x10);
else
m_pViewModel.AdvanceCursorLine();
return true;
case VK_UP:
if (bControlHeld)
m_pViewModel.SetFirstAddress(m_pViewModel.GetFirstAddress() - 0x10);
else
m_pViewModel.RetreatCursorLine();
return true;
case VK_PRIOR: // Page up (!)
m_pViewModel.RetreatCursorPage();
return true;
case VK_NEXT: // Page down (!)
m_pViewModel.AdvanceCursorPage();
return true;
case VK_HOME:
if (bControlHeld)
{
m_pViewModel.SetFirstAddress(0);
m_pViewModel.SetAddress(0);
}
else
{
m_pViewModel.SetAddress(m_pViewModel.GetAddress() & ~0x0F);
}
return true;
case VK_END:
if (bControlHeld)
{
const auto& pEmulatorContext = ra::services::ServiceLocator::Get<ra::data::context::EmulatorContext>();
const auto nTotalBytes = gsl::narrow<ra::ByteAddress>(pEmulatorContext.TotalMemorySize());
m_pViewModel.SetFirstAddress(nTotalBytes & ~0x0F);
m_pViewModel.SetAddress(nTotalBytes - 1);
}
else
{
switch (m_pViewModel.GetSize())
{
case MemSize::ThirtyTwoBit:
m_pViewModel.SetAddress((m_pViewModel.GetAddress() & ~0x0F) | 0x0C);
break;
case MemSize::SixteenBit:
m_pViewModel.SetAddress((m_pViewModel.GetAddress() & ~0x0F) | 0x0E);
break;
default:
m_pViewModel.SetAddress(m_pViewModel.GetAddress() | 0x0F);
break;
}
}
return true;
default:
return false;
}
}
bool MemoryViewerControlBinding::OnEditInput(UINT c)
{
// multiple properties may change while typing, we'll do a single Invalidate after we're done
m_bSuppressMemoryViewerInvalidate = true;
const bool bResult = m_pViewModel.OnChar(gsl::narrow_cast<char>(c));
m_bSuppressMemoryViewerInvalidate = false;
if (bResult)
Invalidate();
return bResult;
}
void MemoryViewerControlBinding::OnClick(POINT point)
{
if (m_pViewModel.IsAddressFixed())
return;
// multiple properties may change while typing, we'll do a single Invalidate after we're done
m_bSuppressMemoryViewerInvalidate = true;
m_pViewModel.OnClick(point.x, point.y);
m_bSuppressMemoryViewerInvalidate = false;
SetFocus(m_hWnd);
Invalidate();
}
void MemoryViewerControlBinding::OnGotFocus()
{
m_pViewModel.OnGotFocus();
}
void MemoryViewerControlBinding::OnLostFocus()
{
m_pViewModel.OnLostFocus();
}
void MemoryViewerControlBinding::OnSizeChanged(const ra::ui::Size& pNewSize)
{
m_pViewModel.OnResized(pNewSize.Width - MEMVIEW_MARGIN * 2, pNewSize.Height - MEMVIEW_MARGIN * 2);
}
void MemoryViewerControlBinding::OnViewModelIntValueChanged(const IntModelProperty::ChangeArgs& args) noexcept
{
if (args.Property == ra::ui::viewmodels::MemoryViewerViewModel::AddressProperty ||
args.Property == ra::ui::viewmodels::MemoryViewerViewModel::SizeProperty)
{
// these properties affect the rendered image, immediately invalidate in case the emulator
// is paused - in which case, the update from DoFrame will not occur
PostMessage(m_hWnd, WM_USER_INVALIDATE, 0, 0);
}
}
void MemoryViewerControlBinding::Invalidate()
{
if (m_pViewModel.NeedsRedraw() && !m_bSuppressMemoryViewerInvalidate)
ControlBinding::ForceRepaint(m_hWnd);
}
void MemoryViewerControlBinding::RenderMemViewer()
{
m_pViewModel.UpdateRenderImage();
PAINTSTRUCT ps;
HDC hDC = BeginPaint(m_hWnd, &ps);
RECT rcClient;
GetClientRect(m_hWnd, &rcClient);
const auto& pRenderImage = m_pViewModel.GetRenderImage();
const auto& pEditorTheme = ra::services::ServiceLocator::Get<ra::ui::EditorTheme>();
HBRUSH hBackground = CreateSolidBrush(RGB(pEditorTheme.ColorBackground().Channel.R, pEditorTheme.ColorBackground().Channel.G, pEditorTheme.ColorBackground().Channel.B));
// left margin
RECT rcFill{ rcClient.left, rcClient.top, rcClient.left + MEMVIEW_MARGIN, rcClient.bottom - 1 };
FillRect(hDC, &rcFill, hBackground);
// right margin
rcFill.left = rcClient.left + MEMVIEW_MARGIN + pRenderImage.GetWidth();
rcFill.right = rcClient.right - 1;
FillRect(hDC, &rcFill, hBackground);
// top margin
rcFill.left = rcClient.left;
rcFill.bottom = rcClient.top + MEMVIEW_MARGIN;
FillRect(hDC, &rcFill, hBackground);
// bottom margin
rcFill.top = rcClient.top + MEMVIEW_MARGIN + pRenderImage.GetHeight();
rcFill.bottom = rcClient.bottom - 1;
FillRect(hDC, &rcFill, hBackground);
// frame
FrameRect(hDC, &rcClient, GetSysColorBrush(COLOR_3DSHADOW));
// content
ra::ui::drawing::gdi::GDISurface pSurface(hDC, rcClient);
pSurface.DrawSurface(MEMVIEW_MARGIN, MEMVIEW_MARGIN, pRenderImage);
DeleteObject(hBackground);
EndPaint(m_hWnd, &ps);
}
} // namespace bindings
} // namespace win32
} // namespace ui
} // namespace ra