-
Notifications
You must be signed in to change notification settings - Fork 15
/
HexView.cpp
224 lines (171 loc) · 4.87 KB
/
HexView.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
#include "HexView.h"
#include <wx/settings.h>
#include <wx/dcclient.h>
#include <wx/dcbuffer.h>
#include <wx/sizer.h>
#include <wx/scrolwin.h>
#include <cmath>
class HexRowView : public wxScrolledCanvas
{
public:
HexRowView(HexView *hexView,
wxWindowID winid = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize)
{
Create(hexView, winid, pos, size);
m_view = hexView;
Bind(wxEVT_LEFT_DOWN, &HexRowView::OnLeftDown, this);
Bind(wxEVT_LEFT_UP, &HexRowView::OnLeftUp, this);
SetBackgroundStyle(wxBG_STYLE_PAINT);
SetCursor(wxCURSOR_IBEAM);
CalcSize();
}
void CalcSize()
{
unsigned int rowCount = m_view->m_dataSize / m_view->m_rowsize;
wxSize fntSize = m_view->GetFont().GetPixelSize();
m_rowHeight = fntSize.GetHeight() + 2;
m_charWidth = fntSize.GetWidth();
SetScrollRate(m_charWidth * 4, m_rowHeight * 20);
SetScrollbars(m_charWidth, m_rowHeight, m_view->m_rowsize * 3, rowCount + 2);
Refresh();
}
void OnDraw(wxDC& dc)
{
dc.SetBackground(m_view->GetBackgroundColour());
dc.Clear();
dc.SetFont(m_view->GetFont());
wxRect updateRect = GetUpdateClientRect();
updateRect.SetPosition(CalcUnscrolledPosition(updateRect.GetPosition()));
int startRow = updateRect.GetTop() / m_rowHeight;
int endRow = std::ceil(updateRect.GetBottom() / (double)m_rowHeight);
if (endRow > m_view->m_dataSize / m_view->m_rowsize)
endRow = m_view->m_dataSize / m_view->m_rowsize;
wxSize offsetSize = dc.GetTextExtent("00000000");
offsetSize.x += 4;
wxSize byteSize = dc.GetTextExtent("00");
byteSize.x += 2;
wxRect visRect = GetClientSize();
wxPoint viewStart = GetViewStart();
for (int row = startRow; row <= endRow; ++row)
{
int offset = row * m_view->m_rowsize;
wxPoint rowPos = wxPoint(2, (row * m_rowHeight) + 2);
int rowsize = m_view->m_rowsize;
if (offset + rowsize > m_view->m_dataSize)
rowsize = m_view->m_dataSize - offset;
// Draw offset
dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
dc.DrawText(wxString::Format("%.8X", offset), rowPos);
rowPos.x += offsetSize.GetWidth();
// Draw row values
dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
for (int i = 0; i < m_view->m_rowsize; ++i)
{
if (i < rowsize)
dc.DrawText(wxString::Format("%.2X", m_view->m_data[offset + i]), rowPos);
rowPos.x += byteSize.GetWidth();
}
rowPos.x += m_charWidth;
// Draw row characters
wxString str((const char*)&m_view->m_data[offset], rowsize);
for (auto it = str.begin(); it != str.end(); ++it)
{
if (*it < 32)
*it = '.';
}
dc.DrawText(str, rowPos);
rowPos.y += m_rowHeight;
}
}
void OnLeftDown(wxMouseEvent& event)
{
event.Skip();
}
void OnLeftUp(wxMouseEvent& event)
{
event.Skip();
}
private:
int m_rowHeight;
int m_charWidth;
HexView* m_view;
friend class HexView;
};
bool HexView::Create(wxWindow *parent,
wxWindowID winid,
const wxPoint& pos,
const wxSize& size)
{
if (!wxControl::Create(parent, winid, pos, size))
return false;
m_rowsize = 16;
m_data = NULL;
m_dataSize = 0;
SetFont(wxFont(wxNORMAL_FONT->GetPointSize(), 76, 90, 90, false, wxEmptyString));
SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
wxSizer* sizer = new wxBoxSizer(wxVERTICAL);
m_rowView = new HexRowView(this);
m_header = new wxPanel(this);
m_header->SetMinClientSize(wxSize(0, m_rowView->m_rowHeight + 4));
m_header->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
m_header->Bind(wxEVT_PAINT, &HexView::OnPaintHeader, this);
sizer->Add(m_header, wxSizerFlags(0).Expand());
sizer->Add(m_rowView, wxSizerFlags(1).Expand());
SetSizer(sizer);
m_rowView->CalcSize();
return true;
}
HexView::~HexView()
{
Clear();
}
void HexView::Clear()
{
if (m_data)
{
delete [] m_data;
m_data = NULL;
m_dataSize = 0;
m_rowView->CalcSize();
}
}
bool HexView::Load(const void* data, size_t dataSize)
{
Clear();
m_dataSize = dataSize;
m_data = new unsigned char[dataSize];
memcpy(m_data, data, dataSize);
m_rowView->CalcSize();
return true;
}
bool HexView::Load(wxInputStream& iStr)
{
wxFileOffset strSize = iStr.SeekI(0, wxFromEnd);
iStr.SeekI(0);
unsigned char* data = new unsigned char[strSize];
iStr.Read(data, strSize);
Load(data, strSize);
delete [] data;
return true;
}
void HexView::OnPaintHeader(wxPaintEvent& evt)
{
wxPaintDC dc(m_header);
dc.SetFont(GetFont());
wxPoint rowPos = wxPoint(2, 2);
wxSize offsetSize = dc.GetTextExtent("00000000");
offsetSize.x += 4;
wxSize byteSize = dc.GetTextExtent("00");
byteSize.x += 2;
// Header row
dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
dc.DrawText(_("Offset"), rowPos);
rowPos.x += offsetSize.GetWidth();
for (int i = 0; i < m_rowsize; ++i)
{
dc.DrawText(wxString::Format("%.2X", i), rowPos);
rowPos.x += byteSize.GetWidth();
}
}