MessageBox.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Terminal.Gui {
  5. /// <summary>
  6. /// MessageBox displays a modal message to the user, with a title, a message and a series of options that the user can choose from.
  7. /// </summary>
  8. /// <para>
  9. /// The difference between the <see cref="Query(string, string, string[])"/> and <see cref="ErrorQuery(string, string, string[])"/>
  10. /// method is the default set of colors used for the message box.
  11. /// </para>
  12. /// <para>
  13. /// The following example pops up a <see cref="MessageBox"/> with the specified title and text, plus two <see cref="Button"/>s.
  14. /// The value -1 is returned when the user cancels the <see cref="MessageBox"/> by pressing the ESC key.
  15. /// </para>
  16. /// <example>
  17. /// <code lang="c#">
  18. /// var n = MessageBox.Query ("Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
  19. /// if (n == 0)
  20. /// quit = true;
  21. /// else
  22. /// quit = false;
  23. /// </code>
  24. /// </example>
  25. public static class MessageBox {
  26. /// <summary>
  27. /// Presents a normal <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  28. /// </summary>
  29. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  30. /// <param name="width">Width for the window.</param>
  31. /// <param name="height">Height for the window.</param>
  32. /// <param name="title">Title for the query.</param>
  33. /// <param name="message">Message to display, might contain multiple lines..</param>
  34. /// <param name="buttons">Array of buttons to add.</param>
  35. /// <remarks>
  36. /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on the contents.
  37. /// </remarks>
  38. public static int Query (int width, int height, string title, string message, params string [] buttons)
  39. {
  40. return QueryFull (false, width, height, title, message, buttons);
  41. }
  42. /// <summary>
  43. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  44. /// </summary>
  45. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  46. /// <param name="title">Title for the query.</param>
  47. /// <param name="message">Message to display, might contain multiple lines.</param>
  48. /// <param name="buttons">Array of buttons to add.</param>
  49. /// <remarks>
  50. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  51. /// from the size of the message and buttons.
  52. /// </remarks>
  53. public static int Query (string title, string message, params string [] buttons)
  54. {
  55. return QueryFull (false, 0, 0, title, message, buttons);
  56. }
  57. /// <summary>
  58. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  59. /// </summary>
  60. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  61. /// <param name="width">Width for the window.</param>
  62. /// <param name="height">Height for the window.</param>
  63. /// <param name="title">Title for the query.</param>
  64. /// <param name="message">Message to display, might contain multiple lines.</param>
  65. /// <param name="buttons">Array of buttons to add.</param>
  66. /// <remarks>
  67. /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on the contents.
  68. /// </remarks>
  69. public static int ErrorQuery (int width, int height, string title, string message, params string [] buttons)
  70. {
  71. return QueryFull (true, width, height, title, message, buttons);
  72. }
  73. /// <summary>
  74. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  75. /// </summary>
  76. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  77. /// <param name="title">Title for the query.</param>
  78. /// <param name="message">Message to display, might contain multiple lines.</param>
  79. /// <param name="buttons">Array of buttons to add.</param>
  80. /// <remarks>
  81. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  82. /// from the size of the title, message. and buttons.
  83. /// </remarks>
  84. public static int ErrorQuery (string title, string message, params string [] buttons)
  85. {
  86. return QueryFull (true, 0, 0, title, message, buttons);
  87. }
  88. static int QueryFull (bool useErrorColors, int width, int height, string title, string message, params string [] buttons)
  89. {
  90. const int defaultWidth = 30;
  91. int textWidth = Label.MaxWidth (message, width);
  92. int textHeight = message.ToCharArray ().Count (c => c == '\n') + 1;
  93. int msgboxHeight = Math.Max (1, textHeight) + 4; // textHeight + (top + top padding + buttons + bottom)
  94. // Create button array for Dialog
  95. int count = 0;
  96. List<Button> buttonList = new List<Button> ();
  97. foreach (var s in buttons) {
  98. var b = new Button (s);
  99. if (count == 0) {
  100. b.IsDefault = true;
  101. }
  102. buttonList.Add (b);
  103. count++;
  104. }
  105. // Create Dialog (retain backwards compat by supporting specifying height/width)
  106. Dialog d;
  107. if (width == 0 & height == 0) {
  108. d = new Dialog (title, buttonList.ToArray ());
  109. d.Height = msgboxHeight;
  110. } else {
  111. d = new Dialog (title, Math.Max (width, textWidth) + 4, height, buttonList.ToArray ());
  112. }
  113. if (useErrorColors) {
  114. d.ColorScheme = Colors.Error;
  115. }
  116. if (message != null) {
  117. var l = new Label (textWidth > width ? 0 : (width - 4 - textWidth) / 2, 1, message);
  118. //l.ColorScheme = Colors.Menu;
  119. if (true) { //width == 0 & height == 0) {
  120. l.LayoutStyle = LayoutStyle.Computed;
  121. l.TextAlignment = TextAlignment.Centered;
  122. l.X = Pos.Center ();
  123. l.Y = Pos.Center ();
  124. l.Width = Dim.Fill (2);
  125. l.Height = Dim.Fill (2);
  126. }
  127. d.Add (l);
  128. }
  129. // Dynamically size Width
  130. int msgboxWidth = Math.Max (defaultWidth, Math.Max (title.Length + 8, Math.Max (textWidth + 4, d.GetButtonsWidth ()) + 8)); // textWidth + (left + padding + padding + right)
  131. d.Width = msgboxWidth;
  132. // Setup actions
  133. int clicked = -1;
  134. for (int n = 0; n < buttonList.Count; n++) {
  135. int buttonId = n;
  136. buttonList [n].Clicked += () => {
  137. clicked = buttonId;
  138. Application.RequestStop ();
  139. };
  140. }
  141. Application.Run (d);
  142. return clicked;
  143. }
  144. }
  145. }