demo.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using Terminal.Gui;
  2. using System;
  3. using Mono.Terminal;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. static class Demo {
  7. class Box10x : View {
  8. public Box10x (int x, int y) : base (new Rect (x, y, 10, 10))
  9. {
  10. }
  11. public override void Redraw (Rect region)
  12. {
  13. Driver.SetAttribute (ColorScheme.Focus);
  14. for (int y = 0; y < 10; y++) {
  15. Move (0, y);
  16. for (int x = 0; x < 10; x++) {
  17. Driver.AddRune ((Rune)('0' + (x + y) % 10));
  18. }
  19. }
  20. }
  21. }
  22. class Filler : View {
  23. public Filler (Rect rect) : base (rect)
  24. {
  25. }
  26. public override void Redraw (Rect region)
  27. {
  28. Driver.SetAttribute (ColorScheme.Focus);
  29. var f = Frame;
  30. for (int y = 0; y < f.Width; y++) {
  31. Move (0, y);
  32. for (int x = 0; x < f.Height; x++) {
  33. Rune r;
  34. switch (x % 3) {
  35. case 0:
  36. r = '.';
  37. break;
  38. case 1:
  39. r = 'o';
  40. break;
  41. default:
  42. r = 'O';
  43. break;
  44. }
  45. Driver.AddRune (r);
  46. }
  47. }
  48. }
  49. }
  50. static void ShowTextAlignments (View container)
  51. {
  52. container.Add (
  53. new Label (new Rect (0, 0, 40, 3), "1-Hello world, how are you doing today") { TextAlignment = TextAlignment.Left },
  54. new Label (new Rect (0, 4, 40, 3), "2-Hello world, how are you doing today") { TextAlignment = TextAlignment.Right },
  55. new Label (new Rect (0, 8, 40, 3), "3-Hello world, how are you doing today") { TextAlignment = TextAlignment.Centered },
  56. new Label (new Rect (0, 12, 40, 3), "4-Hello world, how are you doing today") { TextAlignment = TextAlignment.Justified });
  57. }
  58. static void ShowEntries (View container)
  59. {
  60. var scrollView = new ScrollView (new Rect (50, 10, 20, 8)) {
  61. ContentSize = new Size (100, 100),
  62. ContentOffset = new Point (-1, -1),
  63. ShowVerticalScrollIndicator = true,
  64. ShowHorizontalScrollIndicator = true
  65. };
  66. scrollView.Add (new Box10x (0, 0));
  67. //scrollView.Add (new Filler (new Rect (0, 0, 40, 40)));
  68. // This is just to debug the visuals of the scrollview when small
  69. var scrollView2 = new ScrollView (new Rect (72, 10, 3, 3)) {
  70. ContentSize = new Size (100, 100),
  71. ShowVerticalScrollIndicator = true,
  72. ShowHorizontalScrollIndicator = true
  73. };
  74. scrollView2.Add (new Box10x (0, 0));
  75. var progress = new ProgressBar (new Rect (68, 1, 10, 1));
  76. bool timer (MainLoop caller)
  77. {
  78. progress.Pulse ();
  79. return true;
  80. }
  81. //Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (300), timer);
  82. // A little convoluted, this is because I am using this to test the
  83. // layout based on referencing elements of another view:
  84. var login = new Label ("Login: ") { X = 3, Y = 6 };
  85. var password = new Label ("Password: ") {
  86. X = Pos.Left (login),
  87. Y = Pos.Bottom (login) + 1
  88. };
  89. var loginText = new TextField ("") {
  90. X = Pos.Right (password),
  91. Y = Pos.Top (login),
  92. Width = 40
  93. };
  94. var passText = new TextField ("") {
  95. Secret = true,
  96. X = Pos.Left (loginText),
  97. Y = Pos.Top (password),
  98. Width = Dim.Width (loginText)
  99. };
  100. // Add some content
  101. container.Add (
  102. login,
  103. loginText,
  104. password,
  105. passText,
  106. new FrameView (new Rect (3, 10, 25, 6), "Options"){
  107. new CheckBox (1, 0, "Remember me"),
  108. new RadioGroup (1, 2, new [] { "_Personal", "_Company" }),
  109. },
  110. new ListView (new Rect (60, 6, 16, 4), new string [] {
  111. "First row",
  112. "<>",
  113. "This is a very long row that should overflow what is shown",
  114. "4th",
  115. "There is an empty slot on the second row",
  116. "Whoa",
  117. "This is so cool"
  118. }),
  119. scrollView,
  120. //scrollView2,
  121. new Button (3, 19, "Ok"),
  122. new Button (10, 19, "Cancel"),
  123. new TimeField (3, 20, DateTime.Now),
  124. new TimeField (23, 20, DateTime.Now, true),
  125. progress,
  126. new Label (3, 22, "Press F9 (on Unix, ESC+9 is an alias) to activate the menubar")
  127. );
  128. }
  129. public static Label ml2;
  130. static void NewFile ()
  131. {
  132. var d = new Dialog (
  133. "New File", 50, 20,
  134. new Button ("Ok", is_default: true) { Clicked = () => { Application.RequestStop (); } },
  135. new Button ("Cancel") { Clicked = () => { Application.RequestStop (); } });
  136. ml2 = new Label (1, 1, "Mouse Debug Line");
  137. d.Add (ml2);
  138. Application.Run (d);
  139. }
  140. //
  141. // Creates a nested editor
  142. static void Editor(Toplevel top) {
  143. var tframe = top.Frame;
  144. var ntop = new Toplevel(tframe);
  145. var menu = new MenuBar(new MenuBarItem[] {
  146. new MenuBarItem ("_File", new MenuItem [] {
  147. new MenuItem ("_Close", "", () => {Application.RequestStop ();}),
  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. ntop.Add(menu);
  156. string fname = null;
  157. foreach (var s in new[] { "/etc/passwd", "c:\\windows\\win.ini" })
  158. if (System.IO.File.Exists(s)) {
  159. fname = s;
  160. break;
  161. }
  162. var win = new Window(fname ?? "Untitled") {
  163. X = 0,
  164. Y = 1,
  165. Width = Dim.Fill(),
  166. Height = Dim.Fill()
  167. };
  168. ntop.Add(win);
  169. var text = new TextView(new Rect(0, 0, tframe.Width - 2, tframe.Height - 3));
  170. if (fname != null)
  171. text.Text = System.IO.File.ReadAllText (fname);
  172. win.Add (text);
  173. Application.Run (ntop);
  174. }
  175. static bool Quit ()
  176. {
  177. var n = MessageBox.Query (50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
  178. return n == 0;
  179. }
  180. static void Close ()
  181. {
  182. MessageBox.ErrorQuery (50, 5, "Error", "There is nothing to close", "Ok");
  183. }
  184. // Watch what happens when I try to introduce a newline after the first open brace
  185. // it introduces a new brace instead, and does not indent. Then watch me fight
  186. // the editor as more oddities happen.
  187. public static void Open ()
  188. {
  189. var d = new OpenDialog ("Open", "Open a file");
  190. Application.Run (d);
  191. MessageBox.Query (50, 7, "Selected File", string.Join (", ", d.FilePaths), "ok");
  192. }
  193. public static void ShowHex (Toplevel top)
  194. {
  195. var tframe = top.Frame;
  196. var ntop = new Toplevel (tframe);
  197. var menu = new MenuBar (new MenuBarItem [] {
  198. new MenuBarItem ("_File", new MenuItem [] {
  199. new MenuItem ("_Close", "", () => {Application.RequestStop ();}),
  200. }),
  201. });
  202. ntop.Add (menu);
  203. var win = new Window ("/etc/passwd") {
  204. X = 0,
  205. Y = 1,
  206. Width = Dim.Fill (),
  207. Height = Dim.Fill ()
  208. };
  209. ntop.Add (win);
  210. var source = System.IO.File.OpenRead ("/etc/passwd");
  211. var hex = new HexView (source) {
  212. X = 0,
  213. Y = 0,
  214. Width = Dim.Fill (),
  215. Height = Dim.Fill ()
  216. };
  217. win.Add (hex);
  218. Application.Run (ntop);
  219. }
  220. #region Selection Demo
  221. static void ListSelectionDemo ()
  222. {
  223. var d = new Dialog ("Selection Demo", 60, 20,
  224. new Button ("Ok", is_default: true) { Clicked = () => { Application.RequestStop (); } },
  225. new Button ("Cancel") { Clicked = () => { Application.RequestStop (); } });
  226. var animals = new List<string> () { "Alpaca", "Llama", "Lion", "Shark", "Goat" };
  227. var msg = new Label ("Use space bar or control-t to toggle selection") {
  228. X = 1,
  229. Y = 1,
  230. Width = Dim.Fill () - 1,
  231. Height = 1
  232. };
  233. var list = new ListView (animals) {
  234. X = 1,
  235. Y = 3,
  236. Width = Dim.Fill () - 4,
  237. Height = Dim.Fill () - 4,
  238. AllowsMarking = true
  239. };
  240. d.Add (msg, list);
  241. Application.Run (d);
  242. var result = "";
  243. for (int i = 0; i < animals.Count; i++) {
  244. if (list.Source.IsMarked (i)) {
  245. result += animals [i] + " ";
  246. }
  247. }
  248. MessageBox.Query (60, 10, "Selected Animals", result == "" ? "No animals selected" : result, "Ok");
  249. }
  250. #endregion
  251. public static Label ml;
  252. static void Main ()
  253. {
  254. //Application.UseSystemConsole = true;
  255. Application.Init ();
  256. var top = Application.Top;
  257. var tframe = top.Frame;
  258. //Open ();
  259. #if true
  260. var win = new Window ("Hello") {
  261. X = 0,
  262. Y = 1,
  263. Width = Dim.Fill (),
  264. Height = Dim.Fill ()
  265. };
  266. #else
  267. var win = new Window (new Rect (0, 1, tframe.Width, tframe.Height - 1), "Hello");
  268. #endif
  269. var menu = new MenuBar (new MenuBarItem [] {
  270. new MenuBarItem ("_File", new MenuItem [] {
  271. new MenuItem ("Text Editor Demo", "", () => { Editor (top); }),
  272. new MenuItem ("_New", "Creates new file", NewFile),
  273. new MenuItem ("_Open", "", Open),
  274. new MenuItem ("_Hex", "", () => ShowHex (top)),
  275. new MenuItem ("_Close", "", () => Close ()),
  276. new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
  277. }),
  278. new MenuBarItem ("_Edit", new MenuItem [] {
  279. new MenuItem ("_Copy", "", null),
  280. new MenuItem ("C_ut", "", null),
  281. new MenuItem ("_Paste", "", null)
  282. }),
  283. new MenuBarItem ("_List Demos", new MenuItem [] {
  284. new MenuItem ("Select Items", "", ListSelectionDemo),
  285. }),
  286. });
  287. ShowEntries (win);
  288. int count = 0;
  289. ml = new Label (new Rect (3, 17, 47, 1), "Mouse: ");
  290. Application.RootMouseEvent += delegate (MouseEvent me) {
  291. ml.Text = $"Mouse: ({me.X},{me.Y}) - {me.Flags} {count++}";
  292. };
  293. var test = new Label (3, 18, "Se iniciará el análisis");
  294. win.Add (test);
  295. win.Add (ml);
  296. // ShowTextAlignments (win);
  297. top.Add (win);
  298. top.Add (menu);
  299. Application.Run ();
  300. }
  301. }