Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
6.5 KiB

  1. #include "ulib.hxx"
  2. #include "secedit.hxx"
  3. extern "C" {
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. }
  7. BOOLEAN
  8. SECTOR_EDIT::Initialize(
  9. IN HWND WindowHandle,
  10. IN INT ClientHeight,
  11. IN INT ClientWidth,
  12. IN PLOG_IO_DP_DRIVE Drive
  13. )
  14. {
  15. TEXTMETRIC textmetric;
  16. HDC hdc;
  17. hdc = GetDC(WindowHandle);
  18. if (hdc == NULL)
  19. return FALSE;
  20. SelectObject(hdc, GetStockObject(ANSI_FIXED_FONT));
  21. GetTextMetrics(hdc, &textmetric);
  22. ReleaseDC(WindowHandle, hdc);
  23. _buffer = NULL;
  24. _size = 0;
  25. _edit_nibble = 0;
  26. if (!VERTICAL_TEXT_SCROLL::Initialize(
  27. WindowHandle,
  28. 0,
  29. ClientHeight,
  30. ClientWidth,
  31. textmetric.tmExternalLeading + textmetric.tmHeight,
  32. textmetric.tmMaxCharWidth)) {
  33. return FALSE;
  34. }
  35. return TRUE;
  36. }
  37. VOID
  38. SECTOR_EDIT::SetBuf(
  39. IN HWND WindowHandle,
  40. IN OUT PVOID Buffer,
  41. IN ULONG Size
  42. )
  43. {
  44. _buffer = Buffer;
  45. _size = Size;
  46. SetScrollPos(WindowHandle, SB_VERT, 0, FALSE);
  47. InvalidateRect(WindowHandle, NULL, TRUE);
  48. SetRange(WindowHandle, _size/16);
  49. }
  50. VOID
  51. SECTOR_EDIT::Paint(
  52. IN HDC DeviceContext,
  53. IN RECT InvalidRect,
  54. IN HWND WindowHandle
  55. )
  56. {
  57. INT xcoor, ycoor;
  58. PUCHAR p;
  59. TCHAR buf[10];
  60. INT i, j;
  61. SelectObject(DeviceContext, GetStockObject(ANSI_FIXED_FONT));
  62. ycoor = InvalidRect.top/QueryCharHeight()*QueryCharHeight();
  63. p = (PUCHAR) _buffer;
  64. for (i = QueryScrollPosition() + InvalidRect.top/QueryCharHeight();
  65. (unsigned)i < _size/16 && ycoor < InvalidRect.bottom; i++) {
  66. xcoor = 0;
  67. swprintf(buf, TEXT("%04X"), i*16);
  68. TextOut(DeviceContext, xcoor, ycoor, buf, 4);
  69. xcoor += 6*QueryCharWidth();
  70. for (j = 0; j < 16; j++) {
  71. if (j == 8) {
  72. xcoor += QueryCharWidth();
  73. }
  74. swprintf(buf, TEXT("%02X"), p[i*16 + j]);
  75. TextOut(DeviceContext, xcoor, ycoor, buf, 2);
  76. xcoor += 3*QueryCharWidth();
  77. }
  78. xcoor += QueryCharWidth();
  79. for (j = 0; j < 16; j++) {
  80. swprintf(buf, TEXT("%c"), isprint(p[i*16+j]) ? p[i*16+j] : '.');
  81. TextOut(DeviceContext, xcoor, ycoor, buf, 1);
  82. xcoor += QueryCharWidth();
  83. }
  84. ycoor += QueryCharHeight();
  85. }
  86. if (_edit_nibble < ULONG(32*QueryScrollPosition()) ||
  87. _edit_nibble >=
  88. ULONG(32*(QueryScrollPosition() + QueryClientHeight()/QueryCharHeight())) ||
  89. _edit_nibble >= 2*_size) {
  90. _edit_nibble = min((ULONG)32*QueryScrollPosition(), (ULONG)2*_size - 1);
  91. }
  92. SetCaretToNibble();
  93. }
  94. VOID
  95. SECTOR_EDIT::KeyUp(
  96. IN HWND WindowHandle
  97. )
  98. {
  99. if (_edit_nibble < 32) {
  100. return;
  101. }
  102. if (_edit_nibble - 32 < ULONG(32*QueryScrollPosition())) {
  103. ScrollUp(WindowHandle);
  104. }
  105. _edit_nibble -= 32;
  106. SetCaretToNibble();
  107. }
  108. VOID
  109. SECTOR_EDIT::KeyDown(
  110. IN HWND WindowHandle
  111. )
  112. {
  113. if (_edit_nibble + 32 >= 2*_size) {
  114. return;
  115. }
  116. if (_edit_nibble + 32 >= (ULONG)
  117. 32*(QueryScrollPosition() + QueryClientHeight()/QueryCharHeight())) {
  118. ScrollDown(WindowHandle);
  119. }
  120. _edit_nibble += 32;
  121. SetCaretToNibble();
  122. }
  123. VOID
  124. SECTOR_EDIT::KeyLeft(
  125. IN HWND WindowHandle
  126. )
  127. {
  128. if (!_edit_nibble) {
  129. return;
  130. }
  131. if (_edit_nibble == (ULONG)32*QueryScrollPosition()) {
  132. ScrollUp(WindowHandle);
  133. }
  134. _edit_nibble--;
  135. SetCaretToNibble();
  136. }
  137. VOID
  138. SECTOR_EDIT::KeyRight(
  139. IN HWND WindowHandle
  140. )
  141. {
  142. if (_edit_nibble + 1 >= 2*_size) {
  143. return;
  144. }
  145. if (_edit_nibble + 1 == (ULONG)
  146. 32*(QueryScrollPosition() + QueryClientHeight()/QueryCharHeight())) {
  147. ScrollDown(WindowHandle);
  148. }
  149. _edit_nibble++;
  150. SetCaretToNibble();
  151. }
  152. VOID
  153. SECTOR_EDIT::Click(
  154. IN HWND WindowHandle,
  155. IN INT Xcoordinate,
  156. IN INT Ycoordinate
  157. )
  158. {
  159. Xcoordinate /= QueryCharWidth();
  160. Ycoordinate /= QueryCharHeight();
  161. if (Xcoordinate < 6 || Xcoordinate >= 54 || Xcoordinate % 3 == 2 ||
  162. Ycoordinate + QueryScrollPosition() >= QueryNumLines()) {
  163. return;
  164. }
  165. _edit_nibble = (Ycoordinate + QueryScrollPosition())*32 +
  166. ((Xcoordinate - 6)/3)*2 +
  167. ((Xcoordinate % 3 == 1) ? 1 : 0);
  168. SetCaretToNibble();
  169. }
  170. VOID
  171. SECTOR_EDIT::Character(
  172. IN HWND WindowHandle,
  173. IN CHAR Char
  174. )
  175. {
  176. PUCHAR p;
  177. UCHAR nibble;
  178. BOOLEAN nibble_entered;
  179. p = (PUCHAR) _buffer;
  180. if (Char >= '0' && Char <= '9') {
  181. nibble = Char - '0';
  182. nibble_entered = TRUE;
  183. } else if (Char >= 'a' && Char <= 'f') {
  184. nibble = Char - 'a' + 10;
  185. nibble_entered = TRUE;
  186. } else {
  187. nibble_entered = FALSE;
  188. }
  189. if (nibble_entered) {
  190. if (_edit_nibble%2 == 0) {
  191. nibble = (nibble << 4);
  192. p[_edit_nibble/2] &= 0x0F;
  193. p[_edit_nibble/2] |= nibble;
  194. } else {
  195. p[_edit_nibble/2] &= 0xF0;
  196. p[_edit_nibble/2] |= nibble;
  197. }
  198. }
  199. InvalidateNibbleRect(WindowHandle);
  200. _edit_nibble++;
  201. if (_edit_nibble >= _size*2) {
  202. _edit_nibble--;
  203. }
  204. SetCaretToNibble();
  205. }
  206. VOID
  207. SECTOR_EDIT::SetFocus(
  208. IN HWND WindowHandle
  209. )
  210. {
  211. EDIT_OBJECT::SetFocus(WindowHandle);
  212. if (!_size || !_buffer) {
  213. return;
  214. }
  215. CreateCaret(WindowHandle, NULL, QueryCharWidth(), QueryCharHeight());
  216. ShowCaret(WindowHandle);
  217. SetCaretToNibble();
  218. }
  219. VOID
  220. SECTOR_EDIT::KillFocus(
  221. IN HWND WindowHandle
  222. )
  223. {
  224. if (!_size || !_buffer) {
  225. return;
  226. }
  227. HideCaret(WindowHandle);
  228. DestroyCaret();
  229. }
  230. VOID
  231. SECTOR_EDIT::SetCaretToNibble(
  232. )
  233. {
  234. ULONG byte_num;
  235. BOOLEAN high_byte;
  236. ULONG x, y;
  237. if (!_buffer || !_size) {
  238. return;
  239. }
  240. byte_num = _edit_nibble/2;
  241. high_byte = (_edit_nibble%2 == 0);
  242. x = (byte_num%16)*3*QueryCharWidth() + 6*QueryCharWidth() +
  243. (high_byte ? 0 : QueryCharWidth());
  244. if (byte_num % 16 > 7)
  245. x += QueryCharWidth();
  246. y = (byte_num/16 - QueryScrollPosition())*QueryCharHeight();
  247. SetCaretPos(x, y);
  248. }
  249. VOID
  250. SECTOR_EDIT::InvalidateNibbleRect(
  251. IN HWND WindowHandle
  252. )
  253. {
  254. RECT rect;
  255. ULONG byte_num;
  256. byte_num = _edit_nibble/2;
  257. rect.left = 0;
  258. rect.top = (byte_num/16 - QueryScrollPosition())*QueryCharHeight();
  259. rect.right = 84*QueryCharWidth();
  260. rect.bottom = rect.top + QueryCharHeight();
  261. InvalidateRect(WindowHandle, &rect, TRUE);
  262. }