MessageBox.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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="wrapMessagge">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 wrapMessagge = true, params ustring [] buttons)
  155. {
  156. return QueryFull (false, 0, 0, title, message, defaultButton, wrapMessagge, 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 Border DefaultBorder { get; set; } = new Border () {
  231. BorderStyle = BorderStyle.Single,
  232. };
  233. static int QueryFull (bool useErrorColors, int width, int height, ustring title, ustring message,
  234. int defaultButton = 0, bool wrapMessagge = true, params ustring [] buttons)
  235. {
  236. int defaultWidth = 50;
  237. if (defaultWidth > Application.Driver.Cols / 2) {
  238. defaultWidth = (int)(Application.Driver.Cols * 0.60f);
  239. }
  240. int maxWidthLine = TextFormatter.MaxWidthLine (message);
  241. if (wrapMessagge && maxWidthLine > Application.Driver.Cols) {
  242. maxWidthLine = Application.Driver.Cols;
  243. }
  244. if (width == 0) {
  245. maxWidthLine = Math.Max (maxWidthLine, defaultWidth);
  246. } else {
  247. maxWidthLine = width;
  248. }
  249. int textWidth = TextFormatter.MaxWidth (message, maxWidthLine);
  250. int textHeight = TextFormatter.MaxLines (message, textWidth); // message.Count (ustring.Make ('\n')) + 1;
  251. int msgboxHeight = Math.Max (1, textHeight) + 4; // textHeight + (top + top padding + buttons + bottom)
  252. if (wrapMessagge) {
  253. textWidth = Math.Min (textWidth, Application.Driver.Cols);
  254. msgboxHeight = Math.Min (msgboxHeight, Application.Driver.Rows);
  255. }
  256. // Create button array for Dialog
  257. int count = 0;
  258. List<Button> buttonList = new List<Button> ();
  259. if (buttons != null && defaultButton > buttons.Length - 1) {
  260. defaultButton = buttons.Length - 1;
  261. }
  262. foreach (var s in buttons) {
  263. var b = new Button (s);
  264. if (count == defaultButton) {
  265. b.IsDefault = true;
  266. }
  267. buttonList.Add (b);
  268. count++;
  269. }
  270. // Create Dialog (retain backwards compat by supporting specifying height/width)
  271. Dialog d;
  272. if (width == 0 & height == 0) {
  273. d = new Dialog (title, buttonList.ToArray ()) {
  274. Height = msgboxHeight,
  275. Border = DefaultBorder
  276. };
  277. } else {
  278. d = new Dialog (title, width, Math.Max (height, 4), buttonList.ToArray ()) {
  279. Border = DefaultBorder
  280. };
  281. }
  282. if (useErrorColors) {
  283. d.ColorScheme = Colors.Error;
  284. //d.Border.ForgroundColor = Colors.Error.Normal.Foreground;
  285. //d.Border.BackgroundColor = Colors.Error.Normal.Background;
  286. } else {
  287. d.ColorScheme = Colors.Dialog;
  288. //d.Border.ForgroundColor = Colors.Dialog.Normal.Foreground;
  289. //d.Border.BackgroundColor = Colors.Dialog.Normal.Background;
  290. }
  291. if (!ustring.IsNullOrEmpty (message)) {
  292. var l = new Label (message) {
  293. LayoutStyle = LayoutStyle.Computed,
  294. TextAlignment = TextAlignment.Centered,
  295. X = Pos.Center (),
  296. Y = Pos.Center (),
  297. Width = Dim.Fill (),
  298. Height = Dim.Fill (1),
  299. AutoSize = false
  300. };
  301. d.Add (l);
  302. }
  303. if (width == 0 & height == 0) {
  304. // Dynamically size Width
  305. var dWidth = Math.Max (maxWidthLine, Math.Max (title.ConsoleWidth, Math.Max (textWidth + 2, d.GetButtonsWidth () + d.buttons.Count + 2))); // textWidth + (left + padding + padding + right)
  306. if (wrapMessagge) {
  307. d.Width = Math.Min (dWidth, Application.Driver.Cols);
  308. } else {
  309. d.Width = dWidth;
  310. }
  311. }
  312. // Setup actions
  313. Clicked = -1;
  314. for (int n = 0; n < buttonList.Count; n++) {
  315. int buttonId = n;
  316. var b = buttonList [n];
  317. b.Clicked += (s, e) => {
  318. Clicked = buttonId;
  319. Application.RequestStop ();
  320. };
  321. if (b.IsDefault) {
  322. b.SetFocus ();
  323. }
  324. }
  325. // Run the modal; do not shutdown the mainloop driver when done
  326. Application.Run (d);
  327. return Clicked;
  328. }
  329. /// <summary>
  330. /// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
  331. /// This is useful for web based console where by default there is no SynchronizationContext or TaskScheduler.
  332. /// </summary>
  333. public static int Clicked { get; private set; } = -1;
  334. }
  335. }