Editor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Text;
  3. using Terminal.Gui;
  4. namespace UICatalog {
  5. [ScenarioMetadata (Name: "Editor", Description: "A Terminal.Gui Text Editor via TextView")]
  6. [ScenarioCategory ("Controls")]
  7. [ScenarioCategory ("Text")]
  8. class Editor : Scenario {
  9. private string _fileName = "demo.txt";
  10. private TextView _textView;
  11. private bool _saved = true;
  12. public override void Init (Toplevel top)
  13. {
  14. Top = top;
  15. }
  16. public override void Setup ()
  17. {
  18. var menu = new MenuBar (new MenuBarItem [] {
  19. new MenuBarItem ("_File", new MenuItem [] {
  20. new MenuItem ("_New", "", () => New()),
  21. new MenuItem ("_Open", "", () => Open()),
  22. new MenuItem ("_Save", "", () => Save()),
  23. null,
  24. new MenuItem ("_Quit", "", () => Quit()),
  25. }),
  26. new MenuBarItem ("_Edit", new MenuItem [] {
  27. new MenuItem ("_Copy", "", () => Copy()),
  28. new MenuItem ("C_ut", "", () => Cut()),
  29. new MenuItem ("_Paste", "", () => Paste())
  30. }),
  31. });
  32. Top.Add (menu);
  33. var statusBar = new StatusBar (new StatusItem [] {
  34. new StatusItem(Key.F2, "~F2~ Open", () => Open()),
  35. new StatusItem(Key.F3, "~F3~ Save", () => Save()),
  36. new StatusItem(Key.ControlQ, "~^Q~ Quit", () => Quit()),
  37. });
  38. Top.Add (statusBar);
  39. CreateDemoFile (_fileName);
  40. Win = new Window (_fileName ?? "Untitled") {
  41. X = 0,
  42. Y = 1,
  43. Width = Dim.Fill (),
  44. Height = Dim.Fill ()
  45. };
  46. Top.Add (Win);
  47. _textView = new TextView () {
  48. X = 0,
  49. Y = 0,
  50. Width = Dim.Fill (),
  51. Height = Dim.Fill (),
  52. };
  53. LoadFile ();
  54. Win.Add (_textView);
  55. }
  56. private void New ()
  57. {
  58. Win.Title = _fileName = "Untitled";
  59. throw new NotImplementedException ();
  60. }
  61. private void LoadFile ()
  62. {
  63. if (!_saved) {
  64. MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
  65. }
  66. if (_fileName != null) {
  67. // BUGBUG: #452 TextView.LoadFile keeps file open and provides no way of closing it
  68. //_textView.LoadFile(_fileName);
  69. _textView.Text = System.IO.File.ReadAllText (_fileName);
  70. Win.Title = _fileName;
  71. _saved = true;
  72. }
  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. LoadFile ();
  96. }
  97. }
  98. private void Save ()
  99. {
  100. if (_fileName != null) {
  101. // BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
  102. // As a result files saved on Windows and then read back will show invalid chars.
  103. System.IO.File.WriteAllText (_fileName, _textView.Text.ToString());
  104. _saved = true;
  105. }
  106. }
  107. private void Quit ()
  108. {
  109. Application.RequestStop ();
  110. }
  111. private void CreateDemoFile(string fileName)
  112. {
  113. var sb = new StringBuilder ();
  114. // BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
  115. sb.Append ("Hello world.\n");
  116. sb.Append ("This is a test of the Emergency Broadcast System.\n");
  117. var sw = System.IO.File.CreateText (fileName);
  118. sw.Write (sb.ToString ());
  119. sw.Close ();
  120. }
  121. public override void Run ()
  122. {
  123. Application.Run (Top);
  124. }
  125. }
  126. }