MessageBox.cs 3.0 KB

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