Wizard.cs 29 KB

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