HexView.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. OnPositionChanged ();
  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. OnPositionChanged ();
  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 (!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.Position.X < DISPLAY_WIDTH)
  253. {
  254. return true;
  255. }
  256. int nblocks = BytesPerLine / BSIZE;
  257. int blocksSize = nblocks * 14;
  258. int blocksRightOffset = DISPLAY_WIDTH + blocksSize - 1;
  259. if (me.Position.X > blocksRightOffset + BytesPerLine - 1)
  260. {
  261. return true;
  262. }
  263. _leftSide = me.Position.X >= blocksRightOffset;
  264. long lineStart = me.Position.Y * BytesPerLine + _displayStart;
  265. int x = me.Position.X - DISPLAY_WIDTH + 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.Position.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. int nblocks = BytesPerLine / BSIZE;
  306. var data = new byte [nblocks * BSIZE * viewport.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 < viewport.Height; line++)
  312. {
  313. Rectangle lineRect = new (0, line, viewport.Width, 1);
  314. if (!Viewport.Contains (lineRect))
  315. {
  316. continue;
  317. }
  318. Move (0, line);
  319. Driver.SetAttribute (ColorScheme.HotNormal);
  320. Driver.AddStr ($"{_displayStart + line * nblocks * BSIZE:x8} ");
  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 ? " " : $"{value:x2}");
  338. SetAttribute (GetNormalColor ());
  339. Driver.AddRune (_spaceCharRune);
  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 = _spaceCharRune;
  351. }
  352. else
  353. {
  354. if (b < 32)
  355. {
  356. c = _periodCharRune;
  357. }
  358. else if (b > 127)
  359. {
  360. c = _periodCharRune;
  361. }
  362. else
  363. {
  364. Rune.DecodeFromUtf8 (new (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. protected void RaiseEdited (HexViewEditEventArgs e)
  390. {
  391. OnEditied(e);
  392. Edited?.Invoke (this, e);
  393. }
  394. /// <summary>Event to be invoked when an edit is made on the <see cref="Stream"/>.</summary>
  395. public event EventHandler<HexViewEditEventArgs>? Edited;
  396. /// <summary>
  397. ///
  398. /// </summary>
  399. /// <param name="e"></param>
  400. protected virtual void OnEditied (HexViewEditEventArgs e) { }
  401. /// <summary>
  402. /// Method used to invoke the <see cref="PositionChanged"/> event passing the <see cref="HexViewEventArgs"/>
  403. /// arguments.
  404. /// </summary>
  405. public virtual void OnPositionChanged () { PositionChanged?.Invoke (this, new (Position, CursorPosition, BytesPerLine)); }
  406. /// <inheritdoc/>
  407. public override bool OnProcessKeyDown (Key keyEvent)
  408. {
  409. if (!AllowEdits)
  410. {
  411. return false;
  412. }
  413. // Ignore control characters and other special keys
  414. if (keyEvent < Key.Space || keyEvent.KeyCode > KeyCode.CharMask)
  415. {
  416. return false;
  417. }
  418. if (_leftSide)
  419. {
  420. int value;
  421. var k = (char)keyEvent.KeyCode;
  422. if (k >= 'A' && k <= 'F')
  423. {
  424. value = k - 'A' + 10;
  425. }
  426. else if (k >= 'a' && k <= 'f')
  427. {
  428. value = k - 'a' + 10;
  429. }
  430. else if (k >= '0' && k <= '9')
  431. {
  432. value = k - '0';
  433. }
  434. else
  435. {
  436. return false;
  437. }
  438. byte b;
  439. if (!_edits.TryGetValue (position, out b))
  440. {
  441. _source.Position = position;
  442. b = (byte)_source.ReadByte ();
  443. }
  444. RedisplayLine (position);
  445. if (_firstNibble)
  446. {
  447. _firstNibble = false;
  448. b = (byte)((b & 0xf) | (value << BSIZE));
  449. _edits [position] = b;
  450. RaiseEdited (new (position, _edits [position]));
  451. }
  452. else
  453. {
  454. b = (byte)((b & 0xf0) | value);
  455. _edits [position] = b;
  456. RaiseEdited (new (position, _edits [position]));
  457. MoveRight ();
  458. }
  459. return true;
  460. }
  461. return false;
  462. }
  463. /// <summary>Event to be invoked when the position and cursor position changes.</summary>
  464. public event EventHandler<HexViewEventArgs>? PositionChanged;
  465. ///<inheritdoc/>
  466. public override Point? PositionCursor ()
  467. {
  468. var delta = (int)(position - _displayStart);
  469. int line = delta / BytesPerLine;
  470. int item = delta % BytesPerLine;
  471. int block = item / BSIZE;
  472. int column = item % BSIZE * 3;
  473. int x = DISPLAY_WIDTH + block * 14 + column + (_firstNibble ? 0 : 1);
  474. int y = line;
  475. if (!_leftSide)
  476. {
  477. x = DISPLAY_WIDTH + BytesPerLine / BSIZE * 14 + item - 1;
  478. }
  479. Move (x, y);
  480. return new (x, y);
  481. }
  482. //
  483. // This is used to support editing of the buffer on a peer List<>,
  484. // the offset corresponds to an offset relative to DisplayStart, and
  485. // the buffer contains the contents of a screenful of data, so the
  486. // offset is relative to the buffer.
  487. //
  488. //
  489. private byte GetData (byte [] buffer, int offset, out bool edited)
  490. {
  491. long pos = DisplayStart + offset;
  492. if (_edits.TryGetValue (pos, out byte v))
  493. {
  494. edited = true;
  495. return v;
  496. }
  497. edited = false;
  498. return buffer [offset];
  499. }
  500. private void HexView_LayoutComplete (object? sender, LayoutEventArgs e)
  501. {
  502. // Small buffers will just show the position, with the bsize field value (4 bytes)
  503. BytesPerLine = BSIZE;
  504. if (Viewport.Width - DISPLAY_WIDTH > 17)
  505. {
  506. BytesPerLine = BSIZE * ((Viewport.Width - DISPLAY_WIDTH) / 18);
  507. }
  508. }
  509. private bool MoveDown (int bytes)
  510. {
  511. RedisplayLine (position);
  512. if (position + bytes < _source.Length)
  513. {
  514. position += bytes;
  515. }
  516. else if ((bytes == BytesPerLine * Viewport.Height && _source.Length >= DisplayStart + BytesPerLine * Viewport.Height)
  517. || (bytes <= BytesPerLine * Viewport.Height - BytesPerLine
  518. && _source.Length <= DisplayStart + BytesPerLine * Viewport.Height))
  519. {
  520. long p = position;
  521. while (p + BytesPerLine < _source.Length)
  522. {
  523. p += BytesPerLine;
  524. }
  525. position = p;
  526. }
  527. if (position >= DisplayStart + BytesPerLine * Viewport.Height)
  528. {
  529. SetDisplayStart (DisplayStart + bytes);
  530. SetNeedsDisplay ();
  531. }
  532. else
  533. {
  534. RedisplayLine (position);
  535. }
  536. return true;
  537. }
  538. private bool MoveEnd ()
  539. {
  540. position = _source.Length;
  541. if (position >= DisplayStart + BytesPerLine * Viewport.Height)
  542. {
  543. SetDisplayStart (position);
  544. SetNeedsDisplay ();
  545. }
  546. else
  547. {
  548. RedisplayLine (position);
  549. }
  550. return true;
  551. }
  552. private bool MoveEndOfLine ()
  553. {
  554. position = Math.Min (position / BytesPerLine * BytesPerLine + BytesPerLine - 1, _source.Length);
  555. SetNeedsDisplay ();
  556. return true;
  557. }
  558. private bool MoveHome ()
  559. {
  560. DisplayStart = 0;
  561. SetNeedsDisplay ();
  562. return true;
  563. }
  564. private bool MoveLeft ()
  565. {
  566. RedisplayLine (position);
  567. if (_leftSide)
  568. {
  569. if (!_firstNibble)
  570. {
  571. _firstNibble = true;
  572. return true;
  573. }
  574. _firstNibble = false;
  575. }
  576. if (position == 0)
  577. {
  578. return true;
  579. }
  580. if (position - 1 < DisplayStart)
  581. {
  582. SetDisplayStart (_displayStart - BytesPerLine);
  583. SetNeedsDisplay ();
  584. }
  585. else
  586. {
  587. RedisplayLine (position);
  588. }
  589. position--;
  590. return true;
  591. }
  592. private bool MoveRight ()
  593. {
  594. RedisplayLine (position);
  595. if (_leftSide)
  596. {
  597. if (_firstNibble)
  598. {
  599. _firstNibble = false;
  600. return true;
  601. }
  602. _firstNibble = true;
  603. }
  604. if (position < _source.Length)
  605. {
  606. position++;
  607. }
  608. if (position >= DisplayStart + BytesPerLine * Viewport.Height)
  609. {
  610. SetDisplayStart (DisplayStart + BytesPerLine);
  611. SetNeedsDisplay ();
  612. }
  613. else
  614. {
  615. RedisplayLine (position);
  616. }
  617. return true;
  618. }
  619. private bool MoveLeftStart ()
  620. {
  621. position = position / BytesPerLine * BytesPerLine;
  622. SetNeedsDisplay ();
  623. return true;
  624. }
  625. private bool MoveUp (int bytes)
  626. {
  627. RedisplayLine (position);
  628. if (position - bytes > -1)
  629. {
  630. position -= bytes;
  631. }
  632. if (position < DisplayStart)
  633. {
  634. SetDisplayStart (DisplayStart - bytes);
  635. SetNeedsDisplay ();
  636. }
  637. else
  638. {
  639. RedisplayLine (position);
  640. }
  641. return true;
  642. }
  643. private void RedisplayLine (long pos)
  644. {
  645. if (BytesPerLine == 0)
  646. {
  647. return;
  648. }
  649. var delta = (int)(pos - DisplayStart);
  650. int line = delta / BytesPerLine;
  651. SetNeedsDisplay (new (0, line, Viewport.Width, 1));
  652. }
  653. private bool Navigate (NavigationDirection direction)
  654. {
  655. switch (direction)
  656. {
  657. case NavigationDirection.Forward:
  658. if (_leftSide)
  659. {
  660. _leftSide = false;
  661. RedisplayLine (position);
  662. _firstNibble = true;
  663. return true;
  664. }
  665. break;
  666. case NavigationDirection.Backward:
  667. if (!_leftSide)
  668. {
  669. _leftSide = true;
  670. RedisplayLine (position);
  671. _firstNibble = true;
  672. return true;
  673. }
  674. break;
  675. }
  676. return false;
  677. }
  678. /// <inheritdoc/>
  679. bool IDesignable.EnableForDesign ()
  680. {
  681. Source = new MemoryStream (Encoding.UTF8.GetBytes ("HexEditor Unicode that shouldn't 𝔹Aℝ𝔽!"));
  682. return true;
  683. }
  684. }