MessageBox.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  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(ustring, ustring, ustring[])"/> and <see cref="ErrorQuery(ustring, ustring, ustring[])"/>
  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(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  37. /// </remarks>
  38. public static int Query (int width, int height, ustring title, ustring message, params ustring [] buttons)
  39. {
  40. return QueryFull (false, width, height, title, message, 0, null, 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 (ustring title, ustring message, params ustring [] buttons)
  54. {
  55. return QueryFull (false, 0, 0, title, message, 0, null, 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(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  68. /// </remarks>
  69. public static int ErrorQuery (int width, int height, ustring title, ustring message, params ustring [] buttons)
  70. {
  71. return QueryFull (true, width, height, title, message, 0, null, 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 (ustring title, ustring message, params ustring [] buttons)
  85. {
  86. return QueryFull (true, 0, 0, title, message, 0, null, 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(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  100. /// </remarks>
  101. public static int Query (int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
  102. {
  103. return QueryFull (false, width, height, title, message, defaultButton, null, 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 (ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
  118. {
  119. return QueryFull (false, 0, 0, title, message, defaultButton, null, 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="border">The border settings.</param>
  131. /// <param name="wrapMessagge">If wrap the message or not.</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, bool wrapMessagge = true, params ustring [] buttons)
  137. {
  138. return QueryFull (false, width, height, title, message, defaultButton, border, wrapMessagge, 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="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, Border border = null, bool wrapMessagge = true, params ustring [] buttons)
  155. {
  156. return QueryFull (false, 0, 0, title, message, defaultButton, border, 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, null, 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, null, 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="border">The border settings.</param>
  201. /// <param name="wrapMessagge">If wrap the message or not.</param>
  202. /// <param name="buttons">Array of buttons to add.</param>
  203. /// <remarks>
  204. /// Use <see cref="ErrorQuery(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  205. /// </remarks>
  206. public static int ErrorQuery (int width, int height, ustring title, ustring message, int defaultButton = 0, Border border = null, bool wrapMessagge = true, params ustring [] buttons)
  207. {
  208. return QueryFull (true, width, height, title, message, defaultButton, border, wrapMessagge, buttons);
  209. }
  210. /// <summary>
  211. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  212. /// </summary>
  213. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  214. /// <param name="title">Title for the query.</param>
  215. /// <param name="message">Message to display, might contain multiple lines.</param>
  216. /// <param name="defaultButton">Index of the default button.</param>
  217. /// <param name="border">The border settings.</param>
  218. /// <param name="wrapMessagge">If wrap the message or not.</param>
  219. /// <param name="buttons">Array of buttons to add.</param>
  220. /// <remarks>
  221. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  222. /// from the size of the title, message. and buttons.
  223. /// </remarks>
  224. public static int ErrorQuery (ustring title, ustring message, int defaultButton = 0, Border border = null, bool wrapMessagge = true, params ustring [] buttons)
  225. {
  226. return QueryFull (true, 0, 0, title, message, defaultButton, border, wrapMessagge, buttons);
  227. }
  228. static int QueryFull (bool useErrorColors, int width, int height, ustring title, ustring message,
  229. int defaultButton = 0, Border border = null, bool wrapMessagge = true, params ustring [] buttons)
  230. {
  231. int defaultWidth = 50;
  232. if (defaultWidth > Application.Driver.Cols / 2) {
  233. defaultWidth = (int)(Application.Driver.Cols * 0.60f);
  234. }
  235. int maxWidthLine = TextFormatter.MaxWidthLine (message);
  236. if (wrapMessagge && maxWidthLine > Application.Driver.Cols) {
  237. maxWidthLine = Application.Driver.Cols;
  238. }
  239. if (width == 0) {
  240. maxWidthLine = Math.Max (maxWidthLine, defaultWidth);
  241. } else {
  242. maxWidthLine = width;
  243. }
  244. int textWidth = TextFormatter.MaxWidth (message, maxWidthLine);
  245. int textHeight = TextFormatter.MaxLines (message, textWidth); // message.Count (ustring.Make ('\n')) + 1;
  246. int msgboxHeight = Math.Max (1, textHeight) + 4; // textHeight + (top + top padding + buttons + bottom)
  247. if (wrapMessagge) {
  248. textWidth = Math.Min (textWidth, Application.Driver.Cols);
  249. msgboxHeight = Math.Min (msgboxHeight, Application.Driver.Rows);
  250. }
  251. // Create button array for Dialog
  252. int count = 0;
  253. List<Button> buttonList = new List<Button> ();
  254. if (buttons != null && defaultButton > buttons.Length - 1) {
  255. defaultButton = buttons.Length - 1;
  256. }
  257. foreach (var s in buttons) {
  258. var b = new Button (s);
  259. if (count == defaultButton) {
  260. b.IsDefault = true;
  261. }
  262. buttonList.Add (b);
  263. count++;
  264. }
  265. // Create Dialog (retain backwards compat by supporting specifying height/width)
  266. Dialog d;
  267. if (width == 0 & height == 0) {
  268. d = new Dialog (title, border, buttonList.ToArray ()) {
  269. Height = msgboxHeight
  270. };
  271. } else {
  272. d = new Dialog (title, width, Math.Max (height, 4), border, buttonList.ToArray ());
  273. }
  274. if (useErrorColors) {
  275. d.ColorScheme = Colors.Error;
  276. border.BorderBrush = Colors.Error.Normal.Foreground;
  277. border.Background = Colors.Error.Normal.Background;
  278. } else {
  279. d.ColorScheme = Colors.Dialog;
  280. d.Border.BorderBrush = Colors.Dialog.Normal.Foreground;
  281. d.Border.Background = Colors.Dialog.Normal.Background;
  282. }
  283. if (!ustring.IsNullOrEmpty (message)) {
  284. var l = new Label (message) {
  285. LayoutStyle = LayoutStyle.Computed,
  286. TextAlignment = TextAlignment.Centered,
  287. X = Pos.Center (),
  288. Y = Pos.Center (),
  289. Width = Dim.Fill (),
  290. Height = Dim.Fill (1),
  291. AutoSize = false
  292. };
  293. d.Add (l);
  294. }
  295. if (width == 0 & height == 0) {
  296. // Dynamically size Width
  297. var dWidth = Math.Max (maxWidthLine, Math.Max (title.ConsoleWidth, Math.Max (textWidth + 2, d.GetButtonsWidth () + d.buttons.Count + 2))); // textWidth + (left + padding + padding + right)
  298. if (wrapMessagge) {
  299. d.Width = Math.Min (dWidth, Application.Driver.Cols);
  300. } else {
  301. d.Width = dWidth;
  302. }
  303. }
  304. // Setup actions
  305. Clicked = -1;
  306. for (int n = 0; n < buttonList.Count; n++) {
  307. int buttonId = n;
  308. var b = buttonList [n];
  309. b.Clicked += () => {
  310. Clicked = buttonId;
  311. Application.RequestStop ();
  312. };
  313. if (b.IsDefault) {
  314. b.SetFocus ();
  315. }
  316. }
  317. // Run the modal; do not shutdown the mainloop driver when done
  318. Application.Run (d);
  319. return Clicked;
  320. }
  321. /// <summary>
  322. /// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
  323. /// This is useful for web based console where by default there is no SynchronizationContext or TaskScheduler.
  324. /// </summary>
  325. public static int Clicked { get; private set; } = -1;
  326. }
  327. }