HexEditor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Terminal.Gui;
  5. namespace UICatalog {
  6. [ScenarioMetadata (Name: "HexEditor", Description: "A Terminal.Gui binary (hex) editor via HexView")]
  7. [ScenarioCategory ("Controls")]
  8. [ScenarioCategory ("Text")]
  9. class HexEditor : Scenario {
  10. private string _fileName = "demo.bin";
  11. private HexView _hexView;
  12. private bool _saved = true;
  13. public override void Init (Toplevel top)
  14. {
  15. Top = top;
  16. }
  17. public override void Setup ()
  18. {
  19. var menu = new MenuBar (new MenuBarItem [] {
  20. new MenuBarItem ("_File", new MenuItem [] {
  21. new MenuItem ("_New", "", () => New()),
  22. new MenuItem ("_Open", "", () => Open()),
  23. new MenuItem ("_Save", "", () => Save()),
  24. null,
  25. new MenuItem ("_Quit", "", () => Quit()),
  26. }),
  27. new MenuBarItem ("_Edit", new MenuItem [] {
  28. new MenuItem ("_Copy", "", () => Copy()),
  29. new MenuItem ("C_ut", "", () => Cut()),
  30. new MenuItem ("_Paste", "", () => Paste())
  31. }),
  32. });
  33. Top.Add (menu);
  34. var statusBar = new StatusBar (new StatusItem [] {
  35. //new StatusItem(Key.Enter, "~ENTER~ ApplyEdits", () => { _hexView.ApplyEdits(); }),
  36. new StatusItem(Key.F2, "~F2~ Open", () => Open()),
  37. new StatusItem(Key.F3, "~F3~ Save", () => Save()),
  38. new StatusItem(Key.ControlQ, "~^Q~ Quit", () => Quit()),
  39. });
  40. Top.Add (statusBar);
  41. CreateDemoFile (_fileName);
  42. Win = new Window (_fileName ?? "Untitled") {
  43. X = 0,
  44. Y = 1,
  45. Width = Dim.Fill (),
  46. Height = Dim.Fill ()
  47. };
  48. Top.Add (Win);
  49. _hexView = new HexView (LoadFile()) {
  50. X = 0,
  51. Y = 0,
  52. Width = Dim.Fill (),
  53. Height = Dim.Fill (),
  54. };
  55. Win.Add (_hexView);
  56. }
  57. private void New ()
  58. {
  59. Win.Title = _fileName = "Untitled";
  60. throw new NotImplementedException ();
  61. }
  62. private Stream LoadFile ()
  63. {
  64. MemoryStream stream = null;
  65. if (!_saved) {
  66. MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
  67. }
  68. if (_fileName != null) {
  69. var bin = System.IO.File.ReadAllBytes (_fileName);
  70. stream = new MemoryStream (bin);
  71. Win.Title = _fileName;
  72. _saved = true;
  73. }
  74. return stream;
  75. }
  76. private void Paste ()
  77. {
  78. MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
  79. }
  80. private void Cut ()
  81. {
  82. MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
  83. }
  84. private void Copy ()
  85. {
  86. MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
  87. //if (_textView != null && _textView.SelectedLength != 0) {
  88. // _textView.Copy ();
  89. //}
  90. }
  91. private void Open ()
  92. {
  93. var d = new OpenDialog ("Open", "Open a file") { AllowsMultipleSelection = false };
  94. Application.Run (d);
  95. if (!d.Canceled) {
  96. _fileName = d.FilePaths [0];
  97. _hexView.Source = LoadFile ();
  98. _hexView.DisplayStart = 0;
  99. }
  100. }
  101. private void Save ()
  102. {
  103. if (_fileName != null) {
  104. using (FileStream fs = new FileStream (_fileName, FileMode.OpenOrCreate)) {
  105. _hexView.ApplyEdits ();
  106. _hexView.Source.CopyTo (fs);
  107. fs.Flush ();
  108. }
  109. _saved = true;
  110. }
  111. }
  112. private void Quit ()
  113. {
  114. Application.RequestStop ();
  115. }
  116. private void CreateDemoFile(string fileName)
  117. {
  118. var sb = new StringBuilder ();
  119. // BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
  120. sb.Append ("Hello world.\n");
  121. sb.Append ("This is a test of the Emergency Broadcast System.\n");
  122. var sw = System.IO.File.CreateText (fileName);
  123. sw.Write (sb.ToString ());
  124. sw.Close ();
  125. }
  126. public override void Run ()
  127. {
  128. Application.Run (Top);
  129. }
  130. }
  131. }