Dialog.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.
  14. /// </summary>
  15. public class Dialog : Window {
  16. List<Button> buttons = new List<Button> ();
  17. const int padding = 1;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="T:Terminal.Gui.Dialog"/> class with an optional set of buttons to display
  20. /// </summary>
  21. /// <param name="title">Title for the dialog.</param>
  22. /// <param name="width">Width for the dialog.</param>
  23. /// <param name="height">Height for the dialog.</param>
  24. /// <param name="buttons">Optional buttons to lay out at the bottom of the dialog.</param>
  25. public Dialog (ustring title, int width, int height, params Button [] buttons) : base (title, padding: padding)
  26. {
  27. X = Pos.Center ();
  28. Y = Pos.Center ();
  29. Width = width;
  30. Height = height;
  31. ColorScheme = Colors.Dialog;
  32. if (buttons != null) {
  33. foreach (var b in buttons) {
  34. this.buttons.Add (b);
  35. Add (b);
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// Adds a button to the dialog, its layout will be controled by the dialog
  41. /// </summary>
  42. /// <param name="button">Button to add.</param>
  43. public void AddButton (Button button)
  44. {
  45. if (button == null)
  46. return;
  47. buttons.Add (button);
  48. Add (button);
  49. }
  50. public override void LayoutSubviews ()
  51. {
  52. base.LayoutSubviews ();
  53. int buttonSpace = 0;
  54. int maxHeight = 0;
  55. foreach (var b in buttons) {
  56. buttonSpace += b.Frame.Width + 1;
  57. maxHeight = Math.Max (maxHeight, b.Frame.Height);
  58. }
  59. const int borderWidth = 2;
  60. var start = (Frame.Width-borderWidth - buttonSpace) / 2;
  61. var y = Frame.Height - borderWidth - maxHeight-1-padding;
  62. foreach (var b in buttons) {
  63. var bf = b.Frame;
  64. b.Frame = new Rect (start, y, bf.Width, bf.Height);
  65. start += bf.Width + 1;
  66. }
  67. }
  68. public override bool ProcessKey (KeyEvent kb)
  69. {
  70. switch (kb.Key) {
  71. case Key.Esc:
  72. Running = false;
  73. return true;
  74. }
  75. return base.ProcessKey (kb);
  76. }
  77. }
  78. }