Editor.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, ColorScheme colorScheme)
  16. {
  17. Application.Init ();
  18. Top = top;
  19. if (Top == null) {
  20. Top = Application.Top;
  21. }
  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.F2, "~F2~ Open", () => Open()),
  39. new StatusItem(Key.F3, "~F3~ Save", () => Save()),
  40. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),
  41. });
  42. Top.Add (statusBar);
  43. CreateDemoFile (_fileName);
  44. Win = new Window (_fileName ?? "Untitled") {
  45. X = 0,
  46. Y = 1,
  47. Width = Dim.Fill (),
  48. Height = Dim.Fill (),
  49. ColorScheme = colorScheme,
  50. };
  51. Top.Add (Win);
  52. _textView = new TextView () {
  53. X = 0,
  54. Y = 0,
  55. Width = Dim.Fill (),
  56. Height = Dim.Fill (),
  57. };
  58. LoadFile ();
  59. Win.Add (_textView);
  60. }
  61. public override void Setup ()
  62. {
  63. }
  64. private void New ()
  65. {
  66. Win.Title = _fileName = "Untitled";
  67. throw new NotImplementedException ();
  68. }
  69. private void LoadFile ()
  70. {
  71. if (!_saved) {
  72. MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok");
  73. }
  74. if (_fileName != null) {
  75. // BUGBUG: #452 TextView.LoadFile keeps file open and provides no way of closing it
  76. //_textView.LoadFile(_fileName);
  77. _textView.Text = System.IO.File.ReadAllText (_fileName);
  78. Win.Title = _fileName;
  79. _saved = true;
  80. }
  81. }
  82. private void Paste ()
  83. {
  84. MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok");
  85. }
  86. private void Cut ()
  87. {
  88. MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok");
  89. }
  90. private void Copy ()
  91. {
  92. MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok");
  93. //if (_textView != null && _textView.SelectedLength != 0) {
  94. // _textView.Copy ();
  95. //}
  96. }
  97. private void Open ()
  98. {
  99. var d = new OpenDialog ("Open", "Open a file") { AllowsMultipleSelection = false };
  100. Application.Run (d);
  101. if (!d.Canceled) {
  102. _fileName = d.FilePaths [0];
  103. LoadFile ();
  104. }
  105. }
  106. private void Save ()
  107. {
  108. if (_fileName != null) {
  109. // BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
  110. // As a result files saved on Windows and then read back will show invalid chars.
  111. System.IO.File.WriteAllText (_fileName, _textView.Text.ToString());
  112. _saved = true;
  113. }
  114. }
  115. private void Quit ()
  116. {
  117. Application.RequestStop ();
  118. }
  119. private void CreateDemoFile(string fileName)
  120. {
  121. var sb = new StringBuilder ();
  122. // BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
  123. sb.Append ("Hello world.\n");
  124. sb.Append ("This is a test of the Emergency Broadcast System.\n");
  125. var sw = System.IO.File.CreateText (fileName);
  126. sw.Write (sb.ToString ());
  127. sw.Close ();
  128. }
  129. public override void Run ()
  130. {
  131. base.Run ();
  132. }
  133. }
  134. }