Wizard.cs 26 KB

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