HexView.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. #nullable enable
  2. //
  3. // HexView.cs: A hexadecimal viewer
  4. //
  5. // TODO:
  6. // - Support searching and highlighting of the search result
  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, IDesignable
  27. {
  28. private const int BSIZE = 4;
  29. private const int DISPLAY_WIDTH = 9;
  30. private bool _firstNibble;
  31. private bool _leftSide;
  32. private static readonly Rune _spaceCharRune = new (' ');
  33. private static readonly Rune _periodCharRune = new ('.');
  34. /// <summary>Initializes a <see cref="HexView"/> class.</summary>
  35. /// <param name="source">
  36. /// The <see cref="Stream"/> to view and edit as hex, this <see cref="Stream"/> must support seeking,
  37. /// or an exception will be thrown.
  38. /// </param>
  39. public HexView (Stream? source)
  40. {
  41. Source = source;
  42. CanFocus = true;
  43. CursorVisibility = CursorVisibility.Default;
  44. _leftSide = true;
  45. _firstNibble = true;
  46. // PERF: Closure capture of 'this' creates a lot of overhead.
  47. // BUG: Closure capture of 'this' may have unexpected results depending on how this is called.
  48. // The above two comments apply to all of the lambdas passed to all calls to AddCommand below.
  49. // Things this view knows how to do
  50. AddCommand (Command.Left, () => MoveLeft ());
  51. AddCommand (Command.Right, () => MoveRight ());
  52. AddCommand (Command.Down, () => MoveDown (BytesPerLine));
  53. AddCommand (Command.Up, () => MoveUp (BytesPerLine));
  54. AddCommand (Command.Tab, () => Navigate (NavigationDirection.Forward));
  55. AddCommand (Command.BackTab, () => Navigate (NavigationDirection.Backward));
  56. AddCommand (Command.PageUp, () => MoveUp (BytesPerLine * Frame.Height));
  57. AddCommand (Command.PageDown, () => MoveDown (BytesPerLine * Frame.Height));
  58. AddCommand (Command.Start, () => MoveHome ());
  59. AddCommand (Command.End, () => MoveEnd ());
  60. AddCommand (Command.LeftStart, () => MoveLeftStart ());
  61. AddCommand (Command.RightEnd, () => MoveEndOfLine ());
  62. AddCommand (Command.StartOfPage, () => MoveUp (BytesPerLine * ((int)(position - _displayStart) / BytesPerLine)));
  63. AddCommand (
  64. Command.EndOfPage,
  65. () => MoveDown (BytesPerLine * (Frame.Height - 1 - (int)(position - _displayStart) / BytesPerLine))
  66. );
  67. // Default keybindings for this view
  68. KeyBindings.Add (Key.CursorLeft, Command.Left);
  69. KeyBindings.Add (Key.CursorRight, Command.Right);
  70. KeyBindings.Add (Key.CursorDown, Command.Down);
  71. KeyBindings.Add (Key.CursorUp, Command.Up);
  72. KeyBindings.Add (Key.V.WithAlt, Command.PageUp);
  73. KeyBindings.Add (Key.PageUp, Command.PageUp);
  74. KeyBindings.Add (Key.V.WithCtrl, Command.PageDown);
  75. KeyBindings.Add (Key.PageDown, Command.PageDown);
  76. KeyBindings.Add (Key.Home, Command.Start);
  77. KeyBindings.Add (Key.End, Command.End);
  78. KeyBindings.Add (Key.CursorLeft.WithCtrl, Command.LeftStart);
  79. KeyBindings.Add (Key.CursorRight.WithCtrl, Command.RightEnd);
  80. KeyBindings.Add (Key.CursorUp.WithCtrl, Command.StartOfPage);
  81. KeyBindings.Add (Key.CursorDown.WithCtrl, Command.EndOfPage);
  82. KeyBindings.Add (Key.Tab, Command.Tab);
  83. KeyBindings.Add (Key.Tab.WithShift, Command.BackTab);
  84. LayoutComplete += HexView_LayoutComplete;
  85. }
  86. /// <summary>Initializes a <see cref="HexView"/> class.</summary>
  87. public HexView () : this (new MemoryStream ()) { }
  88. /// <summary>
  89. /// Gets or sets whether this <see cref="HexView"/> allow editing of the <see cref="Stream"/> of the underlying
  90. /// <see cref="Stream"/>.
  91. /// </summary>
  92. /// <value><c>true</c> if allow edits; otherwise, <c>false</c>.</value>
  93. public bool AllowEdits { get; set; } = true;
  94. /// <summary>Gets the current cursor position starting at one for both, line and column.</summary>
  95. public Point CursorPosition
  96. {
  97. get
  98. {
  99. if (!IsInitialized)
  100. {
  101. return Point.Empty;
  102. }
  103. var delta = (int)position;
  104. int line = delta / BytesPerLine + 1;
  105. int item = delta % BytesPerLine + 1;
  106. return new (item, line);
  107. }
  108. }
  109. private SortedDictionary<long, byte> _edits = [];
  110. /// <summary>
  111. /// Gets a <see cref="SortedDictionary{TKey, TValue}"/> describing the edits done to the <see cref="HexView"/>.
  112. /// Each Key indicates an offset where an edit was made and the Value is the changed byte.
  113. /// </summary>
  114. /// <value>The edits.</value>
  115. public IReadOnlyDictionary<long, byte> Edits => _edits;
  116. private Stream? _source;
  117. /// <summary>
  118. /// Sets or gets the <see cref="Stream"/> the <see cref="HexView"/> is operating on; the stream must support
  119. /// seeking ( <see cref="Stream.CanSeek"/> == true).
  120. /// </summary>
  121. /// <value>The source.</value>
  122. public Stream? Source
  123. {
  124. get => _source;
  125. set
  126. {
  127. ArgumentNullException.ThrowIfNull (value);
  128. if (!value!.CanSeek)
  129. {
  130. throw new ArgumentException (@"The source stream must be seekable (CanSeek property)");
  131. }
  132. _source = value;
  133. if (_displayStart > _source.Length)
  134. {
  135. DisplayStart = 0;
  136. }
  137. if (position > _source.Length)
  138. {
  139. position = 0;
  140. }
  141. SetNeedsDisplay ();
  142. }
  143. }
  144. private int _bpl;
  145. /// <summary>The bytes length per line.</summary>
  146. public int BytesPerLine
  147. {
  148. get => _bpl;
  149. set
  150. {
  151. _bpl = value;
  152. RaisePositionChanged ();
  153. }
  154. }
  155. private long _pos;
  156. // TODO: Why is this "starting at one"? How does that make sense?
  157. /// <summary>Gets the current character position starting at one, related to the <see cref="Stream"/>.</summary>
  158. public long Position => position + 1;
  159. private long position
  160. {
  161. get => _pos;
  162. set
  163. {
  164. _pos = value;
  165. RaisePositionChanged ();
  166. }
  167. }
  168. private long _displayStart;
  169. // TODO: Use Viewport content scrolling instead
  170. /// <summary>
  171. /// Sets or gets the offset into the <see cref="Stream"/> that will be displayed at the top of the
  172. /// <see cref="HexView"/>.
  173. /// </summary>
  174. /// <value>The display start.</value>
  175. public long DisplayStart
  176. {
  177. get => _displayStart;
  178. set
  179. {
  180. position = value;
  181. SetDisplayStart (value);
  182. }
  183. }
  184. internal void SetDisplayStart (long value)
  185. {
  186. if (value > 0 && value >= _source.Length)
  187. {
  188. _displayStart = _source.Length - 1;
  189. }
  190. else if (value < 0)
  191. {
  192. _displayStart = 0;
  193. }
  194. else
  195. {
  196. _displayStart = value;
  197. }
  198. SetNeedsDisplay ();
  199. }
  200. /// <summary>
  201. /// Applies and edits made to the <see cref="Stream"/> and resets the contents of the
  202. /// <see cref="Edits"/> property.
  203. /// </summary>
  204. /// <param name="stream">If provided also applies the changes to the passed <see cref="Stream"/>.</param>
  205. /// .
  206. public void ApplyEdits (Stream? stream = null)
  207. {
  208. foreach (KeyValuePair<long, byte> kv in _edits)
  209. {
  210. _source!.Position = kv.Key;
  211. _source.WriteByte (kv.Value);
  212. _source.Flush ();
  213. if (stream is { })
  214. {
  215. stream.Position = kv.Key;
  216. stream.WriteByte (kv.Value);
  217. stream.Flush ();
  218. }
  219. }
  220. _edits = new ();
  221. SetNeedsDisplay ();
  222. }
  223. /// <summary>
  224. /// Discards the edits made to the <see cref="Stream"/> by resetting the contents of the
  225. /// <see cref="Edits"/> property.
  226. /// </summary>
  227. public void DiscardEdits () { _edits = new (); }
  228. /// <inheritdoc/>
  229. protected internal override bool OnMouseEvent (MouseEvent me)
  230. {
  231. if (_source is null)
  232. {
  233. return false;
  234. }
  235. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked)
  236. && !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
  237. && !me.Flags.HasFlag (MouseFlags.WheeledDown)
  238. && !me.Flags.HasFlag (MouseFlags.WheeledUp))
  239. {
  240. return false;
  241. }
  242. if (!HasFocus)
  243. {
  244. SetFocus ();
  245. }
  246. if (me.Flags == MouseFlags.WheeledDown)
  247. {
  248. DisplayStart = Math.Min (DisplayStart + BytesPerLine, _source.Length);
  249. return true;
  250. }
  251. if (me.Flags == MouseFlags.WheeledUp)
  252. {
  253. DisplayStart = Math.Max (DisplayStart - BytesPerLine, 0);
  254. return true;
  255. }
  256. if (me.Position.X < DISPLAY_WIDTH)
  257. {
  258. return true;
  259. }
  260. int nblocks = BytesPerLine / BSIZE;
  261. int blocksSize = nblocks * 14;
  262. int blocksRightOffset = DISPLAY_WIDTH + blocksSize - 1;
  263. if (me.Position.X > blocksRightOffset + BytesPerLine - 1)
  264. {
  265. return true;
  266. }
  267. _leftSide = me.Position.X >= blocksRightOffset;
  268. long lineStart = me.Position.Y * BytesPerLine + _displayStart;
  269. int x = me.Position.X - DISPLAY_WIDTH + 1;
  270. int block = x / 14;
  271. x -= block * 2;
  272. int empty = x % 3;
  273. int item = x / 3;
  274. if (!_leftSide && item > 0 && (empty == 0 || x == block * 14 + 14 - 1 - block * 2))
  275. {
  276. return true;
  277. }
  278. _firstNibble = true;
  279. if (_leftSide)
  280. {
  281. position = Math.Min (lineStart + me.Position.X - blocksRightOffset, _source.Length);
  282. }
  283. else
  284. {
  285. position = Math.Min (lineStart + item, _source.Length);
  286. }
  287. if (me.Flags == MouseFlags.Button1DoubleClicked)
  288. {
  289. _leftSide = !_leftSide;
  290. if (_leftSide)
  291. {
  292. _firstNibble = empty == 1;
  293. }
  294. else
  295. {
  296. _firstNibble = true;
  297. }
  298. }
  299. SetNeedsDisplay ();
  300. return true;
  301. }
  302. ///<inheritdoc/>
  303. public override void OnDrawContent (Rectangle viewport)
  304. {
  305. if (Source is null)
  306. {
  307. return;
  308. }
  309. Attribute currentAttribute;
  310. Attribute current = GetFocusColor ();
  311. Driver.SetAttribute (current);
  312. Move (0, 0);
  313. int nblocks = BytesPerLine / BSIZE;
  314. var data = new byte [nblocks * BSIZE * viewport.Height];
  315. Source.Position = _displayStart;
  316. int n = _source.Read (data, 0, data.Length);
  317. Attribute activeColor = GetHotNormalColor ();
  318. Attribute trackingColor = GetHotFocusColor ();
  319. for (var line = 0; line < viewport.Height; line++)
  320. {
  321. Rectangle lineRect = new (0, line, viewport.Width, 1);
  322. if (!Viewport.Contains (lineRect))
  323. {
  324. continue;
  325. }
  326. Move (0, line);
  327. currentAttribute = GetHotNormalColor ();
  328. Driver.SetAttribute (currentAttribute);
  329. Driver.AddStr ($"{_displayStart + line * nblocks * BSIZE:x8} ");
  330. SetAttribute (GetNormalColor ());
  331. for (var block = 0; block < nblocks; block++)
  332. {
  333. for (var b = 0; b < BSIZE; b++)
  334. {
  335. int offset = line * nblocks * BSIZE + block * BSIZE + b;
  336. byte value = GetData (data, offset, out bool edited);
  337. if (offset + _displayStart == position || edited)
  338. {
  339. SetAttribute (_leftSide ? activeColor : trackingColor);
  340. }
  341. else
  342. {
  343. SetAttribute (GetNormalColor ());
  344. }
  345. Driver.AddStr (offset >= n && !edited ? " " : $"{value:x2}");
  346. SetAttribute (GetNormalColor ());
  347. Driver.AddRune (_spaceCharRune);
  348. }
  349. Driver.AddStr (block + 1 == nblocks ? " " : "| ");
  350. }
  351. for (var bitem = 0; bitem < nblocks * BSIZE; bitem++)
  352. {
  353. int offset = line * nblocks * BSIZE + bitem;
  354. byte b = GetData (data, offset, out bool edited);
  355. Rune c;
  356. if (offset >= n && !edited)
  357. {
  358. c = _spaceCharRune;
  359. }
  360. else
  361. {
  362. if (b < 32)
  363. {
  364. c = _periodCharRune;
  365. }
  366. else if (b > 127)
  367. {
  368. c = _periodCharRune;
  369. }
  370. else
  371. {
  372. Rune.DecodeFromUtf8 (new (ref b), out c, out _);
  373. }
  374. }
  375. if (offset + _displayStart == position || edited)
  376. {
  377. SetAttribute (_leftSide ? trackingColor : activeColor);
  378. }
  379. else
  380. {
  381. SetAttribute (GetNormalColor ());
  382. }
  383. Driver.AddRune (c);
  384. }
  385. }
  386. void SetAttribute (Attribute attribute)
  387. {
  388. if (currentAttribute != attribute)
  389. {
  390. currentAttribute = attribute;
  391. Driver.SetAttribute (attribute);
  392. }
  393. }
  394. }
  395. /// <summary>Raises the <see cref="Edited"/> event.</summary>
  396. protected void RaiseEdited (HexViewEditEventArgs e)
  397. {
  398. OnEditied (e);
  399. Edited?.Invoke (this, e);
  400. }
  401. /// <summary>Event to be invoked when an edit is made on the <see cref="Stream"/>.</summary>
  402. public event EventHandler<HexViewEditEventArgs>? Edited;
  403. /// <summary>
  404. ///
  405. /// </summary>
  406. /// <param name="e"></param>
  407. protected virtual void OnEditied (HexViewEditEventArgs e) { }
  408. /// <summary>Raises the <see cref="PositionChanged"/> event.</summary>
  409. protected void RaisePositionChanged ()
  410. {
  411. HexViewEventArgs args = new (Position, CursorPosition, BytesPerLine);
  412. OnPositionChanged (args);
  413. PositionChanged?.Invoke (this, args);
  414. }
  415. /// <summary>
  416. /// Called when <see cref="Position"/> has changed.
  417. /// </summary>
  418. protected virtual void OnPositionChanged (HexViewEventArgs e) { }
  419. /// <inheritdoc/>
  420. public override bool OnProcessKeyDown (Key keyEvent)
  421. {
  422. if (!AllowEdits || _source is null)
  423. {
  424. return false;
  425. }
  426. // Ignore control characters and other special keys
  427. if (keyEvent < Key.Space || keyEvent.KeyCode > KeyCode.CharMask)
  428. {
  429. return false;
  430. }
  431. if (_leftSide)
  432. {
  433. int value;
  434. var k = (char)keyEvent.KeyCode;
  435. if (k >= 'A' && k <= 'F')
  436. {
  437. value = k - 'A' + 10;
  438. }
  439. else if (k >= 'a' && k <= 'f')
  440. {
  441. value = k - 'a' + 10;
  442. }
  443. else if (k >= '0' && k <= '9')
  444. {
  445. value = k - '0';
  446. }
  447. else
  448. {
  449. return false;
  450. }
  451. byte b;
  452. if (!_edits.TryGetValue (position, out b))
  453. {
  454. _source.Position = position;
  455. b = (byte)_source.ReadByte ();
  456. }
  457. RedisplayLine (position);
  458. if (_firstNibble)
  459. {
  460. _firstNibble = false;
  461. b = (byte)((b & 0xf) | (value << BSIZE));
  462. _edits [position] = b;
  463. RaiseEdited (new (position, _edits [position]));
  464. }
  465. else
  466. {
  467. b = (byte)((b & 0xf0) | value);
  468. _edits [position] = b;
  469. RaiseEdited (new (position, _edits [position]));
  470. MoveRight ();
  471. }
  472. return true;
  473. }
  474. return false;
  475. }
  476. /// <summary>Event to be invoked when the position and cursor position changes.</summary>
  477. public event EventHandler<HexViewEventArgs>? PositionChanged;
  478. ///<inheritdoc/>
  479. public override Point? PositionCursor ()
  480. {
  481. var delta = (int)(position - _displayStart);
  482. int line = delta / BytesPerLine;
  483. int item = delta % BytesPerLine;
  484. int block = item / BSIZE;
  485. int column = item % BSIZE * 3;
  486. int x = DISPLAY_WIDTH + block * 14 + column + (_firstNibble ? 0 : 1);
  487. int y = line;
  488. if (!_leftSide)
  489. {
  490. x = DISPLAY_WIDTH + BytesPerLine / BSIZE * 14 + item - 1;
  491. }
  492. Move (x, y);
  493. return new (x, y);
  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 - DISPLAY_WIDTH > 17)
  518. {
  519. BytesPerLine = BSIZE * ((Viewport.Width - DISPLAY_WIDTH) / 18);
  520. }
  521. }
  522. private bool MoveDown (int bytes)
  523. {
  524. RedisplayLine (position);
  525. if (position + bytes < _source.Length)
  526. {
  527. position += bytes;
  528. }
  529. else if ((bytes == BytesPerLine * Viewport.Height && _source.Length >= DisplayStart + BytesPerLine * Viewport.Height)
  530. || (bytes <= BytesPerLine * Viewport.Height - BytesPerLine
  531. && _source.Length <= DisplayStart + BytesPerLine * Viewport.Height))
  532. {
  533. long p = position;
  534. while (p + BytesPerLine < _source.Length)
  535. {
  536. p += BytesPerLine;
  537. }
  538. position = p;
  539. }
  540. if (position >= DisplayStart + BytesPerLine * Viewport.Height)
  541. {
  542. SetDisplayStart (DisplayStart + bytes);
  543. SetNeedsDisplay ();
  544. }
  545. else
  546. {
  547. RedisplayLine (position);
  548. }
  549. return true;
  550. }
  551. private bool MoveEnd ()
  552. {
  553. position = _source.Length;
  554. if (position >= DisplayStart + BytesPerLine * Viewport.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. if (position >= DisplayStart + BytesPerLine * Viewport.Height)
  622. {
  623. SetDisplayStart (DisplayStart + BytesPerLine);
  624. SetNeedsDisplay ();
  625. }
  626. else
  627. {
  628. RedisplayLine (position);
  629. }
  630. return true;
  631. }
  632. private bool MoveLeftStart ()
  633. {
  634. position = position / BytesPerLine * BytesPerLine;
  635. SetNeedsDisplay ();
  636. return true;
  637. }
  638. private bool MoveUp (int bytes)
  639. {
  640. RedisplayLine (position);
  641. if (position - bytes > -1)
  642. {
  643. position -= bytes;
  644. }
  645. if (position < DisplayStart)
  646. {
  647. SetDisplayStart (DisplayStart - bytes);
  648. SetNeedsDisplay ();
  649. }
  650. else
  651. {
  652. RedisplayLine (position);
  653. }
  654. return true;
  655. }
  656. private void RedisplayLine (long pos)
  657. {
  658. if (BytesPerLine == 0)
  659. {
  660. return;
  661. }
  662. var delta = (int)(pos - DisplayStart);
  663. int line = delta / BytesPerLine;
  664. SetNeedsDisplay (new (0, line, Viewport.Width, 1));
  665. }
  666. private bool Navigate (NavigationDirection direction)
  667. {
  668. switch (direction)
  669. {
  670. case NavigationDirection.Forward:
  671. if (_leftSide)
  672. {
  673. _leftSide = false;
  674. RedisplayLine (position);
  675. _firstNibble = true;
  676. return true;
  677. }
  678. break;
  679. case NavigationDirection.Backward:
  680. if (!_leftSide)
  681. {
  682. _leftSide = true;
  683. RedisplayLine (position);
  684. _firstNibble = true;
  685. return true;
  686. }
  687. break;
  688. }
  689. return false;
  690. }
  691. /// <inheritdoc/>
  692. bool IDesignable.EnableForDesign ()
  693. {
  694. Source = new MemoryStream (Encoding.UTF8.GetBytes ("HexEditor Unicode that shouldn't 𝔹Aℝ𝔽!"));
  695. return true;
  696. }
  697. }