forked from zodiacon/TotalRegistry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HexControl.h
168 lines (149 loc) · 5.74 KB
/
HexControl.h
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
#pragma once
#include <map>
#include <string>
struct IBufferManager {
virtual uint32_t GetData(int64_t offset, uint8_t* buffer, uint32_t count) = 0;
virtual bool Insert(int64_t offset, const uint8_t* data, uint32_t count) = 0;
virtual bool Delete(int64_t offset, size_t count) = 0;
virtual bool SetData(int64_t offset, const uint8_t* data, uint32_t count) = 0;
virtual int64_t GetSize() const = 0;
virtual uint8_t* GetRawData(int64_t offset) = 0;
virtual bool IsReadOnly() const = 0;
virtual bool Increase(uint32_t size) {
return false;
}
virtual ~IBufferManager() = default;
};
struct HexControlColors {
COLORREF Text{ ::GetSysColor(COLOR_WINDOWTEXT) };
COLORREF Background{ ::GetSysColor(COLOR_WINDOW) };
COLORREF Ascii{ RGB(128, 0, 0) };
COLORREF Offset{ RGB(0, 0, 128) };
COLORREF SelectionText{ ::GetSysColor(COLOR_HIGHLIGHTTEXT) };
COLORREF SelectionBackground{ ::GetSysColor(COLOR_HIGHLIGHT) };
};
enum class SelectionType {
Simple,
Box
};
class Selection {
public:
void SetSimple(int64_t offset, int64_t len);
void SetBox(int64_t offset, int bytesPerLine, int width, int height);
void SetAnchor(int64_t offset);
int64_t GetOffset() const;
int64_t GetAnchor() const;
bool IsSelected(int64_t offset) const;
bool IsEmpty() const;
SelectionType GetSelectionType() const;
int64_t GetLength() const {
return _length;
}
void Clear();
private:
SelectionType _type{ SelectionType::Simple };
int64_t _offset{ -1 }, _anchor{ -1 };
int64_t _length{ 0 };
int _width, _height, _bytesPerLine;
};
const UINT HCN_SIZECHANGED = NM_FIRST - 3000;
const UINT HCN_RCLICK = NM_FIRST - 3001;
struct HCNOTIFY {
NMHDR hdr;
};
class CHexControl :
public CBufferedPaintWindowImpl<CHexControl> {
public:
DECLARE_WND_CLASS_EX(L"WTLHexControl", CS_OWNDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, NULL)
HWND GetHwnd() const;
void SetBufferManager(IBufferManager* mgr);
IBufferManager* GetBufferManager() const;
void SetReadOnly(bool readonly);
bool IsReadOnly() const;
void SetAllowExtension(bool allow);
bool IsAllowExtension() const;
void SetSize(int64_t size);
bool SetDataSize(int32_t size);
int32_t GetDataSize() const;
bool SetBytesPerLine(int32_t bytesPerLine);
int32_t GetBytesPerLine() const;
bool Copy(int64_t offset = -1, int64_t size = 0);
bool Paste(int64_t offset = -1);
bool CanCopy() const;
bool CanPaste() const;
bool Cut(int64_t offset = -1, int64_t size = 0);
bool Delete(int64_t offset = -1, int64_t size = 0);
bool CanCut() const;
bool CanDelete() const;
int64_t SetBiasOffset(int64_t offset);
int64_t GetBiasOffset() const;
HexControlColors& GetColors();
std::wstring GetText(int64_t offset, int64_t size);
void Refresh();
BEGIN_MSG_MAP(CHexControl)
MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLeftButtonDown)
MESSAGE_HANDLER(WM_SIZE, OnSize)
MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)
MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
MESSAGE_HANDLER(WM_LBUTTONUP, OnLeftButtonUp)
MESSAGE_HANDLER(WM_CHAR, OnChar)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_VSCROLL, OnVScroll)
MESSAGE_HANDLER(WM_HSCROLL, OnHScroll)
MESSAGE_HANDLER(WM_MOUSEWHEEL, OnMouseWheel)
MESSAGE_HANDLER(WM_SETFOCUS, OnSetFocus)
MESSAGE_HANDLER(WM_KILLFOCUS, OnKillFocus)
MESSAGE_HANDLER(WM_GETDLGCODE, OnGetDialogCode)
MESSAGE_HANDLER(WM_CONTEXTMENU, OnContextMenu)
CHAIN_MSG_MAP(CBufferedPaintWindowImpl<CHexControl>)
END_MSG_MAP()
void DoPaint(CDCHandle dc, RECT& rect);
private:
LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnHScroll(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnVScroll(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnMouseWheel(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnSetFocus(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnKillFocus(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnLeftButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnGetDialogCode(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnKeyDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnChar(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnLeftButtonUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnContextMenu(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
private:
void RecalcLayout();
void InitFontMetrics();
CPoint GetPointFromOffset(int64_t offset) const;
int64_t GetOffsetFromPoint(const POINT& pt) const;
void DrawNumber(CDCHandle dc, int64_t offset, uint64_t value, int editDigits);
PCWSTR FormatNumber(ULONGLONG number) const;
LRESULT SendNotify(HCNOTIFY* notify);
void UpdateCaret();
void RedrawWindow(RECT* = nullptr);
void ClearSelection();
void CommitValue(int64_t offset, uint64_t value);
void ResetInput();
int64_t NormalizeOffset(int64_t offset) const;
void RedrawCaretLine();
private:
HexControlColors m_Colors;
CFont m_Font;
int m_FontPointSize{ 100 };
int m_Lines{ 1 };
int m_CharWidth, m_CharHeight;
IBufferManager* m_Buffer{ nullptr };
std::vector<std::wstring> m_Text;
int64_t m_StartOffset{ 0 }, m_EndOffset;
int32_t m_DataSize{ 1 }, m_BytesPerLine{ 32 };
int64_t m_CaretOffset{ 0 };
int m_AddressDigits{ 4 };
int m_EditDigits{ 0 }, m_LastDigits{ 0 };
Selection m_Selection;
HCNOTIFY m_NotifyData;
uint64_t m_CurrentInput{ 0 }, m_OldValue;
bool m_InsertMode{ false };
bool m_ReadOnly{ false };
};