MessageBox.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using Terminal.Gui;
  5. using static Terminal.Gui.ConfigurationManager;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// MessageBox displays a modal message to the user, with a title, a message and a series of options that the user can choose from.
  9. /// </summary>
  10. /// <para>
  11. /// The difference between the <see cref="Query(ustring, ustring, ustring[])"/> and <see cref="ErrorQuery(ustring, ustring, ustring[])"/>
  12. /// method is the default set of colors used for the message box.
  13. /// </para>
  14. /// <para>
  15. /// The following example pops up a <see cref="MessageBox"/> with the specified title and text, plus two <see cref="Button"/>s.
  16. /// The value -1 is returned when the user cancels the <see cref="MessageBox"/> by pressing the ESC key.
  17. /// </para>
  18. /// <example>
  19. /// <code lang="c#">
  20. /// var n = MessageBox.Query ("Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
  21. /// if (n == 0)
  22. /// quit = true;
  23. /// else
  24. /// quit = false;
  25. /// </code>
  26. /// </example>
  27. public static class MessageBox {
  28. /// <summary>
  29. /// Presents a normal <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  30. /// </summary>
  31. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  32. /// <param name="width">Width for the window.</param>
  33. /// <param name="height">Height for the window.</param>
  34. /// <param name="title">Title for the query.</param>
  35. /// <param name="message">Message to display, might contain multiple lines.</param>
  36. /// <param name="buttons">Array of buttons to add.</param>
  37. /// <remarks>
  38. /// Use <see cref="Query(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  39. /// </remarks>
  40. public static int Query (int width, int height, ustring title, ustring message, params ustring [] buttons)
  41. {
  42. return QueryFull (false, width, height, title, message, 0, true, buttons);
  43. }
  44. /// <summary>
  45. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  46. /// </summary>
  47. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  48. /// <param name="title">Title for the query.</param>
  49. /// <param name="message">Message to display, might contain multiple lines.</param>
  50. /// <param name="buttons">Array of buttons to add.</param>
  51. /// <remarks>
  52. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  53. /// from the size of the message and buttons.
  54. /// </remarks>
  55. public static int Query (ustring title, ustring message, params ustring [] buttons)
  56. {
  57. return QueryFull (false, 0, 0, title, message, 0, true, buttons);
  58. }
  59. /// <summary>
  60. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  61. /// </summary>
  62. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  63. /// <param name="width">Width for the window.</param>
  64. /// <param name="height">Height for the window.</param>
  65. /// <param name="title">Title for the query.</param>
  66. /// <param name="message">Message to display, might contain multiple lines.</param>
  67. /// <param name="buttons">Array of buttons to add.</param>
  68. /// <remarks>
  69. /// Use <see cref="ErrorQuery(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  70. /// </remarks>
  71. public static int ErrorQuery (int width, int height, ustring title, ustring message, params ustring [] buttons)
  72. {
  73. return QueryFull (true, width, height, title, message, 0, true, buttons);
  74. }
  75. /// <summary>
  76. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  77. /// </summary>
  78. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  79. /// <param name="title">Title for the query.</param>
  80. /// <param name="message">Message to display, might contain multiple lines.</param>
  81. /// <param name="buttons">Array of buttons to add.</param>
  82. /// <remarks>
  83. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  84. /// from the size of the title, message. and buttons.
  85. /// </remarks>
  86. public static int ErrorQuery (ustring title, ustring message, params ustring [] buttons)
  87. {
  88. return QueryFull (true, 0, 0, title, message, 0, true, buttons);
  89. }
  90. /// <summary>
  91. /// Presents a normal <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  92. /// </summary>
  93. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  94. /// <param name="width">Width for the window.</param>
  95. /// <param name="height">Height for the window.</param>
  96. /// <param name="title">Title for the query.</param>
  97. /// <param name="message">Message to display, might contain multiple lines.</param>
  98. /// <param name="defaultButton">Index of the default button.</param>
  99. /// <param name="buttons">Array of buttons to add.</param>
  100. /// <remarks>
  101. /// Use <see cref="Query(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  102. /// </remarks>
  103. public static int Query (int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
  104. {
  105. return QueryFull (false, width, height, title, message, defaultButton, true, buttons);
  106. }
  107. /// <summary>
  108. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  109. /// </summary>
  110. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  111. /// <param name="title">Title for the query.</param>
  112. /// <param name="message">Message to display, might contain multiple lines.</param>
  113. /// <param name="defaultButton">Index of the default button.</param>
  114. /// <param name="buttons">Array of buttons to add.</param>
  115. /// <remarks>
  116. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  117. /// from the size of the message and buttons.
  118. /// </remarks>
  119. public static int Query (ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
  120. {
  121. return QueryFull (false, 0, 0, title, message, defaultButton, true, buttons);
  122. }
  123. /// <summary>
  124. /// Presents a normal <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  125. /// </summary>
  126. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  127. /// <param name="width">Width for the window.</param>
  128. /// <param name="height">Height for the window.</param>
  129. /// <param name="title">Title for the query.</param>
  130. /// <param name="message">Message to display, might contain multiple lines.</param>
  131. /// <param name="defaultButton">Index of the default button.</param>
  132. /// <param name="wrapMessagge">If wrap the message or not.</param>
  133. /// <param name="buttons">Array of buttons to add.</param>
  134. /// <remarks>
  135. /// Use <see cref="Query(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  136. /// </remarks>
  137. public static int Query (int width, int height, ustring title, ustring message, int defaultButton = 0, bool wrapMessagge = true, params ustring [] buttons)
  138. {
  139. return QueryFull (false, width, height, title, message, defaultButton, wrapMessagge, buttons);
  140. }
  141. /// <summary>
  142. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  143. /// </summary>
  144. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  145. /// <param name="title">Title for the query.</param>
  146. /// <param name="message">Message to display, might contain multiple lines.</param>
  147. /// <param name="defaultButton">Index of the default button.</param>
  148. /// <param name="wrapMessage">If wrap the message or not.</param>
  149. /// <param name="buttons">Array of buttons to add.</param>
  150. /// <remarks>
  151. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  152. /// from the size of the message and buttons.
  153. /// </remarks>
  154. public static int Query (ustring title, ustring message, int defaultButton = 0, bool wrapMessage = true, params ustring [] buttons)
  155. {
  156. return QueryFull (false, 0, 0, title, message, defaultButton, wrapMessage, buttons);
  157. }
  158. /// <summary>
  159. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  160. /// </summary>
  161. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  162. /// <param name="width">Width for the window.</param>
  163. /// <param name="height">Height for the window.</param>
  164. /// <param name="title">Title for the query.</param>
  165. /// <param name="message">Message to display, might contain multiple lines.</param>
  166. /// <param name="defaultButton">Index of the default button.</param>
  167. /// <param name="buttons">Array of buttons to add.</param>
  168. /// <remarks>
  169. /// Use <see cref="ErrorQuery(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  170. /// </remarks>
  171. public static int ErrorQuery (int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
  172. {
  173. return QueryFull (true, width, height, title, message, defaultButton, true, buttons);
  174. }
  175. /// <summary>
  176. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  177. /// </summary>
  178. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  179. /// <param name="title">Title for the query.</param>
  180. /// <param name="message">Message to display, might contain multiple lines.</param>
  181. /// <param name="defaultButton">Index of the default button.</param>
  182. /// <param name="buttons">Array of buttons to add.</param>
  183. /// <remarks>
  184. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  185. /// from the size of the title, message. and buttons.
  186. /// </remarks>
  187. public static int ErrorQuery (ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
  188. {
  189. return QueryFull (true, 0, 0, title, message, defaultButton, true, buttons);
  190. }
  191. /// <summary>
  192. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  193. /// </summary>
  194. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  195. /// <param name="width">Width for the window.</param>
  196. /// <param name="height">Height for the window.</param>
  197. /// <param name="title">Title for the query.</param>
  198. /// <param name="message">Message to display, might contain multiple lines.</param>
  199. /// <param name="defaultButton">Index of the default button.</param>
  200. /// <param name="wrapMessagge">If wrap the message or not.</param>
  201. /// <param name="buttons">Array of buttons to add.</param>
  202. /// <remarks>
  203. /// Use <see cref="ErrorQuery(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  204. /// </remarks>
  205. public static int ErrorQuery (int width, int height, ustring title, ustring message, int defaultButton = 0, bool wrapMessagge = true, params ustring [] buttons)
  206. {
  207. return QueryFull (true, width, height, title, message, defaultButton, wrapMessagge, buttons);
  208. }
  209. /// <summary>
  210. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  211. /// </summary>
  212. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  213. /// <param name="title">Title for the query.</param>
  214. /// <param name="message">Message to display, might contain multiple lines.</param>
  215. /// <param name="defaultButton">Index of the default button.</param>
  216. /// <param name="wrapMessagge">If wrap the message or not.</param>
  217. /// <param name="buttons">Array of buttons to add.</param>
  218. /// <remarks>
  219. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  220. /// from the size of the title, message. and buttons.
  221. /// </remarks>
  222. public static int ErrorQuery (ustring title, ustring message, int defaultButton = 0, bool wrapMessagge = true, params ustring [] buttons)
  223. {
  224. return QueryFull (true, 0, 0, title, message, defaultButton, wrapMessagge, buttons);
  225. }
  226. /// <summary>
  227. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via <see cref="ConfigurationManager"/>.
  228. /// </summary>
  229. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  230. public static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Single;
  231. static int QueryFull (bool useErrorColors, int width, int height, ustring title, ustring message,
  232. int defaultButton = 0, bool wrapMessage = true, params ustring [] buttons)
  233. {
  234. // Create button array for Dialog
  235. int count = 0;
  236. List<Button> buttonList = new List<Button> ();
  237. if (buttons != null) {
  238. if (defaultButton > buttons.Length - 1) {
  239. defaultButton = buttons.Length - 1;
  240. }
  241. foreach (var s in buttons) {
  242. var b = new Button (s);
  243. if (count == defaultButton) {
  244. b.IsDefault = true;
  245. }
  246. buttonList.Add (b);
  247. count++;
  248. }
  249. }
  250. Dialog d;
  251. d = new Dialog (buttonList.ToArray ()) {
  252. Title = title,
  253. BorderStyle = DefaultBorderStyle,
  254. Width = Dim.Percent (60),
  255. Height = 5 // Border + one line of text + vspace + buttons
  256. };
  257. if (width != 0) {
  258. d.Width = width;
  259. }
  260. if (height != 0) {
  261. d.Height = height;
  262. }
  263. if (useErrorColors) {
  264. d.ColorScheme = Colors.Error;
  265. } else {
  266. d.ColorScheme = Colors.Dialog;
  267. }
  268. var messageLabel = new Label () {
  269. AutoSize = false,
  270. Text = message,
  271. TextAlignment = TextAlignment.Centered,
  272. X = 0,
  273. Y = 0,
  274. Width = Dim.Fill (0),
  275. Height = Dim.Fill (1)
  276. };
  277. messageLabel.TextFormatter.WordWrap = wrapMessage; // BUGBUG: This does nothing as it's not implemented by TextFormatter!
  278. d.Add (messageLabel);
  279. d.Loaded += (s, e) => {
  280. if (width != 0 || height != 0) {
  281. return;
  282. }
  283. // TODO: replace with Dim.Fit when implemented
  284. var maxBounds = d.SuperView?.Bounds ?? Application.Top.Bounds;
  285. messageLabel.TextFormatter.Size = new Size (maxBounds.Size.Width - d.GetFramesThickness ().Horizontal, maxBounds.Size.Height - d.GetFramesThickness ().Vertical);
  286. var msg = messageLabel.TextFormatter.Format ();
  287. var messageSize = messageLabel.TextFormatter.GetFormattedSize ();
  288. // Ensure the width fits the text + buttons
  289. var newWidth = Math.Max (width, Math.Max (messageSize.Width + d.GetFramesThickness ().Horizontal,
  290. d.GetButtonsWidth () + d.buttons.Count + d.GetFramesThickness ().Horizontal));
  291. if (newWidth > d.Frame.Width) {
  292. d.Width = newWidth;
  293. }
  294. // Ensure height fits the text + vspace + buttons
  295. d.Height = Math.Max (height, messageSize.Height + 2 + d.GetFramesThickness ().Vertical);
  296. d.SetRelativeLayout (d.SuperView?.Frame ?? Application.Top.Frame);
  297. };
  298. // Setup actions
  299. Clicked = -1;
  300. for (int n = 0; n < buttonList.Count; n++) {
  301. int buttonId = n;
  302. var b = buttonList [n];
  303. b.Clicked += (s, e) => {
  304. Clicked = buttonId;
  305. Application.RequestStop ();
  306. };
  307. if (b.IsDefault) {
  308. b.SetFocus ();
  309. }
  310. }
  311. // Run the modal; do not shutdown the mainloop driver when done
  312. Application.Run (d);
  313. return Clicked;
  314. }
  315. /// <summary>
  316. /// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
  317. /// This is useful for web based console where by default there is no SynchronizationContext or TaskScheduler.
  318. /// </summary>
  319. public static int Clicked { get; private set; } = -1;
  320. }
  321. }