Editor.cs 3.6 KB

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