MessageBoxes.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System;
  2. using System.Collections.Generic;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("MessageBoxes", "Demonstrates how to use the MessageBox class.")]
  6. [ScenarioCategory ("Controls")]
  7. [ScenarioCategory ("Dialogs")]
  8. public class MessageBoxes : Scenario
  9. {
  10. public override void Main ()
  11. {
  12. Application.Init ();
  13. Window app = new ()
  14. {
  15. Title = GetQuitKeyAndName ()
  16. };
  17. var frame = new FrameView
  18. {
  19. X = Pos.Center (),
  20. Y = 1,
  21. Width = Dim.Percent (75),
  22. Height = Dim.Auto (DimAutoStyle.Content),
  23. Title = "MessageBox Options"
  24. };
  25. app.Add (frame);
  26. // TODO: Use Pos.Align her to demo aligning labels and fields
  27. var label = new Label { X = 0, Y = 0, Width = 15, TextAlignment = Alignment.End, Text = "Width:" };
  28. frame.Add (label);
  29. var widthEdit = new TextField
  30. {
  31. X = Pos.Right (label) + 1,
  32. Y = Pos.Top (label),
  33. Width = 5,
  34. Height = 1,
  35. Text = "0"
  36. };
  37. frame.Add (widthEdit);
  38. label = new ()
  39. {
  40. X = 0,
  41. Y = Pos.Bottom (label),
  42. Width = Dim.Width (label),
  43. Height = 1,
  44. TextAlignment = Alignment.End,
  45. Text = "Height:"
  46. };
  47. frame.Add (label);
  48. var heightEdit = new TextField
  49. {
  50. X = Pos.Right (label) + 1,
  51. Y = Pos.Top (label),
  52. Width = 5,
  53. Height = 1,
  54. Text = "0"
  55. };
  56. frame.Add (heightEdit);
  57. frame.Add (
  58. new Label { X = Pos.Right (widthEdit) + 2, Y = Pos.Top (widthEdit), Text = "If height & width are both 0," }
  59. );
  60. frame.Add (
  61. new Label
  62. {
  63. X = Pos.Right (heightEdit) + 2,
  64. Y = Pos.Top (heightEdit),
  65. Text = "the MessageBox will be sized automatically."
  66. }
  67. );
  68. label = new ()
  69. {
  70. X = 0,
  71. Y = Pos.Bottom (label),
  72. Width = Dim.Width (label),
  73. Height = 1,
  74. TextAlignment = Alignment.End,
  75. Text = "Title:"
  76. };
  77. frame.Add (label);
  78. var titleEdit = new TextField
  79. {
  80. X = Pos.Right (label) + 1,
  81. Y = Pos.Top (label),
  82. Width = Dim.Fill (),
  83. Height = 1,
  84. Text = "Title"
  85. };
  86. frame.Add (titleEdit);
  87. label = new ()
  88. {
  89. X = 0,
  90. Y = Pos.Bottom (label),
  91. Width = Dim.Width (label),
  92. Height = 1,
  93. TextAlignment = Alignment.End,
  94. Text = "Message:"
  95. };
  96. frame.Add (label);
  97. var messageEdit = new TextView
  98. {
  99. Text = "Message line 1.\nMessage line two. This is a really long line to force wordwrap. It needs to be long for it to work.",
  100. X = Pos.Right (label) + 1,
  101. Y = Pos.Top (label),
  102. Width = Dim.Fill (),
  103. Height = 5
  104. };
  105. frame.Add (messageEdit);
  106. label = new ()
  107. {
  108. X = 0,
  109. Y = Pos.Bottom (messageEdit),
  110. Width = Dim.Width (label),
  111. Height = 1,
  112. TextAlignment = Alignment.End,
  113. Text = "Num Buttons:"
  114. };
  115. frame.Add (label);
  116. var numButtonsEdit = new TextField
  117. {
  118. X = Pos.Right (label) + 1,
  119. Y = Pos.Top (label),
  120. Width = 5,
  121. Height = 1,
  122. Text = "3"
  123. };
  124. frame.Add (numButtonsEdit);
  125. label = new ()
  126. {
  127. X = 0,
  128. Y = Pos.Bottom (label),
  129. Width = Dim.Width (label),
  130. Height = 1,
  131. TextAlignment = Alignment.End,
  132. Text = "Default Button:"
  133. };
  134. frame.Add (label);
  135. var defaultButtonEdit = new TextField
  136. {
  137. X = Pos.Right (label) + 1,
  138. Y = Pos.Top (label),
  139. Width = 5,
  140. Height = 1,
  141. Text = "0"
  142. };
  143. frame.Add (defaultButtonEdit);
  144. label = new ()
  145. {
  146. X = 0,
  147. Y = Pos.Bottom (label),
  148. Width = Dim.Width (label),
  149. Height = 1,
  150. TextAlignment = Alignment.End,
  151. Text = "Style:"
  152. };
  153. frame.Add (label);
  154. var styleRadioGroup = new RadioGroup
  155. {
  156. X = Pos.Right (label) + 1, Y = Pos.Top (label), RadioLabels = new [] { "_Query", "_Error" }
  157. };
  158. frame.Add (styleRadioGroup);
  159. var ckbWrapMessage = new CheckBox
  160. {
  161. X = Pos.Right (label) + 1, Y = Pos.Bottom (styleRadioGroup), Text = "_Wrap Message", State = CheckState.Checked
  162. };
  163. frame.Add (ckbWrapMessage);
  164. frame.ValidatePosDim = true;
  165. label = new ()
  166. {
  167. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, TextAlignment = Alignment.End, Text = "Button Pressed:"
  168. };
  169. app.Add (label);
  170. var buttonPressedLabel = new Label
  171. {
  172. X = Pos.Center (),
  173. Y = Pos.Bottom (label) + 1,
  174. ColorScheme = Colors.ColorSchemes ["Error"],
  175. TextAlignment = Alignment.Center,
  176. Text = " "
  177. };
  178. var showMessageBoxButton = new Button
  179. {
  180. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show MessageBox"
  181. };
  182. showMessageBoxButton.Accept += (s, e) =>
  183. {
  184. try
  185. {
  186. int width = int.Parse (widthEdit.Text);
  187. int height = int.Parse (heightEdit.Text);
  188. int numButtons = int.Parse (numButtonsEdit.Text);
  189. int defaultButton = int.Parse (defaultButtonEdit.Text);
  190. List<string> btns = new ();
  191. for (var i = 0; i < numButtons; i++)
  192. {
  193. btns.Add (NumberToWords.Convert (i));
  194. }
  195. if (styleRadioGroup.SelectedItem == 0)
  196. {
  197. buttonPressedLabel.Text =
  198. $"{MessageBox.Query (
  199. width,
  200. height,
  201. titleEdit.Text,
  202. messageEdit.Text,
  203. defaultButton,
  204. ckbWrapMessage.State == CheckState.Checked,
  205. btns.ToArray ()
  206. )}";
  207. }
  208. else
  209. {
  210. buttonPressedLabel.Text =
  211. $"{MessageBox.ErrorQuery (
  212. width,
  213. height,
  214. titleEdit.Text,
  215. messageEdit.Text,
  216. defaultButton,
  217. ckbWrapMessage.State == CheckState.Checked,
  218. btns.ToArray ()
  219. )}";
  220. }
  221. }
  222. catch (FormatException)
  223. {
  224. buttonPressedLabel.Text = "Invalid Options";
  225. }
  226. };
  227. app.Add (showMessageBoxButton);
  228. app.Add (buttonPressedLabel);
  229. Application.Run (app);
  230. app.Dispose ();
  231. Application.Shutdown ();
  232. }
  233. }