Wizards.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System;
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("Wizards", "Demonstrates the Wizard class")]
  5. [ScenarioCategory ("Dialogs")]
  6. [ScenarioCategory ("Top Level Windows")]
  7. [ScenarioCategory ("Wizards")]
  8. public class Wizards : Scenario
  9. {
  10. public override void Main ()
  11. {
  12. Application.Init ();
  13. var win = new Window { Title = GetQuitKeyAndName () };
  14. var frame = new FrameView
  15. {
  16. X = Pos.Center (),
  17. Y = 0,
  18. Width = Dim.Percent (75),
  19. ColorScheme = Colors.ColorSchemes ["Base"],
  20. Title = "Wizard Options"
  21. };
  22. win.Add (frame);
  23. var label = new Label { X = 0, Y = 0, TextAlignment = Alignment.End, Text = "Width:" };
  24. frame.Add (label);
  25. var widthEdit = new TextField
  26. {
  27. X = Pos.Right (label) + 1,
  28. Y = Pos.Top (label),
  29. Width = 5,
  30. Height = 1,
  31. Text = "80"
  32. };
  33. frame.Add (widthEdit);
  34. label = new()
  35. {
  36. X = 0,
  37. Y = Pos.Bottom (label),
  38. Width = Dim.Width (label),
  39. Height = 1,
  40. TextAlignment = Alignment.End,
  41. Text = "Height:"
  42. };
  43. frame.Add (label);
  44. var heightEdit = new TextField
  45. {
  46. X = Pos.Right (label) + 1,
  47. Y = Pos.Top (label),
  48. Width = 5,
  49. Height = 1,
  50. Text = "20"
  51. };
  52. frame.Add (heightEdit);
  53. label = new()
  54. {
  55. X = 0,
  56. Y = Pos.Bottom (label),
  57. Width = Dim.Width (label),
  58. Height = 1,
  59. TextAlignment = Alignment.End,
  60. Text = "Title:"
  61. };
  62. frame.Add (label);
  63. var titleEdit = new TextField
  64. {
  65. X = Pos.Right (label) + 1,
  66. Y = Pos.Top (label),
  67. Width = Dim.Fill (),
  68. Height = 1,
  69. Text = "Gandolf"
  70. };
  71. frame.Add (titleEdit);
  72. void Win_Loaded (object sender, EventArgs args)
  73. {
  74. frame.Height = widthEdit.Frame.Height + heightEdit.Frame.Height + titleEdit.Frame.Height + 2;
  75. win.Loaded -= Win_Loaded;
  76. }
  77. win.Loaded += Win_Loaded;
  78. label = new()
  79. {
  80. X = Pos.Center (), Y = Pos.AnchorEnd (1), TextAlignment = Alignment.End, Text = "Action:"
  81. };
  82. win.Add (label);
  83. var actionLabel = new Label
  84. {
  85. X = Pos.Right (label), Y = Pos.AnchorEnd (1), ColorScheme = Colors.ColorSchemes ["Error"]
  86. };
  87. win.Add (actionLabel);
  88. var showWizardButton = new Button
  89. {
  90. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "Show Wizard"
  91. };
  92. showWizardButton.Accept += (s, e) =>
  93. {
  94. try
  95. {
  96. var width = 0;
  97. int.TryParse (widthEdit.Text, out width);
  98. var height = 0;
  99. int.TryParse (heightEdit.Text, out height);
  100. if (width < 1 || height < 1)
  101. {
  102. MessageBox.ErrorQuery (
  103. "Nope",
  104. "Height and width must be greater than 0 (much bigger)",
  105. "Ok"
  106. );
  107. return;
  108. }
  109. actionLabel.Text = string.Empty;
  110. var wizard = new Wizard { Title = titleEdit.Text, Width = width, Height = height };
  111. wizard.MovingBack += (s, args) =>
  112. {
  113. //args.Cancel = true;
  114. actionLabel.Text = "Moving Back";
  115. };
  116. wizard.MovingNext += (s, args) =>
  117. {
  118. //args.Cancel = true;
  119. actionLabel.Text = "Moving Next";
  120. };
  121. wizard.Finished += (s, args) =>
  122. {
  123. //args.Cancel = true;
  124. actionLabel.Text = "Finished";
  125. };
  126. wizard.Cancelled += (s, args) =>
  127. {
  128. //args.Cancel = true;
  129. actionLabel.Text = "Cancelled";
  130. };
  131. // Add 1st step
  132. var firstStep = new WizardStep { Title = "End User License Agreement" };
  133. firstStep.NextButtonText = "Accept!";
  134. firstStep.HelpText =
  135. "This is the End User License Agreement.\n\n\n\n\n\nThis is a test of the emergency broadcast system. This is a test of the emergency broadcast system.\nThis is a test of the emergency broadcast system.\n\n\nThis is a test of the emergency broadcast system.\n\nThis is a test of the emergency broadcast system.\n\n\n\nThe end of the EULA.";
  136. wizard.AddStep (firstStep);
  137. // Add 2nd step
  138. var secondStep = new WizardStep { Title = "Second Step" };
  139. wizard.AddStep (secondStep);
  140. secondStep.HelpText =
  141. "This is the help text for the Second Step.\n\nPress the button to change the Title.\n\nIf First Name is empty the step will prevent moving to the next step.";
  142. var buttonLbl = new Label { Text = "Second Step Button: ", X = 1, Y = 1 };
  143. var button = new Button
  144. {
  145. Text = "Press Me to Rename Step", X = Pos.Right (buttonLbl), Y = Pos.Top (buttonLbl)
  146. };
  147. button.Accept += (s, e) =>
  148. {
  149. secondStep.Title = "2nd Step";
  150. MessageBox.Query (
  151. "Wizard Scenario",
  152. "This Wizard Step's title was changed to '2nd Step'"
  153. );
  154. };
  155. secondStep.Add (buttonLbl, button);
  156. var lbl = new Label { Text = "First Name: ", X = 1, Y = Pos.Bottom (buttonLbl) };
  157. var firstNameField =
  158. new TextField { Text = "Number", Width = 30, X = Pos.Right (lbl), Y = Pos.Top (lbl) };
  159. secondStep.Add (lbl, firstNameField);
  160. lbl = new() { Text = "Last Name: ", X = 1, Y = Pos.Bottom (lbl) };
  161. var lastNameField = new TextField { Text = "Six", Width = 30, X = Pos.Right (lbl), Y = Pos.Top (lbl) };
  162. secondStep.Add (lbl, lastNameField);
  163. var thirdStepEnabledCeckBox = new CheckBox
  164. {
  165. Text = "Enable Step _3",
  166. State = CheckState.UnChecked,
  167. X = Pos.Left (lastNameField),
  168. Y = Pos.Bottom (lastNameField)
  169. };
  170. secondStep.Add (thirdStepEnabledCeckBox);
  171. // Add a frame
  172. var frame = new FrameView
  173. {
  174. X = 0,
  175. Y = Pos.Bottom (thirdStepEnabledCeckBox) + 2,
  176. Width = Dim.Fill (),
  177. Height = 4,
  178. Title = "A Broken Frame (by Depeche Mode)"
  179. };
  180. frame.Add (new TextField { Text = "This is a TextField inside of the frame." });
  181. secondStep.Add (frame);
  182. wizard.StepChanging += (s, args) =>
  183. {
  184. if (args.OldStep == secondStep && string.IsNullOrEmpty (firstNameField.Text))
  185. {
  186. args.Cancel = true;
  187. int btn = MessageBox.ErrorQuery (
  188. "Second Step",
  189. "You must enter a First Name to continue",
  190. "Ok"
  191. );
  192. }
  193. };
  194. // Add 3rd (optional) step
  195. var thirdStep = new WizardStep { Title = "Third Step (Optional)" };
  196. wizard.AddStep (thirdStep);
  197. thirdStep.HelpText =
  198. "This is step is optional (WizardStep.Enabled = false). Enable it with the checkbox in Step 2.";
  199. var step3Label = new Label { Text = "This step is optional.", X = 0, Y = 0 };
  200. thirdStep.Add (step3Label);
  201. var progLbl = new Label { Text = "Third Step ProgressBar: ", X = 1, Y = 10 };
  202. var progressBar = new ProgressBar
  203. {
  204. X = Pos.Right (progLbl), Y = Pos.Top (progLbl), Width = 40, Fraction = 0.42F
  205. };
  206. thirdStep.Add (progLbl, progressBar);
  207. thirdStep.Enabled = thirdStepEnabledCeckBox.State == CheckState.Checked;
  208. thirdStepEnabledCeckBox.Toggle += (s, e) => { thirdStep.Enabled = thirdStepEnabledCeckBox.State == CheckState.Checked; };
  209. // Add 4th step
  210. var fourthStep = new WizardStep { Title = "Step Four" };
  211. wizard.AddStep (fourthStep);
  212. var someText = new TextView
  213. {
  214. Text =
  215. "This step (Step Four) shows how to show/hide the Help pane. The step contains this TextView (but it's hard to tell it's a TextView because of Issue #1800).",
  216. X = 0,
  217. Y = 0,
  218. Width = Dim.Fill (),
  219. Height = Dim.Fill (1),
  220. WordWrap = true,
  221. AllowsTab = false,
  222. ColorScheme = Colors.ColorSchemes ["Base"]
  223. };
  224. var help = "This is helpful.";
  225. fourthStep.Add (someText);
  226. var hideHelpBtn = new Button
  227. {
  228. Text = "Press me to show/hide help", X = Pos.Center (), Y = Pos.AnchorEnd (1)
  229. };
  230. hideHelpBtn.Accept += (s, e) =>
  231. {
  232. if (fourthStep.HelpText.Length > 0)
  233. {
  234. fourthStep.HelpText = string.Empty;
  235. }
  236. else
  237. {
  238. fourthStep.HelpText = help;
  239. }
  240. };
  241. fourthStep.Add (hideHelpBtn);
  242. fourthStep.NextButtonText = "Go To Last Step";
  243. var scrollBar = new ScrollBarView (someText, true);
  244. scrollBar.ChangedPosition += (s, e) =>
  245. {
  246. someText.TopRow = scrollBar.Position;
  247. if (someText.TopRow != scrollBar.Position)
  248. {
  249. scrollBar.Position = someText.TopRow;
  250. }
  251. someText.SetNeedsDisplay ();
  252. };
  253. someText.DrawContent += (s, e) =>
  254. {
  255. scrollBar.Size = someText.Lines;
  256. scrollBar.Position = someText.TopRow;
  257. if (scrollBar.OtherScrollBarView != null)
  258. {
  259. scrollBar.OtherScrollBarView.Size = someText.Maxlength;
  260. scrollBar.OtherScrollBarView.Position = someText.LeftColumn;
  261. }
  262. scrollBar.LayoutSubviews ();
  263. scrollBar.Refresh ();
  264. };
  265. fourthStep.Add (scrollBar);
  266. // Add last step
  267. var lastStep = new WizardStep { Title = "The last step" };
  268. wizard.AddStep (lastStep);
  269. lastStep.HelpText =
  270. "The wizard is complete!\n\nPress the Finish button to continue.\n\nPressing ESC will cancel the wizard.";
  271. var finalFinalStepEnabledCeckBox =
  272. new CheckBox { Text = "Enable _Final Final Step", State = CheckState.UnChecked, X = 0, Y = 1 };
  273. lastStep.Add (finalFinalStepEnabledCeckBox);
  274. // Add an optional FINAL last step
  275. var finalFinalStep = new WizardStep { Title = "The VERY last step" };
  276. wizard.AddStep (finalFinalStep);
  277. finalFinalStep.HelpText =
  278. "This step only shows if it was enabled on the other last step.";
  279. finalFinalStep.Enabled = thirdStepEnabledCeckBox.State == CheckState.Checked;
  280. finalFinalStepEnabledCeckBox.Toggle += (s, e) =>
  281. {
  282. finalFinalStep.Enabled = finalFinalStepEnabledCeckBox.State == CheckState.Checked;
  283. };
  284. Application.Run (wizard);
  285. wizard.Dispose ();
  286. }
  287. catch (FormatException)
  288. {
  289. actionLabel.Text = "Invalid Options";
  290. }
  291. };
  292. win.Add (showWizardButton);
  293. Application.Run (win);
  294. win.Dispose ();
  295. Application.Shutdown ();
  296. }
  297. }