Wizard.cs 29 KB

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