demo.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. using Terminal.Gui;
  2. using System;
  3. using Mono.Terminal;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. using System.Reflection;
  8. using NStack;
  9. static class Demo {
  10. //class Box10x : View, IScrollView {
  11. class Box10x : View {
  12. int w = 40;
  13. int h = 50;
  14. public bool WantCursorPosition { get; set; } = false;
  15. public Box10x (int x, int y) : base (new Rect (x, y, 20, 10))
  16. {
  17. }
  18. public Size GetContentSize ()
  19. {
  20. return new Size (w, h);
  21. }
  22. public void SetCursorPosition (Point pos)
  23. {
  24. throw new NotImplementedException ();
  25. }
  26. public override void Redraw (Rect region)
  27. {
  28. //Point pos = new Point (region.X, region.Y);
  29. Driver.SetAttribute (ColorScheme.Focus);
  30. for (int y = 0; y < h; y++) {
  31. Move (0, y);
  32. Driver.AddStr (y.ToString ());
  33. for (int x = 0; x < w - y.ToString ().Length; x++) {
  34. //Driver.AddRune ((Rune)('0' + (x + y) % 10));
  35. if (y.ToString ().Length < w)
  36. Driver.AddStr (" ");
  37. }
  38. }
  39. //Move (pos.X, pos.Y);
  40. }
  41. }
  42. class Filler : View {
  43. public Filler (Rect rect) : base (rect)
  44. {
  45. }
  46. public override void Redraw (Rect region)
  47. {
  48. Driver.SetAttribute (ColorScheme.Focus);
  49. var f = Frame;
  50. for (int y = 0; y < f.Width; y++) {
  51. Move (0, y);
  52. for (int x = 0; x < f.Height; x++) {
  53. Rune r;
  54. switch (x % 3) {
  55. case 0:
  56. Driver.AddRune (y.ToString ().ToCharArray (0, 1) [0]);
  57. if (y > 9)
  58. Driver.AddRune (y.ToString ().ToCharArray (1, 1) [0]);
  59. r = '.';
  60. break;
  61. case 1:
  62. r = 'o';
  63. break;
  64. default:
  65. r = 'O';
  66. break;
  67. }
  68. Driver.AddRune (r);
  69. }
  70. }
  71. }
  72. }
  73. static void ShowTextAlignments ()
  74. {
  75. var container = new Dialog (
  76. "Text Alignments", 50, 20,
  77. new Button ("Ok", is_default: true) { Clicked = () => { Application.RequestStop (); } },
  78. new Button ("Cancel") { Clicked = () => { Application.RequestStop (); } });
  79. int i = 0;
  80. string txt = "Hello world, how are you doing today";
  81. container.Add (
  82. new Label (new Rect (0, 1, 40, 3), $"{i+1}-{txt}") { TextAlignment = TextAlignment.Left },
  83. new Label (new Rect (0, 3, 40, 3), $"{i+2}-{txt}") { TextAlignment = TextAlignment.Right },
  84. new Label (new Rect (0, 5, 40, 3), $"{i+3}-{txt}") { TextAlignment = TextAlignment.Centered },
  85. new Label (new Rect (0, 7, 40, 3), $"{i+4}-{txt}") { TextAlignment = TextAlignment.Justified }
  86. );
  87. Application.Run (container);
  88. }
  89. static void ShowEntries (View container)
  90. {
  91. var scrollView = new ScrollView (new Rect (50, 10, 20, 8)) {
  92. ContentSize = new Size (20, 50),
  93. //ContentOffset = new Point (0, 0),
  94. ShowVerticalScrollIndicator = true,
  95. ShowHorizontalScrollIndicator = true
  96. };
  97. #if false
  98. scrollView.Add (new Box10x (0, 0));
  99. #else
  100. scrollView.Add (new Filler (new Rect (0, 0, 40, 40)));
  101. #endif
  102. // This is just to debug the visuals of the scrollview when small
  103. var scrollView2 = new ScrollView (new Rect (72, 10, 3, 3)) {
  104. ContentSize = new Size (100, 100),
  105. ShowVerticalScrollIndicator = true,
  106. ShowHorizontalScrollIndicator = true
  107. };
  108. scrollView2.Add (new Box10x (0, 0));
  109. var progress = new ProgressBar (new Rect (68, 1, 10, 1));
  110. bool timer (MainLoop caller)
  111. {
  112. progress.Pulse ();
  113. return true;
  114. }
  115. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (300), timer);
  116. // A little convoluted, this is because I am using this to test the
  117. // layout based on referencing elements of another view:
  118. var login = new Label ("Login: ") { X = 3, Y = 6 };
  119. var password = new Label ("Password: ") {
  120. X = Pos.Left (login),
  121. Y = Pos.Bottom (login) + 1
  122. };
  123. var loginText = new TextField ("") {
  124. X = Pos.Right (password),
  125. Y = Pos.Top (login),
  126. Width = 40
  127. };
  128. var passText = new TextField ("") {
  129. Secret = true,
  130. X = Pos.Left (loginText),
  131. Y = Pos.Top (password),
  132. Width = Dim.Width (loginText)
  133. };
  134. var tf = new Button (3, 19, "Ok");
  135. // Add some content
  136. container.Add (
  137. login,
  138. loginText,
  139. password,
  140. passText,
  141. new FrameView (new Rect (3, 10, 25, 6), "Options"){
  142. new CheckBox (1, 0, "Remember me"),
  143. new RadioGroup (1, 2, new [] { "_Personal", "_Company" }),
  144. },
  145. new ListView (new Rect (59, 6, 16, 4), new string [] {
  146. "First row",
  147. "<>",
  148. "This is a very long row that should overflow what is shown",
  149. "4th",
  150. "There is an empty slot on the second row",
  151. "Whoa",
  152. "This is so cool"
  153. }),
  154. scrollView,
  155. scrollView2,
  156. tf,
  157. new Button (10, 19, "Cancel"),
  158. new TimeField (3, 20, DateTime.Now),
  159. new TimeField (23, 20, DateTime.Now, true),
  160. new DateField (3, 22, DateTime.Now),
  161. new DateField (23, 22, DateTime.Now, true),
  162. progress,
  163. new Label (3, 24, "Press F9 (on Unix, ESC+9 is an alias) to activate the menubar"),
  164. menuKeysStyle,
  165. menuAutoMouseNav
  166. );
  167. container.SendSubviewToBack (tf);
  168. }
  169. public static Label ml2;
  170. static void NewFile ()
  171. {
  172. var d = new Dialog (
  173. "New File", 50, 20,
  174. new Button ("Ok", is_default: true) { Clicked = () => { Application.RequestStop (); } },
  175. new Button ("Cancel") { Clicked = () => { Application.RequestStop (); } });
  176. ml2 = new Label (1, 1, "Mouse Debug Line");
  177. d.Add (ml2);
  178. Application.Run (d);
  179. }
  180. //
  181. // Creates a nested editor
  182. static void Editor (Toplevel top)
  183. {
  184. var tframe = top.Frame;
  185. var ntop = new Toplevel (tframe);
  186. var menu = new MenuBar (new MenuBarItem [] {
  187. new MenuBarItem ("_File", new MenuItem [] {
  188. new MenuItem ("_Close", "", () => {Application.RequestStop ();}),
  189. }),
  190. new MenuBarItem ("_Edit", new MenuItem [] {
  191. new MenuItem ("_Copy", "", null),
  192. new MenuItem ("C_ut", "", null),
  193. new MenuItem ("_Paste", "", null)
  194. }),
  195. });
  196. ntop.Add (menu);
  197. string fname = null;
  198. foreach (var s in new [] { "/etc/passwd", "c:\\windows\\win.ini" })
  199. if (System.IO.File.Exists (s)) {
  200. fname = s;
  201. break;
  202. }
  203. var win = new Window (fname ?? "Untitled") {
  204. X = 0,
  205. Y = 1,
  206. Width = Dim.Fill (),
  207. Height = Dim.Fill ()
  208. };
  209. ntop.Add (win);
  210. var text = new TextView (new Rect (0, 0, tframe.Width - 2, tframe.Height - 3));
  211. if (fname != null)
  212. text.Text = System.IO.File.ReadAllText (fname);
  213. win.Add (text);
  214. Application.Run (ntop);
  215. }
  216. static bool Quit ()
  217. {
  218. var n = MessageBox.Query (50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
  219. return n == 0;
  220. }
  221. static void Close ()
  222. {
  223. MessageBox.ErrorQuery (50, 7, "Error", "There is nothing to close", "Ok");
  224. }
  225. // Watch what happens when I try to introduce a newline after the first open brace
  226. // it introduces a new brace instead, and does not indent. Then watch me fight
  227. // the editor as more oddities happen.
  228. public static void Open ()
  229. {
  230. var d = new OpenDialog ("Open", "Open a file") { AllowsMultipleSelection = true };
  231. Application.Run (d);
  232. if (!d.Canceled)
  233. MessageBox.Query (50, 7, "Selected File", string.Join (", ", d.FilePaths), "Ok");
  234. }
  235. public static void ShowHex (Toplevel top)
  236. {
  237. var tframe = top.Frame;
  238. var ntop = new Toplevel (tframe);
  239. var menu = new MenuBar (new MenuBarItem [] {
  240. new MenuBarItem ("_File", new MenuItem [] {
  241. new MenuItem ("_Close", "", () => {Application.RequestStop ();}),
  242. }),
  243. });
  244. ntop.Add (menu);
  245. var win = new Window ("/etc/passwd") {
  246. X = 0,
  247. Y = 1,
  248. Width = Dim.Fill (),
  249. Height = Dim.Fill ()
  250. };
  251. ntop.Add (win);
  252. var source = System.IO.File.OpenRead ("/etc/passwd");
  253. var hex = new HexView (source) {
  254. X = 0,
  255. Y = 0,
  256. Width = Dim.Fill (),
  257. Height = Dim.Fill ()
  258. };
  259. win.Add (hex);
  260. Application.Run (ntop);
  261. }
  262. public class MenuItemDetails : MenuItem {
  263. ustring title;
  264. string help;
  265. Action action;
  266. public MenuItemDetails (ustring title, string help, Action action) : base (title, help, action)
  267. {
  268. this.title = title;
  269. this.help = help;
  270. this.action = action;
  271. }
  272. public static MenuItemDetails Instance (MenuItem mi)
  273. {
  274. return (MenuItemDetails)mi.GetMenuItem ();
  275. }
  276. }
  277. public delegate MenuItem MenuItemDelegate (MenuItemDetails menuItem);
  278. public static void ShowMenuItem (MenuItem mi)
  279. {
  280. BindingFlags flags = BindingFlags.Public | BindingFlags.Static;
  281. MethodInfo minfo = typeof (MenuItemDetails).GetMethod ("Instance", flags);
  282. MenuItemDelegate mid = (MenuItemDelegate)Delegate.CreateDelegate (typeof (MenuItemDelegate), minfo);
  283. MessageBox.Query (70, 7, mi.Title.ToString (),
  284. $"{mi.Title.ToString ()} selected. Is from submenu: {mi.GetMenuBarItem ()}", "Ok");
  285. }
  286. static void MenuKeysStyle_Toggled (object sender, EventArgs e)
  287. {
  288. menu.UseKeysUpDownAsKeysLeftRight = menuKeysStyle.Checked;
  289. }
  290. static void MenuAutoMouseNav_Toggled (object sender, EventArgs e)
  291. {
  292. menu.WantMousePositionReports = menuAutoMouseNav.Checked;
  293. }
  294. static void Copy ()
  295. {
  296. TextField textField = menu.LastFocused as TextField;
  297. if (textField != null && textField.SelectedLength != 0) {
  298. textField.Copy ();
  299. }
  300. }
  301. static void Cut ()
  302. {
  303. TextField textField = menu.LastFocused as TextField;
  304. if (textField != null && textField.SelectedLength != 0) {
  305. textField.Cut ();
  306. }
  307. }
  308. static void Paste ()
  309. {
  310. TextField textField = menu.LastFocused as TextField;
  311. if (textField != null) {
  312. textField.Paste ();
  313. }
  314. }
  315. static void Help ()
  316. {
  317. MessageBox.Query (50, 7, "Help", "This is a small help\nBe kind.", "Ok");
  318. }
  319. #region Selection Demo
  320. static void ListSelectionDemo (bool multiple)
  321. {
  322. var d = new Dialog ("Selection Demo", 60, 20,
  323. new Button ("Ok", is_default: true) { Clicked = () => { Application.RequestStop (); } },
  324. new Button ("Cancel") { Clicked = () => { Application.RequestStop (); } });
  325. var animals = new List<string> () { "Alpaca", "Llama", "Lion", "Shark", "Goat" };
  326. var msg = new Label ("Use space bar or control-t to toggle selection") {
  327. X = 1,
  328. Y = 1,
  329. Width = Dim.Fill () - 1,
  330. Height = 1
  331. };
  332. var list = new ListView (animals) {
  333. X = 1,
  334. Y = 3,
  335. Width = Dim.Fill () - 4,
  336. Height = Dim.Fill () - 4,
  337. AllowsMarking = true,
  338. AllowsMultipleSelection = multiple
  339. };
  340. d.Add (msg, list);
  341. Application.Run (d);
  342. var result = "";
  343. for (int i = 0; i < animals.Count; i++) {
  344. if (list.Source.IsMarked (i)) {
  345. result += animals [i] + " ";
  346. }
  347. }
  348. MessageBox.Query (60, 10, "Selected Animals", result == "" ? "No animals selected" : result, "Ok");
  349. }
  350. #endregion
  351. #region OnKeyDown / OnKeyUp Demo
  352. private static void OnKeyDownUpDemo ()
  353. {
  354. var container = new Dialog (
  355. "OnKeyDown & OnKeyUp demo", 50, 20,
  356. new Button ("Ok", is_default: true) { Clicked = () => { Application.RequestStop (); } },
  357. new Button ("Cancel") { Clicked = () => { Application.RequestStop (); } });
  358. var kl = new Label (new Rect (3, 3, 40, 1), "Keyboard: ");
  359. container.OnKeyDown += (KeyEvent keyEvent) => KeyUpDown (keyEvent, kl, "Down");
  360. container.OnKeyUp += (KeyEvent keyEvent) => KeyUpDown (keyEvent, kl, "Up");
  361. container.Add (kl);
  362. Application.Run (container);
  363. }
  364. private static void KeyUpDown (KeyEvent keyEvent, Label kl, string updown)
  365. {
  366. kl.TextColor = Colors.TopLevel.Normal;
  367. if ((keyEvent.Key & Key.CtrlMask) != 0) {
  368. kl.Text = $"Keyboard: Ctrl Key{updown}";
  369. } else if ((keyEvent.Key & Key.AltMask) != 0) {
  370. kl.Text = $"Keyboard: Alt Key{updown}";
  371. } else {
  372. kl.Text = $"Keyboard: {(char)keyEvent.KeyValue} Key{updown}";
  373. }
  374. }
  375. #endregion
  376. public static Label ml;
  377. public static MenuBar menu;
  378. public static CheckBox menuKeysStyle;
  379. public static CheckBox menuAutoMouseNav;
  380. static void Main ()
  381. {
  382. if (Debugger.IsAttached)
  383. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  384. //Application.UseSystemConsole = true;
  385. Console.WindowHeight = 35;
  386. Application.Init ();
  387. var top = Application.Top;
  388. //Open ();
  389. #if true
  390. var win = new Window ("Hello") {
  391. X = 1,
  392. Y = 1,
  393. Width = Dim.Fill (),
  394. Height = Dim.Fill () - 1
  395. };
  396. #else
  397. var tframe = top.Frame;
  398. var win = new Window (new Rect (0, 1, tframe.Width, tframe.Height - 1), "Hello");
  399. #endif
  400. MenuItemDetails [] menuItems = {
  401. new MenuItemDetails ("F_ind", "", null),
  402. new MenuItemDetails ("_Replace", "", null),
  403. new MenuItemDetails ("_Item1", "", null),
  404. new MenuItemDetails ("_Not From Sub Menu", "", null)
  405. };
  406. menuItems [0].Action = () => ShowMenuItem (menuItems [0]);
  407. menuItems [1].Action = () => ShowMenuItem (menuItems [1]);
  408. menuItems [2].Action = () => ShowMenuItem (menuItems [2]);
  409. menuItems [3].Action = () => ShowMenuItem (menuItems [3]);
  410. menu = new MenuBar (new MenuBarItem [] {
  411. new MenuBarItem ("_File", new MenuItem [] {
  412. new MenuItem ("Text _Editor Demo", "", () => { Editor (top); }),
  413. new MenuItem ("_New", "Creates new file", NewFile),
  414. new MenuItem ("_Open", "", Open),
  415. new MenuItem ("_Hex", "", () => ShowHex (top)),
  416. new MenuItem ("_Close", "", () => Close ()),
  417. new MenuItem ("_Disabled", "", () => { }, () => false),
  418. null,
  419. new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
  420. }),
  421. new MenuBarItem ("_Edit", new MenuItem [] {
  422. new MenuItem ("_Copy", "", Copy),
  423. new MenuItem ("C_ut", "", Cut),
  424. new MenuItem ("_Paste", "", Paste),
  425. new MenuItem ("_Find and Replace",
  426. new MenuBarItem (new MenuItem[] {menuItems [0], menuItems [1] })),
  427. menuItems[3]
  428. }),
  429. new MenuBarItem ("_List Demos", new MenuItem [] {
  430. new MenuItem ("Select _Multiple Items", "", () => ListSelectionDemo (true)),
  431. new MenuItem ("Select _Single Item", "", () => ListSelectionDemo (false)),
  432. }),
  433. new MenuBarItem ("A_ssorted", new MenuItem [] {
  434. new MenuItem ("_Show text alignments", "", () => ShowTextAlignments ()),
  435. new MenuItem ("_OnKeyDown/Up", "", () => OnKeyDownUpDemo ())
  436. }),
  437. new MenuBarItem ("_Test Menu and SubMenus", new MenuItem [] {
  438. new MenuItem ("SubMenu1Item_1",
  439. new MenuBarItem (new MenuItem[] {
  440. new MenuItem ("SubMenu2Item_1",
  441. new MenuBarItem (new MenuItem [] {
  442. new MenuItem ("SubMenu3Item_1",
  443. new MenuBarItem (new MenuItem [] { menuItems [2] })
  444. )
  445. })
  446. )
  447. })
  448. )
  449. }),
  450. new MenuBarItem ("_About...", "Demonstrates top-level menu item", () => MessageBox.ErrorQuery (50, 7, "About Demo", "This is a demo app for gui.cs", "Ok")),
  451. });
  452. menuKeysStyle = new CheckBox (3, 25, "UseKeysUpDownAsKeysLeftRight", true);
  453. menuKeysStyle.Toggled += MenuKeysStyle_Toggled;
  454. menuAutoMouseNav = new CheckBox (40, 25, "UseMenuAutoNavigation", true);
  455. menuAutoMouseNav.Toggled += MenuAutoMouseNav_Toggled;
  456. ShowEntries (win);
  457. int count = 0;
  458. ml = new Label (new Rect (3, 18, 47, 1), "Mouse: ");
  459. Application.RootMouseEvent += delegate (MouseEvent me) {
  460. ml.TextColor = Colors.TopLevel.Normal;
  461. ml.Text = $"Mouse: ({me.X},{me.Y}) - {me.Flags} {count++}";
  462. };
  463. var test = new Label (3, 18, "Se iniciará el análisis");
  464. win.Add (test);
  465. win.Add (ml);
  466. var drag = new Label ("Drag: ") { X = 70, Y = 24 };
  467. var dragText = new TextField ("") {
  468. X = Pos.Right (drag),
  469. Y = Pos.Top (drag),
  470. Width = 40
  471. };
  472. var statusBar = new StatusBar (new StatusItem [] {
  473. new StatusItem(Key.F1, "~F1~ Help", () => Help()),
  474. new StatusItem(Key.F2, "~F2~ Load", null),
  475. new StatusItem(Key.F3, "~F3~ Save", null),
  476. new StatusItem(Key.ControlX, "~^X~ Quit", () => { if (Quit ()) top.Running = false; }),
  477. });
  478. win.Add (drag, dragText);
  479. #if true
  480. // This currently causes a stack overflow, because it is referencing a window that has not had its size allocated yet
  481. var bottom = new Label ("This should go on the bottom!");
  482. win.Add (bottom);
  483. Application.OnResized = () => {
  484. bottom.X = Pos.Left (win);
  485. bottom.Y = Pos.Bottom (win);
  486. };
  487. #endif
  488. top.Add (win);
  489. //top.Add (menu);
  490. top.Add (menu, statusBar, ml);
  491. Application.Run ();
  492. }
  493. }