MessageBox.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. namespace Terminal.Gui {
  3. /// <summary>
  4. /// MessageBox 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. /// <para>
  7. /// The difference between the <see cref="Query"/> and <see cref="ErrorQuery"/> method is the default set of colors used for the message box.
  8. /// </para>
  9. /// <para>
  10. /// The following example pops up a <see cref="MessageBox"/> with 50 columns, and 7 lines, with the specified title and text, plus two <see cref="Button"/>s.
  11. /// The value -1 is returned when the user cancels the <see cref="MessageBox"/> by pressing the ESC key.
  12. /// </para>
  13. /// <example>
  14. /// <code lang="c#">
  15. /// var n = MessageBox.Query (50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
  16. /// if (n == 0)
  17. /// quit = true;
  18. /// else
  19. /// quit = false;
  20. /// </code>
  21. /// </example>
  22. public static class MessageBox {
  23. /// <summary>
  24. /// Presents a normal <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  25. /// </summary>
  26. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  27. /// <param name="width">Width for the window.</param>
  28. /// <param name="height">Height for the window.</param>
  29. /// <param name="title">Title for the query.</param>
  30. /// <param name="message">Message to display, might contain multiple lines..</param>
  31. /// <param name="buttons">Array of buttons to add.</param>
  32. public static int Query (int width, int height, string title, string message, params string [] buttons)
  33. {
  34. return QueryFull (false, width, height, title, message, buttons);
  35. }
  36. /// <summary>
  37. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  38. /// </summary>
  39. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  40. /// <param name="width">Width for the window.</param>
  41. /// <param name="height">Height for the window.</param>
  42. /// <param name="title">Title for the query.</param>
  43. /// <param name="message">Message to display, might contain multiple lines.</param>
  44. /// <param name="buttons">Array of buttons to add.</param>
  45. public static int ErrorQuery (int width, int height, string title, string message, params string [] buttons)
  46. {
  47. return QueryFull (true, width, height, title, message, buttons);
  48. }
  49. static int QueryFull (bool useErrorColors, int width, int height, string title, string message, params string [] buttons)
  50. {
  51. int textWidth = Label.MaxWidth (message, width);
  52. int clicked = -1, count = 0;
  53. var d = new Dialog (title, Math.Max(width, textWidth) + 4, height);
  54. if (useErrorColors)
  55. d.ColorScheme = Colors.Error;
  56. foreach (var s in buttons) {
  57. int n = count++;
  58. var b = new Button (s);
  59. b.Clicked += delegate {
  60. clicked = n;
  61. d.Running = false;
  62. };
  63. d.AddButton (b);
  64. }
  65. if (message != null) {
  66. var l = new Label (textWidth > width ? 0 : (width - 4 - textWidth) / 2, 0, message);
  67. d.Add (l);
  68. }
  69. Application.Run (d);
  70. return clicked;
  71. }
  72. }
  73. }