Dialog.cs 2.0 KB

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