Program.fs 20 KB

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