Wizard.cs 25 KB

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