Wizards.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Terminal.Gui;
  6. namespace UICatalog.Scenarios {
  7. [ScenarioMetadata (Name: "Wizards", Description: "Demonstrates the Wizard class")]
  8. [ScenarioCategory ("Dialogs"), ScenarioCategory ("Top Level Windows"), ScenarioCategory ("Wizards")]
  9. public class Wizards : Scenario {
  10. public override void Setup ()
  11. {
  12. var frame = new FrameView ("Wizard Options") {
  13. X = Pos.Center (),
  14. Y = 0,
  15. Width = Dim.Percent (75),
  16. ColorScheme = Colors.ColorSchemes ["Base"],
  17. };
  18. Win.Add (frame);
  19. var label = new Label ("Width:") {
  20. X = 0,
  21. Y = 0,
  22. Width = 15,
  23. Height = 1,
  24. TextAlignment = Terminal.Gui.TextAlignment.Right,
  25. AutoSize = false
  26. };
  27. frame.Add (label);
  28. var widthEdit = new TextField ("80") {
  29. X = Pos.Right (label) + 1,
  30. Y = Pos.Top (label),
  31. Width = 5,
  32. Height = 1
  33. };
  34. frame.Add (widthEdit);
  35. label = new Label ("Height:") {
  36. X = 0,
  37. Y = Pos.Bottom (label),
  38. Width = Dim.Width (label),
  39. Height = 1,
  40. TextAlignment = Terminal.Gui.TextAlignment.Right,
  41. AutoSize = false
  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. AutoSize = false
  58. };
  59. frame.Add (label);
  60. var titleEdit = new TextField ("Gandolf") {
  61. X = Pos.Right (label) + 1,
  62. Y = Pos.Top (label),
  63. Width = Dim.Fill (),
  64. Height = 1
  65. };
  66. frame.Add (titleEdit);
  67. void Top_Loaded (object sender, EventArgs args)
  68. {
  69. frame.Height = widthEdit.Frame.Height + heightEdit.Frame.Height + titleEdit.Frame.Height + 2;
  70. Application.Top.Loaded -= Top_Loaded;
  71. }
  72. Application.Top.Loaded += Top_Loaded;
  73. label = new Label ("Action:") {
  74. X = Pos.Center (),
  75. Y = Pos.AnchorEnd (1),
  76. TextAlignment = Terminal.Gui.TextAlignment.Right,
  77. };
  78. Win.Add (label);
  79. var actionLabel = new Label ("") {
  80. X = Pos.Right (label),
  81. Y = Pos.AnchorEnd (1),
  82. ColorScheme = Colors.ColorSchemes ["Error"],
  83. };
  84. Win.Add (actionLabel);
  85. var showWizardButton = new Button ("Show Wizard") {
  86. X = Pos.Center (),
  87. Y = Pos.Bottom (frame) + 2,
  88. IsDefault = true,
  89. };
  90. showWizardButton.Clicked += (s, e) => {
  91. try {
  92. int width = 0;
  93. int.TryParse (widthEdit.Text, out width);
  94. int height = 0;
  95. int.TryParse (heightEdit.Text, out height);
  96. if (width < 1 || height < 1) {
  97. MessageBox.ErrorQuery ("Nope", "Height and width must be greater than 0 (much bigger)", "Ok");
  98. return;
  99. }
  100. actionLabel.Text = string.Empty;
  101. var wizard = new Wizard () {
  102. Title = titleEdit.Text,
  103. Width = width,
  104. Height = height
  105. };
  106. wizard.MovingBack += (s, args) => {
  107. //args.Cancel = true;
  108. actionLabel.Text = "Moving Back";
  109. };
  110. wizard.MovingNext += (s, args) => {
  111. //args.Cancel = true;
  112. actionLabel.Text = "Moving Next";
  113. };
  114. wizard.Finished += (s, args) => {
  115. //args.Cancel = true;
  116. actionLabel.Text = "Finished";
  117. };
  118. wizard.Cancelled += (s, args) => {
  119. //args.Cancel = true;
  120. actionLabel.Text = "Cancelled";
  121. };
  122. // Add 1st step
  123. var firstStep = new WizardStep () { Title = "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 WizardStep () { Title = "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 += (s, e) => {
  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 += (s, args) => {
  160. if (args.OldStep == secondStep && string.IsNullOrEmpty(firstNameField.Text)) {
  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 WizardStep () { Title = "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 = (bool)thirdStepEnabledCeckBox.Checked;
  184. thirdStepEnabledCeckBox.Toggled += (s, e) => {
  185. thirdStep.Enabled = (bool)thirdStepEnabledCeckBox.Checked;
  186. };
  187. // Add 4th step
  188. var fourthStep = new WizardStep () { Title = "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. ColorScheme = Colors.ColorSchemes ["Base"]
  199. };
  200. var help = "This is helpful.";
  201. fourthStep.Add (someText);
  202. var hideHelpBtn = new Button () {
  203. Text = "Press me to show/hide help",
  204. X = Pos.Center (),
  205. Y = Pos.AnchorEnd (1)
  206. };
  207. hideHelpBtn.Clicked += (s, e) => {
  208. if (fourthStep.HelpText.Length > 0) {
  209. fourthStep.HelpText = string.Empty;
  210. } else {
  211. fourthStep.HelpText = help;
  212. }
  213. };
  214. fourthStep.Add (hideHelpBtn);
  215. fourthStep.NextButtonText = "Go To Last Step";
  216. var scrollBar = new ScrollBarView (someText, true);
  217. scrollBar.ChangedPosition += (s, e) => {
  218. someText.TopRow = scrollBar.Position;
  219. if (someText.TopRow != scrollBar.Position) {
  220. scrollBar.Position = someText.TopRow;
  221. }
  222. someText.SetNeedsDisplay ();
  223. };
  224. scrollBar.VisibleChanged += (s, e) => {
  225. if (scrollBar.Visible && someText.RightOffset == 0) {
  226. someText.RightOffset = 1;
  227. } else if (!scrollBar.Visible && someText.RightOffset == 1) {
  228. someText.RightOffset = 0;
  229. }
  230. };
  231. someText.DrawContent += (s, e) => {
  232. scrollBar.Size = someText.Lines;
  233. scrollBar.Position = someText.TopRow;
  234. if (scrollBar.OtherScrollBarView != null) {
  235. scrollBar.OtherScrollBarView.Size = someText.Maxlength;
  236. scrollBar.OtherScrollBarView.Position = someText.LeftColumn;
  237. }
  238. scrollBar.LayoutSubviews ();
  239. scrollBar.Refresh ();
  240. };
  241. fourthStep.Add (scrollBar);
  242. // Add last step
  243. var lastStep = new WizardStep () { Title = "The last step" };
  244. wizard.AddStep (lastStep);
  245. lastStep.HelpText = "The wizard is complete!\n\nPress the Finish button to continue.\n\nPressing ESC will cancel the wizard.";
  246. var finalFinalStepEnabledCeckBox = new CheckBox () { Text = "Enable _Final Final Step", Checked = false, X = 0, Y = 1 };
  247. lastStep.Add (finalFinalStepEnabledCeckBox);
  248. // Add an optional FINAL last step
  249. var finalFinalStep = new WizardStep () { Title = "The VERY last step" };
  250. wizard.AddStep (finalFinalStep);
  251. finalFinalStep.HelpText = "This step only shows if it was enabled on the other last step.";
  252. finalFinalStep.Enabled = (bool)thirdStepEnabledCeckBox.Checked;
  253. finalFinalStepEnabledCeckBox.Toggled += (s, e) => {
  254. finalFinalStep.Enabled = (bool)finalFinalStepEnabledCeckBox.Checked;
  255. };
  256. Application.Run (wizard);
  257. } catch (FormatException) {
  258. actionLabel.Text = "Invalid Options";
  259. }
  260. };
  261. Win.Add (showWizardButton);
  262. }
  263. }
  264. }