MessageBox.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Terminal.Gui {
  6. /// <summary>
  7. /// MessageBox displays a modal message to the user, with a title, a message and a series of options that the user can choose from.
  8. /// </summary>
  9. /// <para>
  10. /// The difference between the <see cref="Query(ustring, ustring, ustring[])"/> and <see cref="ErrorQuery(ustring, ustring, ustring[])"/>
  11. /// method is the default set of colors used for the message box.
  12. /// </para>
  13. /// <para>
  14. /// The following example pops up a <see cref="MessageBox"/> with the specified title and text, plus two <see cref="Button"/>s.
  15. /// The value -1 is returned when the user cancels the <see cref="MessageBox"/> by pressing the ESC key.
  16. /// </para>
  17. /// <example>
  18. /// <code lang="c#">
  19. /// var n = MessageBox.Query ("Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
  20. /// if (n == 0)
  21. /// quit = true;
  22. /// else
  23. /// quit = false;
  24. /// </code>
  25. /// </example>
  26. public static class MessageBox {
  27. /// <summary>
  28. /// Presents a normal <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  29. /// </summary>
  30. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  31. /// <param name="width">Width for the window.</param>
  32. /// <param name="height">Height for the window.</param>
  33. /// <param name="title">Title for the query.</param>
  34. /// <param name="message">Message to display, might contain multiple lines.</param>
  35. /// <param name="buttons">Array of buttons to add.</param>
  36. /// <remarks>
  37. /// Use <see cref="Query(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  38. /// </remarks>
  39. public static int Query (int width, int height, ustring title, ustring message, params ustring [] buttons)
  40. {
  41. return QueryFull (false, width, height, title, message, buttons);
  42. }
  43. /// <summary>
  44. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  45. /// </summary>
  46. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  47. /// <param name="title">Title for the query.</param>
  48. /// <param name="message">Message to display, might contain multiple lines.</param>
  49. /// <param name="buttons">Array of buttons to add.</param>
  50. /// <remarks>
  51. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  52. /// from the size of the message and buttons.
  53. /// </remarks>
  54. public static int Query (ustring title, ustring message, params ustring [] buttons)
  55. {
  56. return QueryFull (false, 0, 0, title, message, buttons);
  57. }
  58. /// <summary>
  59. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  60. /// </summary>
  61. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  62. /// <param name="width">Width for the window.</param>
  63. /// <param name="height">Height for the window.</param>
  64. /// <param name="title">Title for the query.</param>
  65. /// <param name="message">Message to display, might contain multiple lines.</param>
  66. /// <param name="buttons">Array of buttons to add.</param>
  67. /// <remarks>
  68. /// Use <see cref="ErrorQuery(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
  69. /// </remarks>
  70. public static int ErrorQuery (int width, int height, ustring title, ustring message, params ustring [] buttons)
  71. {
  72. return QueryFull (true, width, height, title, message, buttons);
  73. }
  74. /// <summary>
  75. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
  76. /// </summary>
  77. /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
  78. /// <param name="title">Title for the query.</param>
  79. /// <param name="message">Message to display, might contain multiple lines.</param>
  80. /// <param name="buttons">Array of buttons to add.</param>
  81. /// <remarks>
  82. /// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
  83. /// from the size of the title, message. and buttons.
  84. /// </remarks>
  85. public static int ErrorQuery (ustring title, ustring message, params ustring [] buttons)
  86. {
  87. return QueryFull (true, 0, 0, title, message, buttons);
  88. }
  89. static int QueryFull (bool useErrorColors, int width, int height, ustring title, ustring message, params ustring [] buttons)
  90. {
  91. const int defaultWidth = 50;
  92. int textWidth = TextFormatter.MaxWidth (message, width == 0 ? defaultWidth : width);
  93. int textHeight = TextFormatter.MaxLines (message, textWidth); // message.Count (ustring.Make ('\n')) + 1;
  94. int msgboxHeight = Math.Max (1, textHeight) + 3; // textHeight + (top + top padding + buttons + bottom)
  95. // Create button array for Dialog
  96. int count = 0;
  97. List<Button> buttonList = new List<Button> ();
  98. foreach (var s in buttons) {
  99. var b = new Button (s);
  100. if (count == 0) {
  101. b.IsDefault = true;
  102. }
  103. buttonList.Add (b);
  104. count++;
  105. }
  106. // Create Dialog (retain backwards compat by supporting specifying height/width)
  107. Dialog d;
  108. if (width == 0 & height == 0) {
  109. d = new Dialog (title, buttonList.ToArray ());
  110. d.Height = msgboxHeight;
  111. } else {
  112. d = new Dialog (title, Math.Max (width, textWidth) + 4, height, buttonList.ToArray ());
  113. }
  114. if (useErrorColors) {
  115. d.ColorScheme = Colors.Error;
  116. }
  117. if (message != null) {
  118. var l = new Label (textWidth > width ? 0 : (width - 4 - textWidth) / 2, 1, message);
  119. l.LayoutStyle = LayoutStyle.Computed;
  120. l.TextAlignment = TextAlignment.Centered;
  121. l.X = Pos.Center ();
  122. l.Y = Pos.Center ();
  123. l.Width = Dim.Fill (2);
  124. l.Height = Dim.Fill (1);
  125. d.Add (l);
  126. }
  127. // Dynamically size Width
  128. int msgboxWidth = Math.Max (defaultWidth, Math.Max (title.RuneCount + 8, Math.Max (textWidth + 4, d.GetButtonsWidth ()) + 8)); // textWidth + (left + padding + padding + right)
  129. d.Width = msgboxWidth;
  130. // Setup actions
  131. int clicked = -1;
  132. for (int n = 0; n < buttonList.Count; n++) {
  133. int buttonId = n;
  134. buttonList [n].Clicked += () => {
  135. clicked = buttonId;
  136. Application.RequestStop ();
  137. };
  138. }
  139. // Rin the modal; do not shutdown the mainloop driver when done
  140. Application.Run (d, false);
  141. return clicked;
  142. }
  143. }
  144. }