Wizard.cs 25 KB

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