Dialog.cs 2.1 KB

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