4 * Copyright 2005 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * - Cut, copy and paste
39 /* spaces dividing hex and ASCII */
42 typedef struct tagHEXEDIT_INFO
47 BOOL bFocusHex : 1; /* TRUE if focus is on hex, FALSE if focus on ASCII */
48 BOOL bInsert : 1; /* insert mode if TRUE, overwrite mode if FALSE */
49 INT nHeight; /* height of text */
50 INT nCaretPos; /* caret pos in nibbles */
53 INT nBytesPerLine; /* bytes of hex to display per line of the control */
54 INT nScrollPos; /* first visible line */
57 const WCHAR szHexEditClass[] = {'H','e','x','E','d','i','t',0};
59 static inline LRESULT HexEdit_SetFont (HEXEDIT_INFO *infoPtr, HFONT hFont, BOOL redraw);
61 static inline BYTE hexchar_to_byte(TCHAR ch)
63 if (ch >= '0' && ch <= '9')
65 else if (ch >= 'a' && ch <= 'f')
67 else if (ch >= 'A' && ch <= 'F')
73 static LPTSTR HexEdit_GetLineText(BYTE *pData, LONG cbData, LONG pad)
75 LPTSTR lpszLine = HeapAlloc(GetProcessHeap(), 0,
76 (cbData * 3 + pad * 3 + DIV_SPACES + cbData + 1) * sizeof(TCHAR));
82 for (i = 0; i < cbData; i++)
83 wsprintf(lpszLine + i*3, TEXT("%02X "), pData[i]);
84 for (i = 0; i < pad * 3; i++)
85 lpszLine[cbData * 3 + i] = ' ';
87 for (i = 0; i < DIV_SPACES; i++)
88 lpszLine[cbData * 3 + pad * 3 + i] = ' ';
90 /* attempt an ASCII representation if the characters are printable,
91 * otherwise display a '.' */
92 for (i = 0; i < cbData; i++)
94 /* (C1_ALPHA|C1_BLANK|C1_PUNCT|C1_DIGIT|C1_LOWER|C1_UPPER) */
95 if (isprint(pData[i]))
96 lpszLine[cbData * 3 + pad * 3 + DIV_SPACES + i] = pData[i];
98 lpszLine[cbData * 3 + pad * 3 + DIV_SPACES + i] = '.';
100 lpszLine[cbData * 3 + pad * 3 + DIV_SPACES + cbData] = 0;
105 HexEdit_Paint(HEXEDIT_INFO *infoPtr)
108 HDC hdc = BeginPaint(infoPtr->hwndSelf, &ps);
109 INT nXStart, nYStart;
114 LONG lByteOffset = infoPtr->nScrollPos * infoPtr->nBytesPerLine;
116 /* Make a gap from the frame */
117 nXStart = GetSystemMetrics(SM_CXBORDER);
118 nYStart = GetSystemMetrics(SM_CYBORDER);
120 if (GetWindowLong(infoPtr->hwndSelf, GWL_STYLE) & WS_DISABLED)
121 clrOldText = SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
123 clrOldText = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
125 iMode = SetBkMode(hdc, TRANSPARENT);
126 hOldFont = SelectObject(hdc, infoPtr->hFont);
128 for (pData = infoPtr->pData + lByteOffset; pData < infoPtr->pData + infoPtr->cbData; pData += infoPtr->nBytesPerLine)
131 LONG nLineLen = min((LONG)((infoPtr->pData + infoPtr->cbData) - pData),
132 infoPtr->nBytesPerLine);
134 lpszLine = HexEdit_GetLineText(pData, nLineLen, infoPtr->nBytesPerLine - nLineLen);
136 /* FIXME: draw hex <-> ASCII mapping highlighted? */
137 TextOut(hdc, nXStart, nYStart, lpszLine, infoPtr->nBytesPerLine * 3 + DIV_SPACES + nLineLen);
139 nYStart += infoPtr->nHeight;
140 HeapFree(GetProcessHeap(), 0, lpszLine);
143 SelectObject(hdc, hOldFont);
144 SetBkMode(hdc, iMode);
145 SetTextColor(hdc, clrOldText);
147 EndPaint(infoPtr->hwndSelf, &ps);
151 HexEdit_UpdateCaret(HEXEDIT_INFO *infoPtr)
156 INT nCaretBytePos = infoPtr->nCaretPos/2;
157 INT nByteLinePos = nCaretBytePos % infoPtr->nBytesPerLine;
158 INT nLine = nCaretBytePos / infoPtr->nBytesPerLine;
159 LONG nLineLen = min(infoPtr->cbData - nLine * infoPtr->nBytesPerLine, infoPtr->nBytesPerLine);
160 LPTSTR lpszLine = HexEdit_GetLineText(infoPtr->pData + nLine * infoPtr->nBytesPerLine, nLineLen, infoPtr->nBytesPerLine - nLineLen);
163 /* calculate offset of character caret is on in the line */
164 if (infoPtr->bFocusHex)
165 nCharOffset = nByteLinePos*3 + infoPtr->nCaretPos % 2;
167 nCharOffset = infoPtr->nBytesPerLine*3 + DIV_SPACES + nByteLinePos;
169 hdc = GetDC(infoPtr->hwndSelf);
170 hOldFont = SelectObject(hdc, infoPtr->hFont);
172 GetTextExtentPoint32(hdc, lpszLine, nCharOffset, &size);
174 SelectObject(hdc, hOldFont);
175 ReleaseDC(infoPtr->hwndSelf, hdc);
177 if (!nLineLen) size.cx = 0;
179 HeapFree(GetProcessHeap(), 0, lpszLine);
182 GetSystemMetrics(SM_CXBORDER) + size.cx,
183 GetSystemMetrics(SM_CYBORDER) + (nLine - infoPtr->nScrollPos) * infoPtr->nHeight);
187 HexEdit_UpdateScrollbars(HEXEDIT_INFO *infoPtr)
190 INT nLines = infoPtr->cbData / infoPtr->nBytesPerLine;
194 GetClientRect(infoPtr->hwndSelf, &rcClient);
195 InflateRect(&rcClient, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER));
197 nVisibleLines = (rcClient.bottom - rcClient.top) / infoPtr->nHeight;
198 si.cbSize = sizeof(si);
199 si.fMask = SIF_RANGE | SIF_PAGE;
201 si.nMax = max(nLines - nVisibleLines, nLines);
202 si.nPage = nVisibleLines;
203 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si, TRUE);
207 HexEdit_EnsureVisible(HEXEDIT_INFO *infoPtr, INT nCaretPos)
209 INT nLine = nCaretPos / (2 * infoPtr->nBytesPerLine);
212 si.cbSize = sizeof(si);
213 si.fMask = SIF_POS | SIF_PAGE;
214 GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
217 else if (nLine >= si.nPos + si.nPage)
218 si.nPos = nLine - si.nPage + 1;
223 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si, FALSE);
224 SendMessage(infoPtr->hwndSelf, WM_VSCROLL, MAKELONG(SB_THUMBPOSITION, 0), 0);
229 HexEdit_SetData(HEXEDIT_INFO *infoPtr, INT cbData, const BYTE *pData)
231 HeapFree(GetProcessHeap(), 0, infoPtr->pData);
234 infoPtr->pData = HeapAlloc(GetProcessHeap(), 0, cbData);
237 memcpy(infoPtr->pData, pData, cbData);
238 infoPtr->cbData = cbData;
240 infoPtr->nCaretPos = 0;
241 HexEdit_UpdateScrollbars(infoPtr);
242 HexEdit_UpdateCaret(infoPtr);
243 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
250 HexEdit_GetData(HEXEDIT_INFO *infoPtr, INT cbData, BYTE *pData)
253 memcpy(pData, infoPtr->pData, min(cbData, infoPtr->cbData));
254 return infoPtr->cbData;
257 static inline LRESULT
258 HexEdit_Char (HEXEDIT_INFO *infoPtr, TCHAR ch)
260 INT nCaretBytePos = infoPtr->nCaretPos/2;
262 assert(nCaretBytePos >= 0);
264 /* backspace is special */
267 if (infoPtr->nCaretPos == 0)
270 /* if at end of byte then delete the whole byte */
271 if (infoPtr->bFocusHex && (infoPtr->nCaretPos % 2 == 0))
273 memmove(infoPtr->pData + nCaretBytePos - 1,
274 infoPtr->pData + nCaretBytePos,
275 infoPtr->cbData - nCaretBytePos);
277 infoPtr->nCaretPos -= 2; /* backtrack two nibble */
279 else /* blank upper nibble */
281 infoPtr->pData[nCaretBytePos] &= 0x0f;
282 infoPtr->nCaretPos--; /* backtrack one nibble */
287 if (infoPtr->bFocusHex && hexchar_to_byte(ch) == (BYTE)-1)
289 MessageBeep(MB_ICONWARNING);
293 if ((infoPtr->bInsert && (infoPtr->nCaretPos % 2 == 0)) || (nCaretBytePos >= infoPtr->cbData))
295 /* make room for another byte */
297 infoPtr->pData = HeapReAlloc(GetProcessHeap(), 0, infoPtr->pData, infoPtr->cbData + 1);
298 if (!infoPtr->pData) return 0;
299 /* move everything after caret up one byte */
300 memmove(infoPtr->pData + nCaretBytePos + 1,
301 infoPtr->pData + nCaretBytePos,
302 infoPtr->cbData - nCaretBytePos);
304 infoPtr->pData[nCaretBytePos] = 0x0;
307 /* overwrite a byte */
309 assert(nCaretBytePos < infoPtr->cbData);
311 if (infoPtr->bFocusHex)
313 BYTE orig_byte = infoPtr->pData[nCaretBytePos];
314 BYTE digit = hexchar_to_byte(ch);
315 if (infoPtr->nCaretPos % 2) /* set low nibble */
316 infoPtr->pData[nCaretBytePos] = (orig_byte & 0xf0) | digit;
317 else /* set high nibble */
318 infoPtr->pData[nCaretBytePos] = (orig_byte & 0x0f) | digit << 4;
319 infoPtr->nCaretPos++; /* advance one nibble */
323 infoPtr->pData[nCaretBytePos] = (BYTE)ch;
324 infoPtr->nCaretPos += 2; /* advance two nibbles */
328 HexEdit_UpdateScrollbars(infoPtr);
329 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
330 HexEdit_UpdateCaret(infoPtr);
331 HexEdit_EnsureVisible(infoPtr, infoPtr->nCaretPos);
335 static inline LRESULT
336 HexEdit_Create (HEXEDIT_INFO *infoPtr, LPCREATESTRUCTW lpcs)
338 HexEdit_SetFont(infoPtr, GetStockObject(SYSTEM_FONT), FALSE);
339 HexEdit_UpdateScrollbars(infoPtr);
345 static inline LRESULT
346 HexEdit_Destroy (HEXEDIT_INFO *infoPtr)
348 HWND hwnd = infoPtr->hwndSelf;
349 HeapFree(GetProcessHeap(), 0, infoPtr->pData);
351 HeapFree(GetProcessHeap(), 0, infoPtr);
352 SetWindowLongPtr(hwnd, 0, 0);
357 static inline LRESULT
358 HexEdit_EraseBackground (HEXEDIT_INFO *infoPtr, HDC hdc)
360 HBRUSH hBrush, hSolidBrush = NULL;
363 if (GetWindowLong(infoPtr->hwndSelf, GWL_STYLE) & WS_DISABLED)
364 hBrush = hSolidBrush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
367 hBrush = (HBRUSH)SendMessage(GetParent(infoPtr->hwndSelf), WM_CTLCOLOREDIT,
368 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
370 hBrush = hSolidBrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
373 GetClientRect (infoPtr->hwndSelf, &rc);
375 FillRect (hdc, &rc, hBrush);
378 DeleteObject(hSolidBrush);
384 static inline LRESULT
385 HexEdit_GetFont (HEXEDIT_INFO *infoPtr)
387 return (LRESULT)infoPtr->hFont;
390 static inline LRESULT
391 HexEdit_KeyDown (HEXEDIT_INFO *infoPtr, DWORD key, DWORD flags)
393 INT nInc = (infoPtr->bFocusHex) ? 1 : 2;
399 infoPtr->nCaretPos -= nInc;
400 if (infoPtr->nCaretPos < 0)
401 infoPtr->nCaretPos = 0;
404 infoPtr->nCaretPos += nInc;
405 if (infoPtr->nCaretPos > infoPtr->cbData*2)
406 infoPtr->nCaretPos = infoPtr->cbData*2;
409 if ((infoPtr->nCaretPos - infoPtr->nBytesPerLine*2) >= 0)
410 infoPtr->nCaretPos -= infoPtr->nBytesPerLine*2;
413 if ((infoPtr->nCaretPos + infoPtr->nBytesPerLine*2) <= infoPtr->cbData*2)
414 infoPtr->nCaretPos += infoPtr->nBytesPerLine*2;
417 infoPtr->nCaretPos = 0;
420 infoPtr->nCaretPos = infoPtr->cbData*2;
422 case VK_PRIOR: /* page up */
423 si.cbSize = sizeof(si);
425 GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
426 if ((infoPtr->nCaretPos - (INT)si.nPage*infoPtr->nBytesPerLine*2) >= 0)
427 infoPtr->nCaretPos -= si.nPage*infoPtr->nBytesPerLine*2;
429 infoPtr->nCaretPos = 0;
431 case VK_NEXT: /* page down */
432 si.cbSize = sizeof(si);
434 GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
435 if ((infoPtr->nCaretPos + (INT)si.nPage*infoPtr->nBytesPerLine*2) <= infoPtr->cbData*2)
436 infoPtr->nCaretPos += si.nPage*infoPtr->nBytesPerLine*2;
438 infoPtr->nCaretPos = infoPtr->cbData*2;
444 HexEdit_UpdateCaret(infoPtr);
445 HexEdit_EnsureVisible(infoPtr, infoPtr->nCaretPos);
451 static inline LRESULT
452 HexEdit_KillFocus (HEXEDIT_INFO *infoPtr, HWND receiveFocus)
454 infoPtr->bFocus = FALSE;
461 static inline LRESULT
462 HexEdit_LButtonDown (HEXEDIT_INFO *infoPtr)
464 SetFocus(infoPtr->hwndSelf);
466 /* FIXME: hittest and set caret */
472 static inline LRESULT HexEdit_NCCreate (HWND hwnd, LPCREATESTRUCTW lpcs)
474 HEXEDIT_INFO *infoPtr;
475 SetWindowLongW(hwnd, GWL_EXSTYLE,
476 lpcs->dwExStyle | WS_EX_CLIENTEDGE);
478 /* allocate memory for info structure */
479 infoPtr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HEXEDIT_INFO));
480 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
482 /* initialize info structure */
483 infoPtr->nCaretPos = 0;
484 infoPtr->hwndSelf = hwnd;
485 infoPtr->nBytesPerLine = 2;
486 infoPtr->bFocusHex = TRUE;
487 infoPtr->bInsert = TRUE;
489 return DefWindowProcW(infoPtr->hwndSelf, WM_NCCREATE, 0, (LPARAM)lpcs);
492 static inline LRESULT
493 HexEdit_SetFocus (HEXEDIT_INFO *infoPtr, HWND lostFocus)
495 infoPtr->bFocus = TRUE;
497 CreateCaret(infoPtr->hwndSelf, NULL, 1, infoPtr->nHeight);
498 HexEdit_UpdateCaret(infoPtr);
499 ShowCaret(infoPtr->hwndSelf);
505 static inline LRESULT
506 HexEdit_SetFont (HEXEDIT_INFO *infoPtr, HFONT hFont, BOOL redraw)
510 HFONT hOldFont = NULL;
514 infoPtr->hFont = hFont;
516 hdc = GetDC(infoPtr->hwndSelf);
518 hOldFont = SelectObject(hdc, infoPtr->hFont);
520 GetTextMetrics(hdc, &tm);
521 infoPtr->nHeight = tm.tmHeight + tm.tmExternalLeading;
523 GetClientRect(infoPtr->hwndSelf, &rcClient);
527 BYTE *pData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, i);
528 LPTSTR lpszLine = HexEdit_GetLineText(pData, i, 0);
530 GetTextExtentPoint32(hdc, lpszLine, lstrlen(lpszLine), &size);
531 HeapFree(GetProcessHeap(), 0, lpszLine);
532 HeapFree(GetProcessHeap(), 0, pData);
533 if (size.cx > (rcClient.right - rcClient.left))
535 infoPtr->nBytesPerLine = i - 1;
541 SelectObject(hdc, hOldFont);
542 ReleaseDC (infoPtr->hwndSelf, hdc);
545 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
550 static inline LRESULT
551 HexEdit_VScroll (HEXEDIT_INFO *infoPtr, INT action)
555 /* get all scroll bar info */
556 si.cbSize = sizeof(si);
558 GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
560 switch (LOWORD(action))
562 case SB_TOP: /* user pressed the home key */
566 case SB_BOTTOM: /* user pressed the end key */
570 case SB_LINEUP: /* user clicked the up arrow */
574 case SB_LINEDOWN: /* user clicked the down arrow */
578 case SB_PAGEUP: /* user clicked the scroll bar above the scroll thumb */
582 case SB_PAGEDOWN: /* user clicked the scroll bar below the scroll thumb */
586 case SB_THUMBTRACK: /* user dragged the scroll thumb */
587 si.nPos = si.nTrackPos;
594 /* set the position and then retrieve it to let the system handle the
595 * cases where the position is out of range */
597 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si, TRUE);
598 GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
600 if (si.nPos != infoPtr->nScrollPos)
602 ScrollWindow(infoPtr->hwndSelf, 0, infoPtr->nHeight * (infoPtr->nScrollPos - si.nPos), NULL, NULL);
603 infoPtr->nScrollPos = si.nPos;
604 UpdateWindow(infoPtr->hwndSelf);
606 /* need to update caret position since it depends on the scroll position */
607 HexEdit_UpdateCaret(infoPtr);
613 static LRESULT WINAPI
614 HexEdit_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
616 HEXEDIT_INFO *infoPtr = (HEXEDIT_INFO *)GetWindowLongPtrW(hwnd, 0);
618 if (!infoPtr && (uMsg != WM_NCCREATE))
619 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
624 return HexEdit_SetData (infoPtr, (INT)wParam, (const BYTE *)lParam);
627 return HexEdit_GetData (infoPtr, (INT)wParam, (BYTE *)lParam);
630 return HexEdit_Char (infoPtr, (WCHAR)wParam);
633 return HexEdit_Create (infoPtr, (LPCREATESTRUCTW)lParam);
636 return HexEdit_Destroy (infoPtr);
639 return HexEdit_EraseBackground (infoPtr, (HDC)wParam);
642 return DLGC_WANTCHARS | DLGC_WANTARROWS;
645 return HexEdit_GetFont (infoPtr);
648 return HexEdit_KeyDown (infoPtr, wParam, lParam);
651 return HexEdit_KillFocus (infoPtr, (HWND)wParam);
654 return HexEdit_LButtonDown (infoPtr);
657 return HexEdit_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
660 HexEdit_Paint(infoPtr);
664 return HexEdit_SetFocus (infoPtr, (HWND)wParam);
667 return HexEdit_SetFont (infoPtr, (HFONT)wParam, LOWORD(lParam));
670 return HexEdit_VScroll (infoPtr, (INT)wParam);
673 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
678 void HexEdit_Register(void)
682 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
684 wndClass.lpfnWndProc = HexEdit_WindowProc;
685 wndClass.cbClsExtra = 0;
686 wndClass.cbWndExtra = sizeof(HEXEDIT_INFO *);
687 wndClass.hCursor = NULL;
688 wndClass.hbrBackground = NULL;
689 wndClass.lpszClassName = szHexEditClass;
691 RegisterClassW(&wndClass);