demo.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 (5, -2)
  60. };
  61. //scrollView.Add (new Box10x (0, 0));
  62. scrollView.Add (new Filler (new Rect (0, 0, 40, 40)));
  63. container.Add (
  64. new Label (3, 6, "Login: "),
  65. new TextField (14, 6, 40, ""),
  66. new Label (3, 8, "Password: "),
  67. new TextField (14, 8, 40, "") { Secret = true },
  68. new FrameView (new Rect (3, 10, 25, 6), "Options"){
  69. new CheckBox (1, 0, "Remember me"),
  70. new RadioGroup (1, 2, new [] { "_Personal", "_Company" }),
  71. },
  72. scrollView,
  73. new Button (3, 19, "Ok"),
  74. new Button (10, 19, "Cancel"),
  75. new Label (3, 22, "Press ESC and 9 to activate the menubar")
  76. );
  77. }
  78. public static Label ml2;
  79. static void NewFile ()
  80. {
  81. var d = new Dialog (
  82. "New File", 50, 20,
  83. new Button ("Ok", is_default: true) { Clicked = () => { Application.RequestStop (); } },
  84. new Button ("Cancel") { Clicked = () => { Application.RequestStop (); } });
  85. ml2 = new Label (1, 1, "Mouse Debug Line");
  86. d.Add (ml2);
  87. Application.Run (d);
  88. }
  89. static bool Quit ()
  90. {
  91. var n = MessageBox.Query (50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
  92. return n == 0;
  93. }
  94. static void Close ()
  95. {
  96. MessageBox.ErrorQuery (50, 5, "Error", "There is nothing to close", "Ok");
  97. }
  98. public static Label ml;
  99. static void Main ()
  100. {
  101. Application.Init ();
  102. var top = Application.Top;
  103. var tframe = top.Frame;
  104. var win = new Window (new Rect (0, 1, tframe.Width, tframe.Height-1), "Hello");
  105. var menu = new MenuBar (new MenuBarItem [] {
  106. new MenuBarItem ("_File", new MenuItem [] {
  107. new MenuItem ("_New", "Creates new file", NewFile),
  108. new MenuItem ("_Open", "", null),
  109. new MenuItem ("_Close", "", () => Close ()),
  110. new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
  111. }),
  112. new MenuBarItem ("_Edit", new MenuItem [] {
  113. new MenuItem ("_Copy", "", null),
  114. new MenuItem ("C_ut", "", null),
  115. new MenuItem ("_Paste", "", null)
  116. })
  117. });
  118. ShowEntries (win);
  119. int count = 0;
  120. ml = new Label (new Rect (3, 17, 47, 1), "Mouse: ");
  121. Application.RootMouseEvent += delegate (MouseEvent me) {
  122. ml.Text = $"Mouse: ({me.X},{me.Y}) - {me.Flags} {count++}";
  123. };
  124. win.Add (ml);
  125. // ShowTextAlignments (win);
  126. top.Add (win);
  127. top.Add (menu);
  128. Application.Run ();
  129. }
  130. }