2
0

Dialog.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 (Application.MakeCenteredRect (new Size (width, height)), title, padding: padding)
  26. {
  27. ColorScheme = Colors.Dialog;
  28. if (buttons != null) {
  29. foreach (var b in buttons) {
  30. this.buttons.Add (b);
  31. Add (b);
  32. }
  33. }
  34. }
  35. /// <summary>
  36. /// Adds a button to the dialog, its layout will be controled by the dialog
  37. /// </summary>
  38. /// <param name="button">Button to add.</param>
  39. public void AddButton (Button button)
  40. {
  41. if (button == null)
  42. return;
  43. buttons.Add (button);
  44. Add (button);
  45. }
  46. public override void LayoutSubviews ()
  47. {
  48. base.LayoutSubviews ();
  49. int buttonSpace = 0;
  50. int maxHeight = 0;
  51. foreach (var b in buttons) {
  52. buttonSpace += b.Frame.Width + 1;
  53. maxHeight = Math.Max (maxHeight, b.Frame.Height);
  54. }
  55. const int borderWidth = 2;
  56. var start = (Frame.Width-borderWidth - buttonSpace) / 2;
  57. var y = Frame.Height - borderWidth - maxHeight-1-padding;
  58. foreach (var b in buttons) {
  59. var bf = b.Frame;
  60. b.Frame = new Rect (start, y, bf.Width, bf.Height);
  61. start += bf.Width + 1;
  62. }
  63. }
  64. public override bool ProcessKey (KeyEvent kb)
  65. {
  66. switch (kb.Key) {
  67. case Key.Esc:
  68. Running = false;
  69. return true;
  70. }
  71. return base.ProcessKey (kb);
  72. }
  73. }
  74. }