Program.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. namespace StandaloneExample {
  2. using System.Linq;
  3. using Terminal.Gui;
  4. using System;
  5. using NStack;
  6. using System.Text;
  7. using Rune = System.Rune;
  8. static class Demo {
  9. class Box10x : View {
  10. public Box10x (int x, int y) : base (new Rect (x, y, 10, 10))
  11. {
  12. }
  13. public override void Redraw (Rect region)
  14. {
  15. Driver.SetAttribute (ColorScheme.Focus);
  16. for (int y = 0; y < 10; y++) {
  17. Move (0, y);
  18. for (int x = 0; x < 10; x++) {
  19. Driver.AddRune ((Rune)('0' + ((x + y) % 10)));
  20. }
  21. }
  22. }
  23. }
  24. class Filler : View {
  25. public Filler (Rect rect) : base (rect)
  26. {
  27. }
  28. public override void Redraw (Rect region)
  29. {
  30. Driver.SetAttribute (ColorScheme.Focus);
  31. var f = Frame;
  32. for (int y = 0; y < f.Width; y++) {
  33. Move (0, y);
  34. for (int x = 0; x < f.Height; x++) {
  35. var r = (x % 3) switch {
  36. 0 => '.',
  37. 1 => 'o',
  38. _ => 'O',
  39. };
  40. Driver.AddRune (r);
  41. }
  42. }
  43. }
  44. }
  45. static void ShowTextAlignments ()
  46. {
  47. var container = new Window ("Show Text Alignments - Press Esc to return") {
  48. X = 0,
  49. Y = 0,
  50. Width = Dim.Fill (),
  51. Height = Dim.Fill ()
  52. };
  53. container.KeyUp += (e) => {
  54. if (e.KeyEvent.Key == Key.Esc)
  55. container.Running = false;
  56. };
  57. const int i = 0;
  58. const string txt = "Hello world, how are you doing today?";
  59. container.Add (
  60. new Label ($"{i + 1}-{txt}") { TextAlignment = TextAlignment.Left, Y = 3, Width = Dim.Fill () },
  61. new Label ($"{i + 2}-{txt}") { TextAlignment = TextAlignment.Right, Y = 5, Width = Dim.Fill () },
  62. new Label ($"{i + 3}-{txt}") { TextAlignment = TextAlignment.Centered, Y = 7, Width = Dim.Fill () },
  63. new Label ($"{i + 4}-{txt}") { TextAlignment = TextAlignment.Justified, Y = 9, Width = Dim.Fill () }
  64. );
  65. Application.Run (container);
  66. }
  67. static void ShowEntries (View container)
  68. {
  69. scrollView = new ScrollView (new Rect (50, 10, 20, 8)) {
  70. ContentSize = new Size (100, 100),
  71. ContentOffset = new Point (-1, -1),
  72. ShowVerticalScrollIndicator = true,
  73. ShowHorizontalScrollIndicator = true
  74. };
  75. AddScrollViewChild ();
  76. // This is just to debug the visuals of the scrollview when small
  77. var scrollView2 = new ScrollView (new Rect (72, 10, 3, 3)) {
  78. ContentSize = new Size (100, 100),
  79. ShowVerticalScrollIndicator = true,
  80. ShowHorizontalScrollIndicator = true
  81. };
  82. scrollView2.Add (new Box10x (0, 0));
  83. var progress = new ProgressBar (new Rect (68, 1, 10, 1));
  84. bool timer (MainLoop _)
  85. {
  86. progress.Pulse ();
  87. return true;
  88. }
  89. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (300), timer);
  90. // A little convoluted, this is because I am using this to test the
  91. // layout based on referencing elements of another view:
  92. var login = new Label ("Login: ") { X = 3, Y = 6 };
  93. var password = new Label ("Password: ") {
  94. X = Pos.Left (login),
  95. Y = Pos.Bottom (login) + 1
  96. };
  97. var loginText = new TextField ("") {
  98. X = Pos.Right (password),
  99. Y = Pos.Top (login),
  100. Width = 40
  101. };
  102. var passText = new TextField ("") {
  103. Secret = true,
  104. X = Pos.Left (loginText),
  105. Y = Pos.Top (password),
  106. Width = Dim.Width (loginText)
  107. };
  108. // Add some content
  109. container.Add (
  110. login,
  111. loginText,
  112. password,
  113. passText,
  114. new FrameView (new Rect (3, 10, 25, 6), "Options", new View [] {
  115. new CheckBox (1, 0, "Remember me"),
  116. new RadioGroup (1, 2, new ustring [] { "_Personal", "_Company" }) }
  117. ),
  118. new ListView (new Rect (60, 6, 16, 4), new string [] {
  119. "First row",
  120. "<>",
  121. "This is a very long row that should overflow what is shown",
  122. "4th",
  123. "There is an empty slot on the second row",
  124. "Whoa",
  125. "This is so cool"
  126. }),
  127. scrollView,
  128. scrollView2,
  129. new Button ("Ok") { X = 3, Y = 19 },
  130. new Button ("Cancel") { X = 10, Y = 19 },
  131. progress,
  132. new Label ("Press F9 (on Unix ESC+9 is an alias) to activate the menubar") { X = 3, Y = 22 }
  133. );
  134. }
  135. private static void AddScrollViewChild ()
  136. {
  137. if (isBox10x) {
  138. scrollView.Add (new Box10x (0, 0));
  139. } else {
  140. scrollView.Add (new Filler (new Rect (0, 0, 40, 40)));
  141. }
  142. scrollView.ContentOffset = Point.Empty;
  143. }
  144. static void NewFile ()
  145. {
  146. var okButton = new Button ("Ok", is_default: true);
  147. okButton.Clicked += () => Application.RequestStop ();
  148. var cancelButton = new Button ("Cancel");
  149. cancelButton.Clicked += () => Application.RequestStop ();
  150. var d = new Dialog (
  151. "New File", 50, 20,
  152. okButton,
  153. cancelButton);
  154. var ml2 = new Label (1, 1, "Mouse Debug Line");
  155. d.Add (ml2);
  156. Application.Run (d);
  157. }
  158. static bool Quit ()
  159. {
  160. var n = MessageBox.Query (50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
  161. return n == 0;
  162. }
  163. static void Close ()
  164. {
  165. MessageBox.ErrorQuery (50, 7, "Error", "There is nothing to close", "Ok");
  166. }
  167. private static void ScrollViewCheck ()
  168. {
  169. isBox10x = miScrollViewCheck.Children [0].Checked = !miScrollViewCheck.Children [0].Checked;
  170. miScrollViewCheck.Children [1].Checked = !miScrollViewCheck.Children [1].Checked;
  171. scrollView.RemoveAll ();
  172. AddScrollViewChild ();
  173. }
  174. public static Label ml;
  175. private static MenuBarItem miScrollViewCheck;
  176. private static bool isBox10x = true;
  177. private static Window win;
  178. private static ScrollView scrollView;
  179. static void Main (string [] args)
  180. {
  181. if (args.Length > 0 && args.Contains ("-usc")) {
  182. Application.UseSystemConsole = true;
  183. }
  184. Console.OutputEncoding = Encoding.Default;
  185. Application.Init ();
  186. var top = Application.Top;
  187. win = new Window ("Hello") {
  188. X = 0,
  189. Y = 1,
  190. Width = Dim.Fill (),
  191. Height = Dim.Fill () - 1
  192. };
  193. var menu = new MenuBar (new MenuBarItem [] {
  194. new MenuBarItem ("_File", new MenuItem [] {
  195. new MenuItem ("_New", "Creates new file", NewFile),
  196. new MenuItem ("_Open", "", null),
  197. new MenuItem ("_Close", "", () => Close ()),
  198. new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
  199. }),
  200. new MenuBarItem ("_Edit", new MenuItem [] {
  201. new MenuItem ("_Copy", "", null),
  202. new MenuItem ("C_ut", "", null),
  203. new MenuItem ("_Paste", "", null)
  204. }),
  205. new MenuBarItem ("A_ssorted", new MenuItem [] {
  206. new MenuItem ("_Show text alignments", "", () => ShowTextAlignments (), null, null, Key.AltMask | Key.CtrlMask | Key.G)
  207. }),
  208. miScrollViewCheck = new MenuBarItem ("ScrollView", new MenuItem [] {
  209. new MenuItem ("Box10x", "", () => ScrollViewCheck()) {CheckType = MenuItemCheckStyle.Radio, Checked = true },
  210. new MenuItem ("Filler", "", () => ScrollViewCheck()) {CheckType = MenuItemCheckStyle.Radio }
  211. })
  212. });
  213. ShowEntries (win);
  214. int count = 0;
  215. ml = new Label (new Rect (3, 17, 47, 1), "Mouse: ");
  216. Application.RootMouseEvent += (me) => ml.Text = $"Mouse: ({me.X},{me.Y}) - {me.Flags} {count++}";
  217. win.Add (ml);
  218. var statusBar = new StatusBar (new StatusItem [] {
  219. new StatusItem(Key.F1, "~F1~ Help", () => MessageBox.Query (50, 7, "Help", "Helping", "Ok")),
  220. new StatusItem(Key.F2, "~F2~ Load", () => MessageBox.Query (50, 7, "Load", "Loading", "Ok")),
  221. new StatusItem(Key.F3, "~F3~ Save", () => MessageBox.Query (50, 7, "Save", "Saving", "Ok")),
  222. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => { if (Quit ()) top.Running = false; }),
  223. new StatusItem(Key.Null, Application.Driver.GetType().Name, null)
  224. });
  225. top.Add (win, menu, statusBar);
  226. Application.Run ();
  227. Application.Shutdown ();
  228. }
  229. }
  230. }