Wizard.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Terminal.Gui.Resources;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// Provides navigation and a user interface (UI) to collect related data across 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. /// The Wizard can be displayed either as a modal (pop-up) <see cref="Window"/> (like <see cref="Dialog"/>) or as an embedded <see cref="View"/>.
  14. ///
  15. /// By default, <see cref="Wizard.Modal"/> is <c>true</c>. In this case launch the Wizard with <c>Application.Run(wizard)</c>.
  16. ///
  17. /// See <see cref="Wizard.Modal"/> for more details.
  18. /// </remarks>
  19. /// <example>
  20. /// <code>
  21. /// using Terminal.Gui;
  22. /// using System.Text;
  23. ///
  24. /// Application.Init();
  25. ///
  26. /// var wizard = new Wizard ($"Setup Wizard");
  27. ///
  28. /// // Add 1st step
  29. /// var firstStep = new Wizard.WizardStep ("End User License Agreement");
  30. /// wizard.AddStep(firstStep);
  31. /// firstStep.NextButtonText = "Accept!";
  32. /// firstStep.HelpText = "This is the End User License Agreement.";
  33. ///
  34. /// // Add 2nd step
  35. /// var secondStep = new Wizard.WizardStep ("Second Step");
  36. /// wizard.AddStep(secondStep);
  37. /// secondStep.HelpText = "This is the help text for the Second Step.";
  38. /// var lbl = new Label ("Name:") { AutoSize = true };
  39. /// secondStep.Add(lbl);
  40. ///
  41. /// var name = new TextField () { X = Pos.Right (lbl) + 1, Width = Dim.Fill () - 1 };
  42. /// secondStep.Add(name);
  43. ///
  44. /// wizard.Finished += (args) =>
  45. /// {
  46. /// MessageBox.Query("Wizard", $"Finished. The Name entered is '{name.Text}'", "Ok");
  47. /// Application.RequestStop();
  48. /// };
  49. ///
  50. /// Application.Top.Add (wizard);
  51. /// Application.Run ();
  52. /// Application.Shutdown ();
  53. /// </code>
  54. /// </example>
  55. public class Wizard : Dialog {
  56. /// <summary>
  57. /// Represents a basic step that is displayed in a <see cref="Wizard"/>. The <see cref="WizardStep"/> view is divided horizontally in two. On the left is the
  58. /// content view where <see cref="View"/>s can be added, On the right is the help for the step.
  59. /// Set <see cref="WizardStep.HelpText"/> to set the help text. If the help text is empty the help pane will not
  60. /// be shown.
  61. ///
  62. /// If there are no Views added to the WizardStep the <see cref="HelpText"/> (if not empty) will fill the wizard step.
  63. /// </summary>
  64. /// <remarks>
  65. /// If <see cref="Button"/>s are added, do not set <see cref="Button.IsDefault"/> to true as this will conflict
  66. /// with the Next button of the Wizard.
  67. ///
  68. /// Subscribe to the <see cref="View.VisibleChanged"/> event to be notified when the step is active; see also: <see cref="Wizard.StepChanged"/>.
  69. ///
  70. /// To enable or disable a step from being shown to the user, set <see cref="View.Enabled"/>.
  71. ///
  72. /// </remarks>
  73. public class WizardStep : FrameView {
  74. ///// <summary>
  75. ///// The title of the <see cref="WizardStep"/>.
  76. ///// </summary>
  77. ///// <remarks>The Title is only displayed when the <see cref="Wizard"/> is used as a modal pop-up (see <see cref="Wizard.Modal"/>.</remarks>
  78. //public new string Title {
  79. // // BUGBUG: v2 - No need for this as View now has Title w/ notifications.
  80. // get => title;
  81. // set {
  82. // if (!OnTitleChanging (title, value)) {
  83. // var old = title;
  84. // title = value;
  85. // OnTitleChanged (old, title);
  86. // }
  87. // base.Title = value;
  88. // SetNeedsDisplay ();
  89. // }
  90. //}
  91. //private string title = string.Empty;
  92. // The contentView works like the ContentView in FrameView.
  93. private View contentView = new View () { Data = "WizardContentView" };
  94. /// <summary>
  95. /// Sets or gets help text for the <see cref="WizardStep"/>.If <see cref="WizardStep.HelpText"/> is empty
  96. /// the help pane will not be visible and the content will fill the entire WizardStep.
  97. /// </summary>
  98. /// <remarks>The help text is displayed using a read-only <see cref="TextView"/>.</remarks>
  99. public string HelpText {
  100. get => helpTextView.Text;
  101. set {
  102. helpTextView.Text = value;
  103. ShowHide ();
  104. SetNeedsDisplay ();
  105. }
  106. }
  107. private TextView helpTextView = new TextView ();
  108. /// <summary>
  109. /// Sets or gets the text for the back button. The back button will only be visible on
  110. /// steps after the first step.
  111. /// </summary>
  112. /// <remarks>The default text is "Back"</remarks>
  113. public string BackButtonText { get; set; } = string.Empty;
  114. /// <summary>
  115. /// Sets or gets the text for the next/finish button.
  116. /// </summary>
  117. /// <remarks>The default text is "Next..." if the Pane is not the last pane. Otherwise it is "Finish"</remarks>
  118. public string NextButtonText { get; set; } = string.Empty;
  119. /// <summary>
  120. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  121. /// </summary>
  122. public WizardStep ()
  123. {
  124. BorderStyle = LineStyle.None;
  125. base.Add (contentView);
  126. helpTextView.ReadOnly = true;
  127. helpTextView.WordWrap = true;
  128. base.Add (helpTextView);
  129. // BUGBUG: v2 - Disabling scrolling for now
  130. //var scrollBar = new ScrollBarView (helpTextView, true);
  131. //scrollBar.ChangedPosition += (s,e) => {
  132. // helpTextView.TopRow = scrollBar.Position;
  133. // if (helpTextView.TopRow != scrollBar.Position) {
  134. // scrollBar.Position = helpTextView.TopRow;
  135. // }
  136. // helpTextView.SetNeedsDisplay ();
  137. //};
  138. //scrollBar.OtherScrollBarView.ChangedPosition += (s,e) => {
  139. // helpTextView.LeftColumn = scrollBar.OtherScrollBarView.Position;
  140. // if (helpTextView.LeftColumn != scrollBar.OtherScrollBarView.Position) {
  141. // scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  142. // }
  143. // helpTextView.SetNeedsDisplay ();
  144. //};
  145. //scrollBar.VisibleChanged += (s,e) => {
  146. // if (scrollBar.Visible && helpTextView.RightOffset == 0) {
  147. // helpTextView.RightOffset = 1;
  148. // } else if (!scrollBar.Visible && helpTextView.RightOffset == 1) {
  149. // helpTextView.RightOffset = 0;
  150. // }
  151. //};
  152. //scrollBar.OtherScrollBarView.VisibleChanged += (s,e) => {
  153. // if (scrollBar.OtherScrollBarView.Visible && helpTextView.BottomOffset == 0) {
  154. // helpTextView.BottomOffset = 1;
  155. // } else if (!scrollBar.OtherScrollBarView.Visible && helpTextView.BottomOffset == 1) {
  156. // helpTextView.BottomOffset = 0;
  157. // }
  158. //};
  159. //helpTextView.DrawContent += (s,e) => {
  160. // scrollBar.Size = helpTextView.Lines;
  161. // scrollBar.Position = helpTextView.TopRow;
  162. // if (scrollBar.OtherScrollBarView != null) {
  163. // scrollBar.OtherScrollBarView.Size = helpTextView.Maxlength;
  164. // scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  165. // }
  166. // scrollBar.LayoutSubviews ();
  167. // scrollBar.Refresh ();
  168. //};
  169. //base.Add (scrollBar);
  170. ShowHide ();
  171. }
  172. /// <summary>
  173. /// Does the work to show and hide the contentView and helpView as appropriate
  174. /// </summary>
  175. internal void ShowHide ()
  176. {
  177. contentView.Height = Dim.Fill ();
  178. helpTextView.Height = Dim.Fill ();
  179. helpTextView.Width = Dim.Fill ();
  180. if (contentView.InternalSubviews?.Count > 0) {
  181. if (helpTextView.Text.Length > 0) {
  182. contentView.Width = Dim.Percent (70);
  183. helpTextView.X = Pos.Right (contentView);
  184. helpTextView.Width = Dim.Fill ();
  185. } else {
  186. contentView.Width = Dim.Fill ();
  187. }
  188. } else {
  189. if (helpTextView.Text.Length > 0) {
  190. helpTextView.X = 0;
  191. } else {
  192. // Error - no pane shown
  193. }
  194. }
  195. contentView.Visible = contentView.InternalSubviews?.Count > 0;
  196. helpTextView.Visible = helpTextView.Text.Length > 0;
  197. }
  198. /// <summary>
  199. /// Add the specified <see cref="View"/> to the <see cref="WizardStep"/>.
  200. /// </summary>
  201. /// <param name="view"><see cref="View"/> to add to this container</param>
  202. public override void Add (View view)
  203. {
  204. contentView.Add (view);
  205. if (view.CanFocus) {
  206. CanFocus = true;
  207. }
  208. ShowHide ();
  209. }
  210. /// <summary>
  211. /// Removes a <see cref="View"/> from <see cref="WizardStep"/>.
  212. /// </summary>
  213. /// <remarks>
  214. /// </remarks>
  215. public override void Remove (View view)
  216. {
  217. if (view == null) {
  218. return;
  219. }
  220. SetNeedsDisplay ();
  221. var touched = view.Frame;
  222. contentView.Remove (view);
  223. if (contentView.InternalSubviews.Count < 1) {
  224. this.CanFocus = false;
  225. }
  226. ShowHide ();
  227. }
  228. /// <summary>
  229. /// Removes all <see cref="View"/>s from the <see cref="WizardStep"/>.
  230. /// </summary>
  231. /// <remarks>
  232. /// </remarks>
  233. public override void RemoveAll ()
  234. {
  235. contentView.RemoveAll ();
  236. ShowHide ();
  237. }
  238. } // end of WizardStep class
  239. /// <summary>
  240. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  241. /// </summary>
  242. /// <remarks>
  243. /// The Wizard will be vertically and horizontally centered in the container.
  244. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> change size and position.
  245. /// </remarks>
  246. public Wizard () : base ()
  247. {
  248. // Using Justify causes the Back and Next buttons to be hard justified against
  249. // the left and right edge
  250. ButtonAlignment = ButtonAlignments.Justify;
  251. BorderStyle = LineStyle.Double;
  252. //// Add a horiz separator
  253. var separator = new LineView (Orientation.Horizontal) {
  254. Y = Pos.AnchorEnd (2)
  255. };
  256. Add (separator);
  257. // BUGBUG: Space is to work around https://github.com/gui-cs/Terminal.Gui/issues/1812
  258. backBtn = new Button (Strings.wzBack) { AutoSize = true };
  259. AddButton (backBtn);
  260. nextfinishBtn = new Button (Strings.wzFinish) { AutoSize = true };
  261. nextfinishBtn.IsDefault = true;
  262. AddButton (nextfinishBtn);
  263. backBtn.Clicked += BackBtn_Clicked;
  264. nextfinishBtn.Clicked += NextfinishBtn_Clicked;
  265. Loaded += Wizard_Loaded;
  266. Closing += Wizard_Closing;
  267. TitleChanged += Wizard_TitleChanged;
  268. if (Modal) {
  269. ClearKeyBinding (Command.QuitToplevel);
  270. AddKeyBinding (Key.Esc, Command.QuitToplevel);
  271. }
  272. SetNeedsLayout ();
  273. }
  274. private void Wizard_TitleChanged (object sender, TitleEventArgs e)
  275. {
  276. if (string.IsNullOrEmpty (wizardTitle)) {
  277. wizardTitle = e.NewTitle;
  278. }
  279. }
  280. private void Wizard_Loaded (object sender, EventArgs args)
  281. {
  282. CurrentStep = GetFirstStep (); // gets the first step if CurrentStep == null
  283. }
  284. private bool finishedPressed = false;
  285. private void Wizard_Closing (object sender, ToplevelClosingEventArgs obj)
  286. {
  287. if (!finishedPressed) {
  288. var args = new WizardButtonEventArgs ();
  289. Cancelled?.Invoke (this, args);
  290. }
  291. }
  292. private void NextfinishBtn_Clicked (object sender, EventArgs e)
  293. {
  294. if (CurrentStep == GetLastStep ()) {
  295. var args = new WizardButtonEventArgs ();
  296. Finished?.Invoke (this, args);
  297. if (!args.Cancel) {
  298. finishedPressed = true;
  299. if (IsCurrentTop) {
  300. Application.RequestStop (this);
  301. } else {
  302. // Wizard was created as a non-modal (just added to another View).
  303. // Do nothing
  304. }
  305. }
  306. } else {
  307. var args = new WizardButtonEventArgs ();
  308. MovingNext?.Invoke (this, args);
  309. if (!args.Cancel) {
  310. GoNext ();
  311. }
  312. }
  313. }
  314. /// <summary>
  315. /// <see cref="Wizard"/> is derived from <see cref="Dialog"/> and Dialog causes <c>Esc</c> to call
  316. /// <see cref="Application.RequestStop(Toplevel)"/>, closing the Dialog. Wizard overrides <see cref="Responder.ProcessKey(KeyEvent)"/>
  317. /// to instead fire the <see cref="Cancelled"/> event when Wizard is being used as a non-modal (see <see cref="Wizard.Modal"/>.
  318. /// See <see cref="Responder.ProcessKey(KeyEvent)"/> for more.
  319. /// </summary>
  320. /// <param name="kb"></param>
  321. /// <returns></returns>
  322. public override bool ProcessKey (KeyEvent kb)
  323. {
  324. if (!Modal) {
  325. switch (kb.Key) {
  326. case Key.Esc:
  327. var args = new WizardButtonEventArgs ();
  328. Cancelled?.Invoke (this, args);
  329. return false;
  330. }
  331. }
  332. return base.ProcessKey (kb);
  333. }
  334. /// <summary>
  335. /// Causes the wizad to move to the next enabled step (or last step if <see cref="CurrentStep"/> is not set).
  336. /// If there is no previous step, does nothing.
  337. /// </summary>
  338. public void GoNext ()
  339. {
  340. var nextStep = GetNextStep ();
  341. if (nextStep != null) {
  342. GoToStep (nextStep);
  343. }
  344. }
  345. /// <summary>
  346. /// Returns the next enabled <see cref="WizardStep"/> after the current step. Takes into account steps which
  347. /// are disabled. If <see cref="CurrentStep"/> is <c>null</c> returns the first enabled step.
  348. /// </summary>
  349. /// <returns>The next step after the current step, if there is one; otherwise returns <c>null</c>, which
  350. /// indicates either there are no enabled steps or the current step is the last enabled step.</returns>
  351. public WizardStep GetNextStep ()
  352. {
  353. LinkedListNode<WizardStep> step = null;
  354. if (CurrentStep == null) {
  355. // Get first step, assume it is next
  356. step = steps.First;
  357. } else {
  358. // Get the step after current
  359. step = steps.Find (CurrentStep);
  360. if (step != null) {
  361. step = step.Next;
  362. }
  363. }
  364. // step now points to the potential next step
  365. while (step != null) {
  366. if (step.Value.Enabled) {
  367. return step.Value;
  368. }
  369. step = step.Next;
  370. }
  371. return null;
  372. }
  373. private void BackBtn_Clicked (object sender, EventArgs e)
  374. {
  375. var args = new WizardButtonEventArgs ();
  376. MovingBack?.Invoke (this, args);
  377. if (!args.Cancel) {
  378. GoBack ();
  379. }
  380. }
  381. /// <summary>
  382. /// Causes the wizad to move to the previous enabled step (or first step if <see cref="CurrentStep"/> is not set).
  383. /// If there is no previous step, does nothing.
  384. /// </summary>
  385. public void GoBack ()
  386. {
  387. var previous = GetPreviousStep ();
  388. if (previous != null) {
  389. GoToStep (previous);
  390. }
  391. }
  392. /// <summary>
  393. /// Returns the first enabled <see cref="WizardStep"/> before the current step. Takes into account steps which
  394. /// are disabled. If <see cref="CurrentStep"/> is <c>null</c> returns the last enabled step.
  395. /// </summary>
  396. /// <returns>The first step ahead of the current step, if there is one; otherwise returns <c>null</c>, which
  397. /// indicates either there are no enabled steps or the current step is the first enabled step.</returns>
  398. public WizardStep GetPreviousStep ()
  399. {
  400. LinkedListNode<WizardStep> step = null;
  401. if (CurrentStep == null) {
  402. // Get last step, assume it is previous
  403. step = steps.Last;
  404. } else {
  405. // Get the step before current
  406. step = steps.Find (CurrentStep);
  407. if (step != null) {
  408. step = step.Previous;
  409. }
  410. }
  411. // step now points to the potential previous step
  412. while (step != null) {
  413. if (step.Value.Enabled) {
  414. return step.Value;
  415. }
  416. step = step.Previous;
  417. }
  418. return null;
  419. }
  420. /// <summary>
  421. /// Returns the first enabled step in the Wizard
  422. /// </summary>
  423. /// <returns>The last enabled step</returns>
  424. public WizardStep GetFirstStep ()
  425. {
  426. return steps.FirstOrDefault (s => s.Enabled);
  427. }
  428. /// <summary>
  429. /// Returns the last enabled step in the Wizard
  430. /// </summary>
  431. /// <returns>The last enabled step</returns>
  432. public WizardStep GetLastStep ()
  433. {
  434. return steps.LastOrDefault (s => s.Enabled);
  435. }
  436. private LinkedList<WizardStep> steps = new LinkedList<WizardStep> ();
  437. private WizardStep currentStep = null;
  438. /// <summary>
  439. /// If the <see cref="CurrentStep"/> is not the first step in the wizard, this button causes
  440. /// the <see cref="MovingBack"/> event to be fired and the wizard moves to the previous step.
  441. /// </summary>
  442. /// <remarks>
  443. /// Use the <see cref="MovingBack"></see> event to be notified when the user attempts to go back.
  444. /// </remarks>
  445. public Button BackButton { get => backBtn; }
  446. private Button backBtn;
  447. /// <summary>
  448. /// If the <see cref="CurrentStep"/> is the last step in the wizard, this button causes
  449. /// the <see cref="Finished"/> event to be fired and the wizard to close. If the step is not the last step,
  450. /// the <see cref="MovingNext"/> event will be fired and the wizard will move next step.
  451. /// </summary>
  452. /// <remarks>
  453. /// Use the <see cref="MovingNext"></see> and <see cref="Finished"></see> events to be notified
  454. /// when the user attempts go to the next step or finish the wizard.
  455. /// </remarks>
  456. public Button NextFinishButton { get => nextfinishBtn; }
  457. private Button nextfinishBtn;
  458. /// <summary>
  459. /// Adds a step to the wizard. The Next and Back buttons navigate through the added steps in the
  460. /// order they were added.
  461. /// </summary>
  462. /// <param name="newStep"></param>
  463. /// <remarks>The "Next..." button of the last step added will read "Finish" (unless changed from default).</remarks>
  464. public void AddStep (WizardStep newStep)
  465. {
  466. SizeStep (newStep);
  467. newStep.EnabledChanged += (s, e) => UpdateButtonsAndTitle ();
  468. newStep.TitleChanged += (s, e) => UpdateButtonsAndTitle ();
  469. steps.AddLast (newStep);
  470. this.Add (newStep);
  471. UpdateButtonsAndTitle ();
  472. }
  473. ///// <summary>
  474. ///// The title of the Wizard, shown at the top of the Wizard with " - currentStep.Title" appended.
  475. ///// </summary>
  476. ///// <remarks>
  477. ///// The Title is only displayed when the <see cref="Wizard"/> <see cref="Wizard.Modal"/> is set to <c>false</c>.
  478. ///// </remarks>
  479. //public new string Title {
  480. // get {
  481. // // The base (Dialog) Title holds the full title ("Wizard Title - Step Title")
  482. // return base.Title;
  483. // }
  484. // set {
  485. // wizardTitle = value;
  486. // base.Title = $"{wizardTitle}{(steps.Count > 0 && currentStep != null ? " - " + currentStep.Title : string.Empty)}";
  487. // }
  488. //}
  489. private string wizardTitle = string.Empty;
  490. /// <summary>
  491. /// Raised when the Back button in the <see cref="Wizard"/> is clicked. The Back button is always
  492. /// the first button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any.
  493. /// </summary>
  494. public event EventHandler<WizardButtonEventArgs> MovingBack;
  495. /// <summary>
  496. /// Raised when the Next/Finish button in the <see cref="Wizard"/> is clicked (or the user presses Enter).
  497. /// The Next/Finish button is always the last button in the array of Buttons passed to the <see cref="Wizard"/> constructor,
  498. /// if any. This event is only raised if the <see cref="CurrentStep"/> is the last Step in the Wizard flow
  499. /// (otherwise the <see cref="Finished"/> event is raised).
  500. /// </summary>
  501. public event EventHandler<WizardButtonEventArgs> MovingNext;
  502. /// <summary>
  503. /// Raised when the Next/Finish button in the <see cref="Wizard"/> is clicked. The Next/Finish button is always
  504. /// the last button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any. This event is only
  505. /// raised if the <see cref="CurrentStep"/> is the last Step in the Wizard flow
  506. /// (otherwise the <see cref="Finished"/> event is raised).
  507. /// </summary>
  508. public event EventHandler<WizardButtonEventArgs> Finished;
  509. /// <summary>
  510. /// Raised when the user has cancelled the <see cref="Wizard"/> by pressin the Esc key.
  511. /// To prevent a modal (<see cref="Wizard.Modal"/> is <c>true</c>) Wizard from
  512. /// closing, cancel the event by setting <see cref="WizardButtonEventArgs.Cancel"/> to
  513. /// <c>true</c> before returning from the event handler.
  514. /// </summary>
  515. public event EventHandler<WizardButtonEventArgs> Cancelled;
  516. /// <summary>
  517. /// This event is raised when the current <see cref="CurrentStep"/>) is about to change. Use <see cref="StepChangeEventArgs.Cancel"/>
  518. /// to abort the transition.
  519. /// </summary>
  520. public event EventHandler<StepChangeEventArgs> StepChanging;
  521. /// <summary>
  522. /// This event is raised after the <see cref="Wizard"/> has changed the <see cref="CurrentStep"/>.
  523. /// </summary>
  524. public event EventHandler<StepChangeEventArgs> StepChanged;
  525. /// <summary>
  526. /// Gets or sets the currently active <see cref="WizardStep"/>.
  527. /// </summary>
  528. public WizardStep CurrentStep {
  529. get => currentStep;
  530. set {
  531. GoToStep (value);
  532. }
  533. }
  534. /// <summary>
  535. /// Called when the <see cref="Wizard"/> is about to transition to another <see cref="WizardStep"/>. Fires the <see cref="StepChanging"/> event.
  536. /// </summary>
  537. /// <param name="oldStep">The step the Wizard is about to change from</param>
  538. /// <param name="newStep">The step the Wizard is about to change to</param>
  539. /// <returns>True if the change is to be cancelled.</returns>
  540. public virtual bool OnStepChanging (WizardStep oldStep, WizardStep newStep)
  541. {
  542. var args = new StepChangeEventArgs (oldStep, newStep);
  543. StepChanging?.Invoke (this, args);
  544. return args.Cancel;
  545. }
  546. /// <summary>
  547. /// Called when the <see cref="Wizard"/> has completed transition to a new <see cref="WizardStep"/>. Fires the <see cref="StepChanged"/> event.
  548. /// </summary>
  549. /// <param name="oldStep">The step the Wizard changed from</param>
  550. /// <param name="newStep">The step the Wizard has changed to</param>
  551. /// <returns>True if the change is to be cancelled.</returns>
  552. public virtual bool OnStepChanged (WizardStep oldStep, WizardStep newStep)
  553. {
  554. var args = new StepChangeEventArgs (oldStep, newStep);
  555. StepChanged?.Invoke (this, args);
  556. return args.Cancel;
  557. }
  558. /// <summary>
  559. /// Changes to the specified <see cref="WizardStep"/>.
  560. /// </summary>
  561. /// <param name="newStep">The step to go to.</param>
  562. /// <returns>True if the transition to the step succeeded. False if the step was not found or the operation was cancelled.</returns>
  563. public bool GoToStep (WizardStep newStep)
  564. {
  565. if (OnStepChanging (currentStep, newStep) || (newStep != null && !newStep.Enabled)) {
  566. return false;
  567. }
  568. // Hide all but the new step
  569. foreach (WizardStep step in steps) {
  570. step.Visible = (step == newStep);
  571. step.ShowHide ();
  572. }
  573. var oldStep = currentStep;
  574. currentStep = newStep;
  575. UpdateButtonsAndTitle ();
  576. // Set focus to the nav buttons
  577. if (backBtn.HasFocus) {
  578. backBtn.SetFocus ();
  579. } else {
  580. nextfinishBtn.SetFocus ();
  581. }
  582. if (OnStepChanged (oldStep, currentStep)) {
  583. // For correctness we do this, but it's meaningless because there's nothing to cancel
  584. return false;
  585. }
  586. return true;
  587. }
  588. private void UpdateButtonsAndTitle ()
  589. {
  590. if (CurrentStep == null) return;
  591. Title = $"{wizardTitle}{(steps.Count > 0 ? " - " + CurrentStep.Title : string.Empty)}";
  592. // Configure the Back button
  593. backBtn.Text = CurrentStep.BackButtonText != string.Empty ? CurrentStep.BackButtonText : Strings.wzBack; // "_Back";
  594. backBtn.Visible = (CurrentStep != GetFirstStep ());
  595. // Configure the Next/Finished button
  596. if (CurrentStep == GetLastStep ()) {
  597. nextfinishBtn.Text = CurrentStep.NextButtonText != string.Empty ? CurrentStep.NextButtonText : Strings.wzFinish; // "Fi_nish";
  598. } else {
  599. nextfinishBtn.Text = CurrentStep.NextButtonText != string.Empty ? CurrentStep.NextButtonText : Strings.wzNext; // "_Next...";
  600. }
  601. SizeStep (CurrentStep);
  602. SetNeedsLayout ();
  603. LayoutSubviews ();
  604. Draw ();
  605. }
  606. private void SizeStep (WizardStep step)
  607. {
  608. if (Modal) {
  609. // If we're modal, then we expand the WizardStep so that the top and side
  610. // borders and not visible. The bottom border is the separator above the buttons.
  611. step.X = step.Y = 0;
  612. step.Height = Dim.Fill (2); // for button frame
  613. step.Width = Dim.Fill (0);
  614. } else {
  615. // If we're not a modal, then we show the border around the WizardStep
  616. step.X = step.Y = 0;
  617. step.Height = Dim.Fill (1); // for button frame
  618. step.Width = Dim.Fill (0);
  619. }
  620. }
  621. /// <summary>
  622. /// Determines whether the <see cref="Wizard"/> is displayed as modal pop-up or not.
  623. ///
  624. /// The default is <see langword="true"/>. The Wizard will be shown with a frame and title and will behave like
  625. /// any <see cref="Toplevel"/> window.
  626. ///
  627. /// If set to <c>false</c> the Wizard will have no frame and will behave like any embedded <see cref="View"/>.
  628. ///
  629. /// To use Wizard as an embedded View
  630. /// <list type="number">
  631. /// <item><description>Set <see cref="Modal"/> to <c>false</c>.</description></item>
  632. /// <item><description>Add the Wizard to a containing view with <see cref="View.Add(View)"/>.</description></item>
  633. /// </list>
  634. ///
  635. /// If a non-Modal Wizard is added to the application after <see cref="Application.Run(Func{Exception, bool})"/> has been called
  636. /// the first step must be explicitly set by setting <see cref="CurrentStep"/> to <see cref="GetNextStep()"/>:
  637. /// <code>
  638. /// wizard.CurrentStep = wizard.GetNextStep();
  639. /// </code>
  640. /// </summary>
  641. public new bool Modal {
  642. get => base.Modal;
  643. set {
  644. base.Modal = value;
  645. foreach (var step in steps) {
  646. SizeStep (step);
  647. }
  648. if (base.Modal) {
  649. ColorScheme = Colors.Dialog;
  650. BorderStyle = LineStyle.Rounded;
  651. } else {
  652. if (SuperView != null) {
  653. ColorScheme = SuperView.ColorScheme;
  654. } else {
  655. ColorScheme = Colors.Base;
  656. }
  657. CanFocus = true;
  658. BorderStyle = LineStyle.None;
  659. }
  660. }
  661. }
  662. }
  663. }