demo.cs 4.4 KB

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