hexedit.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * Hex Edit control
  3. *
  4. * Copyright 2005 Robert Shearman
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. *
  20. * TODO:
  21. * - Selection support
  22. * - Cut, copy and paste
  23. * - Mouse support
  24. */
  25. #include <stdarg.h>
  26. #include <string.h>
  27. #include <assert.h>
  28. #include "windef.h"
  29. #include "winbase.h"
  30. #include "wingdi.h"
  31. #include "winuser.h"
  32. #include "winnls.h"
  33. #include "commctrl.h"
  34. #include "wine/heap.h"
  35. #include "main.h"
  36. /* spaces dividing hex and ASCII */
  37. #define DIV_SPACES 4
  38. typedef struct tagHEXEDIT_INFO
  39. {
  40. HWND hwndSelf;
  41. HFONT hFont;
  42. BOOL bFocus : 1;
  43. BOOL bFocusHex : 1; /* TRUE if focus is on hex, FALSE if focus on ASCII */
  44. BOOL bInsert : 1; /* insert mode if TRUE, overwrite mode if FALSE */
  45. INT nHeight; /* height of text */
  46. INT nCaretPos; /* caret pos in nibbles */
  47. BYTE *pData;
  48. INT cbData;
  49. INT nBytesPerLine; /* bytes of hex to display per line of the control */
  50. INT nScrollPos; /* first visible line */
  51. } HEXEDIT_INFO;
  52. static inline LRESULT HexEdit_SetFont (HEXEDIT_INFO *infoPtr, HFONT hFont, BOOL redraw);
  53. static inline BYTE hexchar_to_byte(WCHAR ch)
  54. {
  55. if (ch >= '0' && ch <= '9')
  56. return ch - '0';
  57. else if (ch >= 'a' && ch <= 'f')
  58. return ch - 'a' + 10;
  59. else if (ch >= 'A' && ch <= 'F')
  60. return ch - 'A' + 10;
  61. else
  62. return -1;
  63. }
  64. static LPWSTR HexEdit_GetLineText(int offset, BYTE *pData, LONG cbData, LONG pad)
  65. {
  66. WCHAR *lpszLine = heap_xalloc((6 + cbData * 3 + pad * 3 + DIV_SPACES + cbData + 1) * sizeof(WCHAR));
  67. LONG i;
  68. wsprintfW(lpszLine, L"%04X ", offset);
  69. for (i = 0; i < cbData; i++)
  70. wsprintfW(lpszLine + 6 + i*3, L"%02X ", pData[offset + i]);
  71. for (i = 0; i < pad * 3; i++)
  72. lpszLine[6 + cbData * 3 + i] = ' ';
  73. for (i = 0; i < DIV_SPACES; i++)
  74. lpszLine[6 + cbData * 3 + pad * 3 + i] = ' ';
  75. /* attempt an ASCII representation if the characters are printable,
  76. * otherwise display a '.' */
  77. for (i = 0; i < cbData; i++)
  78. {
  79. /* (C1_ALPHA|C1_BLANK|C1_PUNCT|C1_DIGIT|C1_LOWER|C1_UPPER) */
  80. if (iswprint(pData[offset + i]))
  81. lpszLine[6 + cbData * 3 + pad * 3 + DIV_SPACES + i] = pData[offset + i];
  82. else
  83. lpszLine[6 + cbData * 3 + pad * 3 + DIV_SPACES + i] = '.';
  84. }
  85. lpszLine[6 + cbData * 3 + pad * 3 + DIV_SPACES + cbData] = 0;
  86. return lpszLine;
  87. }
  88. static void
  89. HexEdit_Paint(HEXEDIT_INFO *infoPtr)
  90. {
  91. PAINTSTRUCT ps;
  92. HDC hdc = BeginPaint(infoPtr->hwndSelf, &ps);
  93. INT nXStart, nYStart;
  94. COLORREF clrOldText;
  95. HFONT hOldFont;
  96. INT iMode;
  97. LONG lByteOffset = infoPtr->nScrollPos * infoPtr->nBytesPerLine;
  98. int i;
  99. /* Make a gap from the frame */
  100. nXStart = GetSystemMetrics(SM_CXBORDER);
  101. nYStart = GetSystemMetrics(SM_CYBORDER);
  102. if (GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_DISABLED)
  103. clrOldText = SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
  104. else
  105. clrOldText = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
  106. iMode = SetBkMode(hdc, TRANSPARENT);
  107. hOldFont = SelectObject(hdc, infoPtr->hFont);
  108. for (i = lByteOffset; i < infoPtr->cbData; i += infoPtr->nBytesPerLine)
  109. {
  110. LPWSTR lpszLine;
  111. LONG nLineLen = min(infoPtr->cbData - i, infoPtr->nBytesPerLine);
  112. lpszLine = HexEdit_GetLineText(i, infoPtr->pData, nLineLen, infoPtr->nBytesPerLine - nLineLen);
  113. /* FIXME: draw hex <-> ASCII mapping highlighted? */
  114. TextOutW(hdc, nXStart, nYStart, lpszLine, lstrlenW(lpszLine));
  115. nYStart += infoPtr->nHeight;
  116. heap_free(lpszLine);
  117. }
  118. SelectObject(hdc, hOldFont);
  119. SetBkMode(hdc, iMode);
  120. SetTextColor(hdc, clrOldText);
  121. EndPaint(infoPtr->hwndSelf, &ps);
  122. }
  123. static void
  124. HexEdit_UpdateCaret(HEXEDIT_INFO *infoPtr)
  125. {
  126. HDC hdc;
  127. HFONT hOldFont;
  128. SIZE size;
  129. INT nCaretBytePos = infoPtr->nCaretPos/2;
  130. INT nByteLinePos = nCaretBytePos % infoPtr->nBytesPerLine;
  131. INT nLine = nCaretBytePos / infoPtr->nBytesPerLine;
  132. LONG nLineLen = min(infoPtr->cbData - nLine * infoPtr->nBytesPerLine, infoPtr->nBytesPerLine);
  133. LPWSTR lpszLine = HexEdit_GetLineText(nLine * infoPtr->nBytesPerLine, infoPtr->pData, nLineLen, infoPtr->nBytesPerLine - nLineLen);
  134. INT nCharOffset;
  135. /* calculate offset of character caret is on in the line */
  136. if (infoPtr->bFocusHex)
  137. nCharOffset = 6 + nByteLinePos*3 + infoPtr->nCaretPos % 2;
  138. else
  139. nCharOffset = 6 + infoPtr->nBytesPerLine*3 + DIV_SPACES + nByteLinePos;
  140. hdc = GetDC(infoPtr->hwndSelf);
  141. hOldFont = SelectObject(hdc, infoPtr->hFont);
  142. GetTextExtentPoint32W(hdc, lpszLine, nCharOffset, &size);
  143. SelectObject(hdc, hOldFont);
  144. ReleaseDC(infoPtr->hwndSelf, hdc);
  145. if (!nLineLen) size.cx = 0;
  146. heap_free(lpszLine);
  147. SetCaretPos(
  148. GetSystemMetrics(SM_CXBORDER) + size.cx,
  149. GetSystemMetrics(SM_CYBORDER) + (nLine - infoPtr->nScrollPos) * infoPtr->nHeight);
  150. }
  151. static void
  152. HexEdit_UpdateScrollbars(HEXEDIT_INFO *infoPtr)
  153. {
  154. RECT rcClient;
  155. INT nLines = infoPtr->cbData / infoPtr->nBytesPerLine;
  156. INT nVisibleLines;
  157. SCROLLINFO si;
  158. GetClientRect(infoPtr->hwndSelf, &rcClient);
  159. InflateRect(&rcClient, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER));
  160. nVisibleLines = (rcClient.bottom - rcClient.top) / infoPtr->nHeight;
  161. si.cbSize = sizeof(si);
  162. si.fMask = SIF_RANGE | SIF_PAGE;
  163. si.nMin = 0;
  164. si.nMax = max(nLines - nVisibleLines, nLines);
  165. si.nPage = nVisibleLines;
  166. SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si, TRUE);
  167. }
  168. static void
  169. HexEdit_EnsureVisible(HEXEDIT_INFO *infoPtr, INT nCaretPos)
  170. {
  171. INT nLine = nCaretPos / (2 * infoPtr->nBytesPerLine);
  172. SCROLLINFO si;
  173. si.cbSize = sizeof(si);
  174. si.fMask = SIF_POS | SIF_PAGE;
  175. GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
  176. if (nLine < si.nPos)
  177. si.nPos = nLine;
  178. else if (nLine >= si.nPos + si.nPage)
  179. si.nPos = nLine - si.nPage + 1;
  180. else
  181. return;
  182. si.fMask = SIF_POS;
  183. SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si, FALSE);
  184. SendMessageW(infoPtr->hwndSelf, WM_VSCROLL, MAKELONG(SB_THUMBPOSITION, 0), 0);
  185. }
  186. static LRESULT
  187. HexEdit_SetData(HEXEDIT_INFO *infoPtr, INT cbData, const BYTE *pData)
  188. {
  189. heap_free(infoPtr->pData);
  190. infoPtr->cbData = 0;
  191. infoPtr->pData = heap_xalloc(cbData);
  192. memcpy(infoPtr->pData, pData, cbData);
  193. infoPtr->cbData = cbData;
  194. infoPtr->nCaretPos = 0;
  195. HexEdit_UpdateScrollbars(infoPtr);
  196. HexEdit_UpdateCaret(infoPtr);
  197. InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
  198. return TRUE;
  199. }
  200. static LRESULT
  201. HexEdit_GetData(HEXEDIT_INFO *infoPtr, INT cbData, BYTE *pData)
  202. {
  203. if (pData)
  204. memcpy(pData, infoPtr->pData, min(cbData, infoPtr->cbData));
  205. return infoPtr->cbData;
  206. }
  207. static inline LRESULT
  208. HexEdit_Char (HEXEDIT_INFO *infoPtr, WCHAR ch)
  209. {
  210. INT nCaretBytePos = infoPtr->nCaretPos/2;
  211. assert(nCaretBytePos >= 0);
  212. /* backspace is special */
  213. if (ch == '\b')
  214. {
  215. if (infoPtr->nCaretPos == 0)
  216. return 0;
  217. /* if at end of byte then delete the whole byte */
  218. if (infoPtr->bFocusHex && (infoPtr->nCaretPos % 2 == 0))
  219. {
  220. memmove(infoPtr->pData + nCaretBytePos - 1,
  221. infoPtr->pData + nCaretBytePos,
  222. infoPtr->cbData - nCaretBytePos);
  223. infoPtr->cbData--;
  224. infoPtr->nCaretPos -= 2; /* backtrack two nibble */
  225. }
  226. else /* blank upper nibble */
  227. {
  228. infoPtr->pData[nCaretBytePos] &= 0x0f;
  229. infoPtr->nCaretPos--; /* backtrack one nibble */
  230. }
  231. }
  232. else
  233. {
  234. if (infoPtr->bFocusHex && hexchar_to_byte(ch) == (BYTE)-1)
  235. {
  236. MessageBeep(MB_ICONWARNING);
  237. return 0;
  238. }
  239. if ((infoPtr->bInsert && (infoPtr->nCaretPos % 2 == 0)) || (nCaretBytePos >= infoPtr->cbData))
  240. {
  241. /* make room for another byte */
  242. infoPtr->cbData++;
  243. infoPtr->pData = heap_xrealloc(infoPtr->pData, infoPtr->cbData + 1);
  244. /* move everything after caret up one byte */
  245. memmove(infoPtr->pData + nCaretBytePos + 1,
  246. infoPtr->pData + nCaretBytePos,
  247. infoPtr->cbData - nCaretBytePos);
  248. /* zero new byte */
  249. infoPtr->pData[nCaretBytePos] = 0x0;
  250. }
  251. /* overwrite a byte */
  252. assert(nCaretBytePos < infoPtr->cbData);
  253. if (infoPtr->bFocusHex)
  254. {
  255. BYTE orig_byte = infoPtr->pData[nCaretBytePos];
  256. BYTE digit = hexchar_to_byte(ch);
  257. if (infoPtr->nCaretPos % 2) /* set low nibble */
  258. infoPtr->pData[nCaretBytePos] = (orig_byte & 0xf0) | digit;
  259. else /* set high nibble */
  260. infoPtr->pData[nCaretBytePos] = (orig_byte & 0x0f) | digit << 4;
  261. infoPtr->nCaretPos++; /* advance one nibble */
  262. }
  263. else
  264. {
  265. infoPtr->pData[nCaretBytePos] = (BYTE)ch;
  266. infoPtr->nCaretPos += 2; /* advance two nibbles */
  267. }
  268. }
  269. HexEdit_UpdateScrollbars(infoPtr);
  270. InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
  271. HexEdit_UpdateCaret(infoPtr);
  272. HexEdit_EnsureVisible(infoPtr, infoPtr->nCaretPos);
  273. return 0;
  274. }
  275. static inline LRESULT
  276. HexEdit_Destroy (HEXEDIT_INFO *infoPtr)
  277. {
  278. HWND hwnd = infoPtr->hwndSelf;
  279. heap_free(infoPtr->pData);
  280. /* free info data */
  281. heap_free(infoPtr);
  282. SetWindowLongPtrW(hwnd, 0, 0);
  283. return 0;
  284. }
  285. static inline LRESULT
  286. HexEdit_GetFont (HEXEDIT_INFO *infoPtr)
  287. {
  288. return (LRESULT)infoPtr->hFont;
  289. }
  290. static inline LRESULT
  291. HexEdit_KeyDown (HEXEDIT_INFO *infoPtr, DWORD key, DWORD flags)
  292. {
  293. INT nInc = (infoPtr->bFocusHex) ? 1 : 2;
  294. SCROLLINFO si;
  295. switch (key)
  296. {
  297. case VK_LEFT:
  298. infoPtr->nCaretPos -= nInc;
  299. if (infoPtr->nCaretPos < 0)
  300. infoPtr->nCaretPos = 0;
  301. break;
  302. case VK_RIGHT:
  303. infoPtr->nCaretPos += nInc;
  304. if (infoPtr->nCaretPos > infoPtr->cbData*2)
  305. infoPtr->nCaretPos = infoPtr->cbData*2;
  306. break;
  307. case VK_UP:
  308. if ((infoPtr->nCaretPos - infoPtr->nBytesPerLine*2) >= 0)
  309. infoPtr->nCaretPos -= infoPtr->nBytesPerLine*2;
  310. break;
  311. case VK_DOWN:
  312. if ((infoPtr->nCaretPos + infoPtr->nBytesPerLine*2) <= infoPtr->cbData*2)
  313. infoPtr->nCaretPos += infoPtr->nBytesPerLine*2;
  314. break;
  315. case VK_HOME:
  316. infoPtr->nCaretPos = 0;
  317. break;
  318. case VK_END:
  319. infoPtr->nCaretPos = infoPtr->cbData*2;
  320. break;
  321. case VK_PRIOR: /* page up */
  322. si.cbSize = sizeof(si);
  323. si.fMask = SIF_PAGE;
  324. GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
  325. if ((infoPtr->nCaretPos - (INT)si.nPage*infoPtr->nBytesPerLine*2) >= 0)
  326. infoPtr->nCaretPos -= si.nPage*infoPtr->nBytesPerLine*2;
  327. else
  328. infoPtr->nCaretPos = 0;
  329. break;
  330. case VK_NEXT: /* page down */
  331. si.cbSize = sizeof(si);
  332. si.fMask = SIF_PAGE;
  333. GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
  334. if ((infoPtr->nCaretPos + (INT)si.nPage*infoPtr->nBytesPerLine*2) <= infoPtr->cbData*2)
  335. infoPtr->nCaretPos += si.nPage*infoPtr->nBytesPerLine*2;
  336. else
  337. infoPtr->nCaretPos = infoPtr->cbData*2;
  338. break;
  339. default:
  340. return 0;
  341. }
  342. HexEdit_UpdateCaret(infoPtr);
  343. HexEdit_EnsureVisible(infoPtr, infoPtr->nCaretPos);
  344. return 0;
  345. }
  346. static inline LRESULT
  347. HexEdit_KillFocus (HEXEDIT_INFO *infoPtr, HWND receiveFocus)
  348. {
  349. infoPtr->bFocus = FALSE;
  350. DestroyCaret();
  351. return 0;
  352. }
  353. static inline LRESULT
  354. HexEdit_LButtonDown (HEXEDIT_INFO *infoPtr)
  355. {
  356. SetFocus(infoPtr->hwndSelf);
  357. /* FIXME: hittest and set caret */
  358. return 0;
  359. }
  360. static inline LRESULT HexEdit_NCCreate (HWND hwnd, LPCREATESTRUCTW lpcs)
  361. {
  362. HEXEDIT_INFO *infoPtr;
  363. SetWindowLongW(hwnd, GWL_EXSTYLE,
  364. lpcs->dwExStyle | WS_EX_CLIENTEDGE);
  365. /* allocate memory for info structure */
  366. infoPtr = heap_xalloc(sizeof(HEXEDIT_INFO));
  367. memset(infoPtr, 0, sizeof(HEXEDIT_INFO));
  368. SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
  369. /* initialize info structure */
  370. infoPtr->nCaretPos = 0;
  371. infoPtr->hwndSelf = hwnd;
  372. infoPtr->nBytesPerLine = 2;
  373. infoPtr->bFocusHex = TRUE;
  374. infoPtr->bInsert = TRUE;
  375. return DefWindowProcW(infoPtr->hwndSelf, WM_NCCREATE, 0, (LPARAM)lpcs);
  376. }
  377. static inline LRESULT
  378. HexEdit_SetFocus (HEXEDIT_INFO *infoPtr, HWND lostFocus)
  379. {
  380. infoPtr->bFocus = TRUE;
  381. CreateCaret(infoPtr->hwndSelf, NULL, 1, infoPtr->nHeight);
  382. HexEdit_UpdateCaret(infoPtr);
  383. ShowCaret(infoPtr->hwndSelf);
  384. return 0;
  385. }
  386. static inline LRESULT
  387. HexEdit_SetFont (HEXEDIT_INFO *infoPtr, HFONT hFont, BOOL redraw)
  388. {
  389. TEXTMETRICW tm;
  390. HDC hdc;
  391. HFONT hOldFont = NULL;
  392. LONG i;
  393. RECT rcClient;
  394. infoPtr->hFont = hFont;
  395. hdc = GetDC(infoPtr->hwndSelf);
  396. if (infoPtr->hFont)
  397. hOldFont = SelectObject(hdc, infoPtr->hFont);
  398. GetTextMetricsW(hdc, &tm);
  399. infoPtr->nHeight = tm.tmHeight + tm.tmExternalLeading;
  400. GetClientRect(infoPtr->hwndSelf, &rcClient);
  401. for (i = 0; ; i++)
  402. {
  403. BYTE *pData = heap_xalloc(i);
  404. WCHAR *lpszLine;
  405. SIZE size;
  406. memset(pData, 0, i);
  407. lpszLine = HexEdit_GetLineText(0, pData, i, 0);
  408. GetTextExtentPoint32W(hdc, lpszLine, lstrlenW(lpszLine), &size);
  409. heap_free(lpszLine);
  410. heap_free(pData);
  411. if (size.cx > (rcClient.right - rcClient.left))
  412. {
  413. infoPtr->nBytesPerLine = i - 1;
  414. break;
  415. }
  416. }
  417. HexEdit_UpdateScrollbars(infoPtr);
  418. if (infoPtr->hFont)
  419. SelectObject(hdc, hOldFont);
  420. ReleaseDC (infoPtr->hwndSelf, hdc);
  421. if (redraw)
  422. InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
  423. return 0;
  424. }
  425. static inline LRESULT
  426. HexEdit_VScroll (HEXEDIT_INFO *infoPtr, INT action)
  427. {
  428. SCROLLINFO si;
  429. /* get all scroll bar info */
  430. si.cbSize = sizeof(si);
  431. si.fMask = SIF_ALL;
  432. GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
  433. switch (LOWORD(action))
  434. {
  435. case SB_TOP: /* user pressed the home key */
  436. si.nPos = si.nMin;
  437. break;
  438. case SB_BOTTOM: /* user pressed the end key */
  439. si.nPos = si.nMax;
  440. break;
  441. case SB_LINEUP: /* user clicked the up arrow */
  442. si.nPos -= 1;
  443. break;
  444. case SB_LINEDOWN: /* user clicked the down arrow */
  445. si.nPos += 1;
  446. break;
  447. case SB_PAGEUP: /* user clicked the scroll bar above the scroll thumb */
  448. si.nPos -= si.nPage;
  449. break;
  450. case SB_PAGEDOWN: /* user clicked the scroll bar below the scroll thumb */
  451. si.nPos += si.nPage;
  452. break;
  453. case SB_THUMBTRACK: /* user dragged the scroll thumb */
  454. si.nPos = si.nTrackPos;
  455. break;
  456. default:
  457. break;
  458. }
  459. /* set the position and then retrieve it to let the system handle the
  460. * cases where the position is out of range */
  461. si.fMask = SIF_POS;
  462. SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si, TRUE);
  463. GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &si);
  464. if (si.nPos != infoPtr->nScrollPos)
  465. {
  466. ScrollWindow(infoPtr->hwndSelf, 0, infoPtr->nHeight * (infoPtr->nScrollPos - si.nPos), NULL, NULL);
  467. infoPtr->nScrollPos = si.nPos;
  468. UpdateWindow(infoPtr->hwndSelf);
  469. /* need to update caret position since it depends on the scroll position */
  470. HexEdit_UpdateCaret(infoPtr);
  471. }
  472. return 0;
  473. }
  474. static LRESULT WINAPI
  475. HexEdit_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  476. {
  477. HEXEDIT_INFO *infoPtr = (HEXEDIT_INFO *)GetWindowLongPtrW(hwnd, 0);
  478. if (!infoPtr && (uMsg != WM_NCCREATE))
  479. return DefWindowProcW(hwnd, uMsg, wParam, lParam);
  480. switch (uMsg)
  481. {
  482. case HEM_SETDATA:
  483. return HexEdit_SetData (infoPtr, (INT)wParam, (const BYTE *)lParam);
  484. case HEM_GETDATA:
  485. return HexEdit_GetData (infoPtr, (INT)wParam, (BYTE *)lParam);
  486. case WM_CHAR:
  487. return HexEdit_Char (infoPtr, (WCHAR)wParam);
  488. case WM_DESTROY:
  489. return HexEdit_Destroy (infoPtr);
  490. case WM_GETDLGCODE:
  491. return DLGC_WANTCHARS | DLGC_WANTARROWS;
  492. case WM_GETFONT:
  493. return HexEdit_GetFont (infoPtr);
  494. case WM_KEYDOWN:
  495. return HexEdit_KeyDown (infoPtr, wParam, lParam);
  496. case WM_KILLFOCUS:
  497. return HexEdit_KillFocus (infoPtr, (HWND)wParam);
  498. case WM_LBUTTONDOWN:
  499. return HexEdit_LButtonDown (infoPtr);
  500. case WM_NCCREATE:
  501. return HexEdit_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
  502. case WM_PAINT:
  503. HexEdit_Paint(infoPtr);
  504. return 0;
  505. case WM_SETFOCUS:
  506. return HexEdit_SetFocus (infoPtr, (HWND)wParam);
  507. case WM_SETFONT:
  508. return HexEdit_SetFont (infoPtr, (HFONT)wParam, LOWORD(lParam));
  509. case WM_VSCROLL:
  510. return HexEdit_VScroll (infoPtr, (INT)wParam);
  511. default:
  512. return DefWindowProcW(hwnd, uMsg, wParam, lParam);
  513. }
  514. return 0;
  515. }
  516. void HexEdit_Register(void)
  517. {
  518. WNDCLASSW wndClass;
  519. ZeroMemory(&wndClass, sizeof(WNDCLASSW));
  520. wndClass.style = 0;
  521. wndClass.lpfnWndProc = HexEdit_WindowProc;
  522. wndClass.cbClsExtra = 0;
  523. wndClass.cbWndExtra = sizeof(HEXEDIT_INFO *);
  524. wndClass.hCursor = LoadCursorW(0, (const WCHAR *)IDC_IBEAM);
  525. wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  526. wndClass.lpszClassName = L"HexEdit";
  527. RegisterClassW(&wndClass);
  528. }