Wizards.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. namespace UICatalog.Scenarios;
  2. [ScenarioMetadata ("Wizards", "Demonstrates the Wizard class")]
  3. [ScenarioCategory ("Dialogs")]
  4. [ScenarioCategory ("Wizards")]
  5. [ScenarioCategory ("Runnable")]
  6. public class Wizards : Scenario
  7. {
  8. public override void Main ()
  9. {
  10. Application.Init ();
  11. var win = new Window { Title = GetQuitKeyAndName () };
  12. var frame = new FrameView
  13. {
  14. X = Pos.Center (),
  15. Y = 0,
  16. Width = Dim.Percent (75),
  17. SchemeName = "Base",
  18. Title = "Wizard Options"
  19. };
  20. win.Add (frame);
  21. var label = new Label { X = 0, Y = 0, TextAlignment = Alignment.End, Text = "_Width:", Width = 10 };
  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 Win_Loaded (object sender, EventArgs args)
  71. {
  72. frame.Height = widthEdit.Frame.Height + heightEdit.Frame.Height + titleEdit.Frame.Height + 2;
  73. win.IsModalChanged -= Win_Loaded;
  74. }
  75. win.IsModalChanged += Win_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), SchemeName = "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.Accepting += (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. (s as View)?.App,
  102. "Nope",
  103. "Height and width must be greater than 0 (much bigger)",
  104. "Ok"
  105. );
  106. return;
  107. }
  108. actionLabel.Text = string.Empty;
  109. var wizard = new Wizard { Title = titleEdit.Text, Width = width, Height = height };
  110. wizard.MovingBack += (s, args) =>
  111. {
  112. //args.Cancel = true;
  113. actionLabel.Text = "Moving Back";
  114. };
  115. wizard.MovingNext += (s, args) =>
  116. {
  117. //args.Cancel = true;
  118. actionLabel.Text = "Moving Next";
  119. };
  120. wizard.Finished += (s, args) =>
  121. {
  122. //args.Cancel = true;
  123. actionLabel.Text = "Finished";
  124. };
  125. wizard.Cancelled += (s, args) =>
  126. {
  127. //args.Cancel = true;
  128. actionLabel.Text = "Cancelled";
  129. };
  130. // Add 1st step
  131. var firstStep = new WizardStep { Title = "End User License Agreement" };
  132. firstStep.NextButtonText = "Accept!";
  133. firstStep.HelpText =
  134. "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.";
  135. OptionSelector optionSelector = new ()
  136. {
  137. Labels = ["_One", "_Two", "_3"]
  138. };
  139. firstStep.Add (optionSelector);
  140. wizard.AddStep (firstStep);
  141. // Add 2nd step
  142. var secondStep = new WizardStep { Title = "Second Step" };
  143. wizard.AddStep (secondStep);
  144. secondStep.HelpText =
  145. "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.";
  146. var buttonLbl = new Label { Text = "Second Step Button: ", X = 1, Y = 1 };
  147. var button = new Button
  148. {
  149. Text = "Press Me to Rename Step", X = Pos.Right (buttonLbl), Y = Pos.Top (buttonLbl)
  150. };
  151. OptionSelector optionSelecor2 = new ()
  152. {
  153. Labels = ["_A", "_B", "_C"],
  154. Orientation = Orientation.Horizontal
  155. };
  156. secondStep.Add (optionSelecor2);
  157. button.Accepting += (s, e) =>
  158. {
  159. secondStep.Title = "2nd Step";
  160. MessageBox.Query (
  161. (s as View)?.App,
  162. "Wizard Scenario",
  163. "This Wizard Step's title was changed to '2nd Step'"
  164. );
  165. };
  166. secondStep.Add (buttonLbl, button);
  167. var lbl = new Label { Text = "First Name: ", X = 1, Y = Pos.Bottom (buttonLbl) };
  168. var firstNameField =
  169. new TextField { Text = "Number", Width = 30, X = Pos.Right (lbl), Y = Pos.Top (lbl) };
  170. secondStep.Add (lbl, firstNameField);
  171. lbl = new () { Text = "Last Name: ", X = 1, Y = Pos.Bottom (lbl) };
  172. var lastNameField = new TextField { Text = "Six", Width = 30, X = Pos.Right (lbl), Y = Pos.Top (lbl) };
  173. secondStep.Add (lbl, lastNameField);
  174. var thirdStepEnabledCeckBox = new CheckBox
  175. {
  176. Text = "Enable Step _3",
  177. CheckedState = CheckState.UnChecked,
  178. X = Pos.Left (lastNameField),
  179. Y = Pos.Bottom (lastNameField)
  180. };
  181. secondStep.Add (thirdStepEnabledCeckBox);
  182. // Add a frame
  183. var frame = new FrameView
  184. {
  185. X = 0,
  186. Y = Pos.Bottom (thirdStepEnabledCeckBox) + 2,
  187. Width = Dim.Fill (),
  188. Height = 4,
  189. Title = "A Broken Frame (by Depeche Mode)",
  190. TabStop = TabBehavior.NoStop
  191. };
  192. frame.Add (new TextField { Text = "This is a TextField inside of the frame." });
  193. secondStep.Add (frame);
  194. wizard.StepChanging += (s, args) =>
  195. {
  196. if (args.OldStep == secondStep && string.IsNullOrEmpty (firstNameField.Text))
  197. {
  198. args.Cancel = true;
  199. int? btn = MessageBox.ErrorQuery (
  200. (s as View)?.App,
  201. "Second Step",
  202. "You must enter a First Name to continue",
  203. "Ok"
  204. );
  205. }
  206. };
  207. // Add 3rd (optional) step
  208. var thirdStep = new WizardStep { Title = "Third Step (Optional)" };
  209. wizard.AddStep (thirdStep);
  210. thirdStep.HelpText =
  211. "This is step is optional (WizardStep.Enabled = false). Enable it with the checkbox in Step 2.";
  212. var step3Label = new Label { Text = "This step is optional.", X = 0, Y = 0 };
  213. thirdStep.Add (step3Label);
  214. var progLbl = new Label { Text = "Third Step ProgressBar: ", X = 1, Y = 10 };
  215. var progressBar = new ProgressBar
  216. {
  217. X = Pos.Right (progLbl), Y = Pos.Top (progLbl), Width = 40, Fraction = 0.42F
  218. };
  219. thirdStep.Add (progLbl, progressBar);
  220. thirdStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked;
  221. thirdStepEnabledCeckBox.CheckedStateChanged += (s, e) =>
  222. {
  223. thirdStep.Enabled =
  224. thirdStepEnabledCeckBox.CheckedState == CheckState.Checked;
  225. };
  226. // Add 4th step
  227. var fourthStep = new WizardStep { Title = "Step Four" };
  228. wizard.AddStep (fourthStep);
  229. var someText = new TextView
  230. {
  231. Text =
  232. "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).",
  233. X = 0,
  234. Y = 0,
  235. Width = Dim.Fill (),
  236. WordWrap = true,
  237. AllowsTab = false,
  238. SchemeName = "Base"
  239. };
  240. someText.Height = Dim.Fill (
  241. Dim.Func (v => someText.SuperView is { IsInitialized: true }
  242. ? someText.SuperView.SubViews
  243. .First (view => view.Y.Has<PosAnchorEnd> (out _))
  244. .Frame.Height
  245. : 1));
  246. var help = "This is helpful.";
  247. fourthStep.Add (someText);
  248. var hideHelpBtn = new Button
  249. {
  250. Text = "Press me to show/hide help",
  251. X = Pos.Center (),
  252. Y = Pos.AnchorEnd ()
  253. };
  254. hideHelpBtn.Accepting += (s, e) =>
  255. {
  256. if (fourthStep.HelpText.Length > 0)
  257. {
  258. fourthStep.HelpText = string.Empty;
  259. }
  260. else
  261. {
  262. fourthStep.HelpText = help;
  263. }
  264. };
  265. fourthStep.Add (hideHelpBtn);
  266. fourthStep.NextButtonText = "_Go To Last Step";
  267. //var scrollBar = new ScrollBarView (someText, true);
  268. //scrollBar.ChangedPosition += (s, e) =>
  269. // {
  270. // someText.TopRow = scrollBar.Position;
  271. // if (someText.TopRow != scrollBar.Position)
  272. // {
  273. // scrollBar.Position = someText.TopRow;
  274. // }
  275. // someText.SetNeedsDraw ();
  276. // };
  277. //someText.DrawingContent += (s, e) =>
  278. // {
  279. // scrollBar.Size = someText.Lines;
  280. // scrollBar.Position = someText.TopRow;
  281. // if (scrollBar.OtherScrollBarView != null)
  282. // {
  283. // scrollBar.OtherScrollBarView.Size = someText.Maxlength;
  284. // scrollBar.OtherScrollBarView.Position = someText.LeftColumn;
  285. // }
  286. // };
  287. //fourthStep.Add (scrollBar);
  288. // Add last step
  289. var lastStep = new WizardStep { Title = "The last step" };
  290. wizard.AddStep (lastStep);
  291. lastStep.HelpText =
  292. "The wizard is complete!\n\nPress the Finish button to continue.\n\nPressing ESC will cancel the wizard.";
  293. var finalFinalStepEnabledCeckBox =
  294. new CheckBox { Text = "Enable _Final Final Step", CheckedState = CheckState.UnChecked, X = 0, Y = 1 };
  295. lastStep.Add (finalFinalStepEnabledCeckBox);
  296. // Add an optional FINAL last step
  297. var finalFinalStep = new WizardStep { Title = "The VERY last step" };
  298. wizard.AddStep (finalFinalStep);
  299. finalFinalStep.HelpText =
  300. "This step only shows if it was enabled on the other last step.";
  301. finalFinalStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked;
  302. finalFinalStepEnabledCeckBox.CheckedStateChanged += (s, e) =>
  303. {
  304. finalFinalStep.Enabled =
  305. finalFinalStepEnabledCeckBox.CheckedState
  306. == CheckState.Checked;
  307. };
  308. Application.Run (wizard);
  309. wizard.Dispose ();
  310. }
  311. catch (FormatException)
  312. {
  313. actionLabel.Text = "Invalid Options";
  314. }
  315. };
  316. win.Add (showWizardButton);
  317. Application.Run (win);
  318. win.Dispose ();
  319. Application.Shutdown ();
  320. }
  321. private void Wizard_StepChanged (object sender, StepChangeEventArgs e) { throw new NotImplementedException (); }
  322. }