Wizard.cs 25 KB

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