HexEditor.cs 3.7 KB

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