Dialog.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. 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 button to the dialog, its layout will be controled by the 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. public override void LayoutSubviews ()
  57. {
  58. base.LayoutSubviews ();
  59. int buttonSpace = 0;
  60. int maxHeight = 0;
  61. foreach (var b in buttons) {
  62. buttonSpace += b.Frame.Width + 1;
  63. maxHeight = Math.Max (maxHeight, b.Frame.Height);
  64. }
  65. const int borderWidth = 2;
  66. var start = (Frame.Width-borderWidth - buttonSpace) / 2;
  67. var y = Frame.Height - borderWidth - maxHeight-1-padding;
  68. foreach (var b in buttons) {
  69. var bf = b.Frame;
  70. b.Frame = new Rect (start, y, bf.Width, bf.Height);
  71. start += bf.Width + 1;
  72. }
  73. }
  74. public override bool ProcessKey (KeyEvent kb)
  75. {
  76. switch (kb.Key) {
  77. case Key.Esc:
  78. Running = false;
  79. return true;
  80. }
  81. return base.ProcessKey (kb);
  82. }
  83. }
  84. }