HexView.cs 23 KB

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