Dialog.cs 2.7 KB

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