Dialog.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Dialog.cs: Dialog box
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using NStack;
  10. namespace Terminal.Gui {
  11. /// <summary>
  12. /// The <see cref="Dialog"/> <see cref="View"/> is a <see cref="Window"/> that by default is centered and contains one
  13. /// or more <see cref="Button"/>. It defaults to the <see cref="Colors.Dialog"/> color scheme and has a 1 cell padding around the edges.
  14. /// </summary>
  15. /// <remarks>
  16. /// To run the <see cref="Dialog"/> modally, create the <see cref="Dialog"/>, and pass it to <see cref="Application.Run()"/>.
  17. /// This will execute the dialog until it terminates via the [ESC] or [CTRL-Q] key, or when one of the views
  18. /// or buttons added to the dialog calls <see cref="Application.RequestStop"/>.
  19. /// </remarks>
  20. public class Dialog : Window {
  21. List<Button> buttons = new List<Button> ();
  22. const int padding = 1;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="Dialog"/> class with an optional set of <see cref="Button"/>s to display
  25. /// </summary>
  26. /// <param name="title">Title for the dialog.</param>
  27. /// <param name="width">Width for the dialog.</param>
  28. /// <param name="height">Height for the dialog.</param>
  29. /// <param name="buttons">Optional buttons to lay out at the bottom of the dialog.</param>
  30. public Dialog (ustring title, int width, int height, params Button [] buttons) : base (title, padding: padding)
  31. {
  32. X = Pos.Center ();
  33. Y = Pos.Center ();
  34. Width = width;
  35. Height = height;
  36. ColorScheme = Colors.Dialog;
  37. Modal = true;
  38. if (buttons != null) {
  39. foreach (var b in buttons) {
  40. this.buttons.Add (b);
  41. Add (b);
  42. }
  43. }
  44. }
  45. /// <summary>
  46. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controled by the <see cref="Dialog"/>
  47. /// </summary>
  48. /// <param name="button">Button to add.</param>
  49. public void AddButton (Button button)
  50. {
  51. if (button == null)
  52. return;
  53. buttons.Add (button);
  54. Add (button);
  55. }
  56. ///<inheritdoc cref="LayoutSubviews"/>
  57. public override void LayoutSubviews ()
  58. {
  59. base.LayoutSubviews ();
  60. int buttonSpace = 0;
  61. int maxHeight = 0;
  62. foreach (var b in buttons) {
  63. buttonSpace += b.Frame.Width + 1;
  64. maxHeight = Math.Max (maxHeight, b.Frame.Height);
  65. }
  66. const int borderWidth = 2;
  67. var start = (Frame.Width-borderWidth - buttonSpace) / 2;
  68. var y = Frame.Height - borderWidth - maxHeight-1-padding;
  69. foreach (var b in buttons) {
  70. var bf = b.Frame;
  71. b.Frame = new Rect (start, y, bf.Width, bf.Height);
  72. start += bf.Width + 1;
  73. }
  74. }
  75. ///<inheritdoc cref="ProcessKey"/>
  76. public override bool ProcessKey (KeyEvent kb)
  77. {
  78. switch (kb.Key) {
  79. case Key.Esc:
  80. Running = false;
  81. return true;
  82. }
  83. return base.ProcessKey (kb);
  84. }
  85. }
  86. }