Wizards.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using System;
  2. using System.Linq;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("Wizards", "Demonstrates the Wizard class")]
  6. [ScenarioCategory ("Dialogs")]
  7. [ScenarioCategory ("Wizards")]
  8. [ScenarioCategory ("Runnable")]
  9. public class Wizards : Scenario
  10. {
  11. public override void Main ()
  12. {
  13. Application.Init ();
  14. var win = new Window { Title = GetQuitKeyAndName () };
  15. var frame = new FrameView
  16. {
  17. X = Pos.Center (),
  18. Y = 0,
  19. Width = Dim.Percent (75),
  20. SchemeName = "Base",
  21. Title = "Wizard Options"
  22. };
  23. win.Add (frame);
  24. var label = new Label { X = 0, Y = 0, TextAlignment = Alignment.End, Text = "_Width:", Width = 10 };
  25. frame.Add (label);
  26. var widthEdit = new TextField
  27. {
  28. X = Pos.Right (label) + 1,
  29. Y = Pos.Top (label),
  30. Width = 5,
  31. Height = 1,
  32. Text = "80"
  33. };
  34. frame.Add (widthEdit);
  35. label = new ()
  36. {
  37. X = 0,
  38. Y = Pos.Bottom (label),
  39. Width = Dim.Width (label),
  40. Height = 1,
  41. TextAlignment = Alignment.End,
  42. Text = "_Height:"
  43. };
  44. frame.Add (label);
  45. var heightEdit = new TextField
  46. {
  47. X = Pos.Right (label) + 1,
  48. Y = Pos.Top (label),
  49. Width = 5,
  50. Height = 1,
  51. Text = "20"
  52. };
  53. frame.Add (heightEdit);
  54. label = new ()
  55. {
  56. X = 0,
  57. Y = Pos.Bottom (label),
  58. Width = Dim.Width (label),
  59. Height = 1,
  60. TextAlignment = Alignment.End,
  61. Text = "_Title:"
  62. };
  63. frame.Add (label);
  64. var titleEdit = new TextField
  65. {
  66. X = Pos.Right (label) + 1,
  67. Y = Pos.Top (label),
  68. Width = Dim.Fill (),
  69. Height = 1,
  70. Text = "Gandolf"
  71. };
  72. frame.Add (titleEdit);
  73. void Win_Loaded (object sender, EventArgs args)
  74. {
  75. frame.Height = widthEdit.Frame.Height + heightEdit.Frame.Height + titleEdit.Frame.Height + 2;
  76. win.Loaded -= Win_Loaded;
  77. }
  78. win.Loaded += Win_Loaded;
  79. label = new ()
  80. {
  81. X = Pos.Center (), Y = Pos.AnchorEnd (1), TextAlignment = Alignment.End, Text = "Action:"
  82. };
  83. win.Add (label);
  84. var actionLabel = new Label
  85. {
  86. X = Pos.Right (label), Y = Pos.AnchorEnd (1), SchemeName = "Error"
  87. };
  88. win.Add (actionLabel);
  89. var showWizardButton = new Button
  90. {
  91. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show Wizard"
  92. };
  93. showWizardButton.Accepting += (s, e) =>
  94. {
  95. try
  96. {
  97. var width = 0;
  98. int.TryParse (widthEdit.Text, out width);
  99. var height = 0;
  100. int.TryParse (heightEdit.Text, out height);
  101. if (width < 1 || height < 1)
  102. {
  103. MessageBox.ErrorQuery (
  104. "Nope",
  105. "Height and width must be greater than 0 (much bigger)",
  106. "Ok"
  107. );
  108. return;
  109. }
  110. actionLabel.Text = string.Empty;
  111. var wizard = new Wizard { Title = titleEdit.Text, Width = width, Height = height };
  112. wizard.MovingBack += (s, args) =>
  113. {
  114. //args.Cancel = true;
  115. actionLabel.Text = "Moving Back";
  116. };
  117. wizard.MovingNext += (s, args) =>
  118. {
  119. //args.Cancel = true;
  120. actionLabel.Text = "Moving Next";
  121. };
  122. wizard.Finished += (s, args) =>
  123. {
  124. //args.Cancel = true;
  125. actionLabel.Text = "Finished";
  126. };
  127. wizard.Cancelled += (s, args) =>
  128. {
  129. //args.Cancel = true;
  130. actionLabel.Text = "Cancelled";
  131. };
  132. // Add 1st step
  133. var firstStep = new WizardStep { Title = "End User License Agreement" };
  134. firstStep.NextButtonText = "Accept!";
  135. firstStep.HelpText =
  136. "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.";
  137. RadioGroup radioGroup = new ()
  138. {
  139. RadioLabels = ["_One", "_Two", "_3"]
  140. };
  141. firstStep.Add (radioGroup);
  142. wizard.AddStep (firstStep);
  143. // Add 2nd step
  144. var secondStep = new WizardStep { Title = "Second Step" };
  145. wizard.AddStep (secondStep);
  146. secondStep.HelpText =
  147. "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.";
  148. var buttonLbl = new Label { Text = "Second Step Button: ", X = 1, Y = 1 };
  149. var button = new Button
  150. {
  151. Text = "Press Me to Rename Step", X = Pos.Right (buttonLbl), Y = Pos.Top (buttonLbl)
  152. };
  153. RadioGroup radioGroup2 = new ()
  154. {
  155. RadioLabels = ["_A", "_B", "_C"],
  156. Orientation = Orientation.Horizontal
  157. };
  158. secondStep.Add (radioGroup2);
  159. button.Accepting += (s, e) =>
  160. {
  161. secondStep.Title = "2nd Step";
  162. MessageBox.Query (
  163. "Wizard Scenario",
  164. "This Wizard Step's title was changed to '2nd Step'"
  165. );
  166. };
  167. secondStep.Add (buttonLbl, button);
  168. var lbl = new Label { Text = "First Name: ", X = 1, Y = Pos.Bottom (buttonLbl) };
  169. var firstNameField =
  170. new TextField { Text = "Number", Width = 30, X = Pos.Right (lbl), Y = Pos.Top (lbl) };
  171. secondStep.Add (lbl, firstNameField);
  172. lbl = new () { Text = "Last Name: ", X = 1, Y = Pos.Bottom (lbl) };
  173. var lastNameField = new TextField { Text = "Six", Width = 30, X = Pos.Right (lbl), Y = Pos.Top (lbl) };
  174. secondStep.Add (lbl, lastNameField);
  175. var thirdStepEnabledCeckBox = new CheckBox
  176. {
  177. Text = "Enable Step _3",
  178. CheckedState = CheckState.UnChecked,
  179. X = Pos.Left (lastNameField),
  180. Y = Pos.Bottom (lastNameField)
  181. };
  182. secondStep.Add (thirdStepEnabledCeckBox);
  183. // Add a frame
  184. var frame = new FrameView
  185. {
  186. X = 0,
  187. Y = Pos.Bottom (thirdStepEnabledCeckBox) + 2,
  188. Width = Dim.Fill (),
  189. Height = 4,
  190. Title = "A Broken Frame (by Depeche Mode)",
  191. TabStop = TabBehavior.NoStop
  192. };
  193. frame.Add (new TextField { Text = "This is a TextField inside of the frame." });
  194. secondStep.Add (frame);
  195. wizard.StepChanging += (s, args) =>
  196. {
  197. if (args.OldStep == secondStep && string.IsNullOrEmpty (firstNameField.Text))
  198. {
  199. args.Cancel = true;
  200. int btn = MessageBox.ErrorQuery (
  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) => { thirdStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked; };
  222. // Add 4th step
  223. var fourthStep = new WizardStep { Title = "Step Four" };
  224. wizard.AddStep (fourthStep);
  225. var someText = new TextView
  226. {
  227. Text =
  228. "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).",
  229. X = 0,
  230. Y = 0,
  231. Width = Dim.Fill (),
  232. WordWrap = true,
  233. AllowsTab = false,
  234. SchemeName = "Base"
  235. };
  236. someText.Height = Dim.Fill (
  237. Dim.Func (
  238. () => someText.SuperView is { IsInitialized: true }
  239. ? someText.SuperView.SubViews
  240. .First (view => view.Y.Has<PosAnchorEnd> (out _))
  241. .Frame.Height
  242. : 1));
  243. var help = "This is helpful.";
  244. fourthStep.Add (someText);
  245. var hideHelpBtn = new Button
  246. {
  247. Text = "Press me to show/hide help",
  248. X = Pos.Center (),
  249. Y = Pos.AnchorEnd ()
  250. };
  251. hideHelpBtn.Accepting += (s, e) =>
  252. {
  253. if (fourthStep.HelpText.Length > 0)
  254. {
  255. fourthStep.HelpText = string.Empty;
  256. }
  257. else
  258. {
  259. fourthStep.HelpText = help;
  260. }
  261. };
  262. fourthStep.Add (hideHelpBtn);
  263. fourthStep.NextButtonText = "_Go To Last Step";
  264. //var scrollBar = new ScrollBarView (someText, true);
  265. //scrollBar.ChangedPosition += (s, e) =>
  266. // {
  267. // someText.TopRow = scrollBar.Position;
  268. // if (someText.TopRow != scrollBar.Position)
  269. // {
  270. // scrollBar.Position = someText.TopRow;
  271. // }
  272. // someText.SetNeedsDraw ();
  273. // };
  274. //someText.DrawingContent += (s, e) =>
  275. // {
  276. // scrollBar.Size = someText.Lines;
  277. // scrollBar.Position = someText.TopRow;
  278. // if (scrollBar.OtherScrollBarView != null)
  279. // {
  280. // scrollBar.OtherScrollBarView.Size = someText.Maxlength;
  281. // scrollBar.OtherScrollBarView.Position = someText.LeftColumn;
  282. // }
  283. // };
  284. //fourthStep.Add (scrollBar);
  285. // Add last step
  286. var lastStep = new WizardStep { Title = "The last step" };
  287. wizard.AddStep (lastStep);
  288. lastStep.HelpText =
  289. "The wizard is complete!\n\nPress the Finish button to continue.\n\nPressing ESC will cancel the wizard.";
  290. var finalFinalStepEnabledCeckBox =
  291. new CheckBox { Text = "Enable _Final Final Step", CheckedState = CheckState.UnChecked, X = 0, Y = 1 };
  292. lastStep.Add (finalFinalStepEnabledCeckBox);
  293. // Add an optional FINAL last step
  294. var finalFinalStep = new WizardStep { Title = "The VERY last step" };
  295. wizard.AddStep (finalFinalStep);
  296. finalFinalStep.HelpText =
  297. "This step only shows if it was enabled on the other last step.";
  298. finalFinalStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked;
  299. finalFinalStepEnabledCeckBox.CheckedStateChanged += (s, e) =>
  300. {
  301. finalFinalStep.Enabled = finalFinalStepEnabledCeckBox.CheckedState == CheckState.Checked;
  302. };
  303. Application.Run (wizard);
  304. wizard.Dispose ();
  305. }
  306. catch (FormatException)
  307. {
  308. actionLabel.Text = "Invalid Options";
  309. }
  310. };
  311. win.Add (showWizardButton);
  312. Application.Run (win);
  313. win.Dispose ();
  314. Application.Shutdown ();
  315. }
  316. private void Wizard_StepChanged (object sender, StepChangeEventArgs e)
  317. {
  318. throw new NotImplementedException ();
  319. }
  320. }