| 1 |
namespace phoenix { |
| 2 |
|
| 3 |
static LRESULT CALLBACK HexEdit_windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { |
| 4 |
HexEdit& hexEdit = *(HexEdit*)GetWindowLongPtr(hwnd, GWLP_USERDATA); |
| 5 |
|
| 6 |
switch(msg) { |
| 7 |
case WM_KEYDOWN: |
| 8 |
if(hexEdit.p.keyPress(wparam)) return 0; |
| 9 |
break; |
| 10 |
|
| 11 |
case WM_MOUSEWHEEL: { |
| 12 |
signed offset = -((int16_t)HIWORD(wparam) / WHEEL_DELTA); |
| 13 |
hexEdit.p.scrollTo(hexEdit.p.scrollPosition() + offset); |
| 14 |
return true; |
| 15 |
} |
| 16 |
|
| 17 |
case WM_SIZE: { |
| 18 |
RECT rc; |
| 19 |
GetClientRect(hexEdit.p.hwnd, &rc); |
| 20 |
SetWindowPos(hexEdit.p.scrollBar, HWND_TOP, rc.right - 18, 0, 18, rc.bottom, SWP_SHOWWINDOW); |
| 21 |
break; |
| 22 |
} |
| 23 |
|
| 24 |
case WM_VSCROLL: { |
| 25 |
SCROLLINFO info; |
| 26 |
memset(&info, 0, sizeof(SCROLLINFO)); |
| 27 |
info.cbSize = sizeof(SCROLLINFO); |
| 28 |
info.fMask = SIF_ALL; |
| 29 |
GetScrollInfo((HWND)lparam, SB_CTL, &info); |
| 30 |
switch(LOWORD(wparam)) { |
| 31 |
case SB_LEFT: info.nPos = info.nMin; break; |
| 32 |
case SB_RIGHT: info.nPos = info.nMax; break; |
| 33 |
case SB_LINELEFT: info.nPos--; break; |
| 34 |
case SB_LINERIGHT: info.nPos++; break; |
| 35 |
case SB_PAGELEFT: info.nPos -= info.nMax >> 3; break; |
| 36 |
case SB_PAGERIGHT: info.nPos += info.nMax >> 3; break; |
| 37 |
case SB_THUMBTRACK: info.nPos = info.nTrackPos; break; |
| 38 |
} |
| 39 |
|
| 40 |
info.fMask = SIF_POS; |
| 41 |
SetScrollInfo((HWND)lparam, SB_CTL, &info, TRUE); |
| 42 |
GetScrollInfo((HWND)lparam, SB_CTL, &info); //get clamped position |
| 43 |
|
| 44 |
hexEdit.p.scrollTo(info.nPos); |
| 45 |
return TRUE; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return hexEdit.p.windowProc(hwnd, msg, wparam, lparam); |
| 50 |
} |
| 51 |
|
| 52 |
void pHexEdit::setColumns(unsigned columns) { |
| 53 |
update(); |
| 54 |
} |
| 55 |
|
| 56 |
void pHexEdit::setLength(unsigned length) { |
| 57 |
SetScrollRange(scrollBar, SB_CTL, 0, rowsScrollable(), TRUE); |
| 58 |
EnableWindow(scrollBar, rowsScrollable() > 0); |
| 59 |
update(); |
| 60 |
} |
| 61 |
|
| 62 |
void pHexEdit::setOffset(unsigned offset) { |
| 63 |
SetScrollPos(scrollBar, SB_CTL, offset / hexEdit.state.columns, TRUE); |
| 64 |
update(); |
| 65 |
} |
| 66 |
|
| 67 |
void pHexEdit::setRows(unsigned rows) { |
| 68 |
update(); |
| 69 |
} |
| 70 |
|
| 71 |
void pHexEdit::update() { |
| 72 |
if(!hexEdit.onRead) { |
| 73 |
SetWindowText(hwnd, L""); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
unsigned cursorPosition = Edit_GetSel(hwnd); |
| 78 |
|
| 79 |
string output; |
| 80 |
unsigned offset = hexEdit.state.offset; |
| 81 |
for(unsigned row = 0; row < hexEdit.state.rows; row++) { |
| 82 |
output.append(hex<8>(offset)); |
| 83 |
output.append(" "); |
| 84 |
|
| 85 |
string hexdata; |
| 86 |
string ansidata = " "; |
| 87 |
for(unsigned column = 0; column < hexEdit.state.columns; column++) { |
| 88 |
if(offset < hexEdit.state.length) { |
| 89 |
uint8_t data = hexEdit.onRead(offset++); |
| 90 |
hexdata.append(hex<2>(data)); |
| 91 |
hexdata.append(" "); |
| 92 |
ansidata.append(data >= 0x20 && data <= 0x7e ? (char)data : '.'); |
| 93 |
} else { |
| 94 |
hexdata.append(" "); |
| 95 |
ansidata.append(" "); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
output.append(hexdata); |
| 100 |
output.append(ansidata); |
| 101 |
if(offset >= hexEdit.state.length) break; |
| 102 |
if(row != hexEdit.state.rows - 1) output.append("\r\n"); |
| 103 |
} |
| 104 |
|
| 105 |
SetWindowText(hwnd, utf16_t(output)); |
| 106 |
Edit_SetSel(hwnd, LOWORD(cursorPosition), HIWORD(cursorPosition)); |
| 107 |
} |
| 108 |
|
| 109 |
void pHexEdit::constructor() { |
| 110 |
hwnd = CreateWindowEx( |
| 111 |
WS_EX_CLIENTEDGE, L"EDIT", L"", |
| 112 |
WS_CHILD | WS_TABSTOP | ES_AUTOHSCROLL | ES_READONLY | ES_MULTILINE | ES_WANTRETURN, |
| 113 |
0, 0, 0, 0, parentHwnd, (HMENU)id, GetModuleHandle(0), 0 |
| 114 |
); |
| 115 |
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)&hexEdit); |
| 116 |
|
| 117 |
scrollBar = CreateWindowEx( |
| 118 |
0, L"SCROLLBAR", L"", |
| 119 |
WS_VISIBLE | WS_CHILD | SBS_VERT, |
| 120 |
0, 0, 0, 0, hwnd, (HMENU)id, GetModuleHandle(0), 0 |
| 121 |
); |
| 122 |
SetWindowLongPtr(scrollBar, GWLP_USERDATA, (LONG_PTR)&hexEdit); |
| 123 |
|
| 124 |
windowProc = (WindowProc)GetWindowLongPtr(hwnd, GWLP_WNDPROC); |
| 125 |
SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)HexEdit_windowProc); |
| 126 |
|
| 127 |
setDefaultFont(); |
| 128 |
setLength(hexEdit.state.length); |
| 129 |
setOffset(hexEdit.state.offset); |
| 130 |
update(); |
| 131 |
PostMessage(hwnd, EM_SETSEL, 10, 10); |
| 132 |
|
| 133 |
synchronize(); |
| 134 |
} |
| 135 |
|
| 136 |
void pHexEdit::destructor() { |
| 137 |
DestroyWindow(hwnd); |
| 138 |
} |
| 139 |
|
| 140 |
void pHexEdit::orphan() { |
| 141 |
destructor(); |
| 142 |
constructor(); |
| 143 |
} |
| 144 |
|
| 145 |
bool pHexEdit::keyPress(unsigned scancode) { |
| 146 |
if(!hexEdit.onRead) return false; |
| 147 |
|
| 148 |
signed position = LOWORD(Edit_GetSel(hwnd)); |
| 149 |
signed lineWidth = 10 + (hexEdit.state.columns * 3) + 1 + hexEdit.state.columns + 2; |
| 150 |
signed cursorY = position / lineWidth; |
| 151 |
signed cursorX = position % lineWidth; |
| 152 |
|
| 153 |
if(scancode == VK_HOME) { |
| 154 |
signed offset = cursorY * lineWidth + 10; |
| 155 |
Edit_SetSel(hwnd, offset, offset); |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
if(scancode == VK_END) { |
| 160 |
signed offset = cursorY * lineWidth + 57; |
| 161 |
Edit_SetSel(hwnd, offset, offset); |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
if(scancode == VK_UP) { |
| 166 |
if(cursorY > 0) return false; |
| 167 |
scrollTo(scrollPosition() - 1); |
| 168 |
return true; |
| 169 |
} |
| 170 |
|
| 171 |
if(scancode == VK_DOWN) { |
| 172 |
if(cursorY >= rows() - 1) return true; |
| 173 |
if(cursorY < hexEdit.state.rows - 1) return false; |
| 174 |
scrollTo(scrollPosition() + 1); |
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
if(scancode == VK_PRIOR) { |
| 179 |
scrollTo(scrollPosition() - hexEdit.state.rows); |
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
if(scancode == VK_NEXT) { |
| 184 |
scrollTo(scrollPosition() + hexEdit.state.rows); |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
//convert scancode to hex nibble |
| 189 |
if(scancode >= '0' && scancode <= '9') scancode = scancode - '0'; |
| 190 |
else if(scancode >= 'A' && scancode <= 'F') scancode = scancode - 'A' + 10; |
| 191 |
else if(scancode >= 'a' && scancode <= 'f') scancode = scancode - 'a' + 10; |
| 192 |
else return false; |
| 193 |
|
| 194 |
if(cursorX >= 10) { |
| 195 |
//not on an offset |
| 196 |
cursorX -= 10; |
| 197 |
if((cursorX % 3) != 2) { |
| 198 |
//not on a space |
| 199 |
bool cursorNibble = (cursorX % 3) == 1; //0 = high, 1 = low |
| 200 |
cursorX /= 3; |
| 201 |
if(cursorX < hexEdit.state.columns) { |
| 202 |
//not in ANSI region |
| 203 |
unsigned offset = hexEdit.state.offset + (cursorY * hexEdit.state.columns + cursorX); |
| 204 |
|
| 205 |
if(offset >= hexEdit.state.length) return false; //do not edit past end of data |
| 206 |
uint8_t data = hexEdit.onRead(offset); |
| 207 |
|
| 208 |
//write modified value |
| 209 |
if(cursorNibble == 1) { |
| 210 |
data = (data & 0xf0) | (scancode << 0); |
| 211 |
} else { |
| 212 |
data = (data & 0x0f) | (scancode << 4); |
| 213 |
} |
| 214 |
if(hexEdit.onWrite) hexEdit.onWrite(offset, data); |
| 215 |
|
| 216 |
//auto-advance cursor to next nibble or byte |
| 217 |
position++; |
| 218 |
if(cursorNibble && cursorX != hexEdit.state.columns - 1) position++; |
| 219 |
Edit_SetSel(hwnd, position, position); |
| 220 |
|
| 221 |
//refresh output to reflect modified data |
| 222 |
update(); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
return true; |
| 228 |
} |
| 229 |
|
| 230 |
signed pHexEdit::rows() { |
| 231 |
return (max(1u, hexEdit.state.length) + hexEdit.state.columns - 1) / hexEdit.state.columns; |
| 232 |
} |
| 233 |
|
| 234 |
signed pHexEdit::rowsScrollable() { |
| 235 |
return max(0u, rows() - hexEdit.state.rows); |
| 236 |
} |
| 237 |
|
| 238 |
signed pHexEdit::scrollPosition() { |
| 239 |
return hexEdit.state.offset / hexEdit.state.columns; |
| 240 |
} |
| 241 |
|
| 242 |
void pHexEdit::scrollTo(signed position) { |
| 243 |
if(position > rowsScrollable()) position = rowsScrollable(); |
| 244 |
if(position < 0) position = 0; |
| 245 |
if(position == scrollPosition()) return; |
| 246 |
hexEdit.setOffset(position * hexEdit.state.columns); |
| 247 |
} |
| 248 |
|
| 249 |
} |