Editor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 ("Dialogs")]
  8. [ScenarioCategory ("Text")]
  9. [ScenarioCategory ("Dialogs")]
  10. [ScenarioCategory ("TopLevel")]
  11. class Editor : Scenario {
  12. private string _fileName = "demo.txt";
  13. private TextView _textView;
  14. private bool _saved = true;
  15. public override void Init (Toplevel top)
  16. {
  17. base.Init (top);
  18. }
  19. public override void Setup ()
  20. {
  21. var menu = new MenuBar (new MenuBarItem [] {
  22. new MenuBarItem ("_File", new MenuItem [] {
  23. new MenuItem ("_New", "", () => New()),
  24. new MenuItem ("_Open", "", () => Open()),
  25. new MenuItem ("_Save", "", () => Save()),
  26. null,
  27. new MenuItem ("_Quit", "", () => Quit()),
  28. }),
  29. new MenuBarItem ("_Edit", new MenuItem [] {
  30. new MenuItem ("_Copy", "", () => Copy()),
  31. new MenuItem ("C_ut", "", () => Cut()),
  32. new MenuItem ("_Paste", "", () => Paste())
  33. }),
  34. });
  35. Top.Add (menu);
  36. var statusBar = new StatusBar (new StatusItem [] {
  37. new StatusItem(Key.F2, "~F2~ Open", () => Open()),
  38. new StatusItem(Key.F3, "~F3~ Save", () => Save()),
  39. new StatusItem(Key.ControlQ, "~^Q~ Quit", () => Quit()),
  40. });
  41. Top.Add (statusBar);
  42. CreateDemoFile (_fileName);
  43. Win = new Window (_fileName ?? "Untitled") {
  44. X = 0,
  45. Y = 1,
  46. Width = Dim.Fill (),
  47. Height = Dim.Fill ()
  48. };
  49. Top.Add (Win);
  50. _textView = new TextView () {
  51. X = 0,
  52. Y = 0,
  53. Width = Dim.Fill (),
  54. Height = Dim.Fill (),
  55. };
  56. LoadFile ();
  57. Win.Add (_textView);
  58. }
  59. private void New ()
  60. {
  61. Win.Title = _fileName = "Untitled";
  62. throw new NotImplementedException ();
  63. }
  64. private void LoadFile ()
  65. {
  66. if (!_saved) {
  67. MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
  68. }
  69. if (_fileName != null) {
  70. // BUGBUG: #452 TextView.LoadFile keeps file open and provides no way of closing it
  71. //_textView.LoadFile(_fileName);
  72. _textView.Text = System.IO.File.ReadAllText (_fileName);
  73. Win.Title = _fileName;
  74. _saved = true;
  75. }
  76. }
  77. private void Paste ()
  78. {
  79. MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
  80. }
  81. private void Cut ()
  82. {
  83. MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
  84. }
  85. private void Copy ()
  86. {
  87. MessageBox.ErrorQuery (0, 10, "Not Implemented", "Functionality not yet implemented.", "Ok");
  88. //if (_textView != null && _textView.SelectedLength != 0) {
  89. // _textView.Copy ();
  90. //}
  91. }
  92. private void Open ()
  93. {
  94. var d = new OpenDialog ("Open", "Open a file") { AllowsMultipleSelection = false };
  95. Application.Run (d);
  96. if (!d.Canceled) {
  97. _fileName = d.FilePaths [0];
  98. LoadFile ();
  99. }
  100. }
  101. private void Save ()
  102. {
  103. if (_fileName != null) {
  104. // BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
  105. // As a result files saved on Windows and then read back will show invalid chars.
  106. System.IO.File.WriteAllText (_fileName, _textView.Text.ToString());
  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. public override void Run ()
  125. {
  126. base.Run ();
  127. }
  128. }
  129. }