Wizards.cs 21 KB

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