MessageBox.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using System;
  2. using System.Collections.Generic;
  3. using static Terminal.Gui.ConfigurationManager;
  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, 0, true, 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, 0, true, 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, 0, true, 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, 0, true, buttons);
  87. }
  88. /// <summary>
  89. /// Presents a normal <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  90. /// </summary>
  91. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  92. /// <param name="width">Width for the window.</param>
  93. /// <param name="height">Height for the window.</param>
  94. /// <param name="title">Title for the query.</param>
  95. /// <param name="message">Message to display, might contain multiple lines.</param>
  96. /// <param name="defaultButton">Index of the default button.</param>
  97. /// <param name="buttons">Array of buttons to add.</param>
  98. /// <remarks>
  99. /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on the contents.
  100. /// </remarks>
  101. public static int Query (int width, int height, string title, string message, int defaultButton = 0, params string [] buttons)
  102. {
  103. return QueryFull (false, width, height, title, message, defaultButton, true, buttons);
  104. }
  105. /// <summary>
  106. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  107. /// </summary>
  108. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  109. /// <param name="title">Title for the query.</param>
  110. /// <param name="message">Message to display, might contain multiple lines.</param>
  111. /// <param name="defaultButton">Index of the default button.</param>
  112. /// <param name="buttons">Array of buttons to add.</param>
  113. /// <remarks>
  114. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  115. /// from the size of the message and buttons.
  116. /// </remarks>
  117. public static int Query (string title, string message, int defaultButton = 0, params string [] buttons)
  118. {
  119. return QueryFull (false, 0, 0, title, message, defaultButton, true, buttons);
  120. }
  121. /// <summary>
  122. /// Presents a normal <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  123. /// </summary>
  124. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  125. /// <param name="width">Width for the window.</param>
  126. /// <param name="height">Height for the window.</param>
  127. /// <param name="title">Title for the query.</param>
  128. /// <param name="message">Message to display, might contain multiple lines.</param>
  129. /// <param name="defaultButton">Index of the default button.</param>
  130. /// <param name="wrapMessagge">If wrap the message or not.</param>
  131. /// <param name="buttons">Array of buttons to add.</param>
  132. /// <remarks>
  133. /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on the contents.
  134. /// </remarks>
  135. public static int Query (int width, int height, string title, string message, int defaultButton = 0, bool wrapMessagge = true, params string [] buttons)
  136. {
  137. return QueryFull (false, width, height, title, message, defaultButton, wrapMessagge, 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="wrapMessage">If wrap the message or not.</param>
  147. /// <param name="buttons">Array of buttons to add.</param>
  148. /// <remarks>
  149. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  150. /// from the size of the message and buttons.
  151. /// </remarks>
  152. public static int Query (string title, string message, int defaultButton = 0, bool wrapMessage = true, params string [] buttons)
  153. {
  154. return QueryFull (false, 0, 0, title, message, defaultButton, wrapMessage, buttons);
  155. }
  156. /// <summary>
  157. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  158. /// </summary>
  159. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  160. /// <param name="width">Width for the window.</param>
  161. /// <param name="height">Height for the window.</param>
  162. /// <param name="title">Title for the query.</param>
  163. /// <param name="message">Message to display, might contain multiple lines.</param>
  164. /// <param name="defaultButton">Index of the default button.</param>
  165. /// <param name="buttons">Array of buttons to add.</param>
  166. /// <remarks>
  167. /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on the contents.
  168. /// </remarks>
  169. public static int ErrorQuery (int width, int height, string title, string message, int defaultButton = 0, params string [] buttons)
  170. {
  171. return QueryFull (true, width, height, title, message, defaultButton, true, buttons);
  172. }
  173. /// <summary>
  174. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  175. /// </summary>
  176. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  177. /// <param name="title">Title for the query.</param>
  178. /// <param name="message">Message to display, might contain multiple lines.</param>
  179. /// <param name="defaultButton">Index of the default button.</param>
  180. /// <param name="buttons">Array of buttons to add.</param>
  181. /// <remarks>
  182. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  183. /// from the size of the title, message. and buttons.
  184. /// </remarks>
  185. public static int ErrorQuery (string title, string message, int defaultButton = 0, params string [] buttons)
  186. {
  187. return QueryFull (true, 0, 0, title, message, defaultButton, true, buttons);
  188. }
  189. /// <summary>
  190. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  191. /// </summary>
  192. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  193. /// <param name="width">Width for the window.</param>
  194. /// <param name="height">Height for the window.</param>
  195. /// <param name="title">Title for the query.</param>
  196. /// <param name="message">Message to display, might contain multiple lines.</param>
  197. /// <param name="defaultButton">Index of the default button.</param>
  198. /// <param name="wrapMessagge">If wrap the message or not.</param>
  199. /// <param name="buttons">Array of buttons to add.</param>
  200. /// <remarks>
  201. /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on the contents.
  202. /// </remarks>
  203. public static int ErrorQuery (int width, int height, string title, string message, int defaultButton = 0, bool wrapMessagge = true, params string [] buttons)
  204. {
  205. return QueryFull (true, width, height, title, message, defaultButton, wrapMessagge, buttons);
  206. }
  207. /// <summary>
  208. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  209. /// </summary>
  210. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  211. /// <param name="title">Title for the query.</param>
  212. /// <param name="message">Message to display, might contain multiple lines.</param>
  213. /// <param name="defaultButton">Index of the default button.</param>
  214. /// <param name="wrapMessagge">If wrap the message or not.</param>
  215. /// <param name="buttons">Array of buttons to add.</param>
  216. /// <remarks>
  217. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  218. /// from the size of the title, message. and buttons.
  219. /// </remarks>
  220. public static int ErrorQuery (string title, string message, int defaultButton = 0, bool wrapMessagge = true, params string [] buttons)
  221. {
  222. return QueryFull (true, 0, 0, title, message, defaultButton, wrapMessagge, buttons);
  223. }
  224. /// <summary>
  225. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via <see cref="ConfigurationManager"/>.
  226. /// </summary>
  227. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  228. public static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Single;
  229. static int QueryFull (bool useErrorColors, int width, int height, string title, string message,
  230. int defaultButton = 0, bool wrapMessage = true, params string [] buttons)
  231. {
  232. // Create button array for Dialog
  233. int count = 0;
  234. List<Button> buttonList = new List<Button> ();
  235. if (buttons != null) {
  236. if (defaultButton > buttons.Length - 1) {
  237. defaultButton = buttons.Length - 1;
  238. }
  239. foreach (var s in buttons) {
  240. var b = new Button (s);
  241. if (count == defaultButton) {
  242. b.IsDefault = true;
  243. }
  244. buttonList.Add (b);
  245. count++;
  246. }
  247. }
  248. Dialog d;
  249. d = new Dialog (buttonList.ToArray ()) {
  250. Title = title,
  251. BorderStyle = DefaultBorderStyle,
  252. Width = Dim.Percent (60),
  253. Height = 5 // Border + one line of text + vspace + buttons
  254. };
  255. if (width != 0) {
  256. d.Width = width;
  257. }
  258. if (height != 0) {
  259. d.Height = height;
  260. }
  261. if (useErrorColors) {
  262. d.ColorScheme = Colors.ColorSchemes ["Error"];
  263. } else {
  264. d.ColorScheme = Colors.ColorSchemes ["Dialog"];
  265. }
  266. var messageLabel = new Label () {
  267. AutoSize = wrapMessage ? false : true,
  268. Text = message,
  269. TextAlignment = TextAlignment.Centered,
  270. X = 0,
  271. Y = 0,
  272. Width = Dim.Fill (0),
  273. Height = Dim.Fill (1),
  274. };
  275. messageLabel.TextFormatter.WordWrap = wrapMessage;
  276. messageLabel.TextFormatter.MultiLine = wrapMessage ? false : true;
  277. d.Add (messageLabel);
  278. d.Loaded += (s, e) => {
  279. if (width != 0 || height != 0) {
  280. return;
  281. }
  282. // TODO: replace with Dim.Fit when implemented
  283. var maxBounds = d.SuperView?.Bounds ?? Application.Top.Bounds;
  284. if (wrapMessage) {
  285. messageLabel.TextFormatter.Size = new Size (maxBounds.Size.Width - d.GetAdornmentsThickness ().Horizontal, maxBounds.Size.Height - d.GetAdornmentsThickness ().Vertical);
  286. }
  287. var msg = messageLabel.TextFormatter.Format ();
  288. var messageSize = messageLabel.TextFormatter.GetFormattedSize ();
  289. // Ensure the width fits the text + buttons
  290. var newWidth = Math.Max (width, Math.Max (messageSize.Width + d.GetAdornmentsThickness ().Horizontal,
  291. d.GetButtonsWidth () + d.buttons.Count + d.GetAdornmentsThickness ().Horizontal));
  292. if (newWidth > d.Frame.Width) {
  293. d.Width = newWidth;
  294. }
  295. // Ensure height fits the text + vspace + buttons
  296. var lastLine = messageLabel.TextFormatter.Lines [^1];
  297. d.Height = Math.Max (height, messageSize.Height + (lastLine.EndsWith ("\r\n") || lastLine.EndsWith ('\n') ? 1 : 2) + d.GetAdornmentsThickness ().Vertical);
  298. d.SetRelativeLayout (d.SuperView?.Frame ?? Application.Top.Frame);
  299. };
  300. // Setup actions
  301. Clicked = -1;
  302. for (int n = 0; n < buttonList.Count; n++) {
  303. int buttonId = n;
  304. var b = buttonList [n];
  305. b.Clicked += (s, e) => {
  306. Clicked = buttonId;
  307. Application.RequestStop ();
  308. };
  309. if (b.IsDefault) {
  310. b.SetFocus ();
  311. }
  312. }
  313. // Run the modal; do not shutdown the mainloop driver when done
  314. Application.Run (d);
  315. return Clicked;
  316. }
  317. /// <summary>
  318. /// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
  319. /// This is useful for web based console where by default there is no SynchronizationContext or TaskScheduler.
  320. /// </summary>
  321. public static int Clicked { get; private set; } = -1;
  322. }
  323. }