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 = TextAlignment.Right, 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 Label
  33. {
  34. X = 0,
  35. Y = Pos.Bottom (label),
  36. AutoSize = false,
  37. Width = Dim.Width (label),
  38. Height = 1,
  39. TextAlignment = TextAlignment.Right,
  40. Text = "Height:"
  41. };
  42. frame.Add (label);
  43. var heightEdit = new TextField
  44. {
  45. X = Pos.Right (label) + 1,
  46. Y = Pos.Top (label),
  47. Width = 5,
  48. Height = 1,
  49. Text = "20"
  50. };
  51. frame.Add (heightEdit);
  52. label = new Label
  53. {
  54. X = 0,
  55. Y = Pos.Bottom (label),
  56. AutoSize = false,
  57. Width = Dim.Width (label),
  58. Height = 1,
  59. TextAlignment = TextAlignment.Right,
  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 Top_Loaded (object sender, EventArgs args)
  73. {
  74. frame.Height = widthEdit.Frame.Height + heightEdit.Frame.Height + titleEdit.Frame.Height + 2;
  75. Application.Top.Loaded -= Top_Loaded;
  76. }
  77. Application.Top.Loaded += Top_Loaded;
  78. label = new Label
  79. {
  80. X = Pos.Center (), Y = Pos.AnchorEnd (1), TextAlignment = TextAlignment.Right, 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 Label { 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. Checked = false,
  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 = (bool)thirdStepEnabledCeckBox.Checked;
  208. thirdStepEnabledCeckBox.Toggled += (s, e) => { thirdStep.Enabled = (bool)thirdStepEnabledCeckBox.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. scrollBar.VisibleChanged += (s, e) =>
  254. {
  255. if (scrollBar.Visible && someText.RightOffset == 0)
  256. {
  257. someText.RightOffset = 1;
  258. }
  259. else if (!scrollBar.Visible && someText.RightOffset == 1)
  260. {
  261. someText.RightOffset = 0;
  262. }
  263. };
  264. someText.DrawContent += (s, e) =>
  265. {
  266. scrollBar.Size = someText.Lines;
  267. scrollBar.Position = someText.TopRow;
  268. if (scrollBar.OtherScrollBarView != null)
  269. {
  270. scrollBar.OtherScrollBarView.Size = someText.Maxlength;
  271. scrollBar.OtherScrollBarView.Position = someText.LeftColumn;
  272. }
  273. scrollBar.LayoutSubviews ();
  274. scrollBar.Refresh ();
  275. };
  276. fourthStep.Add (scrollBar);
  277. // Add last step
  278. var lastStep = new WizardStep { Title = "The last step" };
  279. wizard.AddStep (lastStep);
  280. lastStep.HelpText =
  281. "The wizard is complete!\n\nPress the Finish button to continue.\n\nPressing ESC will cancel the wizard.";
  282. var finalFinalStepEnabledCeckBox =
  283. new CheckBox { Text = "Enable _Final Final Step", Checked = false, X = 0, Y = 1 };
  284. lastStep.Add (finalFinalStepEnabledCeckBox);
  285. // Add an optional FINAL last step
  286. var finalFinalStep = new WizardStep { Title = "The VERY last step" };
  287. wizard.AddStep (finalFinalStep);
  288. finalFinalStep.HelpText =
  289. "This step only shows if it was enabled on the other last step.";
  290. finalFinalStep.Enabled = (bool)thirdStepEnabledCeckBox.Checked;
  291. finalFinalStepEnabledCeckBox.Toggled += (s, e) =>
  292. {
  293. finalFinalStep.Enabled = (bool)finalFinalStepEnabledCeckBox.Checked;
  294. };
  295. Application.Run (wizard);
  296. wizard.Dispose ();
  297. }
  298. catch (FormatException)
  299. {
  300. actionLabel.Text = "Invalid Options";
  301. }
  302. };
  303. Win.Add (showWizardButton);
  304. }
  305. }