HexView.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. //
  2. // HexView.cs: A hexadecimal viewer
  3. //
  4. // TODO:
  5. // - Support searching and highlighting of the search result
  6. // - Bug showing the last line
  7. //
  8. namespace Terminal.Gui;
  9. /// <summary>An hex viewer and editor <see cref="View"/> over a <see cref="System.IO.Stream"/></summary>
  10. /// <remarks>
  11. /// <para>
  12. /// <see cref="HexView"/> provides a hex editor on top of a seekable <see cref="Stream"/> with the left side
  13. /// showing an hex dump of the values in the <see cref="Stream"/> and the right side showing the contents (filtered
  14. /// to non-control sequence ASCII characters).
  15. /// </para>
  16. /// <para>Users can switch from one side to the other by using the tab key.</para>
  17. /// <para>
  18. /// To enable editing, set <see cref="AllowEdits"/> to true. When <see cref="AllowEdits"/> is true the user can
  19. /// make changes to the hexadecimal values of the <see cref="Stream"/>. Any changes are tracked in the
  20. /// <see cref="Edits"/> property (a <see cref="SortedDictionary{TKey, TValue}"/>) indicating the position where the
  21. /// changes were made and the new values. A convenience method, <see cref="ApplyEdits"/> will apply the edits to
  22. /// the <see cref="Stream"/>.
  23. /// </para>
  24. /// <para>Control the first byte shown by setting the <see cref="DisplayStart"/> property to an offset in the stream.</para>
  25. /// </remarks>
  26. public class HexView : View
  27. {
  28. private const int bsize = 4;
  29. private const int displayWidth = 9;
  30. private int bpl;
  31. private CursorVisibility desiredCursorVisibility = CursorVisibility.Default;
  32. private long displayStart, pos;
  33. private SortedDictionary<long, byte> edits = new ();
  34. private bool firstNibble, leftSide;
  35. private Stream source;
  36. /// <summary>Initializes a <see cref="HexView"/> class using <see cref="LayoutStyle.Computed"/> layout.</summary>
  37. /// <param name="source">
  38. /// The <see cref="Stream"/> to view and edit as hex, this <see cref="Stream"/> must support seeking,
  39. /// or an exception will be thrown.
  40. /// </param>
  41. public HexView (Stream source)
  42. {
  43. Source = source;
  44. CanFocus = true;
  45. leftSide = true;
  46. firstNibble = true;
  47. // Things this view knows how to do
  48. AddCommand (Command.Left, () => MoveLeft ());
  49. AddCommand (Command.Right, () => MoveRight ());
  50. AddCommand (Command.LineDown, () => MoveDown (bytesPerLine));
  51. AddCommand (Command.LineUp, () => MoveUp (bytesPerLine));
  52. AddCommand (Command.ToggleChecked, () => ToggleSide ());
  53. AddCommand (Command.PageUp, () => MoveUp (bytesPerLine * Frame.Height));
  54. AddCommand (Command.PageDown, () => MoveDown (bytesPerLine * Frame.Height));
  55. AddCommand (Command.TopHome, () => MoveHome ());
  56. AddCommand (Command.BottomEnd, () => MoveEnd ());
  57. AddCommand (Command.StartOfLine, () => MoveStartOfLine ());
  58. AddCommand (Command.EndOfLine, () => MoveEndOfLine ());
  59. AddCommand (Command.StartOfPage, () => MoveUp (bytesPerLine * ((int)(position - displayStart) / bytesPerLine)));
  60. AddCommand (
  61. Command.EndOfPage,
  62. () => MoveDown (bytesPerLine * (Frame.Height - 1 - (int)(position - displayStart) / bytesPerLine))
  63. );
  64. // Default keybindings for this view
  65. KeyBindings.Add (KeyCode.CursorLeft, Command.Left);
  66. KeyBindings.Add (KeyCode.CursorRight, Command.Right);
  67. KeyBindings.Add (KeyCode.CursorDown, Command.LineDown);
  68. KeyBindings.Add (KeyCode.CursorUp, Command.LineUp);
  69. KeyBindings.Add (KeyCode.Enter, Command.ToggleChecked);
  70. KeyBindings.Add ('v' + KeyCode.AltMask, Command.PageUp);
  71. KeyBindings.Add (KeyCode.PageUp, Command.PageUp);
  72. KeyBindings.Add (KeyCode.V | KeyCode.CtrlMask, Command.PageDown);
  73. KeyBindings.Add (KeyCode.PageDown, Command.PageDown);
  74. KeyBindings.Add (KeyCode.Home, Command.TopHome);
  75. KeyBindings.Add (KeyCode.End, Command.BottomEnd);
  76. KeyBindings.Add (KeyCode.CursorLeft | KeyCode.CtrlMask, Command.StartOfLine);
  77. KeyBindings.Add (KeyCode.CursorRight | KeyCode.CtrlMask, Command.EndOfLine);
  78. KeyBindings.Add (KeyCode.CursorUp | KeyCode.CtrlMask, Command.StartOfPage);
  79. KeyBindings.Add (KeyCode.CursorDown | KeyCode.CtrlMask, Command.EndOfPage);
  80. LayoutComplete += HexView_LayoutComplete;
  81. }
  82. /// <summary>Initializes a <see cref="HexView"/> class using <see cref="LayoutStyle.Computed"/> layout.</summary>
  83. public HexView () : this (new MemoryStream ()) { }
  84. /// <summary>
  85. /// Gets or sets whether this <see cref="HexView"/> allow editing of the <see cref="Stream"/> of the underlying
  86. /// <see cref="Stream"/>.
  87. /// </summary>
  88. /// <value><c>true</c> if allow edits; otherwise, <c>false</c>.</value>
  89. public bool AllowEdits { get; set; } = true;
  90. /// <summary>The bytes length per line.</summary>
  91. public int BytesPerLine => bytesPerLine;
  92. /// <summary>Gets the current cursor position starting at one for both, line and column.</summary>
  93. public Point CursorPosition
  94. {
  95. get
  96. {
  97. if (!IsInitialized)
  98. {
  99. return new Point (0, 0);
  100. }
  101. var delta = (int)position;
  102. int line = delta / bytesPerLine + 1;
  103. int item = delta % bytesPerLine + 1;
  104. return new Point (item, line);
  105. }
  106. }
  107. /// <summary>Get / Set the wished cursor when the field is focused</summary>
  108. public CursorVisibility DesiredCursorVisibility
  109. {
  110. get => desiredCursorVisibility;
  111. set
  112. {
  113. if (desiredCursorVisibility != value && HasFocus)
  114. {
  115. Application.Driver.SetCursorVisibility (value);
  116. }
  117. desiredCursorVisibility = value;
  118. }
  119. }
  120. /// <summary>
  121. /// Sets or gets the offset into the <see cref="Stream"/> that will displayed at the top of the
  122. /// <see cref="HexView"/>
  123. /// </summary>
  124. /// <value>The display start.</value>
  125. public long DisplayStart
  126. {
  127. get => displayStart;
  128. set
  129. {
  130. position = value;
  131. SetDisplayStart (value);
  132. }
  133. }
  134. /// <summary>
  135. /// Gets a <see cref="SortedDictionary{TKey, TValue}"/> describing the edits done to the <see cref="HexView"/>.
  136. /// Each Key indicates an offset where an edit was made and the Value is the changed byte.
  137. /// </summary>
  138. /// <value>The edits.</value>
  139. public IReadOnlyDictionary<long, byte> Edits => edits;
  140. /// <summary>Gets the current character position starting at one, related to the <see cref="Stream"/>.</summary>
  141. public long Position => position + 1;
  142. /// <summary>
  143. /// Sets or gets the <see cref="Stream"/> the <see cref="HexView"/> is operating on; the stream must support
  144. /// seeking ( <see cref="Stream.CanSeek"/> == true).
  145. /// </summary>
  146. /// <value>The source.</value>
  147. public Stream Source
  148. {
  149. get => source;
  150. set
  151. {
  152. if (value is null)
  153. {
  154. throw new ArgumentNullException ("source");
  155. }
  156. if (!value.CanSeek)
  157. {
  158. throw new ArgumentException ("The source stream must be seekable (CanSeek property)", "source");
  159. }
  160. source = value;
  161. if (displayStart > source.Length)
  162. {
  163. DisplayStart = 0;
  164. }
  165. if (position > source.Length)
  166. {
  167. position = 0;
  168. }
  169. SetNeedsDisplay ();
  170. }
  171. }
  172. private int bytesPerLine
  173. {
  174. get => bpl;
  175. set
  176. {
  177. bpl = value;
  178. OnPositionChanged ();
  179. }
  180. }
  181. private long position
  182. {
  183. get => pos;
  184. set
  185. {
  186. pos = value;
  187. OnPositionChanged ();
  188. }
  189. }
  190. /// <summary>
  191. /// This method applies and edits made to the <see cref="Stream"/> and resets the contents of the
  192. /// <see cref="Edits"/> property.
  193. /// </summary>
  194. /// <param name="stream">If provided also applies the changes to the passed <see cref="Stream"/></param>
  195. /// .
  196. public void ApplyEdits (Stream stream = null)
  197. {
  198. foreach (KeyValuePair<long, byte> kv in edits)
  199. {
  200. source.Position = kv.Key;
  201. source.WriteByte (kv.Value);
  202. source.Flush ();
  203. if (stream is { })
  204. {
  205. stream.Position = kv.Key;
  206. stream.WriteByte (kv.Value);
  207. stream.Flush ();
  208. }
  209. }
  210. edits = new SortedDictionary<long, byte> ();
  211. SetNeedsDisplay ();
  212. }
  213. /// <summary>
  214. /// This method discards the edits made to the <see cref="Stream"/> by resetting the contents of the
  215. /// <see cref="Edits"/> property.
  216. /// </summary>
  217. public void DiscardEdits () { edits = new SortedDictionary<long, byte> (); }
  218. /// <summary>Event to be invoked when an edit is made on the <see cref="Stream"/>.</summary>
  219. public event EventHandler<HexViewEditEventArgs> Edited;
  220. /// <inheritdoc/>
  221. public override bool MouseEvent (MouseEvent me)
  222. {
  223. // BUGBUG: Test this with a border! Assumes Frame == Bounds!
  224. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked)
  225. && !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
  226. && !me.Flags.HasFlag (MouseFlags.WheeledDown)
  227. && !me.Flags.HasFlag (MouseFlags.WheeledUp))
  228. {
  229. return false;
  230. }
  231. if (!HasFocus)
  232. {
  233. SetFocus ();
  234. }
  235. if (me.Flags == MouseFlags.WheeledDown)
  236. {
  237. DisplayStart = Math.Min (DisplayStart + bytesPerLine, source.Length);
  238. return true;
  239. }
  240. if (me.Flags == MouseFlags.WheeledUp)
  241. {
  242. DisplayStart = Math.Max (DisplayStart - bytesPerLine, 0);
  243. return true;
  244. }
  245. if (me.X < displayWidth)
  246. {
  247. return true;
  248. }
  249. int nblocks = bytesPerLine / bsize;
  250. int blocksSize = nblocks * 14;
  251. int blocksRightOffset = displayWidth + blocksSize - 1;
  252. if (me.X > blocksRightOffset + bytesPerLine - 1)
  253. {
  254. return true;
  255. }
  256. leftSide = me.X >= blocksRightOffset;
  257. long lineStart = me.Y * bytesPerLine + displayStart;
  258. int x = me.X - displayWidth + 1;
  259. int block = x / 14;
  260. x -= block * 2;
  261. int empty = x % 3;
  262. int item = x / 3;
  263. if (!leftSide && item > 0 && (empty == 0 || x == block * 14 + 14 - 1 - block * 2))
  264. {
  265. return true;
  266. }
  267. firstNibble = true;
  268. if (leftSide)
  269. {
  270. position = Math.Min (lineStart + me.X - blocksRightOffset, source.Length);
  271. }
  272. else
  273. {
  274. position = Math.Min (lineStart + item, source.Length);
  275. }
  276. if (me.Flags == MouseFlags.Button1DoubleClicked)
  277. {
  278. leftSide = !leftSide;
  279. if (leftSide)
  280. {
  281. firstNibble = empty == 1;
  282. }
  283. else
  284. {
  285. firstNibble = true;
  286. }
  287. }
  288. SetNeedsDisplay ();
  289. return true;
  290. }
  291. ///<inheritdoc/>
  292. public override void OnDrawContent (Rectangle contentArea)
  293. {
  294. Attribute currentAttribute;
  295. Attribute current = ColorScheme.Focus;
  296. Driver.SetAttribute (current);
  297. Move (0, 0);
  298. // BUGBUG: Bounds!!!!
  299. Rectangle frame = Frame;
  300. int nblocks = bytesPerLine / bsize;
  301. var data = new byte [nblocks * bsize * frame.Height];
  302. Source.Position = displayStart;
  303. int n = source.Read (data, 0, data.Length);
  304. Attribute activeColor = ColorScheme.HotNormal;
  305. Attribute trackingColor = ColorScheme.HotFocus;
  306. for (var line = 0; line < frame.Height; line++)
  307. {
  308. var lineRect = new Rectangle (0, line, frame.Width, 1);
  309. if (!Bounds.Contains (lineRect))
  310. {
  311. continue;
  312. }
  313. Move (0, line);
  314. Driver.SetAttribute (ColorScheme.HotNormal);
  315. Driver.AddStr (string.Format ("{0:x8} ", displayStart + line * nblocks * bsize));
  316. currentAttribute = ColorScheme.HotNormal;
  317. SetAttribute (GetNormalColor ());
  318. for (var block = 0; block < nblocks; block++)
  319. {
  320. for (var b = 0; b < bsize; b++)
  321. {
  322. int offset = line * nblocks * bsize + block * bsize + b;
  323. byte value = GetData (data, offset, out bool edited);
  324. if (offset + displayStart == position || edited)
  325. {
  326. SetAttribute (leftSide ? activeColor : trackingColor);
  327. }
  328. else
  329. {
  330. SetAttribute (GetNormalColor ());
  331. }
  332. Driver.AddStr (offset >= n && !edited ? " " : string.Format ("{0:x2}", value));
  333. SetAttribute (GetNormalColor ());
  334. Driver.AddRune ((Rune)' ');
  335. }
  336. Driver.AddStr (block + 1 == nblocks ? " " : "| ");
  337. }
  338. for (var bitem = 0; bitem < nblocks * bsize; bitem++)
  339. {
  340. int offset = line * nblocks * bsize + bitem;
  341. byte b = GetData (data, offset, out bool edited);
  342. Rune c;
  343. if (offset >= n && !edited)
  344. {
  345. c = (Rune)' ';
  346. }
  347. else
  348. {
  349. if (b < 32)
  350. {
  351. c = (Rune)'.';
  352. }
  353. else if (b > 127)
  354. {
  355. c = (Rune)'.';
  356. }
  357. else
  358. {
  359. Rune.DecodeFromUtf8 (new ReadOnlySpan<byte> (ref b), out c, out _);
  360. }
  361. }
  362. if (offset + displayStart == position || edited)
  363. {
  364. SetAttribute (leftSide ? trackingColor : activeColor);
  365. }
  366. else
  367. {
  368. SetAttribute (GetNormalColor ());
  369. }
  370. Driver.AddRune (c);
  371. }
  372. }
  373. void SetAttribute (Attribute attribute)
  374. {
  375. if (currentAttribute != attribute)
  376. {
  377. currentAttribute = attribute;
  378. Driver.SetAttribute (attribute);
  379. }
  380. }
  381. }
  382. /// <summary>Method used to invoke the <see cref="Edited"/> event passing the <see cref="KeyValuePair{TKey, TValue}"/>.</summary>
  383. /// <param name="e">The key value pair.</param>
  384. public virtual void OnEdited (HexViewEditEventArgs e) { Edited?.Invoke (this, e); }
  385. ///<inheritdoc/>
  386. public override bool OnEnter (View view)
  387. {
  388. Application.Driver.SetCursorVisibility (DesiredCursorVisibility);
  389. return base.OnEnter (view);
  390. }
  391. /// <summary>
  392. /// Method used to invoke the <see cref="PositionChanged"/> event passing the <see cref="HexViewEventArgs"/>
  393. /// arguments.
  394. /// </summary>
  395. public virtual void OnPositionChanged () { PositionChanged?.Invoke (this, new HexViewEventArgs (Position, CursorPosition, BytesPerLine)); }
  396. /// <inheritdoc/>
  397. public override bool OnProcessKeyDown (Key keyEvent)
  398. {
  399. if (!AllowEdits)
  400. {
  401. return false;
  402. }
  403. // Ignore control characters and other special keys
  404. if (keyEvent.KeyCode < KeyCode.Space || keyEvent.KeyCode > KeyCode.CharMask)
  405. {
  406. return false;
  407. }
  408. if (leftSide)
  409. {
  410. int value;
  411. var k = (char)keyEvent.KeyCode;
  412. if (k >= 'A' && k <= 'F')
  413. {
  414. value = k - 'A' + 10;
  415. }
  416. else if (k >= 'a' && k <= 'f')
  417. {
  418. value = k - 'a' + 10;
  419. }
  420. else if (k >= '0' && k <= '9')
  421. {
  422. value = k - '0';
  423. }
  424. else
  425. {
  426. return false;
  427. }
  428. byte b;
  429. if (!edits.TryGetValue (position, out b))
  430. {
  431. source.Position = position;
  432. b = (byte)source.ReadByte ();
  433. }
  434. RedisplayLine (position);
  435. if (firstNibble)
  436. {
  437. firstNibble = false;
  438. b = (byte)((b & 0xf) | (value << bsize));
  439. edits [position] = b;
  440. OnEdited (new HexViewEditEventArgs (position, edits [position]));
  441. }
  442. else
  443. {
  444. b = (byte)((b & 0xf0) | value);
  445. edits [position] = b;
  446. OnEdited (new HexViewEditEventArgs (position, edits [position]));
  447. MoveRight ();
  448. }
  449. return true;
  450. }
  451. return false;
  452. }
  453. /// <summary>Event to be invoked when the position and cursor position changes.</summary>
  454. public event EventHandler<HexViewEventArgs> PositionChanged;
  455. ///<inheritdoc/>
  456. public override void PositionCursor ()
  457. {
  458. var delta = (int)(position - displayStart);
  459. int line = delta / bytesPerLine;
  460. int item = delta % bytesPerLine;
  461. int block = item / bsize;
  462. int column = item % bsize * 3;
  463. if (leftSide)
  464. {
  465. Move (displayWidth + block * 14 + column + (firstNibble ? 0 : 1), line);
  466. }
  467. else
  468. {
  469. Move (displayWidth + bytesPerLine / bsize * 14 + item - 1, line);
  470. }
  471. }
  472. internal void SetDisplayStart (long value)
  473. {
  474. if (value > 0 && value >= source.Length)
  475. {
  476. displayStart = source.Length - 1;
  477. }
  478. else if (value < 0)
  479. {
  480. displayStart = 0;
  481. }
  482. else
  483. {
  484. displayStart = value;
  485. }
  486. SetNeedsDisplay ();
  487. }
  488. //
  489. // This is used to support editing of the buffer on a peer List<>,
  490. // the offset corresponds to an offset relative to DisplayStart, and
  491. // the buffer contains the contents of a screenful of data, so the
  492. // offset is relative to the buffer.
  493. //
  494. //
  495. private byte GetData (byte [] buffer, int offset, out bool edited)
  496. {
  497. long pos = DisplayStart + offset;
  498. if (edits.TryGetValue (pos, out byte v))
  499. {
  500. edited = true;
  501. return v;
  502. }
  503. edited = false;
  504. return buffer [offset];
  505. }
  506. private void HexView_LayoutComplete (object sender, LayoutEventArgs e)
  507. {
  508. // Small buffers will just show the position, with the bsize field value (4 bytes)
  509. bytesPerLine = bsize;
  510. if (Bounds.Width - displayWidth > 17)
  511. {
  512. bytesPerLine = bsize * ((Bounds.Width - displayWidth) / 18);
  513. }
  514. }
  515. private bool MoveDown (int bytes)
  516. {
  517. // BUGBUG: Bounds!
  518. RedisplayLine (position);
  519. if (position + bytes < source.Length)
  520. {
  521. position += bytes;
  522. }
  523. else if ((bytes == bytesPerLine * Frame.Height && source.Length >= DisplayStart + bytesPerLine * Frame.Height)
  524. || (bytes <= bytesPerLine * Frame.Height - bytesPerLine
  525. && source.Length <= DisplayStart + bytesPerLine * Frame.Height))
  526. {
  527. long p = position;
  528. while (p + bytesPerLine < source.Length)
  529. {
  530. p += bytesPerLine;
  531. }
  532. position = p;
  533. }
  534. if (position >= DisplayStart + bytesPerLine * Frame.Height)
  535. {
  536. SetDisplayStart (DisplayStart + bytes);
  537. SetNeedsDisplay ();
  538. }
  539. else
  540. {
  541. RedisplayLine (position);
  542. }
  543. return true;
  544. }
  545. private bool MoveEnd ()
  546. {
  547. position = source.Length;
  548. // BUGBUG: Bounds!
  549. if (position >= DisplayStart + bytesPerLine * Frame.Height)
  550. {
  551. SetDisplayStart (position);
  552. SetNeedsDisplay ();
  553. }
  554. else
  555. {
  556. RedisplayLine (position);
  557. }
  558. return true;
  559. }
  560. private bool MoveEndOfLine ()
  561. {
  562. position = Math.Min (position / bytesPerLine * bytesPerLine + bytesPerLine - 1, source.Length);
  563. SetNeedsDisplay ();
  564. return true;
  565. }
  566. private bool MoveHome ()
  567. {
  568. DisplayStart = 0;
  569. SetNeedsDisplay ();
  570. return true;
  571. }
  572. private bool MoveLeft ()
  573. {
  574. RedisplayLine (position);
  575. if (leftSide)
  576. {
  577. if (!firstNibble)
  578. {
  579. firstNibble = true;
  580. return true;
  581. }
  582. firstNibble = false;
  583. }
  584. if (position == 0)
  585. {
  586. return true;
  587. }
  588. if (position - 1 < DisplayStart)
  589. {
  590. SetDisplayStart (displayStart - bytesPerLine);
  591. SetNeedsDisplay ();
  592. }
  593. else
  594. {
  595. RedisplayLine (position);
  596. }
  597. position--;
  598. return true;
  599. }
  600. private bool MoveRight ()
  601. {
  602. RedisplayLine (position);
  603. if (leftSide)
  604. {
  605. if (firstNibble)
  606. {
  607. firstNibble = false;
  608. return true;
  609. }
  610. firstNibble = true;
  611. }
  612. if (position < source.Length)
  613. {
  614. position++;
  615. }
  616. // BUGBUG: Bounds!
  617. if (position >= DisplayStart + bytesPerLine * Frame.Height)
  618. {
  619. SetDisplayStart (DisplayStart + bytesPerLine);
  620. SetNeedsDisplay ();
  621. }
  622. else
  623. {
  624. RedisplayLine (position);
  625. }
  626. return true;
  627. }
  628. private bool MoveStartOfLine ()
  629. {
  630. position = position / bytesPerLine * bytesPerLine;
  631. SetNeedsDisplay ();
  632. return true;
  633. }
  634. private bool MoveUp (int bytes)
  635. {
  636. RedisplayLine (position);
  637. if (position - bytes > -1)
  638. {
  639. position -= bytes;
  640. }
  641. if (position < DisplayStart)
  642. {
  643. SetDisplayStart (DisplayStart - bytes);
  644. SetNeedsDisplay ();
  645. }
  646. else
  647. {
  648. RedisplayLine (position);
  649. }
  650. return true;
  651. }
  652. private void RedisplayLine (long pos)
  653. {
  654. var delta = (int)(pos - DisplayStart);
  655. int line = delta / bytesPerLine;
  656. // BUGBUG: Bounds!
  657. SetNeedsDisplay (new Rectangle (0, line, Frame.Width, 1));
  658. }
  659. private bool ToggleSide ()
  660. {
  661. leftSide = !leftSide;
  662. RedisplayLine (position);
  663. firstNibble = true;
  664. return true;
  665. }
  666. }