HexView.cs 22 KB

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