Dialog.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. public Dialog (string title, int width, int height, params Button [] buttons) : base (Application.MakeCenteredRect (new Size (width, height)), title)
  17. {
  18. foreach (var b in buttons) {
  19. this.buttons.Add (b);
  20. Add (b);
  21. }
  22. }
  23. public override void LayoutSubviews ()
  24. {
  25. base.LayoutSubviews ();
  26. int buttonSpace = 0;
  27. int maxHeight = 0;
  28. foreach (var b in buttons) {
  29. buttonSpace += b.Frame.Width + 1;
  30. maxHeight = Math.Max (maxHeight, b.Frame.Height);
  31. }
  32. const int borderWidth = 2;
  33. var start = (Frame.Width-borderWidth - buttonSpace) / 2;
  34. var y = Frame.Height - borderWidth - 2 - maxHeight;
  35. foreach (var b in buttons) {
  36. var bf = b.Frame;
  37. b.Frame = new Rect (start, y, bf.Width, bf.Height);
  38. start += bf.Width + 1;
  39. }
  40. }
  41. public override bool ProcessKey (KeyEvent kb)
  42. {
  43. switch (kb.Key) {
  44. case Key.Esc:
  45. Running = false;
  46. return true;
  47. }
  48. return base.ProcessKey (kb);
  49. }
  50. }
  51. }