HexView.cs 23 KB

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