demo.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using Terminal.Gui;
  2. using System;
  3. using Mono.Terminal;
  4. static 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. // A little convoluted, this is because I am using this to test the
  81. // layout based on referencing elements of another view:
  82. var login = new Label ("Login: ") { X = 3, Y = 6 };
  83. var password = new Label ("Password: ") {
  84. X = Pos.Left (login),
  85. Y = Pos.Bottom (login) + 1
  86. };
  87. var loginText = new TextField ("") {
  88. X = Pos.Right (password),
  89. Y = Pos.Top (login),
  90. Width = 40
  91. };
  92. var passText = new TextField ("") {
  93. Secret = true,
  94. X = Pos.Left (loginText),
  95. Y = Pos.Top (password),
  96. Width = Dim.Width (loginText)
  97. };
  98. // Add some content
  99. container.Add (
  100. login,
  101. loginText,
  102. password,
  103. passText,
  104. new FrameView (new Rect (3, 10, 25, 6), "Options"){
  105. new CheckBox (1, 0, "Remember me"),
  106. new RadioGroup (1, 2, new [] { "_Personal", "_Company" }),
  107. },
  108. new ListView (new Rect (60, 6, 16, 4), new string [] {
  109. "First row",
  110. "<>",
  111. "This is a very long row that should overflow what is shown",
  112. "4th",
  113. "There is an empty slot on the second row",
  114. "Whoa",
  115. "This is so cool"
  116. }),
  117. scrollView,
  118. //scrollView2,
  119. new Button (3, 19, "Ok"),
  120. new Button (10, 19, "Cancel"),
  121. new TimeField (3, 20, DateTime.Now),
  122. new TimeField (23, 20, DateTime.Now, true),
  123. progress,
  124. new Label (3, 22, "Press F9 (on Unix, ESC+9 is an alias) to activate the menubar")
  125. );
  126. }
  127. public static Label ml2;
  128. static void NewFile ()
  129. {
  130. var d = new Dialog (
  131. "New File", 50, 20,
  132. new Button ("Ok", is_default: true) { Clicked = () => { Application.RequestStop (); } },
  133. new Button ("Cancel") { Clicked = () => { Application.RequestStop (); } });
  134. ml2 = new Label (1, 1, "Mouse Debug Line");
  135. d.Add (ml2);
  136. Application.Run (d);
  137. }
  138. //
  139. // Creates a nested editor
  140. static void Editor(Toplevel top) {
  141. var tframe = top.Frame;
  142. var ntop = new Toplevel(tframe);
  143. var menu = new MenuBar(new MenuBarItem[] {
  144. new MenuBarItem ("_File", new MenuItem [] {
  145. new MenuItem ("_Close", "", () => {Application.RequestStop ();}),
  146. }),
  147. new MenuBarItem ("_Edit", new MenuItem [] {
  148. new MenuItem ("_Copy", "", null),
  149. new MenuItem ("C_ut", "", null),
  150. new MenuItem ("_Paste", "", null)
  151. }),
  152. });
  153. ntop.Add(menu);
  154. string fname = null;
  155. foreach (var s in new[] { "/etc/passwd", "c:\\windows\\win.ini" })
  156. if (System.IO.File.Exists(s)) {
  157. fname = s;
  158. break;
  159. }
  160. var win = new Window(fname ?? "Untitled") {
  161. X = 0,
  162. Y = 1,
  163. Width = Dim.Fill(),
  164. Height = Dim.Fill()
  165. };
  166. ntop.Add(win);
  167. var text = new TextView(new Rect(0, 0, tframe.Width - 2, tframe.Height - 3));
  168. if (fname != null)
  169. text.Text = System.IO.File.ReadAllText (fname);
  170. win.Add (text);
  171. Application.Run (ntop);
  172. }
  173. static bool Quit ()
  174. {
  175. var n = MessageBox.Query (50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
  176. return n == 0;
  177. }
  178. static void Close ()
  179. {
  180. MessageBox.ErrorQuery (50, 5, "Error", "There is nothing to close", "Ok");
  181. }
  182. // Watch what happens when I try to introduce a newline after the first open brace
  183. // it introduces a new brace instead, and does not indent. Then watch me fight
  184. // the editor as more oddities happen.
  185. public static void Open ()
  186. {
  187. var d = new OpenDialog ("Open", "Open a file");
  188. Application.Run (d);
  189. MessageBox.Query (50, 7, "Selected File", string.Join (", ", d.FilePaths), "ok");
  190. }
  191. public static void ShowHex (Toplevel top)
  192. {
  193. var tframe = top.Frame;
  194. var ntop = new Toplevel (tframe);
  195. var menu = new MenuBar (new MenuBarItem [] {
  196. new MenuBarItem ("_File", new MenuItem [] {
  197. new MenuItem ("_Close", "", () => {Application.RequestStop ();}),
  198. }),
  199. });
  200. ntop.Add (menu);
  201. var win = new Window ("/etc/passwd") {
  202. X = 0,
  203. Y = 1,
  204. Width = Dim.Fill (),
  205. Height = Dim.Fill ()
  206. };
  207. ntop.Add (win);
  208. var source = System.IO.File.OpenRead ("/etc/passwd");
  209. var hex = new HexView (source) {
  210. X = 0,
  211. Y = 0,
  212. Width = Dim.Fill (),
  213. Height = Dim.Fill ()
  214. };
  215. win.Add (hex);
  216. Application.Run (ntop);
  217. }
  218. public static Label ml;
  219. static void Main ()
  220. {
  221. //Application.UseSystemConsole = true;
  222. Application.Init ();
  223. var top = Application.Top;
  224. var tframe = top.Frame;
  225. //Open ();
  226. #if true
  227. var win = new Window ("Hello") {
  228. X = 0,
  229. Y = 1,
  230. Width = Dim.Fill (),
  231. Height = Dim.Fill ()
  232. };
  233. #else
  234. var win = new Window (new Rect (0, 1, tframe.Width, tframe.Height - 1), "Hello");
  235. #endif
  236. var menu = new MenuBar (new MenuBarItem [] {
  237. new MenuBarItem ("_File", new MenuItem [] {
  238. new MenuItem ("Text Editor Demo", "", () => { Editor (top); }),
  239. new MenuItem ("_New", "Creates new file", NewFile),
  240. new MenuItem ("_Open", "", Open),
  241. new MenuItem ("_Hex", "", () => ShowHex (top)),
  242. new MenuItem ("_Close", "", () => Close ()),
  243. new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
  244. }),
  245. new MenuBarItem ("_Edit", new MenuItem [] {
  246. new MenuItem ("_Copy", "", null),
  247. new MenuItem ("C_ut", "", null),
  248. new MenuItem ("_Paste", "", null)
  249. }),
  250. });
  251. ShowEntries (win);
  252. int count = 0;
  253. ml = new Label (new Rect (3, 17, 47, 1), "Mouse: ");
  254. Application.RootMouseEvent += delegate (MouseEvent me) {
  255. ml.Text = $"Mouse: ({me.X},{me.Y}) - {me.Flags} {count++}";
  256. };
  257. var test = new Label (3, 18, "Se iniciará el análisis");
  258. win.Add (test);
  259. win.Add (ml);
  260. // ShowTextAlignments (win);
  261. top.Add (win);
  262. top.Add (menu);
  263. Application.Run ();
  264. }
  265. }