MessageBox.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. /// Presents a message with the specified title and message and a list of buttons to show to the user.
  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. return QueryFull (false, width, height, title, message, buttons);
  19. }
  20. /// <summary>
  21. /// Presents an error message box with the specified title and message and a list of buttons to show to the user.
  22. /// </summary>
  23. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  24. /// <param name="width">Width for the window.</param>
  25. /// <param name="height">Height for the window.</param>
  26. /// <param name="title">Title for the query.</param>
  27. /// <param name="message">Message to display, might contain multiple lines..</param>
  28. /// <param name="buttons">Array of buttons to add.</param>
  29. public static int ErrorQuery (int width, int height, string title, string message, params string [] buttons)
  30. {
  31. return QueryFull (true, width, height, title, message, buttons);
  32. }
  33. static int QueryFull (bool useErrorColors, int width, int height, string title, string message, params string [] buttons)
  34. {
  35. int lines = Label.MeasureLines (message, width);
  36. int clicked = -1, count = 0;
  37. var d = new Dialog (title, width, height);
  38. if (useErrorColors)
  39. d.ColorScheme = Colors.Error;
  40. foreach (var s in buttons) {
  41. int n = count++;
  42. var b = new Button (s);
  43. b.Clicked += delegate {
  44. clicked = n;
  45. d.Running = false;
  46. };
  47. d.AddButton (b);
  48. }
  49. if (message != null) {
  50. var l = new Label ((width - 4 - message.Length) / 2, 0, message);
  51. d.Add (l);
  52. }
  53. Application.Run (d);
  54. return clicked;
  55. }
  56. }
  57. }