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 static inline LRESULT
HexEdit_SetFont (HEXEDIT_INFO
*infoPtr
, HFONT hFont
, BOOL redraw
);
59 static inline BYTE
hexchar_to_byte(WCHAR ch
)
61 if (ch
>= '0' && ch
<= '9')
63 else if (ch
>= 'a' && ch
<= 'f')
65 else if (ch
>= 'A' && ch
<= 'F')
71 static LPWSTR
HexEdit_GetLineText(int offset
, BYTE
*pData
, LONG cbData
, LONG pad
)
73 WCHAR
*lpszLine
= malloc((6 + cbData
* 3 + pad
* 3 + DIV_SPACES
+ cbData
+ 1) * sizeof(WCHAR
));
76 wsprintfW(lpszLine
, L
"%04X ", offset
);
78 for (i
= 0; i
< cbData
; i
++)
79 wsprintfW(lpszLine
+ 6 + i
*3, L
"%02X ", pData
[offset
+ i
]);
80 for (i
= 0; i
< pad
* 3; i
++)
81 lpszLine
[6 + cbData
* 3 + i
] = ' ';
83 for (i
= 0; i
< DIV_SPACES
; i
++)
84 lpszLine
[6 + cbData
* 3 + pad
* 3 + i
] = ' ';
86 /* attempt an ASCII representation if the characters are printable,
87 * otherwise display a '.' */
88 for (i
= 0; i
< cbData
; i
++)
90 /* (C1_ALPHA|C1_BLANK|C1_PUNCT|C1_DIGIT|C1_LOWER|C1_UPPER) */
91 if (iswprint(pData
[offset
+ i
]))
92 lpszLine
[6 + cbData
* 3 + pad
* 3 + DIV_SPACES
+ i
] = pData
[offset
+ i
];
94 lpszLine
[6 + cbData
* 3 + pad
* 3 + DIV_SPACES
+ i
] = '.';
96 lpszLine
[6 + cbData
* 3 + pad
* 3 + DIV_SPACES
+ cbData
] = 0;
101 HexEdit_Paint(HEXEDIT_INFO
*infoPtr
)
104 HDC hdc
= BeginPaint(infoPtr
->hwndSelf
, &ps
);
105 INT nXStart
, nYStart
;
109 LONG lByteOffset
= infoPtr
->nScrollPos
* infoPtr
->nBytesPerLine
;
112 /* Make a gap from the frame */
113 nXStart
= GetSystemMetrics(SM_CXBORDER
);
114 nYStart
= GetSystemMetrics(SM_CYBORDER
);
116 if (GetWindowLongW(infoPtr
->hwndSelf
, GWL_STYLE
) & WS_DISABLED
)
117 clrOldText
= SetTextColor(hdc
, GetSysColor(COLOR_GRAYTEXT
));
119 clrOldText
= SetTextColor(hdc
, GetSysColor(COLOR_WINDOWTEXT
));
121 iMode
= SetBkMode(hdc
, TRANSPARENT
);
122 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
124 for (i
= lByteOffset
; i
< infoPtr
->cbData
; i
+= infoPtr
->nBytesPerLine
)
127 LONG nLineLen
= min(infoPtr
->cbData
- i
, infoPtr
->nBytesPerLine
);
129 lpszLine
= HexEdit_GetLineText(i
, infoPtr
->pData
, nLineLen
, infoPtr
->nBytesPerLine
- nLineLen
);
131 /* FIXME: draw hex <-> ASCII mapping highlighted? */
132 TextOutW(hdc
, nXStart
, nYStart
, lpszLine
, lstrlenW(lpszLine
));
134 nYStart
+= infoPtr
->nHeight
;
138 SelectObject(hdc
, hOldFont
);
139 SetBkMode(hdc
, iMode
);
140 SetTextColor(hdc
, clrOldText
);
142 EndPaint(infoPtr
->hwndSelf
, &ps
);
146 HexEdit_UpdateCaret(HEXEDIT_INFO
*infoPtr
)
151 INT nCaretBytePos
= infoPtr
->nCaretPos
/2;
152 INT nByteLinePos
= nCaretBytePos
% infoPtr
->nBytesPerLine
;
153 INT nLine
= nCaretBytePos
/ infoPtr
->nBytesPerLine
;
154 LONG nLineLen
= min(infoPtr
->cbData
- nLine
* infoPtr
->nBytesPerLine
, infoPtr
->nBytesPerLine
);
155 LPWSTR lpszLine
= HexEdit_GetLineText(nLine
* infoPtr
->nBytesPerLine
, infoPtr
->pData
, nLineLen
, infoPtr
->nBytesPerLine
- nLineLen
);
158 /* calculate offset of character caret is on in the line */
159 if (infoPtr
->bFocusHex
)
160 nCharOffset
= 6 + nByteLinePos
*3 + infoPtr
->nCaretPos
% 2;
162 nCharOffset
= 6 + infoPtr
->nBytesPerLine
*3 + DIV_SPACES
+ nByteLinePos
;
164 hdc
= GetDC(infoPtr
->hwndSelf
);
165 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
167 GetTextExtentPoint32W(hdc
, lpszLine
, nCharOffset
, &size
);
169 SelectObject(hdc
, hOldFont
);
170 ReleaseDC(infoPtr
->hwndSelf
, hdc
);
172 if (!nLineLen
) size
.cx
= 0;
177 GetSystemMetrics(SM_CXBORDER
) + size
.cx
,
178 GetSystemMetrics(SM_CYBORDER
) + (nLine
- infoPtr
->nScrollPos
) * infoPtr
->nHeight
);
182 HexEdit_UpdateScrollbars(HEXEDIT_INFO
*infoPtr
)
185 INT nLines
= infoPtr
->cbData
/ infoPtr
->nBytesPerLine
;
189 GetClientRect(infoPtr
->hwndSelf
, &rcClient
);
190 InflateRect(&rcClient
, -GetSystemMetrics(SM_CXBORDER
), -GetSystemMetrics(SM_CYBORDER
));
192 nVisibleLines
= (rcClient
.bottom
- rcClient
.top
) / infoPtr
->nHeight
;
193 si
.cbSize
= sizeof(si
);
194 si
.fMask
= SIF_RANGE
| SIF_PAGE
;
196 si
.nMax
= max(nLines
- nVisibleLines
, nLines
);
197 si
.nPage
= nVisibleLines
;
198 SetScrollInfo(infoPtr
->hwndSelf
, SB_VERT
, &si
, TRUE
);
202 HexEdit_EnsureVisible(HEXEDIT_INFO
*infoPtr
, INT nCaretPos
)
204 INT nLine
= nCaretPos
/ (2 * infoPtr
->nBytesPerLine
);
207 si
.cbSize
= sizeof(si
);
208 si
.fMask
= SIF_POS
| SIF_PAGE
;
209 GetScrollInfo(infoPtr
->hwndSelf
, SB_VERT
, &si
);
212 else if (nLine
>= si
.nPos
+ si
.nPage
)
213 si
.nPos
= nLine
- si
.nPage
+ 1;
218 SetScrollInfo(infoPtr
->hwndSelf
, SB_VERT
, &si
, FALSE
);
219 SendMessageW(infoPtr
->hwndSelf
, WM_VSCROLL
, MAKELONG(SB_THUMBPOSITION
, 0), 0);
224 HexEdit_SetData(HEXEDIT_INFO
*infoPtr
, INT cbData
, const BYTE
*pData
)
226 free(infoPtr
->pData
);
229 infoPtr
->pData
= malloc(cbData
);
230 memcpy(infoPtr
->pData
, pData
, cbData
);
231 infoPtr
->cbData
= cbData
;
233 infoPtr
->nCaretPos
= 0;
234 HexEdit_UpdateScrollbars(infoPtr
);
235 HexEdit_UpdateCaret(infoPtr
);
236 InvalidateRect(infoPtr
->hwndSelf
, NULL
, TRUE
);
241 HexEdit_GetData(HEXEDIT_INFO
*infoPtr
, INT cbData
, BYTE
*pData
)
244 memcpy(pData
, infoPtr
->pData
, min(cbData
, infoPtr
->cbData
));
245 return infoPtr
->cbData
;
248 static inline LRESULT
249 HexEdit_Char (HEXEDIT_INFO
*infoPtr
, WCHAR ch
)
251 INT nCaretBytePos
= infoPtr
->nCaretPos
/2;
253 assert(nCaretBytePos
>= 0);
255 /* backspace is special */
258 if (infoPtr
->nCaretPos
== 0)
261 /* if at end of byte then delete the whole byte */
262 if (infoPtr
->bFocusHex
&& (infoPtr
->nCaretPos
% 2 == 0))
264 memmove(infoPtr
->pData
+ nCaretBytePos
- 1,
265 infoPtr
->pData
+ nCaretBytePos
,
266 infoPtr
->cbData
- nCaretBytePos
);
268 infoPtr
->nCaretPos
-= 2; /* backtrack two nibble */
270 else /* blank upper nibble */
272 infoPtr
->pData
[nCaretBytePos
] &= 0x0f;
273 infoPtr
->nCaretPos
--; /* backtrack one nibble */
278 if (infoPtr
->bFocusHex
&& hexchar_to_byte(ch
) == (BYTE
)-1)
280 MessageBeep(MB_ICONWARNING
);
284 if ((infoPtr
->bInsert
&& (infoPtr
->nCaretPos
% 2 == 0)) || (nCaretBytePos
>= infoPtr
->cbData
))
286 /* make room for another byte */
288 infoPtr
->pData
= realloc(infoPtr
->pData
, infoPtr
->cbData
+ 1);
290 /* move everything after caret up one byte */
291 memmove(infoPtr
->pData
+ nCaretBytePos
+ 1,
292 infoPtr
->pData
+ nCaretBytePos
,
293 infoPtr
->cbData
- nCaretBytePos
);
295 infoPtr
->pData
[nCaretBytePos
] = 0x0;
298 /* overwrite a byte */
300 assert(nCaretBytePos
< infoPtr
->cbData
);
302 if (infoPtr
->bFocusHex
)
304 BYTE orig_byte
= infoPtr
->pData
[nCaretBytePos
];
305 BYTE digit
= hexchar_to_byte(ch
);
306 if (infoPtr
->nCaretPos
% 2) /* set low nibble */
307 infoPtr
->pData
[nCaretBytePos
] = (orig_byte
& 0xf0) | digit
;
308 else /* set high nibble */
309 infoPtr
->pData
[nCaretBytePos
] = (orig_byte
& 0x0f) | digit
<< 4;
310 infoPtr
->nCaretPos
++; /* advance one nibble */
314 infoPtr
->pData
[nCaretBytePos
] = (BYTE
)ch
;
315 infoPtr
->nCaretPos
+= 2; /* advance two nibbles */
319 HexEdit_UpdateScrollbars(infoPtr
);
320 InvalidateRect(infoPtr
->hwndSelf
, NULL
, TRUE
);
321 HexEdit_UpdateCaret(infoPtr
);
322 HexEdit_EnsureVisible(infoPtr
, infoPtr
->nCaretPos
);
326 static inline LRESULT
327 HexEdit_Destroy (HEXEDIT_INFO
*infoPtr
)
329 HWND hwnd
= infoPtr
->hwndSelf
;
330 free(infoPtr
->pData
);
333 SetWindowLongPtrW(hwnd
, 0, 0);
337 static inline LRESULT
338 HexEdit_GetFont (HEXEDIT_INFO
*infoPtr
)
340 return (LRESULT
)infoPtr
->hFont
;
343 static inline LRESULT
344 HexEdit_KeyDown (HEXEDIT_INFO
*infoPtr
, DWORD key
, DWORD flags
)
346 INT nInc
= (infoPtr
->bFocusHex
) ? 1 : 2;
352 infoPtr
->nCaretPos
-= nInc
;
353 if (infoPtr
->nCaretPos
< 0)
354 infoPtr
->nCaretPos
= 0;
357 infoPtr
->nCaretPos
+= nInc
;
358 if (infoPtr
->nCaretPos
> infoPtr
->cbData
*2)
359 infoPtr
->nCaretPos
= infoPtr
->cbData
*2;
362 if ((infoPtr
->nCaretPos
- infoPtr
->nBytesPerLine
*2) >= 0)
363 infoPtr
->nCaretPos
-= infoPtr
->nBytesPerLine
*2;
366 if ((infoPtr
->nCaretPos
+ infoPtr
->nBytesPerLine
*2) <= infoPtr
->cbData
*2)
367 infoPtr
->nCaretPos
+= infoPtr
->nBytesPerLine
*2;
370 infoPtr
->nCaretPos
= 0;
373 infoPtr
->nCaretPos
= infoPtr
->cbData
*2;
375 case VK_PRIOR
: /* page up */
376 si
.cbSize
= sizeof(si
);
378 GetScrollInfo(infoPtr
->hwndSelf
, SB_VERT
, &si
);
379 if ((infoPtr
->nCaretPos
- (INT
)si
.nPage
*infoPtr
->nBytesPerLine
*2) >= 0)
380 infoPtr
->nCaretPos
-= si
.nPage
*infoPtr
->nBytesPerLine
*2;
382 infoPtr
->nCaretPos
= 0;
384 case VK_NEXT
: /* page down */
385 si
.cbSize
= sizeof(si
);
387 GetScrollInfo(infoPtr
->hwndSelf
, SB_VERT
, &si
);
388 if ((infoPtr
->nCaretPos
+ (INT
)si
.nPage
*infoPtr
->nBytesPerLine
*2) <= infoPtr
->cbData
*2)
389 infoPtr
->nCaretPos
+= si
.nPage
*infoPtr
->nBytesPerLine
*2;
391 infoPtr
->nCaretPos
= infoPtr
->cbData
*2;
397 HexEdit_UpdateCaret(infoPtr
);
398 HexEdit_EnsureVisible(infoPtr
, infoPtr
->nCaretPos
);
404 static inline LRESULT
405 HexEdit_KillFocus (HEXEDIT_INFO
*infoPtr
, HWND receiveFocus
)
407 infoPtr
->bFocus
= FALSE
;
414 static inline LRESULT
415 HexEdit_LButtonDown (HEXEDIT_INFO
*infoPtr
)
417 SetFocus(infoPtr
->hwndSelf
);
419 /* FIXME: hittest and set caret */
425 static inline LRESULT
HexEdit_NCCreate (HWND hwnd
, LPCREATESTRUCTW lpcs
)
427 HEXEDIT_INFO
*infoPtr
;
428 SetWindowLongW(hwnd
, GWL_EXSTYLE
,
429 lpcs
->dwExStyle
| WS_EX_CLIENTEDGE
);
431 /* allocate memory for info structure */
432 infoPtr
= malloc(sizeof(HEXEDIT_INFO
));
433 memset(infoPtr
, 0, sizeof(HEXEDIT_INFO
));
434 SetWindowLongPtrW(hwnd
, 0, (DWORD_PTR
)infoPtr
);
436 /* initialize info structure */
437 infoPtr
->nCaretPos
= 0;
438 infoPtr
->hwndSelf
= hwnd
;
439 infoPtr
->nBytesPerLine
= 2;
440 infoPtr
->bFocusHex
= TRUE
;
441 infoPtr
->bInsert
= TRUE
;
443 return DefWindowProcW(infoPtr
->hwndSelf
, WM_NCCREATE
, 0, (LPARAM
)lpcs
);
446 static inline LRESULT
447 HexEdit_SetFocus (HEXEDIT_INFO
*infoPtr
, HWND lostFocus
)
449 infoPtr
->bFocus
= TRUE
;
451 CreateCaret(infoPtr
->hwndSelf
, NULL
, 1, infoPtr
->nHeight
);
452 HexEdit_UpdateCaret(infoPtr
);
453 ShowCaret(infoPtr
->hwndSelf
);
459 static inline LRESULT
460 HexEdit_SetFont (HEXEDIT_INFO
*infoPtr
, HFONT hFont
, BOOL redraw
)
464 HFONT hOldFont
= NULL
;
468 infoPtr
->hFont
= hFont
;
470 hdc
= GetDC(infoPtr
->hwndSelf
);
472 hOldFont
= SelectObject(hdc
, infoPtr
->hFont
);
474 GetTextMetricsW(hdc
, &tm
);
475 infoPtr
->nHeight
= tm
.tmHeight
+ tm
.tmExternalLeading
;
477 GetClientRect(infoPtr
->hwndSelf
, &rcClient
);
481 BYTE
*pData
= malloc(i
);
486 lpszLine
= HexEdit_GetLineText(0, pData
, i
, 0);
487 GetTextExtentPoint32W(hdc
, lpszLine
, lstrlenW(lpszLine
), &size
);
490 if (size
.cx
> (rcClient
.right
- rcClient
.left
))
492 infoPtr
->nBytesPerLine
= i
- 1;
497 HexEdit_UpdateScrollbars(infoPtr
);
500 SelectObject(hdc
, hOldFont
);
501 ReleaseDC (infoPtr
->hwndSelf
, hdc
);
504 InvalidateRect(infoPtr
->hwndSelf
, NULL
, TRUE
);
509 static inline LRESULT
510 HexEdit_VScroll (HEXEDIT_INFO
*infoPtr
, INT action
)
514 /* get all scroll bar info */
515 si
.cbSize
= sizeof(si
);
517 GetScrollInfo(infoPtr
->hwndSelf
, SB_VERT
, &si
);
519 switch (LOWORD(action
))
521 case SB_TOP
: /* user pressed the home key */
525 case SB_BOTTOM
: /* user pressed the end key */
529 case SB_LINEUP
: /* user clicked the up arrow */
533 case SB_LINEDOWN
: /* user clicked the down arrow */
537 case SB_PAGEUP
: /* user clicked the scroll bar above the scroll thumb */
541 case SB_PAGEDOWN
: /* user clicked the scroll bar below the scroll thumb */
545 case SB_THUMBTRACK
: /* user dragged the scroll thumb */
546 si
.nPos
= si
.nTrackPos
;
553 /* set the position and then retrieve it to let the system handle the
554 * cases where the position is out of range */
556 SetScrollInfo(infoPtr
->hwndSelf
, SB_VERT
, &si
, TRUE
);
557 GetScrollInfo(infoPtr
->hwndSelf
, SB_VERT
, &si
);
559 if (si
.nPos
!= infoPtr
->nScrollPos
)
561 ScrollWindow(infoPtr
->hwndSelf
, 0, infoPtr
->nHeight
* (infoPtr
->nScrollPos
- si
.nPos
), NULL
, NULL
);
562 infoPtr
->nScrollPos
= si
.nPos
;
563 UpdateWindow(infoPtr
->hwndSelf
);
565 /* need to update caret position since it depends on the scroll position */
566 HexEdit_UpdateCaret(infoPtr
);
572 static LRESULT WINAPI
573 HexEdit_WindowProc (HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
575 HEXEDIT_INFO
*infoPtr
= (HEXEDIT_INFO
*)GetWindowLongPtrW(hwnd
, 0);
577 if (!infoPtr
&& (uMsg
!= WM_NCCREATE
))
578 return DefWindowProcW(hwnd
, uMsg
, wParam
, lParam
);
583 return HexEdit_SetData (infoPtr
, (INT
)wParam
, (const BYTE
*)lParam
);
586 return HexEdit_GetData (infoPtr
, (INT
)wParam
, (BYTE
*)lParam
);
589 return HexEdit_Char (infoPtr
, (WCHAR
)wParam
);
592 return HexEdit_Destroy (infoPtr
);
595 return DLGC_WANTCHARS
| DLGC_WANTARROWS
;
598 return HexEdit_GetFont (infoPtr
);
601 return HexEdit_KeyDown (infoPtr
, wParam
, lParam
);
604 return HexEdit_KillFocus (infoPtr
, (HWND
)wParam
);
607 return HexEdit_LButtonDown (infoPtr
);
610 return HexEdit_NCCreate (hwnd
, (LPCREATESTRUCTW
)lParam
);
613 HexEdit_Paint(infoPtr
);
617 return HexEdit_SetFocus (infoPtr
, (HWND
)wParam
);
620 return HexEdit_SetFont (infoPtr
, (HFONT
)wParam
, LOWORD(lParam
));
623 return HexEdit_VScroll (infoPtr
, (INT
)wParam
);
626 return DefWindowProcW(hwnd
, uMsg
, wParam
, lParam
);
631 void HexEdit_Register(void)
635 ZeroMemory(&wndClass
, sizeof(WNDCLASSW
));
637 wndClass
.lpfnWndProc
= HexEdit_WindowProc
;
638 wndClass
.cbClsExtra
= 0;
639 wndClass
.cbWndExtra
= sizeof(HEXEDIT_INFO
*);
640 wndClass
.hCursor
= LoadCursorW(0, (const WCHAR
*)IDC_IBEAM
);
641 wndClass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
642 wndClass
.lpszClassName
= L
"HexEdit";
644 RegisterClassW(&wndClass
);