Wizards.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using System;
  2. using System.Linq;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("Wizards", "Demonstrates the Wizard class")]
  5. [ScenarioCategory ("Dialogs")]
  6. [ScenarioCategory ("Wizards")]
  7. [ScenarioCategory ("Runnable")]
  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. SchemeName = "Base",
  20. Title = "Wizard Options"
  21. };
  22. win.Add (frame);
  23. var label = new Label { X = 0, Y = 0, TextAlignment = Alignment.End, Text = "_Width:", Width = 10 };
  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), SchemeName = "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.Accepting += (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. RadioGroup radioGroup = new ()
  137. {
  138. RadioLabels = ["_One", "_Two", "_3"]
  139. };
  140. firstStep.Add (radioGroup);
  141. wizard.AddStep (firstStep);
  142. // Add 2nd step
  143. var secondStep = new WizardStep { Title = "Second Step" };
  144. wizard.AddStep (secondStep);
  145. secondStep.HelpText =
  146. "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.";
  147. var buttonLbl = new Label { Text = "Second Step Button: ", X = 1, Y = 1 };
  148. var button = new Button
  149. {
  150. Text = "Press Me to Rename Step", X = Pos.Right (buttonLbl), Y = Pos.Top (buttonLbl)
  151. };
  152. RadioGroup radioGroup2 = new ()
  153. {
  154. RadioLabels = ["_A", "_B", "_C"],
  155. Orientation = Orientation.Horizontal
  156. };
  157. secondStep.Add (radioGroup2);
  158. button.Accepting += (s, e) =>
  159. {
  160. secondStep.Title = "2nd Step";
  161. MessageBox.Query (
  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. "Second Step",
  201. "You must enter a First Name to continue",
  202. "Ok"
  203. );
  204. }
  205. };
  206. // Add 3rd (optional) step
  207. var thirdStep = new WizardStep { Title = "Third Step (Optional)" };
  208. wizard.AddStep (thirdStep);
  209. thirdStep.HelpText =
  210. "This is step is optional (WizardStep.Enabled = false). Enable it with the checkbox in Step 2.";
  211. var step3Label = new Label { Text = "This step is optional.", X = 0, Y = 0 };
  212. thirdStep.Add (step3Label);
  213. var progLbl = new Label { Text = "Third Step ProgressBar: ", X = 1, Y = 10 };
  214. var progressBar = new ProgressBar
  215. {
  216. X = Pos.Right (progLbl), Y = Pos.Top (progLbl), Width = 40, Fraction = 0.42F
  217. };
  218. thirdStep.Add (progLbl, progressBar);
  219. thirdStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked;
  220. thirdStepEnabledCeckBox.CheckedStateChanged += (s, e) => { thirdStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked; };
  221. // Add 4th step
  222. var fourthStep = new WizardStep { Title = "Step Four" };
  223. wizard.AddStep (fourthStep);
  224. var someText = new TextView
  225. {
  226. Text =
  227. "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).",
  228. X = 0,
  229. Y = 0,
  230. Width = Dim.Fill (),
  231. WordWrap = true,
  232. AllowsTab = false,
  233. SchemeName = "Base"
  234. };
  235. someText.Height = Dim.Fill (
  236. Dim.Func (
  237. v => someText.SuperView is { IsInitialized: true }
  238. ? someText.SuperView.SubViews
  239. .First (view => view.Y.Has<PosAnchorEnd> (out _))
  240. .Frame.Height
  241. : 1));
  242. var help = "This is helpful.";
  243. fourthStep.Add (someText);
  244. var hideHelpBtn = new Button
  245. {
  246. Text = "Press me to show/hide help",
  247. X = Pos.Center (),
  248. Y = Pos.AnchorEnd ()
  249. };
  250. hideHelpBtn.Accepting += (s, e) =>
  251. {
  252. if (fourthStep.HelpText.Length > 0)
  253. {
  254. fourthStep.HelpText = string.Empty;
  255. }
  256. else
  257. {
  258. fourthStep.HelpText = help;
  259. }
  260. };
  261. fourthStep.Add (hideHelpBtn);
  262. fourthStep.NextButtonText = "_Go To Last Step";
  263. //var scrollBar = new ScrollBarView (someText, true);
  264. //scrollBar.ChangedPosition += (s, e) =>
  265. // {
  266. // someText.TopRow = scrollBar.Position;
  267. // if (someText.TopRow != scrollBar.Position)
  268. // {
  269. // scrollBar.Position = someText.TopRow;
  270. // }
  271. // someText.SetNeedsDraw ();
  272. // };
  273. //someText.DrawingContent += (s, e) =>
  274. // {
  275. // scrollBar.Size = someText.Lines;
  276. // scrollBar.Position = someText.TopRow;
  277. // if (scrollBar.OtherScrollBarView != null)
  278. // {
  279. // scrollBar.OtherScrollBarView.Size = someText.Maxlength;
  280. // scrollBar.OtherScrollBarView.Position = someText.LeftColumn;
  281. // }
  282. // };
  283. //fourthStep.Add (scrollBar);
  284. // Add last step
  285. var lastStep = new WizardStep { Title = "The last step" };
  286. wizard.AddStep (lastStep);
  287. lastStep.HelpText =
  288. "The wizard is complete!\n\nPress the Finish button to continue.\n\nPressing ESC will cancel the wizard.";
  289. var finalFinalStepEnabledCeckBox =
  290. new CheckBox { Text = "Enable _Final Final Step", CheckedState = CheckState.UnChecked, X = 0, Y = 1 };
  291. lastStep.Add (finalFinalStepEnabledCeckBox);
  292. // Add an optional FINAL last step
  293. var finalFinalStep = new WizardStep { Title = "The VERY last step" };
  294. wizard.AddStep (finalFinalStep);
  295. finalFinalStep.HelpText =
  296. "This step only shows if it was enabled on the other last step.";
  297. finalFinalStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked;
  298. finalFinalStepEnabledCeckBox.CheckedStateChanged += (s, e) =>
  299. {
  300. finalFinalStep.Enabled = finalFinalStepEnabledCeckBox.CheckedState == CheckState.Checked;
  301. };
  302. Application.Run (wizard);
  303. wizard.Dispose ();
  304. }
  305. catch (FormatException)
  306. {
  307. actionLabel.Text = "Invalid Options";
  308. }
  309. };
  310. win.Add (showWizardButton);
  311. Application.Run (win);
  312. win.Dispose ();
  313. Application.Shutdown ();
  314. }
  315. private void Wizard_StepChanged (object sender, StepChangeEventArgs e)
  316. {
  317. throw new NotImplementedException ();
  318. }
  319. }