Wizard.cs 29 KB

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