Wizards.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios {
  8. [ScenarioMetadata (Name: "Wizards", Description: "Demonstrates how to the Wizard class")]
  9. [ScenarioCategory ("Dialogs")]
  10. public class Wizards : Scenario {
  11. public override void Setup ()
  12. {
  13. Win.ColorScheme = Colors.Base;
  14. var frame = new FrameView ("Wizard Options") {
  15. X = Pos.Center (),
  16. Y = 0,
  17. Width = Dim.Percent (75),
  18. ColorScheme = Colors.Base,
  19. };
  20. Win.Add (frame);
  21. var label = new Label ("Width:") {
  22. X = 0,
  23. Y = 0,
  24. Width = 15,
  25. Height = 1,
  26. TextAlignment = Terminal.Gui.TextAlignment.Right,
  27. };
  28. frame.Add (label);
  29. var widthEdit = new TextField ("80") {
  30. X = Pos.Right (label) + 1,
  31. Y = Pos.Top (label),
  32. Width = 5,
  33. Height = 1
  34. };
  35. frame.Add (widthEdit);
  36. label = new Label ("Height:") {
  37. X = 0,
  38. Y = Pos.Bottom (label),
  39. Width = Dim.Width (label),
  40. Height = 1,
  41. TextAlignment = Terminal.Gui.TextAlignment.Right,
  42. };
  43. frame.Add (label);
  44. var heightEdit = new TextField ("20") {
  45. X = Pos.Right (label) + 1,
  46. Y = Pos.Top (label),
  47. Width = 5,
  48. Height = 1
  49. };
  50. frame.Add (heightEdit);
  51. label = new Label ("Title:") {
  52. X = 0,
  53. Y = Pos.Bottom (label),
  54. Width = Dim.Width (label),
  55. Height = 1,
  56. TextAlignment = Terminal.Gui.TextAlignment.Right,
  57. };
  58. frame.Add (label);
  59. var titleEdit = new TextField ("Title") {
  60. X = Pos.Right (label) + 1,
  61. Y = Pos.Top (label),
  62. Width = Dim.Fill (),
  63. Height = 1
  64. };
  65. frame.Add (titleEdit);
  66. var useStepView = new CheckBox () {
  67. Text = "Add 3rd step controls to WizardStep instead of WizardStep.Controls",
  68. Checked = false,
  69. X = Pos.Left (titleEdit),
  70. Y = Pos.Bottom (titleEdit)
  71. };
  72. frame.Add (useStepView);
  73. void Top_Loaded ()
  74. {
  75. frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit) + Dim.Height (useStepView) + 2;
  76. Top.Loaded -= Top_Loaded;
  77. }
  78. Top.Loaded += Top_Loaded;
  79. label = new Label ("Action:") {
  80. X = Pos.Center (),
  81. Y = Pos.AnchorEnd (1),
  82. AutoSize = true,
  83. TextAlignment = Terminal.Gui.TextAlignment.Right,
  84. };
  85. Win.Add (label);
  86. var actionLabel = new Label (" ") {
  87. X = Pos.Right (label),
  88. Y = Pos.AnchorEnd (1),
  89. AutoSize = true,
  90. ColorScheme = Colors.Error,
  91. };
  92. var showWizardButton = new Button ("Show Wizard") {
  93. X = Pos.Center (),
  94. Y = Pos.Bottom (frame) + 2,
  95. IsDefault = true,
  96. };
  97. showWizardButton.Clicked += () => {
  98. try {
  99. int width = 0;
  100. int.TryParse (widthEdit.Text.ToString (), out width);
  101. int height = 0;
  102. int.TryParse (heightEdit.Text.ToString (), out height);
  103. if (width < 1 || height < 1) {
  104. MessageBox.ErrorQuery ("Nope", "Height and width must be greater than 0 (much bigger)", "Ok");
  105. return;
  106. }
  107. var wizard = new Wizard (titleEdit.Text) {
  108. Width = width,
  109. Height = height
  110. };
  111. wizard.MovingBack += (args) => {
  112. //args.Cancel = true;
  113. actionLabel.Text = "Moving Back";
  114. };
  115. wizard.MovingNext += (args) => {
  116. //args.Cancel = true;
  117. actionLabel.Text = "Moving Next";
  118. };
  119. wizard.Finished += (args) => {
  120. //args.Cancel = true;
  121. actionLabel.Text = "Finished";
  122. };
  123. wizard.Cancelled += (args) => {
  124. //args.Cancel = true;
  125. actionLabel.Text = "Cancelled";
  126. };
  127. // Add 1st step
  128. var firstStep = new Wizard.WizardStep ("End User License Agreement");
  129. wizard.AddStep (firstStep);
  130. firstStep.ShowControls = false;
  131. firstStep.NextButtonText = "Accept!";
  132. firstStep.HelpText = "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.";
  133. // Add 2nd step
  134. var secondStep = new Wizard.WizardStep ("Second Step");
  135. wizard.AddStep (secondStep);
  136. secondStep.HelpText = "This is the help text for the Second Step.\n\nPress the button to see a message box.\n\nEnter First Name too.";
  137. View viewForControls = secondStep.Controls;
  138. ustring frameMsg = "Added to WizardStep.Controls";
  139. if (useStepView.Checked) {
  140. viewForControls = secondStep;
  141. frameMsg = "Added to WizardStep directly";
  142. }
  143. var buttonLbl = new Label () { Text = "Second Step Button: ", AutoSize = true, X = 1, Y = 1 };
  144. var button = new Button () {
  145. Text = "Press Me",
  146. X = Pos.Right (buttonLbl),
  147. Y = Pos.Top (buttonLbl)
  148. };
  149. button.Clicked += () => {
  150. MessageBox.Query ("Wizard Scenario", "The Second Step Button was pressed.");
  151. };
  152. viewForControls.Add (buttonLbl, button);
  153. var lbl = new Label () { Text = "First Name: ", AutoSize = true, X = 1, Y = Pos.Bottom (buttonLbl) };
  154. var firstNameField = new TextField () { Width = 30, X = Pos.Right (lbl), Y = Pos.Top (lbl) };
  155. viewForControls.Add (lbl, firstNameField);
  156. lbl = new Label () { Text = "Last Name: ", AutoSize = true, X = 1, Y = Pos.Bottom (lbl) };
  157. var lastNameField = new TextField () { Width = 30, X = Pos.Right (lbl), Y = Pos.Top (lbl) };
  158. viewForControls.Add (lbl, lastNameField);
  159. var thirdStepEnabledCeckBox = new CheckBox () { Text = "Enable Step _3", Checked = false, X = Pos.Left (lastNameField), Y = Pos.Bottom (lastNameField) };
  160. viewForControls.Add (thirdStepEnabledCeckBox);
  161. // Add a frame to demonstrate difference between adding controls to
  162. // WizardStep.Controls vs. WizardStep directly. This is here to demonstrate why
  163. // adding to .Controls is preferred.
  164. var frame = new FrameView ($"A Broken Frame - {frameMsg}") {
  165. X = 0,
  166. Y = Pos.Bottom (thirdStepEnabledCeckBox) + 2,
  167. Width = Dim.Fill (),
  168. Height = 4,
  169. //ColorScheme = Colors.Error,
  170. };
  171. frame.Add (new TextField ("This is a TextField inside of the frame."));
  172. viewForControls.Add (frame);
  173. wizard.StepChanging += (args) => {
  174. if (args.OldStep == secondStep && firstNameField.Text.IsEmpty ) {
  175. args.Cancel = true;
  176. var btn = MessageBox.ErrorQuery ("Second Step", "You must enter a First Name to continue", "Ok");
  177. }
  178. };
  179. // Add 3rd (optional) step
  180. var thirdStep = new Wizard.WizardStep ("Third Step (Optional)");
  181. wizard.AddStep (thirdStep);
  182. thirdStep.HelpText = "This is step is optional (WizardStep.Enabled = false). Enable it with the checkbox in Step 2.";
  183. var step3Label = new Label () {
  184. Text = "This step is optional.",
  185. X = 0,
  186. Y = 0,
  187. AutoSize = true
  188. };
  189. thirdStep.Controls.Add (step3Label);
  190. var progLbl = new Label () { Text = "Third Step ProgressBar: ", AutoSize = true, X = 1, Y = 10 };
  191. var progressBar = new ProgressBar () {
  192. X = Pos.Right (progLbl),
  193. Y = Pos.Top (progLbl),
  194. Width = 40,
  195. Fraction = 0.42F
  196. };
  197. thirdStep.Controls.Add (progLbl, progressBar);
  198. thirdStep.Enabled = thirdStepEnabledCeckBox.Checked;
  199. thirdStepEnabledCeckBox.Toggled += (args) => {
  200. thirdStep.Enabled = thirdStepEnabledCeckBox.Checked;
  201. };
  202. // Add 4th step
  203. var fourthStep = new Wizard.WizardStep ("Step Four");
  204. wizard.AddStep (fourthStep);
  205. fourthStep.ShowHelp = false;
  206. var someText = new TextView () {
  207. Text = "This step (Step Four) shows how to hide the Help pane. The control pane contains this TextView (but it's hard to tell it's a TextView because of Issue #1800).",
  208. X = 0,
  209. Y = 0,
  210. Width = Dim.Fill (),
  211. Height = Dim.Fill (),
  212. WordWrap = true,
  213. AllowsTab = false
  214. };
  215. fourthStep.Controls.Add (someText);
  216. fourthStep.NextButtonText = "Go To Last Step";
  217. var scrollBar = new ScrollBarView (someText, true);
  218. scrollBar.ChangedPosition += () => {
  219. someText.TopRow = scrollBar.Position;
  220. if (someText.TopRow != scrollBar.Position) {
  221. scrollBar.Position = someText.TopRow;
  222. }
  223. someText.SetNeedsDisplay ();
  224. };
  225. scrollBar.VisibleChanged += () => {
  226. if (scrollBar.Visible && someText.RightOffset == 0) {
  227. someText.RightOffset = 1;
  228. } else if (!scrollBar.Visible && someText.RightOffset == 1) {
  229. someText.RightOffset = 0;
  230. }
  231. };
  232. someText.DrawContent += (e) => {
  233. scrollBar.Size = someText.Lines;
  234. scrollBar.Position = someText.TopRow;
  235. if (scrollBar.OtherScrollBarView != null) {
  236. scrollBar.OtherScrollBarView.Size = someText.Maxlength;
  237. scrollBar.OtherScrollBarView.Position = someText.LeftColumn;
  238. }
  239. scrollBar.LayoutSubviews ();
  240. scrollBar.Refresh ();
  241. };
  242. fourthStep.Controls.Add (scrollBar);
  243. // Add last step
  244. var lastStep = new Wizard.WizardStep ("The last step");
  245. wizard.AddStep (lastStep);
  246. lastStep.HelpText = "The wizard is complete! Press the Finish button to continue. Pressing ESC will cancel the wizard.";
  247. // TODO: Demo setting initial Pane
  248. Application.Run (wizard);
  249. } catch (FormatException) {
  250. actionLabel.Text = "Invalid Options";
  251. }
  252. };
  253. Win.Add (showWizardButton);
  254. Win.Add (actionLabel);
  255. }
  256. }
  257. }