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 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
43 /* spaces dividing hex and ASCII */
46 typedef struct tagHEXEDIT_INFO
51 BOOL bFocusHex : 1; /* TRUE if focus is on hex, FALSE if focus on ASCII */
52 BOOL bInsert : 1; /* insert mode if TRUE, overwrite mode if FALSE */
53 INT nHeight; /* height of text */
54 INT nCaretPos; /* caret pos in nibbles */
57 INT nBytesPerLine; /* bytes of hex to display per line of the control */
58 INT nScrollPos; /* first visible line */
61 static inline LRESULT HexEdit_SetFont (HEXEDIT_INFO *infoPtr, HFONT hFont, BOOL redraw);
63 static inline BYTE hexchar_to_byte(TCHAR ch)
65 if (ch >= '0' && ch <= '9')
67 else if (ch >= 'a' && ch <= 'f')
69 else if (ch >= 'A' && ch <= 'F')
75 static LPTSTR HexEdit_GetLineText(BYTE *pData, LONG cbData, LONG pad)
77 LPTSTR lpszLine = HeapAlloc(GetProcessHeap(), 0,
78 (cbData * 3 + pad * 3 + DIV_SPACES + cbData + 1) * sizeof(TCHAR));
84 for (i = 0; i < cbData; i++)
85 wsprintf(lpszLine + i*3, TEXT("%02X "), pData[i]);
86 for (i = 0; i < pad * 3; i++)
87 lpszLine[cbData * 3 + i] = ' ';
89 for (i = 0; i < DIV_SPACES; i++)
90 lpszLine[cbData * 3 + pad * 3 + i] = ' ';
92 /* attempt an ASCII representation if the characters are printable,
93 * otherwise display a '.' */
94 for (i = 0; i < cbData; i++)
96 /* (C1_ALPHA|C1_BLANK|C1_PUNCT|C1_DIGIT|C1_LOWER|C1_UPPER) */
97 if (isprint(pData[i]))
98 lpszLine[cbData * 3 + pad * 3 + DIV_SPACES + i] = pData[i];
100 lpszLine[cbData * 3 + pad * 3 + DIV_SPACES + i] = '.';
102 lpszLine[cbData * 3 + pad * 3 + DIV_SPACES + cbData] = 0;
107 HexEdit_Paint(HEXEDIT_INFO *infoPtr)
110 HDC hdc = BeginPaint(infoPtr->hwndSelf, &ps);
111 INT nXStart, nYStart;
116 LONG lByteOffset = infoPtr->nScrollPos * infoPtr->nBytesPerLine;
118 /* Make a gap from the frame */
119 nXStart = GetSystemMetrics(SM_CXBORDER);
120 nYStart = GetSystemMetrics(SM_CYBORDER);
122 if (GetWindowLong(infoPtr->hwndSelf, GWL_STYLE) & WS_DISABLED)
123 clrOldText = SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
125 clrOldText = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
127 iMode = SetBkMode(hdc, TRANSPARENT);
128 hOldFont = SelectObject(hdc, infoPtr->hFont);
130 for (pData = infoPtr->pData + lByteOffset; pData < infoPtr->pData + infoPtr->cbData; pData += infoPtr->nBytesPerLine)
133 LONG nLineLen = min((LONG)((infoPtr->pData + infoPtr->cbData) - pData),
134 infoPtr->nBytesPerLine);
136 lpszLine = HexEdit_GetLineText(pData, nLineLen, infoPtr->nBytesPerLine - nLineLen);
138 /* FIXME: draw hex <-> ASCII mapping highlighted? */
139 TextOut(hdc, nXStart, nYStart, lpszLine, infoPtr->nBytesPerLine * 3 + DIV_SPACES + nLineLen);
141 nYStart += infoPtr->nHeight;
142 HeapFree(GetProcessHeap(), 0, lpszLine);
145 SelectObject(hdc, hOldFont);
146 SetBkMode(hdc, iMode);
147 SetTextColor(hdc, clrOldText);
149 EndPaint(infoPtr->hwndSelf, &ps);
153 HexEdit_UpdateCaret(HEXEDIT_INFO *infoPtr)
158 INT nCaretBytePos = infoPtr->nCaretPos/2;
159 INT nByteLinePos = nCaretBytePos % infoPtr->nBytesPerLine;
160 INT nLine = nCaretBytePos / infoPtr->nBytesPerLine;
161 LONG nLineLen = min(infoPtr->cbData - nLine * infoPtr->nBytesPerLine, infoPtr->nBytesPerLine);
162 LPTSTR lpszLine = HexEdit_GetLineText(infoPtr->pData + nLine * infoPtr->nBytesPerLine, nLineLen, infoPtr->nBytesPerLine - nLineLen);
165 /* calculate offset of character caret is on in the line */
166 if (infoPtr->bFocusHex)
167 nCharOffset = nByteLinePos*3 + infoPtr->nCaretPos % 2;
169 nCharOffset = infoPtr->nBytesPerLine*3 + DIV_SPACES + nByteLinePos;
171 hdc = GetDC(infoPtr->hwndSelf);
172 hOldFont = SelectObject(hdc, infoPtr->hFont);
174 GetTextExtentPoint32(hdc, lpszLine, nCharOffset, &size);
176 SelectObject(hdc, hOldFont);
177 ReleaseDC(infoPtr->hwndSelf, hdc);
179 if (!nLineLen) size.cx = 0;
181 HeapFree(GetProcessHeap(), 0, lpszLine);
184 GetSystemMetrics(SM_CXBORDER) + size.cx,
185 GetSystemMetrics(SM_CYBORDER) + (nLine - infoPtr->nScrollPos) * infoPtr->nHeight);
189 HexEdit_UpdateScrollbars(HEXEDIT_INFO *infoPtr)
192 INT nLines = infoPtr->cbData / infoPtr->nBytesPerLine;
196 GetClientRect(infoPtr->hwndSelf, &rcClient);
197 InflateRect(&rcClient, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER));
199 nVisibleLines = (rcClient.bottom - rcClient.top) / infoPtr->nHeight;
200 si.cbSize = sizeof(si);
201 si.fMask = SIF_RANGE | SIF_PAGE;
203 si.nMax = max(nLines - nVisibleLines, nLines);
204 si.nPage = nVisibleLines;
205 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si, TRUE);
209 HexEdit_EnsureVisible(HEXEDIT_INFO *infoPtr, INT nCaretPos)
211 INT nLine = nCaretPos / (2 * infoPtr->nBytesPerLine);
214 si.cbSize = sizeof(si);
215 si.fMask = SIF_POS | SIF_PAGE;
216 GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
219 else if (nLine >= si.nPos + si.nPage)
220 si.nPos = nLine - si.nPage + 1;
225 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si, FALSE);
226 SendMessage(infoPtr->hwndSelf, WM_VSCROLL, MAKELONG(SB_THUMBPOSITION, 0), 0);
231 HexEdit_SetData(HEXEDIT_INFO *infoPtr, INT cbData, const BYTE *pData)
233 HeapFree(GetProcessHeap(), 0, infoPtr->pData);
236 infoPtr->pData = HeapAlloc(GetProcessHeap(), 0, cbData);
239 memcpy(infoPtr->pData, pData, cbData);
240 infoPtr->cbData = cbData;
242 infoPtr->nCaretPos = 0;
243 HexEdit_UpdateScrollbars(infoPtr);
244 HexEdit_UpdateCaret(infoPtr);
245 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
252 HexEdit_GetData(HEXEDIT_INFO *infoPtr, INT cbData, BYTE *pData)
255 memcpy(pData, infoPtr->pData, min(cbData, infoPtr->cbData));
256 return infoPtr->cbData;
259 static inline LRESULT
260 HexEdit_Char (HEXEDIT_INFO *infoPtr, TCHAR ch)
262 INT nCaretBytePos = infoPtr->nCaretPos/2;
264 assert(nCaretBytePos >= 0);
266 /* backspace is special */
269 if (infoPtr->nCaretPos == 0)
272 /* if at end of byte then delete the whole byte */
273 if (infoPtr->bFocusHex && (infoPtr->nCaretPos % 2 == 0))
275 memmove(infoPtr->pData + nCaretBytePos - 1,
276 infoPtr->pData + nCaretBytePos,
277 infoPtr->cbData - nCaretBytePos);
279 infoPtr->nCaretPos -= 2; /* backtrack two nibble */
281 else /* blank upper nibble */
283 infoPtr->pData[nCaretBytePos] &= 0x0f;
284 infoPtr->nCaretPos--; /* backtrack one nibble */
289 if (infoPtr->bFocusHex && hexchar_to_byte(ch) == (BYTE)-1)
291 MessageBeep(MB_ICONWARNING);
295 if ((infoPtr->bInsert && (infoPtr->nCaretPos % 2 == 0)) || (nCaretBytePos >= infoPtr->cbData))
297 /* make room for another byte */
299 infoPtr->pData = HeapReAlloc(GetProcessHeap(), 0, infoPtr->pData, infoPtr->cbData + 1);
300 if (!infoPtr->pData) return 0;
301 /* move everything after caret up one byte */
302 memmove(infoPtr->pData + nCaretBytePos + 1,
303 infoPtr->pData + nCaretBytePos,
304 infoPtr->cbData - nCaretBytePos);
306 infoPtr->pData[nCaretBytePos] = 0x0;
309 /* overwrite a byte */
311 assert(nCaretBytePos < infoPtr->cbData);
313 if (infoPtr->bFocusHex)
315 BYTE orig_byte = infoPtr->pData[nCaretBytePos];
316 BYTE digit = hexchar_to_byte(ch);
317 if (infoPtr->nCaretPos % 2) /* set low nibble */
318 infoPtr->pData[nCaretBytePos] = (orig_byte & 0xf0) | digit;
319 else /* set high nibble */
320 infoPtr->pData[nCaretBytePos] = (orig_byte & 0x0f) | digit << 4;
321 infoPtr->nCaretPos++; /* advance one nibble */
325 infoPtr->pData[nCaretBytePos] = (BYTE)ch;
326 infoPtr->nCaretPos += 2; /* advance two nibbles */
330 HexEdit_UpdateScrollbars(infoPtr);
331 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
332 HexEdit_UpdateCaret(infoPtr);
333 HexEdit_EnsureVisible(infoPtr, infoPtr->nCaretPos);
337 static inline LRESULT
338 HexEdit_Create (HEXEDIT_INFO *infoPtr, LPCREATESTRUCT lpcs)
340 HexEdit_SetFont(infoPtr, GetStockObject(SYSTEM_FONT), FALSE);
341 HexEdit_UpdateScrollbars(infoPtr);
347 static inline LRESULT
348 HexEdit_Destroy (HEXEDIT_INFO *infoPtr)
350 HWND hwnd = infoPtr->hwndSelf;
351 HeapFree(GetProcessHeap(), 0, infoPtr->pData);
353 HeapFree(GetProcessHeap(), 0, infoPtr);
354 SetWindowLongPtr(hwnd, 0, 0);
359 static inline LRESULT
360 HexEdit_EraseBackground (HEXEDIT_INFO *infoPtr, HDC hdc)
362 HBRUSH hBrush, hSolidBrush = NULL;
365 if (GetWindowLong(infoPtr->hwndSelf, GWL_STYLE) & WS_DISABLED)
366 hBrush = hSolidBrush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
369 hBrush = (HBRUSH)SendMessage(GetParent(infoPtr->hwndSelf), WM_CTLCOLOREDIT,
370 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
372 hBrush = hSolidBrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
375 GetClientRect (infoPtr->hwndSelf, &rc);
377 FillRect (hdc, &rc, hBrush);
380 DeleteObject(hSolidBrush);
386 static inline LRESULT
387 HexEdit_GetFont (HEXEDIT_INFO *infoPtr)
389 return (LRESULT)infoPtr->hFont;
392 static inline LRESULT
393 HexEdit_KeyDown (HEXEDIT_INFO *infoPtr, DWORD key, DWORD flags)
395 INT nInc = (infoPtr->bFocusHex) ? 1 : 2;
401 infoPtr->nCaretPos -= nInc;
402 if (infoPtr->nCaretPos < 0)
403 infoPtr->nCaretPos = 0;
406 infoPtr->nCaretPos += nInc;
407 if (infoPtr->nCaretPos > infoPtr->cbData*2)
408 infoPtr->nCaretPos = infoPtr->cbData*2;
411 if ((infoPtr->nCaretPos - infoPtr->nBytesPerLine*2) >= 0)
412 infoPtr->nCaretPos -= infoPtr->nBytesPerLine*2;
415 if ((infoPtr->nCaretPos + infoPtr->nBytesPerLine*2) <= infoPtr->cbData*2)
416 infoPtr->nCaretPos += infoPtr->nBytesPerLine*2;
419 infoPtr->nCaretPos = 0;
422 infoPtr->nCaretPos = infoPtr->cbData*2;
424 case VK_PRIOR: /* page up */
425 si.cbSize = sizeof(si);
427 GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
428 if ((infoPtr->nCaretPos - (INT)si.nPage*infoPtr->nBytesPerLine*2) >= 0)
429 infoPtr->nCaretPos -= si.nPage*infoPtr->nBytesPerLine*2;
431 infoPtr->nCaretPos = 0;
433 case VK_NEXT: /* page down */
434 si.cbSize = sizeof(si);
436 GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
437 if ((infoPtr->nCaretPos + (INT)si.nPage*infoPtr->nBytesPerLine*2) <= infoPtr->cbData*2)
438 infoPtr->nCaretPos += si.nPage*infoPtr->nBytesPerLine*2;
440 infoPtr->nCaretPos = infoPtr->cbData*2;
446 HexEdit_UpdateCaret(infoPtr);
447 HexEdit_EnsureVisible(infoPtr, infoPtr->nCaretPos);
453 static inline LRESULT
454 HexEdit_KillFocus (HEXEDIT_INFO *infoPtr, HWND receiveFocus)
456 infoPtr->bFocus = FALSE;
463 static inline LRESULT
464 HexEdit_LButtonDown (HEXEDIT_INFO *infoPtr)
466 SetFocus(infoPtr->hwndSelf);
468 /* FIXME: hittest and set caret */
474 static inline LRESULT HexEdit_NCCreate (HWND hwnd, LPCREATESTRUCT lpcs)
476 HEXEDIT_INFO *infoPtr;
477 SetWindowLong(hwnd, GWL_EXSTYLE,
478 lpcs->dwExStyle | WS_EX_CLIENTEDGE);
480 /* allocate memory for info structure */
481 infoPtr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HEXEDIT_INFO));
482 SetWindowLongPtr(hwnd, 0, (DWORD_PTR)infoPtr);
484 /* initialize info structure */
485 infoPtr->nCaretPos = 0;
486 infoPtr->hwndSelf = hwnd;
487 infoPtr->nBytesPerLine = 2;
488 infoPtr->bFocusHex = TRUE;
489 infoPtr->bInsert = TRUE;
491 return DefWindowProc(infoPtr->hwndSelf, WM_NCCREATE, 0, (LPARAM)lpcs);
494 static inline LRESULT
495 HexEdit_SetFocus (HEXEDIT_INFO *infoPtr, HWND lostFocus)
497 infoPtr->bFocus = TRUE;
499 CreateCaret(infoPtr->hwndSelf, NULL, 1, infoPtr->nHeight);
500 HexEdit_UpdateCaret(infoPtr);
501 ShowCaret(infoPtr->hwndSelf);
507 static inline LRESULT
508 HexEdit_SetFont (HEXEDIT_INFO *infoPtr, HFONT hFont, BOOL redraw)
512 HFONT hOldFont = NULL;
516 infoPtr->hFont = hFont;
518 hdc = GetDC(infoPtr->hwndSelf);
520 hOldFont = SelectObject(hdc, infoPtr->hFont);
522 GetTextMetrics(hdc, &tm);
523 infoPtr->nHeight = tm.tmHeight + tm.tmExternalLeading;
525 GetClientRect(infoPtr->hwndSelf, &rcClient);
529 BYTE *pData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, i);
530 LPTSTR lpszLine = HexEdit_GetLineText(pData, i, 0);
532 GetTextExtentPoint32(hdc, lpszLine, lstrlen(lpszLine), &size);
533 HeapFree(GetProcessHeap(), 0, lpszLine);
534 HeapFree(GetProcessHeap(), 0, pData);
535 if (size.cx > (rcClient.right - rcClient.left))
537 infoPtr->nBytesPerLine = i - 1;
543 SelectObject(hdc, hOldFont);
544 ReleaseDC (infoPtr->hwndSelf, hdc);
547 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
552 static inline LRESULT
553 HexEdit_VScroll (HEXEDIT_INFO *infoPtr, INT action)
557 /* get all scroll bar info */
558 si.cbSize = sizeof(si);
560 GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
562 switch (LOWORD(action))
564 case SB_TOP: /* user pressed the home key */
568 case SB_BOTTOM: /* user pressed the end key */
572 case SB_LINEUP: /* user clicked the up arrow */
576 case SB_LINEDOWN: /* user clicked the down arrow */
580 case SB_PAGEUP: /* user clicked the scroll bar above the scroll thumb */
584 case SB_PAGEDOWN: /* user clicked the scroll bar below the scroll thumb */
588 case SB_THUMBTRACK: /* user dragged the scroll thumb */
589 si.nPos = si.nTrackPos;
596 /* set the position and then retrieve it to let the system handle the
597 * cases where the position is out of range */
599 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si, TRUE);
600 GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
602 if (si.nPos != infoPtr->nScrollPos)
604 ScrollWindow(infoPtr->hwndSelf, 0, infoPtr->nHeight * (infoPtr->nScrollPos - si.nPos), NULL, NULL);
605 infoPtr->nScrollPos = si.nPos;
606 UpdateWindow(infoPtr->hwndSelf);
608 /* need to update caret position since it depends on the scroll position */
609 HexEdit_UpdateCaret(infoPtr);
615 static LRESULT WINAPI
616 HexEdit_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
618 HEXEDIT_INFO *infoPtr = (HEXEDIT_INFO *)GetWindowLongPtr (hwnd, 0);
620 if (!infoPtr && (uMsg != WM_NCCREATE))
621 return DefWindowProc(hwnd, uMsg, wParam, lParam);
626 return HexEdit_SetData (infoPtr, (INT)wParam, (const BYTE *)lParam);
629 return HexEdit_GetData (infoPtr, (INT)wParam, (BYTE *)lParam);
632 return HexEdit_Char (infoPtr, (TCHAR)wParam);
635 return HexEdit_Create (infoPtr, (LPCREATESTRUCT)lParam);
638 return HexEdit_Destroy (infoPtr);
641 return HexEdit_EraseBackground (infoPtr, (HDC)wParam);
644 return DLGC_WANTCHARS | DLGC_WANTARROWS;
647 return HexEdit_GetFont (infoPtr);
650 return HexEdit_KeyDown (infoPtr, wParam, lParam);
653 return HexEdit_KillFocus (infoPtr, (HWND)wParam);
656 return HexEdit_LButtonDown (infoPtr);
659 return HexEdit_NCCreate (hwnd, (LPCREATESTRUCT)lParam);
662 HexEdit_Paint(infoPtr);
666 return HexEdit_SetFocus (infoPtr, (HWND)wParam);
669 return HexEdit_SetFont (infoPtr, (HFONT)wParam, LOWORD(lParam));
672 return HexEdit_VScroll (infoPtr, (INT)wParam);
675 return DefWindowProc(hwnd, uMsg, wParam, lParam);
680 void HexEdit_Register()
684 ZeroMemory(&wndClass, sizeof(WNDCLASS));
686 wndClass.lpfnWndProc = HexEdit_WindowProc;
687 wndClass.cbClsExtra = 0;
688 wndClass.cbWndExtra = sizeof(HEXEDIT_INFO *);
689 wndClass.hCursor = NULL;
690 wndClass.hbrBackground = NULL;
691 wndClass.lpszClassName = HEXEDIT_CLASS;
693 RegisterClass(&wndClass);
697 void HexEdit_Unregister()
699 UnregisterClass(HEXEDIT_CLASS, NULL);