Editor.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. private ScrollBarView _scrollBar;
  16. public override void Init (Toplevel top, ColorScheme colorScheme)
  17. {
  18. Application.Init ();
  19. Top = top;
  20. if (Top == null) {
  21. Top = Application.Top;
  22. }
  23. var menu = new MenuBar (new MenuBarItem [] {
  24. new MenuBarItem ("_File", new MenuItem [] {
  25. new MenuItem ("_New", "", () => New()),
  26. new MenuItem ("_Open", "", () => Open()),
  27. new MenuItem ("_Save", "", () => Save()),
  28. null,
  29. new MenuItem ("_Quit", "", () => Quit()),
  30. }),
  31. new MenuBarItem ("_Edit", new MenuItem [] {
  32. new MenuItem ("_Copy", "", () => Copy()),
  33. new MenuItem ("C_ut", "", () => Cut()),
  34. new MenuItem ("_Paste", "", () => Paste())
  35. }),
  36. new MenuBarItem ("_ScrollBarView", CreateKeepChecked ()),
  37. new MenuBarItem ("_Cursor", new MenuItem [] {
  38. new MenuItem ("_Invisible", "", () => SetCursor(CursorVisibility.Invisible)),
  39. new MenuItem ("_Box", "", () => SetCursor(CursorVisibility.Box)),
  40. new MenuItem ("_Underline", "", () => SetCursor(CursorVisibility.Underline)),
  41. new MenuItem ("", "", () => {}, () => { return false; }),
  42. new MenuItem ("xTerm :", "", () => {}, () => { return false; }),
  43. new MenuItem ("", "", () => {}, () => { return false; }),
  44. new MenuItem (" _Default", "", () => SetCursor(CursorVisibility.Default)),
  45. new MenuItem (" _Vertical", "", () => SetCursor(CursorVisibility.Vertical)),
  46. new MenuItem (" V_ertical Fix", "", () => SetCursor(CursorVisibility.VerticalFix)),
  47. new MenuItem (" B_ox Fix", "", () => SetCursor(CursorVisibility.BoxFix)),
  48. new MenuItem (" U_nderline Fix","", () => SetCursor(CursorVisibility.UnderlineFix))
  49. })
  50. });
  51. Top.Add (menu);
  52. var statusBar = new StatusBar (new StatusItem [] {
  53. new StatusItem(Key.F2, "~F2~ Open", () => Open()),
  54. new StatusItem(Key.F3, "~F3~ Save", () => Save()),
  55. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),
  56. });
  57. Top.Add (statusBar);
  58. CreateDemoFile (_fileName);
  59. Win = new Window (_fileName ?? "Untitled") {
  60. X = 0,
  61. Y = 1,
  62. Width = Dim.Fill (),
  63. Height = Dim.Fill (),
  64. ColorScheme = colorScheme,
  65. };
  66. Top.Add (Win);
  67. _textView = new TextView () {
  68. X = 0,
  69. Y = 0,
  70. Width = Dim.Fill (),
  71. Height = Dim.Fill (),
  72. };
  73. LoadFile ();
  74. Win.Add (_textView);
  75. _scrollBar = new ScrollBarView (_textView, true);
  76. _scrollBar.ChangedPosition += () => {
  77. _textView.TopRow = _scrollBar.Position;
  78. if (_textView.TopRow != _scrollBar.Position) {
  79. _scrollBar.Position = _textView.TopRow;
  80. }
  81. _textView.SetNeedsDisplay ();
  82. };
  83. _scrollBar.OtherScrollBarView.ChangedPosition += () => {
  84. _textView.LeftColumn = _scrollBar.OtherScrollBarView.Position;
  85. if (_textView.LeftColumn != _scrollBar.OtherScrollBarView.Position) {
  86. _scrollBar.OtherScrollBarView.Position = _textView.LeftColumn;
  87. }
  88. _textView.SetNeedsDisplay ();
  89. };
  90. _textView.DrawContent += (e) => {
  91. _scrollBar.Size = _textView.Lines - 1;
  92. _scrollBar.Position = _textView.TopRow;
  93. _scrollBar.OtherScrollBarView.Size = _textView.Maxlength;
  94. _scrollBar.OtherScrollBarView.Position = _textView.LeftColumn;
  95. _scrollBar.LayoutSubviews ();
  96. _scrollBar.Refresh ();
  97. };
  98. }
  99. public override void Setup ()
  100. {
  101. }
  102. private void New ()
  103. {
  104. Win.Title = _fileName = "Untitled";
  105. throw new NotImplementedException ();
  106. }
  107. private void LoadFile ()
  108. {
  109. if (!_saved) {
  110. MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok");
  111. }
  112. if (_fileName != null) {
  113. // BUGBUG: #452 TextView.LoadFile keeps file open and provides no way of closing it
  114. //_textView.LoadFile(_fileName);
  115. _textView.Text = System.IO.File.ReadAllText (_fileName);
  116. Win.Title = _fileName;
  117. _saved = true;
  118. }
  119. }
  120. private void Paste ()
  121. {
  122. MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok");
  123. }
  124. private void Cut ()
  125. {
  126. MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok");
  127. }
  128. private void Copy ()
  129. {
  130. MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok");
  131. //if (_textView != null && _textView.SelectedLength != 0) {
  132. // _textView.Copy ();
  133. //}
  134. }
  135. private void SetCursor (CursorVisibility visibility)
  136. {
  137. _textView.DesiredCursorVisibility = visibility;
  138. }
  139. private void Open ()
  140. {
  141. var d = new OpenDialog ("Open", "Open a file") { AllowsMultipleSelection = false };
  142. Application.Run (d);
  143. if (!d.Canceled) {
  144. _fileName = d.FilePaths [0];
  145. LoadFile ();
  146. }
  147. }
  148. private void Save ()
  149. {
  150. if (_fileName != null) {
  151. // BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
  152. // As a result files saved on Windows and then read back will show invalid chars.
  153. System.IO.File.WriteAllText (_fileName, _textView.Text.ToString());
  154. _saved = true;
  155. }
  156. }
  157. private void Quit ()
  158. {
  159. Application.RequestStop ();
  160. }
  161. private void CreateDemoFile(string fileName)
  162. {
  163. var sb = new StringBuilder ();
  164. // BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
  165. sb.Append ("Hello world.\n");
  166. sb.Append ("This is a test of the Emergency Broadcast System.\n");
  167. for (int i = 0; i < 30; i++) {
  168. sb.Append ($"{i} - This is a test with a very long line and many lines to test the ScrollViewBar against the TextView. - {i}\n");
  169. }
  170. var sw = System.IO.File.CreateText (fileName);
  171. sw.Write (sb.ToString ());
  172. sw.Close ();
  173. }
  174. private MenuItem [] CreateKeepChecked ()
  175. {
  176. var item = new MenuItem ();
  177. item.Title = "Keep Content Always In Viewport";
  178. item.CheckType |= MenuItemCheckStyle.Checked;
  179. item.Checked = true;
  180. item.Action += () => _scrollBar.KeepContentAlwaysInViewport = item.Checked = !item.Checked;
  181. return new MenuItem [] { item };
  182. }
  183. public override void Run ()
  184. {
  185. base.Run ();
  186. }
  187. }
  188. }