MessageBox.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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, 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, 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, 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, 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, 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, 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="buttons">Array of buttons to add.</param>
  132. /// <remarks>
  133. /// Use <see cref="Query(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  134. /// </remarks>
  135. public static int Query (int width, int height, ustring title, ustring message, int defaultButton = 0, Border border = null, params ustring [] buttons)
  136. {
  137. return QueryFull (false, width, height, title, message, defaultButton, border, 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="border">The border settings.</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 (ustring title, ustring message, int defaultButton = 0, Border border = null, params ustring [] buttons)
  153. {
  154. return QueryFull (false, 0, 0, title, message, defaultButton, border, 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(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  168. /// </remarks>
  169. public static int ErrorQuery (int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
  170. {
  171. return QueryFull (true, width, height, title, message, defaultButton, null, 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 (ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
  186. {
  187. return QueryFull (true, 0, 0, title, message, defaultButton, null, 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="border">The border settings.</param>
  199. /// <param name="buttons">Array of buttons to add.</param>
  200. /// <remarks>
  201. /// Use <see cref="ErrorQuery(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  202. /// </remarks>
  203. public static int ErrorQuery (int width, int height, ustring title, ustring message, int defaultButton = 0, Border border = null, params ustring [] buttons)
  204. {
  205. return QueryFull (true, width, height, title, message, defaultButton, border, 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="border">The border settings.</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 (ustring title, ustring message, int defaultButton = 0, Border border = null, params ustring [] buttons)
  221. {
  222. return QueryFull (true, 0, 0, title, message, defaultButton, border, buttons);
  223. }
  224. static int QueryFull (bool useErrorColors, int width, int height, ustring title, ustring message,
  225. int defaultButton = 0, Border border = null, params ustring [] buttons)
  226. {
  227. int defaultWidth = 50;
  228. if (defaultWidth > Application.Driver.Cols / 2) {
  229. defaultWidth = (int)(Application.Driver.Cols * 0.60f);
  230. }
  231. int maxWidthLine = TextFormatter.MaxWidthLine (message);
  232. if (maxWidthLine > Application.Driver.Cols) {
  233. maxWidthLine = Application.Driver.Cols;
  234. }
  235. if (width == 0) {
  236. maxWidthLine = Math.Max (maxWidthLine, defaultWidth);
  237. } else {
  238. maxWidthLine = width;
  239. }
  240. int textWidth = Math.Min (TextFormatter.MaxWidth (message, maxWidthLine), Application.Driver.Cols);
  241. int textHeight = TextFormatter.MaxLines (message, textWidth); // message.Count (ustring.Make ('\n')) + 1;
  242. int msgboxHeight = Math.Min (Math.Max (1, textHeight) + 4, Application.Driver.Rows); // textHeight + (top + top padding + buttons + bottom)
  243. // Create button array for Dialog
  244. int count = 0;
  245. List<Button> buttonList = new List<Button> ();
  246. if (buttons != null && defaultButton > buttons.Length - 1) {
  247. defaultButton = buttons.Length - 1;
  248. }
  249. foreach (var s in buttons) {
  250. var b = new Button (s);
  251. if (count == defaultButton) {
  252. b.IsDefault = true;
  253. }
  254. buttonList.Add (b);
  255. count++;
  256. }
  257. // Create Dialog (retain backwards compat by supporting specifying height/width)
  258. Dialog d;
  259. if (width == 0 & height == 0) {
  260. d = new Dialog (title, buttonList.ToArray ()) {
  261. Height = msgboxHeight
  262. };
  263. } else {
  264. d = new Dialog (title, width, Math.Max (height, 4), buttonList.ToArray ());
  265. }
  266. if (border != null) {
  267. d.Border = border;
  268. }
  269. if (useErrorColors) {
  270. d.ColorScheme = Colors.Error;
  271. }
  272. if (message != null) {
  273. var l = new Label (message) {
  274. LayoutStyle = LayoutStyle.Computed,
  275. TextAlignment = TextAlignment.Centered,
  276. X = Pos.Center (),
  277. Y = Pos.Center (),
  278. Width = Dim.Fill (),
  279. Height = Dim.Fill (1),
  280. AutoSize = false
  281. };
  282. d.Add (l);
  283. }
  284. if (width == 0 & height == 0) {
  285. // Dynamically size Width
  286. d.Width = Math.Min (Math.Max (maxWidthLine, Math.Max (title.ConsoleWidth, Math.Max (textWidth + 2, d.GetButtonsWidth () + d.buttons.Count + 2))), Application.Driver.Cols); // textWidth + (left + padding + padding + right)
  287. }
  288. // Setup actions
  289. Clicked = -1;
  290. for (int n = 0; n < buttonList.Count; n++) {
  291. int buttonId = n;
  292. var b = buttonList [n];
  293. b.Clicked += () => {
  294. Clicked = buttonId;
  295. Application.RequestStop ();
  296. };
  297. if (b.IsDefault) {
  298. b.SetFocus ();
  299. }
  300. }
  301. // Run the modal; do not shutdown the mainloop driver when done
  302. Application.Run (d);
  303. return Clicked;
  304. }
  305. /// <summary>
  306. /// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
  307. /// This is useful for web based console where by default there is no SynchronizationContext or TaskScheduler.
  308. /// </summary>
  309. public static int Clicked { get; private set; } = -1;
  310. }
  311. }