Wizards.cs 9.5 KB

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