Wizards.cs 20 KB

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