Wizard.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NStack;
  5. using Terminal.Gui.Resources;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// Provides a step-based "wizard" UI. The Wizard supports multiple steps. Each step (<see cref="WizardStep"/>) can host
  9. /// arbitrary <see cref="View"/>s, much like a <see cref="Dialog"/>. Each step also has a pane for help text. Along the
  10. /// bottom of the Wizard view are customizable buttons enabling the user to navigate forward and backward through the Wizard.
  11. /// </summary>
  12. /// <remarks>
  13. /// </remarks>
  14. public class Wizard : Dialog {
  15. /// <summary>
  16. /// One step for the Wizard. The <see cref="WizardStep"/> view hosts two sub-views: 1) add <see cref="View"/>s to <see cref="WizardStep.Controls"/>,
  17. /// 2) use <see cref="WizardStep.HelpText"/> to set the contents of the <see cref="TextView"/> that shows on the
  18. /// right side. Use <see cref="WizardStep.showControls"/> and <see cref="WizardStep.showHelp"/> to
  19. /// control wether the control or help pane are shown.
  20. /// </summary>
  21. /// <remarks>
  22. /// If <see cref="Button"/>s are added, do not set <see cref="Button.IsDefault"/> to true as this will conflict
  23. /// with the Next button of the Wizard.
  24. ///
  25. /// Subscribe to the <see cref="View.VisibleChanged"/> event to be notified when the step is active; see also: <see cref="Wizard.StepChanged"/>.
  26. ///
  27. /// To enable or disable a step from being shown to the user, set <see cref="View.Enabled"/>.
  28. ///
  29. /// </remarks>
  30. public class WizardStep : View {
  31. /// <summary>
  32. /// The title of the <see cref="WizardStep"/>.
  33. /// </summary>
  34. public ustring Title { get => title; set => title = value; }
  35. // TODO: Update Wizard title when step title is changed if step is current - this will require step to slueth it's parent
  36. private ustring title;
  37. // The controlPane is a separate view, so when devs add controls to the Step and help is visible, Y = Pos.AnchorEnd()
  38. // will work as expected.
  39. private View controlPane = new FrameView ();
  40. /// <summary>
  41. /// THe pane that holds the controls for the <see cref="WizardStep"/>. Use <see cref="WizardStep.Controls"/> `Add(View`) to add
  42. /// controls. Note that the Controls view is sized to take 70% of the Wizard's width and the <see cref="WizardStep.HelpText"/>
  43. /// takes the other 30%. This can be adjusted by setting `Width` from `Dim.Percent(70)` to
  44. /// another value. If <see cref="WizardStep.ShowHelp"/> is set to `false` the control pane will fill the entire
  45. /// Wizard.
  46. /// </summary>
  47. public View Controls { get => controlPane; }
  48. /// <summary>
  49. /// Sets or gets help text for the <see cref="WizardStep"/>.If <see cref="WizardStep.ShowHelp"/> is set to
  50. /// `false` the control pane will fill the entire wizard.
  51. /// </summary>
  52. /// <remarks>The help text is displayed using a read-only <see cref="TextView"/>.</remarks>
  53. public ustring HelpText { get => helpTextView.Text; set => helpTextView.Text = value; }
  54. private TextView helpTextView = new TextView ();
  55. /// <summary>
  56. /// Sets or gets the text for the back button. The back button will only be visible on
  57. /// steps after the first step.
  58. /// </summary>
  59. /// <remarks>The default text is "Back"</remarks>
  60. public ustring BackButtonText { get; set; } = ustring.Empty;
  61. // TODO: Update button text of Wizard button when step's button text is changed if step is current - this will require step to slueth it's parent
  62. /// <summary>
  63. /// Sets or gets the text for the next/finish button.
  64. /// </summary>
  65. /// <remarks>The default text is "Next..." if the Pane is not the last pane. Otherwise it is "Finish"</remarks>
  66. public ustring NextButtonText { get; set; } = ustring.Empty;
  67. // TODO: Update button text of Wizard button when step's button text is changed if step is current - this will require step to slueth it's parent
  68. /// <summary>
  69. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  70. /// </summary>
  71. /// <param name="title">Title for the Step. Will be appended to the containing Wizard's title as
  72. /// "Wizard Title - Wizard Step Title" when this step is active.</param>
  73. /// <remarks>
  74. /// </remarks>
  75. public WizardStep (ustring title)
  76. {
  77. this.Title = title; // this.Title holds just the "Wizard Title"; base.Title holds "Wizard Title - Step Title"
  78. this.ColorScheme = Colors.Dialog;
  79. Y = 0;
  80. Height = Dim.Fill (1); // for button frame
  81. Width = Dim.Fill ();
  82. Controls.ColorScheme = Colors.Dialog;
  83. Controls.Border.BorderStyle = BorderStyle.None;
  84. Controls.Border.Padding = new Thickness (0);
  85. Controls.Border.BorderThickness = new Thickness (0);
  86. this.Add (Controls);
  87. helpTextView.ColorScheme = Colors.Menu;
  88. helpTextView.Y = 0;
  89. helpTextView.ReadOnly = true;
  90. helpTextView.WordWrap = true;
  91. this.Add (helpTextView);
  92. ShowHide ();
  93. var scrollBar = new ScrollBarView (helpTextView, true);
  94. scrollBar.ChangedPosition += () => {
  95. helpTextView.TopRow = scrollBar.Position;
  96. if (helpTextView.TopRow != scrollBar.Position) {
  97. scrollBar.Position = helpTextView.TopRow;
  98. }
  99. helpTextView.SetNeedsDisplay ();
  100. };
  101. scrollBar.OtherScrollBarView.ChangedPosition += () => {
  102. helpTextView.LeftColumn = scrollBar.OtherScrollBarView.Position;
  103. if (helpTextView.LeftColumn != scrollBar.OtherScrollBarView.Position) {
  104. scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  105. }
  106. helpTextView.SetNeedsDisplay ();
  107. };
  108. scrollBar.VisibleChanged += () => {
  109. if (scrollBar.Visible && helpTextView.RightOffset == 0) {
  110. helpTextView.RightOffset = 1;
  111. } else if (!scrollBar.Visible && helpTextView.RightOffset == 1) {
  112. helpTextView.RightOffset = 0;
  113. }
  114. };
  115. scrollBar.OtherScrollBarView.VisibleChanged += () => {
  116. if (scrollBar.OtherScrollBarView.Visible && helpTextView.BottomOffset == 0) {
  117. helpTextView.BottomOffset = 1;
  118. } else if (!scrollBar.OtherScrollBarView.Visible && helpTextView.BottomOffset == 1) {
  119. helpTextView.BottomOffset = 0;
  120. }
  121. };
  122. helpTextView.DrawContent += (e) => {
  123. scrollBar.Size = helpTextView.Lines;
  124. scrollBar.Position = helpTextView.TopRow;
  125. if (scrollBar.OtherScrollBarView != null) {
  126. scrollBar.OtherScrollBarView.Size = helpTextView.Maxlength;
  127. scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  128. }
  129. scrollBar.LayoutSubviews ();
  130. scrollBar.Refresh ();
  131. };
  132. this.Add (scrollBar);
  133. }
  134. //public override void OnEnabledChanged()
  135. //{
  136. // if (Enabled) { }
  137. // base.OnEnabledChanged ();
  138. //}
  139. /// <summary>
  140. /// If true (the default) the help will be visible. If false, the help will not be shown and the control pane will
  141. /// fill the wizard step.
  142. /// </summary>
  143. public bool ShowHelp {
  144. get => showHelp;
  145. set {
  146. showHelp = value;
  147. ShowHide ();
  148. }
  149. }
  150. private bool showHelp = true;
  151. /// <summary>
  152. /// If true (the default) the <see cref="Controls"/> View will be visible. If false, the controls will not be shown and the help will
  153. /// fill the wizard step.
  154. /// </summary>
  155. public bool ShowControls {
  156. get => showControls;
  157. set {
  158. showControls = value;
  159. ShowHide ();
  160. }
  161. }
  162. private bool showControls = true;
  163. /// <summary>
  164. /// Does the work to show and hide the controls, help, and buttons as appropriate
  165. /// </summary>
  166. private void ShowHide ()
  167. {
  168. Controls.Height = Dim.Fill (1);
  169. helpTextView.Height = Dim.Fill (1);
  170. helpTextView.Width = Dim.Fill ();
  171. if (showControls) {
  172. if (showHelp) {
  173. Controls.Width = Dim.Percent (70);
  174. helpTextView.X = Pos.Right (Controls);
  175. helpTextView.Width = Dim.Fill ();
  176. } else {
  177. Controls.Width = Dim.Percent (100);
  178. }
  179. } else {
  180. if (showHelp) {
  181. helpTextView.X = 0;
  182. } else {
  183. // Error - no pane shown
  184. }
  185. }
  186. Controls.Visible = showControls;
  187. helpTextView.Visible = showHelp;
  188. }
  189. } // WizardStep
  190. /// <summary>
  191. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  192. /// </summary>
  193. /// <remarks>
  194. /// The Wizard will be vertically and horizontally centered in the container.
  195. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> change size and position.
  196. /// </remarks>
  197. public Wizard () : this (ustring.Empty)
  198. {
  199. }
  200. /// <summary>
  201. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  202. /// </summary>
  203. /// <param name="title">Title for the Wizard.</param>
  204. /// <remarks>
  205. /// The Wizard will be vertically and horizontally centered in the container.
  206. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> change size and position.
  207. /// </remarks>
  208. public Wizard (ustring title) : base (title)
  209. {
  210. wizardTitle = title;
  211. // Using Justify causes the Back and Next buttons to be hard justified against
  212. // the left and right edge
  213. ButtonAlignment = ButtonAlignments.Justify;
  214. this.Border.BorderStyle = BorderStyle.Double;
  215. // Add a horiz separator
  216. var separator = new LineView (Graphs.Orientation.Horizontal) {
  217. Y = Pos.AnchorEnd (2)
  218. };
  219. Add (separator);
  220. // BUGBUG: Space is to work around https://github.com/migueldeicaza/gui.cs/issues/1812
  221. backBtn = new Button (Strings.wzBack) { AutoSize = true };
  222. AddButton (backBtn);
  223. nextfinishBtn = new Button (Strings.wzFinish) { AutoSize = true };
  224. nextfinishBtn.IsDefault = true;
  225. AddButton (nextfinishBtn);
  226. backBtn.Clicked += BackBtn_Clicked;
  227. nextfinishBtn.Clicked += NextfinishBtn_Clicked;
  228. Loaded += Wizard_Loaded;
  229. Closing += Wizard_Closing;
  230. }
  231. private bool finishedPressed = false;
  232. private void Wizard_Closing (ToplevelClosingEventArgs obj)
  233. {
  234. if (!finishedPressed) {
  235. var args = new WizardButtonEventArgs ();
  236. Cancelled?.Invoke (args);
  237. }
  238. }
  239. private void Wizard_Loaded ()
  240. {
  241. foreach (var step in steps) {
  242. step.Y = 0;
  243. }
  244. CurrentStep = GetNextStep (); // gets the first step if CurrentStep == null
  245. }
  246. private void NextfinishBtn_Clicked ()
  247. {
  248. if (CurrentStep == steps.Last.Value) {
  249. var args = new WizardButtonEventArgs ();
  250. Finished?.Invoke (args);
  251. if (!args.Cancel) {
  252. finishedPressed = true;
  253. Application.RequestStop (this);
  254. }
  255. } else {
  256. var args = new WizardButtonEventArgs ();
  257. MovingNext?.Invoke (args);
  258. if (!args.Cancel) {
  259. GoNext ();
  260. }
  261. }
  262. }
  263. /// <summary>
  264. /// Causes the wizad to move to the next enabled step (or last step if <see cref="CurrentStep"/> is not set).
  265. /// If there is no previous step, does nothing.
  266. /// </summary>
  267. public void GoNext ()
  268. {
  269. var nextStep = GetNextStep ();
  270. if (nextStep != null) {
  271. GoToStep (nextStep);
  272. }
  273. }
  274. /// <summary>
  275. /// Returns the next enabled <see cref="WizardStep"/> after the current step. Takes into account steps which
  276. /// are disabled. If <see cref="CurrentStep"/> is `null` returns the first enabled step.
  277. /// </summary>
  278. /// <returns>The next step after the current step, if there is one; otherwise returns `null`, which
  279. /// indicates either there are no enabled steps or the current step is the last enabled step.</returns>
  280. public WizardStep GetNextStep ()
  281. {
  282. LinkedListNode<WizardStep> step = null;
  283. if (CurrentStep == null) {
  284. // Get last step, assume it is next
  285. step = steps.First;
  286. } else {
  287. // Get the step after current
  288. step = steps.Find (CurrentStep);
  289. if (step != null) {
  290. step = step.Next;
  291. }
  292. }
  293. // step now points to the potential next step
  294. while (step != null) {
  295. if (step.Value.Enabled) {
  296. return step.Value;
  297. }
  298. step = step.Next;
  299. }
  300. return null;
  301. }
  302. private void BackBtn_Clicked ()
  303. {
  304. var args = new WizardButtonEventArgs ();
  305. MovingBack?.Invoke (args);
  306. if (!args.Cancel) {
  307. GoBack ();
  308. }
  309. }
  310. /// <summary>
  311. /// Causes the wizad to move to the previous enabled step (or first step if <see cref="CurrentStep"/> is not set).
  312. /// If there is no previous step, does nothing.
  313. /// </summary>
  314. public void GoBack ()
  315. {
  316. var previous = GetPreviousStep ();
  317. if (previous != null) {
  318. GoToStep (previous);
  319. }
  320. }
  321. /// <summary>
  322. /// Returns the first enabled <see cref="WizardStep"/> before the current step. Takes into account steps which
  323. /// are disabled. If <see cref="CurrentStep"/> is `null` returns the last enabled step.
  324. /// </summary>
  325. /// <returns>The first step ahead of the current step, if there is one; otherwise returns `null`, which
  326. /// indicates either there are no enabled steps or the current step is the first enabled step.</returns>
  327. public WizardStep GetPreviousStep ()
  328. {
  329. LinkedListNode<WizardStep> step = null;
  330. if (CurrentStep == null) {
  331. // Get last step, assume it is previous
  332. step = steps.Last;
  333. } else {
  334. // Get the step before current
  335. step = steps.Find (CurrentStep);
  336. if (step != null) {
  337. step = step.Previous;
  338. }
  339. }
  340. // step now points to the potential previous step
  341. while (step != null) {
  342. if (step.Value.Enabled) {
  343. return step.Value;
  344. }
  345. step = step.Previous;
  346. }
  347. return null;
  348. }
  349. /// <summary>
  350. /// Returns the first enabled step in the Wizard
  351. /// </summary>
  352. /// <returns>The last enabled step</returns>
  353. public WizardStep GetFirstStep ()
  354. {
  355. return steps.FirstOrDefault (s => s.Enabled);
  356. }
  357. /// <summary>
  358. /// Returns the last enabled step in the Wizard
  359. /// </summary>
  360. /// <returns>The last enabled step</returns>
  361. public WizardStep GetLastStep ()
  362. {
  363. return steps.LastOrDefault (s => s.Enabled);
  364. }
  365. private LinkedList<WizardStep> steps = new LinkedList<WizardStep> ();
  366. private WizardStep currentStep = null;
  367. /// <summary>
  368. /// If the <see cref="CurrentStep"/> is not the first step in the wizard, this button causes
  369. /// the <see cref="MovingBack"/> event to be fired and the wizard moves to the previous step.
  370. /// </summary>
  371. /// <remarks>
  372. /// Use the <see cref="MovingBack"></see> event to be notified when the user attempts to go back.
  373. /// </remarks>
  374. public Button BackButton { get => backBtn; }
  375. private Button backBtn;
  376. /// <summary>
  377. /// If the <see cref="CurrentStep"/> is the last step in the wizard, this button causes
  378. /// the <see cref="Finished"/> event to be fired and the wizard to close. If the step is not the last step,
  379. /// the <see cref="MovingNext"/> event will be fired and the wizard will move next step.
  380. /// </summary>
  381. /// <remarks>
  382. /// Use the <see cref="MovingNext"></see> and <see cref="Finished"></see> events to be notified
  383. /// when the user attempts go to the next step or finish the wizard.
  384. /// </remarks>
  385. public Button NextFinishButton { get => nextfinishBtn; }
  386. private Button nextfinishBtn;
  387. /// <summary>
  388. /// Adds a step to the wizard. The Next and Back buttons navigate through the added steps in the
  389. /// order they were added.
  390. /// </summary>
  391. /// <param name="newStep"></param>
  392. /// <remarks>The "Next..." button of the last step added will read "Finish" (unless changed from default).</remarks>
  393. public void AddStep (WizardStep newStep)
  394. {
  395. steps.AddLast (newStep);
  396. this.Add (newStep);
  397. newStep.EnabledChanged += UpdateButtonsAndTitle;
  398. //newStep.TitleChanged += UpdateButtonsAndTitle;
  399. UpdateButtonsAndTitle ();
  400. }
  401. /// <summary>
  402. /// The title of the Wizard, shown at the top of the Wizard with " - currentStep.Title" appended.
  403. /// </summary>
  404. public new ustring Title {
  405. get {
  406. // The base (Dialog) Title holds the full title ("Wizard Title - Step Title")
  407. return base.Title;
  408. }
  409. set {
  410. wizardTitle = value;
  411. base.Title = $"{wizardTitle}{(steps.Count > 0 && currentStep != null ? " - " + currentStep.Title : string.Empty)}";
  412. }
  413. }
  414. private ustring wizardTitle = ustring.Empty;
  415. /// <summary>
  416. /// <see cref="EventArgs"/> for <see cref="WizardStep"/> transition events.
  417. /// </summary>
  418. public class WizardButtonEventArgs : EventArgs {
  419. /// <summary>
  420. /// Set to true to cancel the transition to the next step.
  421. /// </summary>
  422. public bool Cancel { get; set; }
  423. /// <summary>
  424. /// Initializes a new instance of <see cref="WizardButtonEventArgs"/>
  425. /// </summary>
  426. public WizardButtonEventArgs ()
  427. {
  428. Cancel = false;
  429. }
  430. }
  431. /// <summary>
  432. /// This event is raised when the Back button in the <see cref="Wizard"/> is clicked. The Back button is always
  433. /// the first button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any.
  434. /// </summary>
  435. public event Action<WizardButtonEventArgs> MovingBack;
  436. /// <summary>
  437. /// This event is raised when the Next/Finish button in the <see cref="Wizard"/> is clicked. The Next/Finish button is always
  438. /// the last button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any. This event is only
  439. /// raised if the <see cref="CurrentStep"/> is the last Step in the Wizard flow
  440. /// (otherwise the <see cref="Finished"/> event is raised).
  441. /// </summary>
  442. public event Action<WizardButtonEventArgs> MovingNext;
  443. /// <summary>
  444. /// This event is raised when the Next/Finish button in the <see cref="Wizard"/> is clicked. The Next/Finish button is always
  445. /// the last button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any. This event is only
  446. /// raised if the <see cref="CurrentStep"/> is the last Step in the Wizard flow
  447. /// (otherwise the <see cref="Finished"/> event is raised).
  448. /// </summary>
  449. public event Action<WizardButtonEventArgs> Finished;
  450. /// <summary>
  451. /// This event is raised when the user has cancelled the <see cref="Wizard"/> (with Ctrl-Q or ESC).
  452. /// </summary>
  453. public event Action<WizardButtonEventArgs> Cancelled;
  454. /// <summary>
  455. /// <see cref="EventArgs"/> for <see cref="WizardStep"/> events.
  456. /// </summary>
  457. public class StepChangeEventArgs : EventArgs {
  458. /// <summary>
  459. /// The current (or previous) <see cref="WizardStep"/>.
  460. /// </summary>
  461. public WizardStep OldStep { get; }
  462. /// <summary>
  463. /// The <see cref="WizardStep"/> the <see cref="Wizard"/> is changing to or has changed to.
  464. /// </summary>
  465. public WizardStep NewStep { get; }
  466. /// <summary>
  467. /// Event handlers can set to true before returning to cancel the step transition.
  468. /// </summary>
  469. public bool Cancel { get; set; }
  470. /// <summary>
  471. /// Initializes a new instance of <see cref="StepChangeEventArgs"/>
  472. /// </summary>
  473. /// <param name="oldStep">The current <see cref="WizardStep"/>.</param>
  474. /// <param name="newStep">The new <see cref="WizardStep"/>.</param>
  475. public StepChangeEventArgs (WizardStep oldStep, WizardStep newStep)
  476. {
  477. OldStep = oldStep;
  478. NewStep = newStep;
  479. Cancel = false;
  480. }
  481. }
  482. /// <summary>
  483. /// This event is raised when the current <see cref="CurrentStep"/>) is about to change. Use <see cref="StepChangeEventArgs.Cancel"/>
  484. /// to abort the transition.
  485. /// </summary>
  486. public event Action<StepChangeEventArgs> StepChanging;
  487. /// <summary>
  488. /// This event is raised after the <see cref="Wizard"/> has changed the <see cref="CurrentStep"/>.
  489. /// </summary>
  490. public event Action<StepChangeEventArgs> StepChanged;
  491. /// <summary>
  492. /// Gets or sets the currently active <see cref="WizardStep"/>.
  493. /// </summary>
  494. public WizardStep CurrentStep {
  495. get => currentStep;
  496. set {
  497. GoToStep (value);
  498. }
  499. }
  500. /// <summary>
  501. /// Called when the <see cref="Wizard"/> is about to transition to another <see cref="WizardStep"/>. Fires the <see cref="StepChanging"/> event.
  502. /// </summary>
  503. /// <param name="oldStep">The step the Wizard is about to change from</param>
  504. /// <param name="newStep">The step the Wizard is about to change to</param>
  505. /// <returns>True if the change is to be cancelled.</returns>
  506. public virtual bool OnStepChanging (WizardStep oldStep, WizardStep newStep)
  507. {
  508. var args = new StepChangeEventArgs (oldStep, newStep);
  509. StepChanging?.Invoke (args);
  510. return args.Cancel;
  511. }
  512. /// <summary>
  513. /// Called when the <see cref="Wizard"/> has completed transition to a new <see cref="WizardStep"/>. Fires the <see cref="StepChanged"/> event.
  514. /// </summary>
  515. /// <param name="oldStep">The step the Wizard changed from</param>
  516. /// <param name="newStep">The step the Wizard has changed to</param>
  517. /// <returns>True if the change is to be cancelled.</returns>
  518. public virtual bool OnStepChanged (WizardStep oldStep, WizardStep newStep)
  519. {
  520. var args = new StepChangeEventArgs (oldStep, newStep);
  521. StepChanged?.Invoke (args);
  522. return args.Cancel;
  523. }
  524. /// <summary>
  525. /// Changes to the specified <see cref="WizardStep"/>.
  526. /// </summary>
  527. /// <param name="newStep">The step to go to.</param>
  528. /// <returns>True if the transition to the step succeeded. False if the step was not found or the operation was cancelled.</returns>
  529. public bool GoToStep (WizardStep newStep)
  530. {
  531. if (OnStepChanging (currentStep, newStep) || (newStep != null && !newStep.Enabled)) {
  532. return false;
  533. }
  534. // Hide all but the new step
  535. foreach (WizardStep step in steps) {
  536. step.Visible = (step == newStep);
  537. }
  538. var oldStep = currentStep;
  539. currentStep = newStep;
  540. UpdateButtonsAndTitle ();
  541. // Set focus to the nav buttons
  542. if (backBtn.HasFocus) {
  543. backBtn.SetFocus ();
  544. } else {
  545. nextfinishBtn.SetFocus ();
  546. }
  547. if (OnStepChanged (oldStep, currentStep)) {
  548. // For correctness we do this, but it's meaningless because there's nothing to cancel
  549. return false;
  550. }
  551. return true;
  552. }
  553. private void UpdateButtonsAndTitle ()
  554. {
  555. if (CurrentStep == null) return;
  556. base.Title = $"{wizardTitle}{(steps.Count > 0 ? " - " + CurrentStep.Title : string.Empty)}";
  557. // Configure the Back button
  558. backBtn.Text = CurrentStep.BackButtonText != ustring.Empty ? CurrentStep.BackButtonText : Strings.wzBack; // "_Back";
  559. backBtn.Visible = (CurrentStep != GetFirstStep ());
  560. // Configure the Next/Finished button
  561. if (CurrentStep == GetLastStep ()) {
  562. nextfinishBtn.Text = CurrentStep.NextButtonText != ustring.Empty ? CurrentStep.NextButtonText : Strings.wzFinish; // "Fi_nish";
  563. } else {
  564. nextfinishBtn.Text = CurrentStep.NextButtonText != ustring.Empty ? CurrentStep.NextButtonText : Strings.wzNext; // "_Next...";
  565. }
  566. SetNeedsLayout ();
  567. LayoutSubviews ();
  568. Redraw (Bounds);
  569. }
  570. }
  571. }