Wizard.cs 28 KB

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