MessageBox.cs 11 KB

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