Program.fs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // Learn more about F# at http://fsharp.org
  2. open Terminal.Gui
  3. open System
  4. open System.Collections.Generic
  5. open System.Diagnostics
  6. open System.Globalization
  7. open System.Reflection
  8. open NStack
  9. type Demo() = class end
  10. let ustr (x:string) = ustring.Make(x)
  11. let mutable ml2 = Unchecked.defaultof<Label>
  12. let mutable ml = Unchecked.defaultof<Label>
  13. let mutable menu = Unchecked.defaultof<MenuBar>
  14. let mutable menuKeysStyle = Unchecked.defaultof<CheckBox>
  15. let mutable menuAutoMouseNav = Unchecked.defaultof<CheckBox>
  16. type Box10x(x : int, y : int) =
  17. inherit View(new Rect(x, y, 20, 10))
  18. let w = 40
  19. let h = 50
  20. member val WantCursorPosition = Unchecked.defaultof<System.Boolean> with get, set
  21. new() as _this =
  22. (Box10x())
  23. then
  24. ()
  25. member this.GetContentSize() =
  26. new Size(w, h)
  27. member this.SetCursorPosition(pos : Point) =
  28. raise (new NotImplementedException())
  29. override this.Redraw(region : Rect) =
  30. Application.Driver.SetAttribute (Application.Current.ColorScheme.Focus)
  31. do
  32. let mutable (y : int) = 0
  33. while (y < h) do
  34. this.Move (0, y)
  35. Application.Driver.AddStr (ustr (y.ToString()))
  36. do
  37. let mutable (x : int) = 0
  38. while (x < w - (y.ToString ()).Length) do
  39. if (y.ToString ()).Length < w
  40. then Application.Driver.AddStr (ustr " ")
  41. x <- x + 1
  42. x
  43. y <- y + 1
  44. y
  45. ()
  46. type Filler(rect : Rect) =
  47. inherit View(rect)
  48. new() as _this =
  49. (Filler ())
  50. then
  51. ()
  52. override this.Redraw(region : Rect) =
  53. Application.Driver.SetAttribute (Application.Current.ColorScheme.Focus)
  54. let mutable f = this.Frame
  55. do
  56. let mutable (y : int) = 0
  57. while (y < f.Width) do
  58. this.Move (0, y)
  59. do
  60. let mutable (x : int) = 0
  61. while (x < f.Height) do
  62. let mutable (r : Rune) = Unchecked.defaultof<Rune>
  63. match (x % 3) with
  64. | 0 ->
  65. Application.Driver.AddRune ((Rune ((y.ToString ()).ToCharArray (0, 1)).[0]))
  66. if y > 9
  67. then Application.Driver.AddRune ((Rune ((y.ToString ()).ToCharArray (1, 1)).[0]))
  68. r <- (Rune '.')
  69. | 1 ->
  70. r <- (Rune 'o')
  71. | _ ->
  72. r <- (Rune 'O')
  73. Application.Driver.AddRune (r)
  74. x <- x + 1
  75. x
  76. y <- y + 1
  77. y
  78. ()
  79. let ShowTextAlignments() =
  80. let okButton = new Button(ustr "Ok", true)
  81. okButton.add_Clicked(Action(Application.RequestStop))
  82. let cancelButton = new Button(ustr "Cancel", true)
  83. cancelButton.add_Clicked(Action(Application.RequestStop))
  84. let mutable container = new Dialog(ustr "Text Alignments", 50, 20, okButton, cancelButton)
  85. let mutable (i : int) = 0
  86. let mutable (txt : string) = "Hello world, how are you doing today"
  87. container.Add (
  88. new Label (new Rect (0, 1, 40, 3), ustr ((sprintf "%O-%O" (i + 1)) txt), TextAlignment = TextAlignment.Left),
  89. new Label (new Rect (0, 3, 40, 3), ustr ((sprintf "%O-%O" (i + 2)) txt), TextAlignment = TextAlignment.Right),
  90. new Label (new Rect (0, 5, 40, 3), ustr ((sprintf "%O-%O" (i + 3)) txt), TextAlignment = TextAlignment.Centered),
  91. new Label (new Rect (0, 7, 40, 3), ustr ((sprintf "%O-%O" (i + 4)) txt), TextAlignment = TextAlignment.Justified)
  92. )
  93. Application.Run (container)
  94. let ShowEntries(container : View) =
  95. let mutable scrollView = new ScrollView (new Rect (50, 10, 20, 8),
  96. ContentSize = new Size (20, 50),
  97. ShowVerticalScrollIndicator = true,
  98. ShowHorizontalScrollIndicator = true
  99. )
  100. scrollView.Add (new Filler(new Rect(0, 0, 40, 40)))
  101. let mutable scrollView2 = new ScrollView (new Rect (72, 10, 3, 3),
  102. ContentSize = new Size (100, 100),
  103. ShowVerticalScrollIndicator = true,
  104. ShowHorizontalScrollIndicator = true
  105. )
  106. scrollView2.Add (new Box10x(0, 0))
  107. let mutable progress = new ProgressBar(new Rect(68, 1, 10, 1))
  108. let timer = Func<MainLoop, bool> (fun (caller) ->
  109. progress.Pulse ();
  110. true)
  111. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (300.0), timer) |> ignore
  112. let mutable login = Label (ustr "Login: ",
  113. X = Pos.At(3),
  114. Y = Pos.At(6)
  115. )
  116. let mutable password = new Label (ustr "Password: ",
  117. X = Pos.Left (login),
  118. Y = Pos.Bottom (login) + Pos.At(1)
  119. )
  120. let mutable loginText = new TextField (ustr "",
  121. X = Pos.Right (password),
  122. Y = Pos.Top (login),
  123. Width = Dim.op_Implicit(40)
  124. )
  125. let mutable passText = new TextField (ustr "",
  126. Secret = true,
  127. X = Pos.Left (loginText),
  128. Y = Pos.Top (password),
  129. Width = Dim.Width (loginText)
  130. )
  131. let mutable tf = new Button(3, 19, ustr "Ok")
  132. container.Add (login, loginText, password, passText,
  133. new FrameView (new Rect (3, 10, 25, 6), ustr "Options",
  134. [|new CheckBox (1, 0, ustr "Remember me");
  135. new RadioGroup (1, 2, [|ustr "_Personal"; ustr "_Company"|])|]
  136. ),
  137. new ListView (new Rect(59, 6, 16, 4),
  138. [|"First row";
  139. "<>";
  140. "This is a very long row that should overflow what is shown";
  141. "4th";
  142. "There is an empty slot on the second row";
  143. "Whoa";
  144. "This is so cool"|]
  145. ),
  146. scrollView, scrollView2, tf,
  147. new Button(10, 19, ustr "Cancel"),
  148. new TimeField(3, 20, DateTime.Now.TimeOfDay),
  149. new TimeField(23, 20, DateTime.Now.TimeOfDay, true),
  150. new DateField(3, 22, DateTime.Now),
  151. new DateField(23, 22, DateTime.Now, true),
  152. progress,
  153. new Label(3, 24, ustr "Press F9 (on Unix, ESC+9 is an alias) to activate the menubar"),
  154. menuKeysStyle,
  155. menuAutoMouseNav
  156. )
  157. container.SendSubviewToBack (tf)
  158. ()
  159. let NewFile() =
  160. let okButton = new Button(ustr "Ok", true)
  161. okButton.add_Clicked(Action(Application.RequestStop))
  162. let cancelButton = new Button(ustr "Cancel", true)
  163. cancelButton.add_Clicked(Action(Application.RequestStop))
  164. let mutable d = new Dialog (ustr "New File", 50, 20, okButton, cancelButton)
  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", ustring.Empty, (fun () -> Application.RequestStop ()))|]);
  173. new MenuBarItem(ustr "_Edit", [|new MenuItem(ustr "_Copy", ustring.Empty, Unchecked.defaultof<_>);
  174. new MenuItem(ustr "C_ut", ustring.Empty, Unchecked.defaultof<_>);
  175. new MenuItem(ustr "_Paste", ustring.Empty, 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, ustr "Quit Demo", ustr "Are you sure you want to quit this demo?", ustr "Yes", ustr "No")
  197. n = 0
  198. let Close() =
  199. MessageBox.ErrorQuery (50, 7, ustr "Error", ustr "There is nothing to close", ustr "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, ustr "Selected File", ustr (String.Join (", ", d.FilePaths)), ustr "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", ustring.Empty, (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, ustr (mi.Title.ToString ()),
  244. ustr ((sprintf "%O selected. Is from submenu: %O" (mi.Title.ToString ())) (mi.GetMenuBarItem ())), ustr "Ok")
  245. |> ignore
  246. let MenuKeysStyle_Toggled(e : bool) =
  247. menu.UseKeysUpDownAsKeysLeftRight <- menuKeysStyle.Checked
  248. let MenuAutoMouseNav_Toggled(e : bool) =
  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, ustr "Help", ustr "This is a small help\nBe kind.", ustr "Ok")
  267. |> ignore
  268. let Load () =
  269. MessageBox.Query (50, 7, ustr "Load", ustr "This is a small load\nBe kind.", ustr "Ok")
  270. |> ignore
  271. let Save () =
  272. MessageBox.Query (50, 7, ustr "Save ", ustr "This is a small save\nBe kind.", ustr "Ok")
  273. |> ignore
  274. let ListSelectionDemo(multiple : System.Boolean) =
  275. let okButton = new Button(ustr "Ok", true)
  276. okButton.add_Clicked(Action(Application.RequestStop))
  277. let cancelButton = new Button(ustr "Cancel")
  278. cancelButton.add_Clicked(Action(Application.RequestStop))
  279. let mutable d = new Dialog (ustr "Selection Demo", 60, 20, okButton, cancelButton)
  280. let mutable animals = new List<string> ()
  281. animals.AddRange([|"Alpaca"; "Llama"; "Lion"; "Shark"; "Goat"|])
  282. let mutable msg = new Label (ustr "Use space bar or control-t to toggle selection",
  283. X = Pos.At(1),
  284. Y = Pos.At(1),
  285. Width = Dim.Fill () - Dim.op_Implicit(1),
  286. Height = Dim.op_Implicit(1)
  287. )
  288. let mutable list = new ListView (animals,
  289. X = Pos.At(1),
  290. Y = Pos.At(3),
  291. Width = Dim.Fill () - Dim.op_Implicit(4),
  292. Height = Dim.Fill () - Dim.op_Implicit(4),
  293. AllowsMarking = true,
  294. AllowsMultipleSelection = multiple
  295. )
  296. d.Add (msg, list)
  297. Application.Run (d)
  298. let mutable result = ""
  299. do
  300. let mutable (i : int) = 0
  301. while (i < animals.Count) do
  302. if list.Source.IsMarked (i)
  303. then result <- result + animals.[i] + " "
  304. i <- i + 1
  305. i
  306. ()
  307. MessageBox.Query (60, 10, ustr "Selected Animals", ustr (if result = "" then "No animals selected" else result), ustr "Ok") |> ignore
  308. let OnKeyDownPressUpDemo() =
  309. let closeButton = new Button(ustr "Close")
  310. closeButton.add_Clicked(Action(Application.RequestStop))
  311. let mutable container = new Dialog (ustr "KeyDown & KeyPress & KeyUp demo", 80, 20, closeButton, Width = Dim.Fill (), Height = Dim.Fill ())
  312. let mutable list = new List<string> ()
  313. let mutable listView = new ListView (list,
  314. X = Pos.At(0),
  315. Y = Pos.At(0),
  316. Width = Dim.Fill () - Dim.op_Implicit(1),
  317. Height = Dim.Fill () - Dim.op_Implicit(2),
  318. ColorScheme = Colors.TopLevel
  319. )
  320. container.Add (listView)
  321. let KeyDownPressUp(keyEvent : KeyEvent, updown : string) =
  322. let ident : int = -5
  323. match updown with
  324. | "Down"
  325. | "Up"
  326. | "Press" ->
  327. list.Add (keyEvent.ToString ())
  328. listView.MoveDown ();
  329. container.KeyDown <- Action<View.KeyEventEventArgs> (fun (e : View.KeyEventEventArgs) -> KeyDownPressUp (e.KeyEvent, "Down") |> ignore)
  330. container.KeyPress <- Action<View.KeyEventEventArgs> (fun (e : View.KeyEventEventArgs) -> KeyDownPressUp (e.KeyEvent, "Press") |> ignore)
  331. container.KeyUp <- Action<View.KeyEventEventArgs> (fun (e : View.KeyEventEventArgs) -> KeyDownPressUp (e.KeyEvent, "Up") |> ignore)
  332. Application.Run (container)
  333. let Main() =
  334. if Debugger.IsAttached
  335. then CultureInfo.DefaultThreadCurrentUICulture <- CultureInfo.GetCultureInfo ("en-US")
  336. Application.Init ()
  337. let mutable top = Application.Top
  338. let mutable (margin : int) = 3
  339. let mutable win = new Window (ustr "Hello",
  340. X = Pos.At(1),
  341. Y = Pos.At(1),
  342. Width = Dim.Fill () - Dim.op_Implicit(margin),
  343. Height = Dim.Fill () - Dim.op_Implicit(margin)
  344. )
  345. let mutable (menuItems : MenuItemDetails[]) = [|new MenuItemDetails(ustr "F_ind", "", Unchecked.defaultof<_>);
  346. new MenuItemDetails(ustr "_Replace", "", Unchecked.defaultof<_>);
  347. new MenuItemDetails(ustr "_Item1", "", Unchecked.defaultof<_>);
  348. new MenuItemDetails(ustr "_Not From Sub Menu", "", Unchecked.defaultof<_>)|]
  349. menuItems.[0].Action <- fun () -> ShowMenuItem (menuItems.[0])
  350. menuItems.[1].Action <- fun () -> ShowMenuItem (menuItems.[1])
  351. menuItems.[2].Action <- fun () -> ShowMenuItem (menuItems.[2])
  352. menuItems.[3].Action <- fun () -> ShowMenuItem (menuItems.[3])
  353. menu <-
  354. new MenuBar ([|new MenuBarItem(ustr "_File",
  355. [|new MenuItem (ustr "Text _Editor Demo", ustring.Empty, (fun () -> Editor (top)));
  356. new MenuItem (ustr "_New", ustr "Creates new file", fun () -> NewFile());
  357. new MenuItem (ustr "_Open", ustring.Empty, fun () -> Open());
  358. new MenuItem (ustr "_Hex", ustring.Empty, (fun () -> ShowHex (top)));
  359. new MenuItem (ustr "_Close", ustring.Empty, (fun () -> Close()));
  360. new MenuItem (ustr "_Disabled", ustring.Empty, (fun () -> ()), (fun () -> false));
  361. Unchecked.defaultof<_>;
  362. new MenuItem (ustr "_Quit", ustring.Empty, (fun () -> if Quit() then top.Running <- false))|]);
  363. new MenuBarItem (ustr "_Edit", [|new MenuItem(ustr "_Copy", ustring.Empty, fun () -> Copy());
  364. new MenuItem(ustr "C_ut", ustring.Empty, fun () -> Cut()); new MenuItem(ustr "_Paste", ustring.Empty, fun () -> Paste());
  365. new MenuItem(ustr "_Find and Replace", new MenuBarItem([|(menuItems.[0]);
  366. (menuItems.[1])|])); (menuItems.[3])|]);
  367. new MenuBarItem(ustr "_List Demos", [|new MenuItem(ustr "Select _Multiple Items", ustring.Empty, (fun () -> ListSelectionDemo (true)));
  368. new MenuItem(ustr "Select _Single Item", ustring.Empty, (fun () -> ListSelectionDemo (false)))|]);
  369. new MenuBarItem(ustr "A_ssorted", [|new MenuItem(ustr "_Show text alignments", ustring.Empty, (fun () -> ShowTextAlignments ()));
  370. new MenuItem(ustr "_OnKeyDown/Press/Up", ustring.Empty, (fun () -> OnKeyDownPressUpDemo ()))|]);
  371. new MenuBarItem(ustr "_Test Menu and SubMenus",
  372. [|new MenuItem(ustr "SubMenu1Item_1", new MenuBarItem([|new MenuItem(ustr "SubMenu2Item_1",
  373. new MenuBarItem([|new MenuItem(ustr "SubMenu3Item_1", new MenuBarItem([|(menuItems.[2])|]))|]))|]))|]);
  374. new MenuBarItem(ustr "_About...", "Demonstrates top-level menu item",
  375. (fun () -> MessageBox.ErrorQuery (50, 7, ustr "About Demo", ustr "This is a demo app for gui.cs", ustr "Ok") |> ignore))|])
  376. menuKeysStyle <- new CheckBox(3, 25, ustr "UseKeysUpDownAsKeysLeftRight", true)
  377. menuKeysStyle.Toggled <- Action<bool> (MenuKeysStyle_Toggled)
  378. menuAutoMouseNav <- new CheckBox(40, 25, ustr "UseMenuAutoNavigation", true)
  379. menuAutoMouseNav.Toggled <- Action<bool> (MenuAutoMouseNav_Toggled)
  380. ShowEntries (win)
  381. let mutable (count : int) = 0
  382. ml <- new Label(new Rect(3, 17, 47, 1), ustr "Mouse: ")
  383. Application.RootMouseEvent <- Action<MouseEvent> (
  384. fun (me : MouseEvent) ->
  385. ml.TextColor <- Colors.TopLevel.Normal
  386. ml.Text <- ustr (
  387. (((sprintf "Mouse: (%O,%O) - %O %O" me.X) me.Y) me.Flags) (
  388. count <- count + 1
  389. count))
  390. )
  391. let mutable test = new Label(3, 18, ustr "Se iniciará el análisis")
  392. win.Add (test)
  393. win.Add (ml)
  394. let mutable drag = new Label (ustr "Drag: ", X = Pos.At(70), Y = Pos.At(24))
  395. let mutable dragText = new TextField (ustr "",
  396. X = Pos.Right (drag),
  397. Y = Pos.Top (drag),
  398. Width = Dim.op_Implicit(40)
  399. )
  400. let mutable statusBar = new StatusBar ([|
  401. new StatusItem(Key.F1, ustr "~F1~ Help", Action(Help));
  402. new StatusItem(Key.F2, ustr "~F2~ Load", Action(Load));
  403. new StatusItem(Key.F3, ustr "~F3~ Save", Action(Save));
  404. new StatusItem(Key.ControlX, ustr "~^X~ Quit", fun () -> if (Quit ()) then top.Running <- false)
  405. |]
  406. )
  407. win.Add (drag, dragText)
  408. let mutable bottom = new Label(ustr "This should go on the bottom of the same top-level!")
  409. win.Add (bottom)
  410. let mutable bottom2 = new Label(ustr "This should go on the bottom of another top-level!")
  411. top.Add (bottom2)
  412. Application.Loaded <- Action<Application.ResizedEventArgs> (
  413. fun (_) ->
  414. bottom.X <- win.X
  415. bottom.Y <- Pos.Bottom (win) - Pos.Top (win) - Pos.At(margin)
  416. bottom2.X <- Pos.Left (win)
  417. bottom2.Y <- Pos.Bottom (win)
  418. )
  419. top.Add (win)
  420. top.Add (menu, statusBar)
  421. Application.Run ()
  422. module Demo__run =
  423. [<EntryPoint>]
  424. let main argv =
  425. Main ()
  426. 0