Dialog.cs 2.1 KB

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