Program.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using Terminal.Gui;
  2. using System;
  3. using Mono.Terminal;
  4. class Demo {
  5. class Box10x : View {
  6. public Box10x (int x, int y) : base (new Rect (x, y, 10, 10))
  7. {
  8. }
  9. public override void Redraw (Rect region)
  10. {
  11. Driver.SetAttribute (ColorScheme.Focus);
  12. for (int y = 0; y < 10; y++) {
  13. Move (0, y);
  14. for (int x = 0; x < 10; x++) {
  15. Driver.AddRune ((Rune)('0' + (x + y) % 10));
  16. }
  17. }
  18. }
  19. }
  20. class Filler : View {
  21. public Filler (Rect rect) : base (rect)
  22. {
  23. }
  24. public override void Redraw (Rect region)
  25. {
  26. Driver.SetAttribute (ColorScheme.Focus);
  27. var f = Frame;
  28. for (int y = 0; y < f.Width; y++) {
  29. Move (0, y);
  30. for (int x = 0; x < f.Height; x++) {
  31. Rune r;
  32. switch (x % 3) {
  33. case 0:
  34. r = '.';
  35. break;
  36. case 1:
  37. r = 'o';
  38. break;
  39. default:
  40. r = 'O';
  41. break;
  42. }
  43. Driver.AddRune (r);
  44. }
  45. }
  46. }
  47. }
  48. static void ShowTextAlignments (View container)
  49. {
  50. container.Add (
  51. new Label (new Rect (0, 0, 40, 3), "1-Hello world, how are you doing today") { TextAlignment = TextAlignment.Left },
  52. new Label (new Rect (0, 4, 40, 3), "2-Hello world, how are you doing today") { TextAlignment = TextAlignment.Right },
  53. new Label (new Rect (0, 8, 40, 3), "3-Hello world, how are you doing today") { TextAlignment = TextAlignment.Centered },
  54. new Label (new Rect (0, 12, 40, 3), "4-Hello world, how are you doing today") { TextAlignment = TextAlignment.Justified });
  55. }
  56. static void ShowEntries (View container)
  57. {
  58. var scrollView = new ScrollView (new Rect (50, 10, 20, 8)) {
  59. ContentSize = new Size (100, 100),
  60. ContentOffset = new Point (-1, -1),
  61. ShowVerticalScrollIndicator = true,
  62. ShowHorizontalScrollIndicator = true
  63. };
  64. scrollView.Add (new Box10x (0, 0));
  65. //scrollView.Add (new Filler (new Rect (0, 0, 40, 40)));
  66. // This is just to debug the visuals of the scrollview when small
  67. var scrollView2 = new ScrollView (new Rect (72, 10, 3, 3)) {
  68. ContentSize = new Size (100, 100),
  69. ShowVerticalScrollIndicator = true,
  70. ShowHorizontalScrollIndicator = true
  71. };
  72. scrollView2.Add (new Box10x (0, 0));
  73. var progress = new ProgressBar (new Rect (68, 1, 10, 1));
  74. bool timer (MainLoop caller)
  75. {
  76. progress.Pulse ();
  77. return true;
  78. }
  79. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (300), timer);
  80. // Add some content
  81. container.Add (
  82. //new Label (3, 6, "Login: "),
  83. //new TextField (14, 6, 40, ""),
  84. new Label ("Login: ") { X = 3, Y = 6 },
  85. new TextField ("") { X = 14, Y = 6, Width = 40 },
  86. new Label ("Password: ") { X = 3, Y = 8 },
  87. new TextField ("") { X = 14, Y = 18, Width = 40, Secret = true },
  88. new FrameView (new Rect (3, 10, 25, 6), "Options"){
  89. new CheckBox (1, 0, "Remember me"),
  90. new RadioGroup (1, 2, new [] { "_Personal", "_Company" }),
  91. },
  92. new ListView (new Rect (60, 6, 16, 4), new string [] {
  93. "First row",
  94. "<>",
  95. "This is a very long row that should overflow what is shown",
  96. "4th",
  97. "There is an empty slot on the second row",
  98. "Whoa",
  99. "This is so cool"
  100. }),
  101. scrollView,
  102. //scrollView2,
  103. new Button ("Ok") { X = 3, Y = 19 },
  104. new Button ("Cancel") { X = 10, Y = 19 },
  105. progress,
  106. new Label ("Press ESC and 9 to activate the menubar") { X = 3, Y = 22 }
  107. );
  108. }
  109. public static Label ml2;
  110. static void NewFile ()
  111. {
  112. var d = new Dialog (
  113. "New File", 50, 20,
  114. new Button ("Ok", is_default: true) { Clicked = () => { Application.RequestStop (); } },
  115. new Button ("Cancel") { Clicked = () => { Application.RequestStop (); } });
  116. ml2 = new Label (1, 1, "Mouse Debug Line");
  117. d.Add (ml2);
  118. Application.Run (d);
  119. }
  120. static bool Quit ()
  121. {
  122. var n = MessageBox.Query (50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
  123. return n == 0;
  124. }
  125. static void Close ()
  126. {
  127. MessageBox.ErrorQuery (50, 5, "Error", "There is nothing to close", "Ok");
  128. }
  129. public static Label ml;
  130. static void Main ()
  131. {
  132. //Application.UseSystemConsole = true;
  133. Application.Init ();
  134. var top = Application.Top;
  135. var tframe = top.Frame;
  136. var win = new Window ("Hello"){
  137. X = 0,
  138. Y = 1,
  139. Width = Dim.Fill (),
  140. Height = Dim.Fill () - 1
  141. };
  142. var menu = new MenuBar (new MenuBarItem [] {
  143. new MenuBarItem ("_File", new MenuItem [] {
  144. new MenuItem ("_New", "Creates new file", NewFile),
  145. new MenuItem ("_Open", "", null),
  146. new MenuItem ("_Close", "", () => Close ()),
  147. new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
  148. }),
  149. new MenuBarItem ("_Edit", new MenuItem [] {
  150. new MenuItem ("_Copy", "", null),
  151. new MenuItem ("C_ut", "", null),
  152. new MenuItem ("_Paste", "", null)
  153. })
  154. });
  155. ShowEntries (win);
  156. int count = 0;
  157. ml = new Label (new Rect (3, 17, 47, 1), "Mouse: ");
  158. Application.RootMouseEvent += delegate (MouseEvent me) {
  159. ml.Text = $"Mouse: ({me.X},{me.Y}) - {me.Flags} {count++}";
  160. };
  161. win.Add (ml);
  162. top.Add (win, menu);
  163. top.Add (menu);
  164. Application.Run ();
  165. }
  166. }