MessageBox.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using Terminal.Gui.Configuration;
  5. using static Terminal.Gui.Configuration.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, null, 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, null, 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, null, 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, null, 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, null, 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, null, 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="border">The border settings.</param>
  133. /// <param name="wrapMessagge">If wrap the message or not.</param>
  134. /// <param name="buttons">Array of buttons to add.</param>
  135. /// <remarks>
  136. /// Use <see cref="Query(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  137. /// </remarks>
  138. public static int Query (int width, int height, ustring title, ustring message, int defaultButton = 0, Border border = null, bool wrapMessagge = true, params ustring [] buttons)
  139. {
  140. return QueryFull (false, width, height, title, message, defaultButton, border, wrapMessagge, buttons);
  141. }
  142. /// <summary>
  143. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  144. /// </summary>
  145. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  146. /// <param name="title">Title for the query.</param>
  147. /// <param name="message">Message to display, might contain multiple lines.</param>
  148. /// <param name="defaultButton">Index of the default button.</param>
  149. /// <param name="border">The border settings.</param>
  150. /// <param name="wrapMessagge">If wrap the message or not.</param>
  151. /// <param name="buttons">Array of buttons to add.</param>
  152. /// <remarks>
  153. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  154. /// from the size of the message and buttons.
  155. /// </remarks>
  156. public static int Query (ustring title, ustring message, int defaultButton = 0, Border border = null, bool wrapMessagge = true, params ustring [] buttons)
  157. {
  158. return QueryFull (false, 0, 0, title, message, defaultButton, border, wrapMessagge, buttons);
  159. }
  160. /// <summary>
  161. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  162. /// </summary>
  163. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  164. /// <param name="width">Width for the window.</param>
  165. /// <param name="height">Height for the window.</param>
  166. /// <param name="title">Title for the query.</param>
  167. /// <param name="message">Message to display, might contain multiple lines.</param>
  168. /// <param name="defaultButton">Index of the default button.</param>
  169. /// <param name="buttons">Array of buttons to add.</param>
  170. /// <remarks>
  171. /// Use <see cref="ErrorQuery(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  172. /// </remarks>
  173. public static int ErrorQuery (int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
  174. {
  175. return QueryFull (true, width, height, title, message, defaultButton, null, true, buttons);
  176. }
  177. /// <summary>
  178. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  179. /// </summary>
  180. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  181. /// <param name="title">Title for the query.</param>
  182. /// <param name="message">Message to display, might contain multiple lines.</param>
  183. /// <param name="defaultButton">Index of the default button.</param>
  184. /// <param name="buttons">Array of buttons to add.</param>
  185. /// <remarks>
  186. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  187. /// from the size of the title, message. and buttons.
  188. /// </remarks>
  189. public static int ErrorQuery (ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
  190. {
  191. return QueryFull (true, 0, 0, title, message, defaultButton, null, true, buttons);
  192. }
  193. /// <summary>
  194. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  195. /// </summary>
  196. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  197. /// <param name="width">Width for the window.</param>
  198. /// <param name="height">Height for the window.</param>
  199. /// <param name="title">Title for the query.</param>
  200. /// <param name="message">Message to display, might contain multiple lines.</param>
  201. /// <param name="defaultButton">Index of the default button.</param>
  202. /// <param name="border">The border settings.</param>
  203. /// <param name="wrapMessagge">If wrap the message or not.</param>
  204. /// <param name="buttons">Array of buttons to add.</param>
  205. /// <remarks>
  206. /// Use <see cref="ErrorQuery(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  207. /// </remarks>
  208. public static int ErrorQuery (int width, int height, ustring title, ustring message, int defaultButton = 0, Border border = null, bool wrapMessagge = true, params ustring [] buttons)
  209. {
  210. return QueryFull (true, width, height, title, message, defaultButton, border, wrapMessagge, buttons);
  211. }
  212. /// <summary>
  213. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  214. /// </summary>
  215. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  216. /// <param name="title">Title for the query.</param>
  217. /// <param name="message">Message to display, might contain multiple lines.</param>
  218. /// <param name="defaultButton">Index of the default button.</param>
  219. /// <param name="border">The border settings.</param>
  220. /// <param name="wrapMessagge">If wrap the message or not.</param>
  221. /// <param name="buttons">Array of buttons to add.</param>
  222. /// <remarks>
  223. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  224. /// from the size of the title, message. and buttons.
  225. /// </remarks>
  226. public static int ErrorQuery (ustring title, ustring message, int defaultButton = 0, Border border = null, bool wrapMessagge = true, params ustring [] buttons)
  227. {
  228. return QueryFull (true, 0, 0, title, message, defaultButton, border, wrapMessagge, buttons);
  229. }
  230. /// <summary>
  231. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via <see cref="ConfigurationManager"/>.
  232. /// </summary>
  233. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  234. public static Border DefaultBorder { get; set; } = new Border () {
  235. BorderStyle = BorderStyle.Single,
  236. DrawMarginFrame = false,
  237. Effect3D = true,
  238. Effect3DOffset = new Point (1, 1),
  239. };
  240. static int QueryFull (bool useErrorColors, int width, int height, ustring title, ustring message,
  241. int defaultButton = 0, Border border = null, bool wrapMessagge = true, params ustring [] buttons)
  242. {
  243. int defaultWidth = 50;
  244. if (defaultWidth > Application.Driver.Cols / 2) {
  245. defaultWidth = (int)(Application.Driver.Cols * 0.60f);
  246. }
  247. int maxWidthLine = TextFormatter.MaxWidthLine (message);
  248. if (wrapMessagge && maxWidthLine > Application.Driver.Cols) {
  249. maxWidthLine = Application.Driver.Cols;
  250. }
  251. if (width == 0) {
  252. maxWidthLine = Math.Max (maxWidthLine, defaultWidth);
  253. } else {
  254. maxWidthLine = width;
  255. }
  256. int textWidth = TextFormatter.MaxWidth (message, maxWidthLine);
  257. int textHeight = TextFormatter.MaxLines (message, textWidth); // message.Count (ustring.Make ('\n')) + 1;
  258. int msgboxHeight = Math.Max (1, textHeight) + 4; // textHeight + (top + top padding + buttons + bottom)
  259. if (wrapMessagge) {
  260. textWidth = Math.Min (textWidth, Application.Driver.Cols);
  261. msgboxHeight = Math.Min (msgboxHeight, Application.Driver.Rows);
  262. }
  263. // Create button array for Dialog
  264. int count = 0;
  265. List<Button> buttonList = new List<Button> ();
  266. if (buttons != null && defaultButton > buttons.Length - 1) {
  267. defaultButton = buttons.Length - 1;
  268. }
  269. foreach (var s in buttons) {
  270. var b = new Button (s);
  271. if (count == defaultButton) {
  272. b.IsDefault = true;
  273. }
  274. buttonList.Add (b);
  275. count++;
  276. }
  277. if (border == null) {
  278. border = DefaultBorder;
  279. border.Title = title;
  280. }
  281. // Create Dialog (retain backwards compat by supporting specifying height/width)
  282. Dialog d;
  283. if (width == 0 & height == 0) {
  284. d = new Dialog (title, border, buttonList.ToArray ()) {
  285. Height = msgboxHeight
  286. };
  287. } else {
  288. d = new Dialog (title, width, Math.Max (height, 4), border, buttonList.ToArray ());
  289. }
  290. if (useErrorColors) {
  291. d.ColorScheme = Colors.Error;
  292. d.Border.BorderBrush = Colors.Error.Normal.Foreground;
  293. d.Border.Background = Colors.Error.Normal.Background;
  294. } else {
  295. d.ColorScheme = Colors.Dialog;
  296. d.Border.BorderBrush = Colors.Dialog.Normal.Foreground;
  297. d.Border.Background = Colors.Dialog.Normal.Background;
  298. }
  299. if (!ustring.IsNullOrEmpty (message)) {
  300. var l = new Label (message) {
  301. LayoutStyle = LayoutStyle.Computed,
  302. TextAlignment = TextAlignment.Centered,
  303. X = Pos.Center (),
  304. Y = Pos.Center (),
  305. Width = Dim.Fill (),
  306. Height = Dim.Fill (1),
  307. AutoSize = false
  308. };
  309. d.Add (l);
  310. }
  311. if (width == 0 & height == 0) {
  312. // Dynamically size Width
  313. var dWidth = Math.Max (maxWidthLine, Math.Max (title.ConsoleWidth, Math.Max (textWidth + 2, d.GetButtonsWidth () + d.buttons.Count + 2))); // textWidth + (left + padding + padding + right)
  314. if (wrapMessagge) {
  315. d.Width = Math.Min (dWidth, Application.Driver.Cols);
  316. } else {
  317. d.Width = dWidth;
  318. }
  319. }
  320. // Setup actions
  321. Clicked = -1;
  322. for (int n = 0; n < buttonList.Count; n++) {
  323. int buttonId = n;
  324. var b = buttonList [n];
  325. b.Clicked += (s, e) => {
  326. Clicked = buttonId;
  327. Application.RequestStop ();
  328. };
  329. if (b.IsDefault) {
  330. b.SetFocus ();
  331. }
  332. }
  333. // Run the modal; do not shutdown the mainloop driver when done
  334. Application.Run (d);
  335. return Clicked;
  336. }
  337. /// <summary>
  338. /// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
  339. /// This is useful for web based console where by default there is no SynchronizationContext or TaskScheduler.
  340. /// </summary>
  341. public static int Clicked { get; private set; } = -1;
  342. }
  343. }