4 * Copyright (C) 2004 Thomas Weidenmueller <w3seek@reactos.com>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
37 DWORD nVisibleLinesComplete
;
52 } HEXEDIT_DATA
, *PHEXEDIT_DATA
;
55 #define HEHT_LEFTMARGIN (0x1)
56 #define HEHT_ADDRESS (0x2)
57 #define HEHT_ADDRESSSPACING (0x3)
58 #define HEHT_HEXDUMP (0x4)
59 #define HEHT_HEXDUMPSPACING (0x5)
60 #define HEHT_ASCIIDUMP (0x6)
61 #define HEHT_RIGHTMARGIN (0x7)
63 LRESULT WINAPI
HexEditWndProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
67 RegisterHexEditorClass(HINSTANCE hInstance
)
71 ZeroMemory(&WndClass
, sizeof(WNDCLASSEX
));
72 WndClass
.cbSize
= sizeof(WNDCLASSEX
);
73 WndClass
.style
= CS_DBLCLKS
;
74 WndClass
.lpfnWndProc
= (WNDPROC
)HexEditWndProc
;
75 WndClass
.cbWndExtra
= sizeof(PHEXEDIT_DATA
);
76 WndClass
.hInstance
= hInstance
;
77 WndClass
.hCursor
= LoadCursor(0, IDC_IBEAM
);
78 WndClass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
79 WndClass
.lpszClassName
= HEX_EDIT_CLASS_NAME
;
81 return RegisterClassEx(&WndClass
);
86 UnregisterHexEditorClass(HINSTANCE hInstance
)
88 return UnregisterClass(HEX_EDIT_CLASS_NAME
, hInstance
);
91 /*** Helper functions *********************************************************/
94 HEXEDIT_MoveCaret(PHEXEDIT_DATA hed
, BOOL Scroll
)
98 si
.cbSize
= sizeof(SCROLLINFO
);
100 GetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
);
104 if(si
.nPos
> hed
->CaretLine
)
106 si
.nPos
= hed
->CaretLine
;
107 SetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
, TRUE
);
108 GetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
);
109 InvalidateRect(hed
->hWndSelf
, NULL
, TRUE
);
111 else if(hed
->CaretLine
>= (hed
->nVisibleLinesComplete
+ si
.nPos
))
113 si
.nPos
= hed
->CaretLine
- hed
->nVisibleLinesComplete
+ 1;
114 SetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
, TRUE
);
115 GetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
);
116 InvalidateRect(hed
->hWndSelf
, NULL
, TRUE
);
120 if(hed
->EditingField
)
121 SetCaretPos(hed
->LeftMargin
+ ((4 + hed
->AddressSpacing
+ (3 * hed
->CaretCol
)) * hed
->CharWidth
) - 1, (hed
->CaretLine
- si
.nPos
) * hed
->LineHeight
);
123 SetCaretPos(hed
->LeftMargin
+ ((4 + hed
->AddressSpacing
+ hed
->SplitSpacing
+ (3 * hed
->ColumnsPerLine
) + hed
->CaretCol
) * hed
->CharWidth
) - 2, (hed
->CaretLine
- si
.nPos
) * hed
->LineHeight
);
127 HEXEDIT_Update(PHEXEDIT_DATA hed
)
132 DWORD bufsize
, cvislines
;
134 GetClientRect(hed
->hWndSelf
, &rcClient
);
135 hed
->style
= GetWindowLong(hed
->hWndSelf
, GWL_STYLE
);
137 bufsize
= (hed
->hBuffer
? LocalSize(hed
->hBuffer
) : 0);
138 hed
->nLines
= max(bufsize
/ hed
->ColumnsPerLine
, 1);
139 if(bufsize
> hed
->ColumnsPerLine
&& (bufsize
% hed
->ColumnsPerLine
) > 0)
144 if(hed
->LineHeight
> 0)
146 hed
->nVisibleLinesComplete
= cvislines
= rcClient
.bottom
/ hed
->LineHeight
;
147 hed
->nVisibleLines
= hed
->nVisibleLinesComplete
;
148 if(rcClient
.bottom
% hed
->LineHeight
)
150 hed
->nVisibleLines
++;
155 hed
->nVisibleLines
= cvislines
= 0;
158 SbVisible
= bufsize
> 0 && cvislines
< hed
->nLines
;
159 ShowScrollBar(hed
->hWndSelf
, SB_VERT
, SbVisible
);
161 /* update scrollbar */
162 si
.cbSize
= sizeof(SCROLLINFO
);
163 si
.fMask
= SIF_RANGE
| SIF_PAGE
;
165 si
.nMax
= ((bufsize
> 0) ? hed
->nLines
- 1 : 0);
166 si
.nPage
= ((hed
->LineHeight
> 0) ? rcClient
.bottom
/ hed
->LineHeight
: 0);
167 SetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
, TRUE
);
169 if(IsWindowVisible(hed
->hWndSelf
) && SbVisible
!= hed
->SbVisible
)
171 InvalidateRect(hed
->hWndSelf
, NULL
, TRUE
);
174 hed
->SbVisible
= SbVisible
;
178 HEXEDIT_GetFixedFont(VOID
)
181 GetObject(GetStockObject(ANSI_FIXED_FONT
), sizeof(LOGFONT
), &lf
);
182 return CreateFontIndirect(&lf
);
186 HEXEDIT_PaintLines(PHEXEDIT_DATA hed
, HDC hDC
, DWORD ScrollPos
, DWORD First
, DWORD Last
, RECT
*rc
)
188 DWORD x
, dx
, dy
, linestart
;
189 PBYTE buf
, current
, end
, line
;
191 TCHAR hex
[3], addr
[17];
194 FillRect(hDC
, rc
, (HBRUSH
)(COLOR_WINDOW
+ 1));
198 bufsize
= LocalSize(hed
->hBuffer
);
199 buf
= LocalLock(hed
->hBuffer
);
206 if(ScrollPos
+ First
== 0)
209 _stprintf(addr
, _T("%04X"), 0);
210 TextOut(hDC
, hed
->LeftMargin
, First
* hed
->LineHeight
, addr
, 4);
217 dy
= First
* hed
->LineHeight
;
218 linestart
= (ScrollPos
+ First
) * hed
->ColumnsPerLine
;
219 current
= buf
+ linestart
;
220 Last
= min(hed
->nLines
- ScrollPos
, Last
);
222 while(First
<= Last
&& current
< end
)
226 dx
= hed
->LeftMargin
;
229 _stprintf(addr
, _T("%04X"), linestart
);
230 TextOut(hDC
, dx
, dy
, addr
, 4);
232 dx
+= ((4 + hed
->AddressSpacing
) * hed
->CharWidth
);
233 dh
= (3 * hed
->CharWidth
);
237 rct
.right
= rct
.left
+ dh
;
238 rct
.bottom
= dy
+ hed
->LineHeight
;
241 dx
+= (hed
->CharWidth
/ 2);
243 for(x
= 0; x
< hed
->ColumnsPerLine
&& current
< end
; x
++)
248 _stprintf(hex
, _T("%02X"), *(current
++));
249 ExtTextOut(hDC
, dx
, dy
, ETO_OPAQUE
, &rct
, hex
, 2, NULL
);
254 dx
= ((4 + hed
->AddressSpacing
+ hed
->SplitSpacing
+ (hed
->ColumnsPerLine
* 3)) * hed
->CharWidth
);
256 for(x
= 0; x
< hed
->ColumnsPerLine
&& current
< end
; x
++)
258 _stprintf(hex
, _T("%C"), *(current
++));
259 hex
[0] = ((hex
[0] & _T('\x007f')) >= _T(' ') ? hex
[0] : _T('.'));
260 TextOut(hDC
, dx
, dy
, hex
, 1);
261 dx
+= hed
->CharWidth
;
264 dy
+= hed
->LineHeight
;
265 linestart
+= hed
->ColumnsPerLine
;
270 LocalUnlock(hed
->hBuffer
);
274 HEXEDIT_HitRegionTest(PHEXEDIT_DATA hed
, POINTS pt
, POINTS
*ptClient
)
279 wi
.cbSize
= sizeof(WINDOWINFO
);
280 GetWindowInfo(hed
->hWndSelf
, &wi
);
282 x
= pt
.x
- wi
.rcClient
.left
;
286 ptClient
->y
= pt
.y
- wi
.rcClient
.top
;
289 if(x
<= hed
->LeftMargin
)
291 return HEHT_LEFTMARGIN
;
294 x
-= hed
->LeftMargin
;
295 d
= (4 * hed
->CharWidth
);
302 d
= (hed
->AddressSpacing
* hed
->CharWidth
);
305 return HEHT_ADDRESSSPACING
;
309 d
= (3 * hed
->ColumnsPerLine
* hed
->CharWidth
);
316 d
= (hed
->SplitSpacing
* hed
->CharWidth
);
319 return HEHT_HEXDUMPSPACING
;
323 d
= (hed
->ColumnsPerLine
* hed
->CharWidth
);
326 return HEHT_ASCIIDUMP
;
329 return HEHT_RIGHTMARGIN
;
333 HEXEDIT_PositionFromPoint(PHEXEDIT_DATA hed
, POINTS pt
, DWORD
*Hit
)
339 *Hit
= HEXEDIT_HitRegionTest(hed
, pt
, &ptClient
);
341 si
.cbSize
= sizeof(SCROLLINFO
);
343 GetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
);
345 if(hed
->LineHeight
> 0)
347 Line
= min(si
.nPos
+ (ptClient
.y
/ hed
->LineHeight
), hed
->nLines
);
354 case HEHT_LEFTMARGIN
:
356 case HEHT_ADDRESSSPACING
:
357 return Line
* hed
->ColumnsPerLine
;
365 case HEHT_RIGHTMARGIN
:
366 return (Line
+ 1) * hed
->ColumnsPerLine
;
370 /*** Control specific messages ************************************************/
373 HEXEDIT_HEM_LOADBUFFER(PHEXEDIT_DATA hed
, PVOID Buffer
, DWORD Size
)
375 if(Buffer
!= NULL
&& Size
> 0)
379 if(hed
->MaxBuffer
> 0 && Size
> hed
->MaxBuffer
)
381 Size
= hed
->MaxBuffer
;
388 if(LocalSize(hed
->hBuffer
) != Size
)
390 hed
->hBuffer
= LocalReAlloc(hed
->hBuffer
, Size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
395 hed
->hBuffer
= LocalFree(hed
->hBuffer
);
404 hed
->hBuffer
= LocalAlloc(LHND
, Size
);
409 buf
= LocalLock(hed
->hBuffer
);
412 memcpy(buf
, Buffer
, Size
);
416 LocalUnlock(hed
->hBuffer
);
423 else if(hed
->hBuffer
)
426 hed
->hBuffer
= LocalFree(hed
->hBuffer
);
434 HEXEDIT_HEM_COPYBUFFER(PHEXEDIT_DATA hed
, PVOID Buffer
, DWORD Size
)
443 if(Buffer
!= NULL
&& Size
> 0)
445 nCpy
= min(Size
, LocalSize(hed
->hBuffer
));
450 buf
= LocalLock(hed
->hBuffer
);
453 memcpy(Buffer
, buf
, nCpy
);
457 LocalUnlock(hed
->hBuffer
);
462 return (LRESULT
)LocalSize(hed
->hBuffer
);
466 HEXEDIT_HEM_SETMAXBUFFERSIZE(PHEXEDIT_DATA hed
, DWORD nMaxSize
)
468 hed
->MaxBuffer
= nMaxSize
;
469 if(hed
->MaxBuffer
> 0 && hed
->hBuffer
&& LocalSize(hed
->hBuffer
) > hed
->MaxBuffer
)
471 /* truncate the buffer */
472 hed
->hBuffer
= LocalReAlloc(hed
->hBuffer
, hed
->MaxBuffer
, LMEM_MOVEABLE
);
477 /*** Message Proc *************************************************************/
480 HEXEDIT_WM_NCCREATE(HWND hWnd
, CREATESTRUCT
*cs
)
484 if(!(hed
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(HEXEDIT_DATA
))))
489 hed
->hWndSelf
= hWnd
;
490 hed
->hWndParent
= cs
->hwndParent
;
491 hed
->style
= cs
->style
;
493 hed
->ColumnsPerLine
= 8;
495 hed
->AddressSpacing
= 2;
496 hed
->SplitSpacing
= 2;
497 hed
->EditingField
= TRUE
; /* in hexdump field */
499 SetWindowLong(hWnd
, 0, (LONG
)hed
);
506 HEXEDIT_WM_NCDESTROY(PHEXEDIT_DATA hed
)
510 while(LocalUnlock(hed
->hBuffer
));
511 LocalFree(hed
->hBuffer
);
516 DeleteObject(hed
->hFont
);
519 SetWindowLong(hed
->hWndSelf
, 0, 0);
520 HeapFree(GetProcessHeap(), 0, hed
);
526 HEXEDIT_WM_CREATE(PHEXEDIT_DATA hed
)
532 HEXEDIT_WM_SETFOCUS(PHEXEDIT_DATA hed
)
534 CreateCaret(hed
->hWndSelf
, 0, 1, hed
->LineHeight
);
535 HEXEDIT_MoveCaret(hed
, FALSE
);
536 ShowCaret(hed
->hWndSelf
);
541 HEXEDIT_WM_KILLFOCUS(PHEXEDIT_DATA hed
)
548 HEXEDIT_WM_VSCROLL(PHEXEDIT_DATA hed
, WORD ThumbPosition
, WORD SbCmd
)
553 ZeroMemory(&si
, sizeof(SCROLLINFO
));
554 si
.cbSize
= sizeof(SCROLLINFO
);
556 GetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
);
586 si
.nPos
= si
.nTrackPos
;
591 SetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
, TRUE
);
592 GetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
);
594 if(si
.nPos
!= ScrollY
)
596 ScrollWindow(hed
->hWndSelf
, 0, (ScrollY
- si
.nPos
) * hed
->LineHeight
, NULL
, NULL
);
597 UpdateWindow(hed
->hWndSelf
);
604 HEXEDIT_WM_SETFONT(PHEXEDIT_DATA hed
, HFONT hFont
, BOOL bRedraw
)
612 hFont
= HEXEDIT_GetFixedFont();
616 hDC
= GetDC(hed
->hWndSelf
);
619 hOldFont
= SelectObject(hDC
, hFont
);
621 GetTextMetrics(hDC
, &tm
);
622 hed
->LineHeight
= tm
.tmHeight
;
623 hed
->CharWidth
= tm
.tmAveCharWidth
;
626 SelectObject(hDC
, hOldFont
);
628 ReleaseDC(hed
->hWndSelf
, hDC
);
632 InvalidateRect(hed
->hWndSelf
, NULL
, TRUE
);
639 HEXEDIT_WM_GETFONT(PHEXEDIT_DATA hed
)
641 return (LRESULT
)hed
->hFont
;
645 HEXEDIT_WM_PAINT(PHEXEDIT_DATA hed
)
650 HBITMAP hbmp
, hbmpold
;
651 DWORD nLines
, nFirst
;
656 if(GetUpdateRect(hed
->hWndSelf
, &rc
, FALSE
) && (hed
->LineHeight
> 0))
658 ZeroMemory(&si
, sizeof(SCROLLINFO
));
659 si
.cbSize
= sizeof(SCROLLINFO
);
661 GetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
);
663 height
= (rc
.bottom
- rc
.top
);
664 nLines
= height
/ hed
->LineHeight
;
665 if((height
% hed
->LineHeight
) > 0)
669 if(nLines
> hed
->nLines
- si
.nPos
)
671 nLines
= hed
->nLines
- si
.nPos
;
673 nFirst
= rc
.top
/ hed
->LineHeight
;
675 BeginPaint(hed
->hWndSelf
, &ps
);
676 if(!(hTempDC
= CreateCompatibleDC(ps
.hdc
)))
678 FillRect(ps
.hdc
, &rc
, (HBRUSH
)(COLOR_WINDOW
+ 1));
681 if(!(hbmp
= CreateCompatibleBitmap(hTempDC
, ps
.rcPaint
.right
, ps
.rcPaint
.bottom
)))
683 FillRect(ps
.hdc
, &rc
, (HBRUSH
)(COLOR_WINDOW
+ 1));
687 hbmpold
= SelectObject(hTempDC
, hbmp
);
688 hOldFont
= SelectObject(hTempDC
, hed
->hFont
);
689 HEXEDIT_PaintLines(hed
, hTempDC
, si
.nPos
, nFirst
, nFirst
+ nLines
, &ps
.rcPaint
);
690 BitBlt(ps
.hdc
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, hTempDC
, rc
.left
, rc
.top
, SRCCOPY
);
691 SelectObject(hTempDC
, hOldFont
);
692 SelectObject(hTempDC
, hbmpold
);
698 EndPaint(hed
->hWndSelf
, &ps
);
705 HEXEDIT_WM_MOUSEWHEEL(PHEXEDIT_DATA hed
, int cyMoveLines
, WORD ButtonsDown
, LPPOINTS MousePos
)
710 SetFocus(hed
->hWndSelf
);
712 si
.cbSize
= sizeof(SCROLLINFO
);
714 GetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
);
719 si
.nPos
+= cyMoveLines
;
720 SetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
, TRUE
);
722 GetScrollInfo(hed
->hWndSelf
, SB_VERT
, &si
);
723 if(si
.nPos
!= ScrollY
)
725 ScrollWindow(hed
->hWndSelf
, 0, (ScrollY
- si
.nPos
) * hed
->LineHeight
, NULL
, NULL
);
726 UpdateWindow(hed
->hWndSelf
);
733 HEXEDIT_WM_GETDLGCODE(LPMSG Msg
)
735 return DLGC_WANTARROWS
| DLGC_WANTCHARS
;
739 HEXEDIT_WM_KEYDOWN(PHEXEDIT_DATA hed
, INT VkCode
)
744 if(GetKeyState(VK_MENU
) & 0x8000)
749 shift
= GetKeyState(VK_SHIFT
) & 0x8000;
750 control
= GetKeyState(VK_CONTROL
) & 0x8000;
752 bufsize
= (hed
->hBuffer
? LocalSize(hed
->hBuffer
) : 0);
757 if(hed
->Position
> 0)
759 if(--hed
->CaretCol
< 0)
762 hed
->CaretCol
= hed
->ColumnsPerLine
;
767 HEXEDIT_MoveCaret(hed
, TRUE
);
771 if(hed
->Position
< bufsize
)
773 if(++hed
->CaretCol
> hed
->ColumnsPerLine
)
781 HEXEDIT_MoveCaret(hed
, TRUE
);
785 if(hed
->Position
> 0)
787 if(hed
->CaretLine
<= 0)
795 hed
->Position
-= hed
->ColumnsPerLine
;
798 HEXEDIT_MoveCaret(hed
, TRUE
);
802 if(hed
->Position
<= bufsize
)
804 if(hed
->CaretLine
< hed
->nLines
- 1)
806 hed
->Position
+= hed
->ColumnsPerLine
;
808 if(hed
->Position
> bufsize
)
810 hed
->Position
= bufsize
;
811 hed
->CaretLine
= (hed
->nLines
> 0 ? hed
->nLines
- 1 : 0);
812 hed
->CaretCol
= bufsize
% hed
->ColumnsPerLine
;
817 INT tmp
= bufsize
% hed
->ColumnsPerLine
;
818 hed
->Position
= bufsize
;
819 hed
->CaretCol
= (tmp
== 0 ? hed
->ColumnsPerLine
: tmp
);
822 HEXEDIT_MoveCaret(hed
, TRUE
);
830 HEXEDIT_WM_SIZE(PHEXEDIT_DATA hed
, DWORD sType
, WORD NewWidth
, WORD NewHeight
)
838 HexEditWndProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
842 hed
= (PHEXEDIT_DATA
)GetWindowLong(hWnd
, 0);
849 return HEXEDIT_WM_PAINT(hed
);
852 return HEXEDIT_WM_KEYDOWN(hed
, (INT
)wParam
);
855 return HEXEDIT_WM_VSCROLL(hed
, HIWORD(wParam
), LOWORD(wParam
));
858 return HEXEDIT_WM_SIZE(hed
, (DWORD
)wParam
, LOWORD(lParam
), HIWORD(lParam
));
862 UINT nScrollLines
= 3;
865 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
, 0, &nScrollLines
, 0);
866 delta
-= (SHORT
)HIWORD(wParam
);
867 if(abs(delta
) >= WHEEL_DELTA
&& nScrollLines
!= 0)
869 return HEXEDIT_WM_MOUSEWHEEL(hed
, nScrollLines
* (delta
/ WHEEL_DELTA
), LOWORD(wParam
), &MAKEPOINTS(lParam
));
875 return HEXEDIT_HEM_LOADBUFFER(hed
, (PVOID
)wParam
, (DWORD
)lParam
);
878 return HEXEDIT_HEM_COPYBUFFER(hed
, (PVOID
)wParam
, (DWORD
)lParam
);
880 case HEM_SETMAXBUFFERSIZE
:
881 return HEXEDIT_HEM_SETMAXBUFFERSIZE(hed
, (DWORD
)lParam
);
884 return HEXEDIT_WM_SETFOCUS(hed
);
887 return HEXEDIT_WM_KILLFOCUS(hed
);
890 return HEXEDIT_WM_GETDLGCODE((LPMSG
)lParam
);
893 return HEXEDIT_WM_SETFONT(hed
, (HFONT
)wParam
, (BOOL
)LOWORD(lParam
));
896 return HEXEDIT_WM_GETFONT(hed
);
899 return HEXEDIT_WM_CREATE(hed
);
904 return HEXEDIT_WM_NCCREATE(hWnd
, (CREATESTRUCT
*)lParam
);
911 return HEXEDIT_WM_NCDESTROY(hed
);
916 return CallWindowProc(DefWindowProc
, hWnd
, uMsg
, wParam
, lParam
);