MemoryView cleanup.

This commit is contained in:
Jean-Philip Desjardins 2014-06-29 22:45:24 -04:00
parent cd653ca0be
commit 31554a7c62
2 changed files with 71 additions and 100 deletions

View file

@ -4,6 +4,7 @@
#include "string_cast.h"
#include "win32/GdiObj.h"
#include "win32/ClientDeviceContext.h"
#include "win32/DefaultWndClass.h"
#include "lexical_cast_ex.h"
#define CLSNAME _T("CMemoryView")
@ -11,30 +12,11 @@
#define YMARGIN 5
#define YSPACE 0
using namespace std;
using namespace Framework;
CMemoryView::CMemoryView(HWND hParent, const RECT& rect)
CMemoryView::CMemoryView(HWND parentWnd, const RECT& rect)
{
if(!DoesWindowClassExist(CLSNAME))
{
WNDCLASSEX w;
memset(&w, 0, sizeof(WNDCLASSEX));
w.cbSize = sizeof(WNDCLASSEX);
w.lpfnWndProc = CWindow::WndProc;
w.lpszClassName = CLSNAME;
w.hbrBackground = NULL;
w.hInstance = GetModuleHandle(NULL);
w.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClassEx(&w);
}
Create(WS_EX_CLIENTEDGE, CLSNAME, _T(""), WS_VISIBLE | WS_VSCROLL | WS_CHILD, rect, hParent, NULL);
Create(WS_EX_CLIENTEDGE, Framework::Win32::CDefaultWndClass::GetName(), _T(""), WS_VISIBLE | WS_VSCROLL | WS_CHILD, rect, parentWnd, NULL);
SetClassPtr();
m_nSize = 0;
m_nSelectionStart = 0;
UpdateScrollRange();
}
@ -50,18 +32,16 @@ HFONT CMemoryView::GetFont()
void CMemoryView::ScrollToAddress(uint32 nAddress)
{
unsigned int nRow;
unsigned int nRows, nCols;
SCROLLINFO si;
unsigned int nRows = 0, nCols = 0;
GetVisibleRowsCols(&nRows, &nCols);
//Humm, nvm...
if(nCols == 0) return;
if(nAddress >= m_nSize) return;
if(nAddress >= m_size) return;
nRow = (nAddress / nCols);
unsigned int nRow = (nAddress / nCols);
SCROLLINFO si;
memset(&si, 0, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.nPos = nRow;
@ -73,27 +53,26 @@ void CMemoryView::ScrollToAddress(uint32 nAddress)
uint32 CMemoryView::GetSelection()
{
return m_nSelectionStart;
return m_selectionStart;
}
void CMemoryView::UpdateScrollRange()
{
unsigned int nTotalRows;
unsigned int nCols, nRows;
SCROLLINFO si;
unsigned int nRows = 0, nCols = 0;
GetVisibleRowsCols(&nRows, &nCols);
unsigned int nTotalRows = 0;
if(nCols != 0)
{
nTotalRows = (m_nSize / nCols);
if((m_nSize % nCols) != 0) nTotalRows++;
nTotalRows = (m_size / nCols);
if((m_size % nCols) != 0) nTotalRows++;
}
else
{
nTotalRows = 0;
}
SCROLLINFO si;
memset(&si, 0, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.nMin = 0;
@ -131,20 +110,19 @@ unsigned int CMemoryView::GetScrollThumbPosition()
void CMemoryView::GetVisibleRowsCols(unsigned int* pRows, unsigned int* pCols)
{
SIZE s;
unsigned int nRows, nCols;
SIZE s = {};
{
Win32::CClientDeviceContext DeviceContext(m_hWnd);
Win32::CFont Font(GetFont());
Framework::Win32::CClientDeviceContext deviceContext(m_hWnd);
Framework::Win32::CFont font(GetFont());
s = DeviceContext.GetFontSize(Font);
s = deviceContext.GetFontSize(font);
}
RECT rc = GetClientRect();
nRows = (rc.bottom - (YMARGIN * 2)) / (s.cy + YSPACE);
nCols = (rc.right - (2 * XMARGIN) - (5 * s.cx) - 17);
unsigned int nRows = (rc.bottom - (YMARGIN * 2)) / (s.cy + YSPACE);
unsigned int nCols = (rc.right - (2 * XMARGIN) - (5 * s.cx) - 17);
nCols /= (3 * (s.cx + 1));
if(nCols != 0)
{
@ -163,41 +141,41 @@ void CMemoryView::GetVisibleRowsCols(unsigned int* pRows, unsigned int* pCols)
void CMemoryView::Paint(HDC hDC)
{
unsigned int nLines, nX, nY, nBytes, nFixedBytes;
uint32 nAddress;
Win32::CDeviceContext DeviceContext(hDC);
Framework::Win32::CDeviceContext deviceContext(hDC);
RECT rwin = GetClientRect();
BitBlt(DeviceContext, 0, 0, rwin.right, rwin.bottom, NULL, 0, 0, WHITENESS);
BitBlt(deviceContext, 0, 0, rwin.right, rwin.bottom, NULL, 0, 0, WHITENESS);
Win32::CFont Font(GetFont());
DeviceContext.SelectObject(Font);
Framework::Win32::CFont font(GetFont());
deviceContext.SelectObject(font);
SIZE s = DeviceContext.GetFontSize(Font);
SIZE s = deviceContext.GetFontSize(font);
unsigned int nLines = 0, nFixedBytes = 0;
uint32 nAddress = 0;
GetRenderParams(s, nLines, nFixedBytes, nAddress);
nBytes = nFixedBytes;
nY = YMARGIN;
unsigned int nBytes = nFixedBytes;
unsigned int nY = YMARGIN;
for(unsigned int i = 0; i < nLines; i++)
{
if(nAddress >= m_nSize)
if(nAddress >= m_size)
{
break;
}
if((nAddress + nBytes) >= m_nSize)
if((nAddress + nBytes) >= m_size)
{
nBytes = (m_nSize - nAddress);
nBytes = (m_size - nAddress);
}
nX = XMARGIN;
DeviceContext.TextOut(nX, nY, lexical_cast_hex<tstring>(nAddress, 8).c_str());
unsigned int nX = XMARGIN;
deviceContext.TextOut(nX, nY, lexical_cast_hex<std::tstring>(nAddress, 8).c_str());
nX += (8 * s.cx) + 10;
for(unsigned int j = 0; j < nBytes; j++)
{
DeviceContext.TextOut(nX, nY, lexical_cast_hex<tstring>(GetByte(nAddress + j), 2).c_str());
deviceContext.TextOut(nX, nY, lexical_cast_hex<std::tstring>(GetByte(nAddress + j), 2).c_str());
nX += (2 * s.cx) + 3;
}
nX += (nFixedBytes - nBytes) * (2 * s.cx + 3);
@ -217,7 +195,7 @@ void CMemoryView::Paint(HDC hDC)
sValue[0] = nValue;
sValue[1] = 0x00;
DeviceContext.TextOut(nX, nY, string_cast<tstring>(sValue).c_str());
deviceContext.TextOut(nX, nY, string_cast<std::tstring>(sValue).c_str());
nX += s.cx;
}
@ -228,15 +206,14 @@ void CMemoryView::Paint(HDC hDC)
void CMemoryView::SetMemorySize(uint32 nSize)
{
m_nSize = nSize;
m_size = nSize;
UpdateScrollRange();
}
long CMemoryView::OnVScroll(unsigned int nType, unsigned int nTrackPos)
{
SCROLLINFO si;
int nOffset;
nOffset = (int)GetScrollOffset();
int nOffset = (int)GetScrollOffset();
switch(nType)
{
case SB_LINEDOWN:
@ -282,10 +259,10 @@ long CMemoryView::OnSize(unsigned int nType, unsigned int nX, unsigned int nY)
long CMemoryView::OnSetFocus()
{
Win32::CClientDeviceContext DeviceContext(m_hWnd);
Win32::CFont Font(GetFont());
Framework::Win32::CClientDeviceContext deviceContext(m_hWnd);
Framework::Win32::CFont font(GetFont());
CreateCaret(m_hWnd, NULL, 2, DeviceContext.GetFontHeight(Font));
CreateCaret(m_hWnd, NULL, 2, deviceContext.GetFontHeight(font));
ShowCaret(m_hWnd);
UpdateCaretPosition();
@ -324,25 +301,23 @@ long CMemoryView::OnLeftButtonDown(int nX, int nY)
long CMemoryView::OnLeftButtonUp(int nX, int nY)
{
SIZE FontSize = Win32::CClientDeviceContext(m_hWnd).GetFontSize(Win32::CFont(GetFont()));
SIZE fontSize = Framework::Win32::CClientDeviceContext(m_hWnd).GetFontSize(Framework::Win32::CFont(GetFont()));
nY -= YMARGIN;
nX -= XMARGIN + (8 * FontSize.cx) + 10;
nX -= XMARGIN + (8 * fontSize.cx) + 10;
if(nY < 0) return FALSE;
if(nX < 0) return FALSE;
unsigned int nLine, nRow;
uint32 nAddress;
unsigned int nCols, nRows;
//Find selected line
nLine = nY / (FontSize.cy + YSPACE);
unsigned int nLine = nY / (fontSize.cy + YSPACE);
//Find selected row;
nRow = nX / ((2 * FontSize.cx) + 3);
GetRenderParams(FontSize, nCols, nRows, nAddress);
unsigned int nRow = nX / ((2 * fontSize.cx) + 3);
uint32 nAddress = 0;
unsigned int nCols = 0, nRows = 0;
GetRenderParams(fontSize, nCols, nRows, nAddress);
if(nRow >= nRows) return FALSE;
@ -356,17 +331,17 @@ long CMemoryView::OnKeyDown(WPARAM nKey, LPARAM)
switch(nKey)
{
case VK_RIGHT:
SetSelectionStart(m_nSelectionStart + 1);
SetSelectionStart(m_selectionStart + 1);
break;
case VK_LEFT:
SetSelectionStart(m_nSelectionStart - 1);
SetSelectionStart(m_selectionStart - 1);
break;
case VK_UP:
case VK_DOWN:
{
unsigned int nCols;
unsigned int nCols = 0;
GetVisibleRowsCols(NULL, &nCols);
SetSelectionStart(m_nSelectionStart + ((nKey == VK_UP) ? (-static_cast<int>(nCols)) : (nCols)));
SetSelectionStart(m_selectionStart + ((nKey == VK_UP) ? (-static_cast<int>(nCols)) : (nCols)));
}
}
@ -376,39 +351,36 @@ long CMemoryView::OnKeyDown(WPARAM nKey, LPARAM)
void CMemoryView::SetSelectionStart(unsigned int nNewAddress)
{
if(static_cast<int>(nNewAddress) < 0) return;
if(nNewAddress >= m_nSize) return;
if(nNewAddress >= m_size) return;
m_nSelectionStart = nNewAddress;
m_selectionStart = nNewAddress;
UpdateCaretPosition();
OnSelectionChange(m_nSelectionStart);
OnSelectionChange(m_selectionStart);
}
void CMemoryView::UpdateCaretPosition()
{
Win32::CClientDeviceContext DeviceContext(m_hWnd);
Win32::CFont Font(GetFont());
unsigned int nLines, nBytes;
uint32 nAddress;
SIZE FontSize(DeviceContext.GetFontSize(Font));
Framework::Win32::CClientDeviceContext deviceContext(m_hWnd);
Framework::Win32::CFont font(GetFont());
SIZE fontSize(deviceContext.GetFontSize(font));
uint32 nAddress = 0;
unsigned int nLines = 0, nBytes = 0;
GetRenderParams(
FontSize,
fontSize,
nLines,
nBytes,
nAddress);
if(
(nBytes != 0) &&
(m_nSelectionStart >= nAddress) &&
(m_nSelectionStart <= (nAddress + (nLines * nBytes)))
(m_selectionStart >= nAddress) &&
(m_selectionStart <= (nAddress + (nLines * nBytes)))
)
{
int nX, nY;
unsigned int nSelectionStart = m_nSelectionStart - nAddress;
nX = XMARGIN + (8 * FontSize.cx) + 10 + (nSelectionStart % nBytes) * ((2 * FontSize.cx) + 3);
nY = YMARGIN + (FontSize.cy + YSPACE) * (nSelectionStart / nBytes);
unsigned int nSelectionStart = m_selectionStart - nAddress;
int nX = XMARGIN + (8 * fontSize.cx) + 10 + (nSelectionStart % nBytes) * ((2 * fontSize.cx) + 3);
int nY = YMARGIN + (fontSize.cy + YSPACE) * (nSelectionStart / nBytes);
SetCaretPos(nX, nY);
}
else
@ -419,11 +391,11 @@ void CMemoryView::UpdateCaretPosition()
void CMemoryView::GetRenderParams(const SIZE& FontSize, unsigned int& nLines, unsigned int& nFixedBytes, uint32& nAddress)
{
RECT ClientRect(GetClientRect());
RECT clientRect(GetClientRect());
nLines = (ClientRect.bottom - (YMARGIN * 2)) / (FontSize.cy + YSPACE);
nLines = (clientRect.bottom - (YMARGIN * 2)) / (FontSize.cy + YSPACE);
nFixedBytes = ClientRect.right - (2 * XMARGIN) - (5 * FontSize.cx) - 17;
nFixedBytes = clientRect.right - (2 * XMARGIN) - (5 * FontSize.cx) - 17;
nFixedBytes /= (3 * (FontSize.cx + 1));
if(nFixedBytes != 0)
{