MessageBox.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. namespace Terminal {
  3. /// <summary>
  4. /// Message box displays a modal message to the user, with a title, a message and a series of options that the user can choose from.
  5. /// </summary>
  6. public class MessageBox {
  7. /// <summary>
  8. /// Runs the dialog bo
  9. /// </summary>
  10. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  11. /// <param name="width">Width for the window.</param>
  12. /// <param name="height">Height for the window.</param>
  13. /// <param name="title">Title for the query.</param>
  14. /// <param name="message">Message to display, might contain multiple lines..</param>
  15. /// <param name="buttons">Array of buttons to add.</param>
  16. public static int Query (int width, int height, string title, string message, params string [] buttons)
  17. {
  18. int lines = Label.MeasureLines (message, width);
  19. int clicked = -1, count = 0;
  20. var d = new Dialog (title, width, height);
  21. foreach (var s in buttons) {
  22. int n = count++;
  23. var b = new Button (s);
  24. b.Clicked += delegate {
  25. clicked = n;
  26. d.Running = false;
  27. };
  28. d.AddButton (b);
  29. }
  30. if (message != null) {
  31. var l = new Label ((width - 4 - message.Length) / 2, 0, message);
  32. d.Add (l);
  33. }
  34. Application.Run (d);
  35. return clicked;
  36. }
  37. }
  38. }