Dialog.cs 2.5 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 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. 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. public override bool ProcessKey (KeyEvent kb)
  76. {
  77. switch (kb.Key) {
  78. case Key.Esc:
  79. Running = false;
  80. return true;
  81. }
  82. return base.ProcessKey (kb);
  83. }
  84. }
  85. }