MessageBox.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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, null, 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, null, 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, null, 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, null, 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, null, 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, null, buttons);
  121. }
  122. /// <summary>
  123. /// Presents a normal <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="border">The border settings.</param>
  132. /// <param name="buttons">Array of buttons to add.</param>
  133. /// <remarks>
  134. /// Use <see cref="Query(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  135. /// </remarks>
  136. public static int Query (int width, int height, ustring title, ustring message, int defaultButton = 0, Border border = null, params ustring [] buttons)
  137. {
  138. return QueryFull (false, width, height, title, message, defaultButton, border, buttons);
  139. }
  140. /// <summary>
  141. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  142. /// </summary>
  143. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  144. /// <param name="title">Title for the query.</param>
  145. /// <param name="message">Message to display, might contain multiple lines.</param>
  146. /// <param name="defaultButton">Index of the default button.</param>
  147. /// <param name="border">The border settings.</param>
  148. /// <param name="buttons">Array of buttons to add.</param>
  149. /// <remarks>
  150. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  151. /// from the size of the message and buttons.
  152. /// </remarks>
  153. public static int Query (ustring title, ustring message, int defaultButton = 0, Border border = null, params ustring [] buttons)
  154. {
  155. return QueryFull (false, 0, 0, title, message, defaultButton, border, buttons);
  156. }
  157. /// <summary>
  158. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  159. /// </summary>
  160. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  161. /// <param name="width">Width for the window.</param>
  162. /// <param name="height">Height for the window.</param>
  163. /// <param name="title">Title for the query.</param>
  164. /// <param name="message">Message to display, might contain multiple lines.</param>
  165. /// <param name="defaultButton">Index of the default button.</param>
  166. /// <param name="buttons">Array of buttons to add.</param>
  167. /// <remarks>
  168. /// Use <see cref="ErrorQuery(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  169. /// </remarks>
  170. public static int ErrorQuery (int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
  171. {
  172. return QueryFull (true, width, height, title, message, defaultButton, null, buttons);
  173. }
  174. /// <summary>
  175. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  176. /// </summary>
  177. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  178. /// <param name="title">Title for the query.</param>
  179. /// <param name="message">Message to display, might contain multiple lines.</param>
  180. /// <param name="defaultButton">Index of the default button.</param>
  181. /// <param name="buttons">Array of buttons to add.</param>
  182. /// <remarks>
  183. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  184. /// from the size of the title, message. and buttons.
  185. /// </remarks>
  186. public static int ErrorQuery (ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
  187. {
  188. return QueryFull (true, 0, 0, title, message, defaultButton, null, buttons);
  189. }
  190. /// <summary>
  191. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  192. /// </summary>
  193. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  194. /// <param name="width">Width for the window.</param>
  195. /// <param name="height">Height for the window.</param>
  196. /// <param name="title">Title for the query.</param>
  197. /// <param name="message">Message to display, might contain multiple lines.</param>
  198. /// <param name="defaultButton">Index of the default button.</param>
  199. /// <param name="border">The border settings.</param>
  200. /// <param name="buttons">Array of buttons to add.</param>
  201. /// <remarks>
  202. /// Use <see cref="ErrorQuery(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  203. /// </remarks>
  204. public static int ErrorQuery (int width, int height, ustring title, ustring message, int defaultButton = 0, Border border = null, params ustring [] buttons)
  205. {
  206. return QueryFull (true, width, height, title, message, defaultButton, border, buttons);
  207. }
  208. /// <summary>
  209. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  210. /// </summary>
  211. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  212. /// <param name="title">Title for the query.</param>
  213. /// <param name="message">Message to display, might contain multiple lines.</param>
  214. /// <param name="defaultButton">Index of the default button.</param>
  215. /// <param name="border">The border settings.</param>
  216. /// <param name="buttons">Array of buttons to add.</param>
  217. /// <remarks>
  218. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  219. /// from the size of the title, message. and buttons.
  220. /// </remarks>
  221. public static int ErrorQuery (ustring title, ustring message, int defaultButton = 0, Border border = null, params ustring [] buttons)
  222. {
  223. return QueryFull (true, 0, 0, title, message, defaultButton, border, buttons);
  224. }
  225. static int QueryFull (bool useErrorColors, int width, int height, ustring title, ustring message,
  226. int defaultButton = 0, Border border = null, params ustring [] buttons)
  227. {
  228. const int defaultWidth = 50;
  229. int textWidth = TextFormatter.MaxWidth (message, width == 0 ? defaultWidth : width);
  230. int textHeight = TextFormatter.MaxLines (message, textWidth); // message.Count (ustring.Make ('\n')) + 1;
  231. int msgboxHeight = Math.Max (1, textHeight) + 4; // textHeight + (top + top padding + buttons + bottom)
  232. // Create button array for Dialog
  233. int count = 0;
  234. List<Button> buttonList = new List<Button> ();
  235. if (buttons != null && defaultButton > buttons.Length - 1) {
  236. defaultButton = buttons.Length - 1;
  237. }
  238. foreach (var s in buttons) {
  239. var b = new Button (s);
  240. if (count == defaultButton) {
  241. b.IsDefault = true;
  242. }
  243. buttonList.Add (b);
  244. count++;
  245. }
  246. // Create Dialog (retain backwards compat by supporting specifying height/width)
  247. Dialog d;
  248. if (width == 0 & height == 0) {
  249. d = new Dialog (title, buttonList.ToArray ());
  250. d.Height = msgboxHeight;
  251. } else {
  252. d = new Dialog (title, Math.Max (width, textWidth) + 4, height, buttonList.ToArray ());
  253. }
  254. if (border != null) {
  255. d.Border = border;
  256. }
  257. if (useErrorColors) {
  258. d.ColorScheme = Colors.Error;
  259. }
  260. if (message != null) {
  261. var l = new Label (textWidth > width ? 0 : (width - 4 - textWidth) / 2, 1, message);
  262. l.LayoutStyle = LayoutStyle.Computed;
  263. l.TextAlignment = TextAlignment.Centered;
  264. l.X = Pos.Center ();
  265. l.Y = Pos.Center ();
  266. l.Width = Dim.Fill (2);
  267. l.Height = Dim.Fill (1);
  268. d.Add (l);
  269. }
  270. // Dynamically size Width
  271. int msgboxWidth = Math.Max (defaultWidth, Math.Max (title.RuneCount + 8, Math.Max (textWidth + 4, d.GetButtonsWidth ()) + 8)); // textWidth + (left + padding + padding + right)
  272. d.Width = msgboxWidth;
  273. // Setup actions
  274. Clicked = -1;
  275. for (int n = 0; n < buttonList.Count; n++) {
  276. int buttonId = n;
  277. var b = buttonList [n];
  278. b.Clicked += () => {
  279. Clicked = buttonId;
  280. Application.RequestStop ();
  281. };
  282. if (b.IsDefault) {
  283. b.SetFocus ();
  284. }
  285. }
  286. // Run the modal; do not shutdown the mainloop driver when done
  287. Application.Run (d);
  288. return Clicked;
  289. }
  290. /// <summary>
  291. /// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
  292. /// This is useful for web based console where by default there is no SynchronizationContext or TaskScheduler.
  293. /// </summary>
  294. public static int Clicked { get; private set; } = -1;
  295. }
  296. }