MessageBoxes.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 = "W_idth:" };
  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
  59. {
  60. X = Pos.Right (widthEdit) + 2,
  61. Y = Pos.Top (widthEdit),
  62. Text = $"If width is 0, the dimension will be greater than {MessageBox.DefaultMinimumWidth}%."
  63. }
  64. );
  65. frame.Add (
  66. new Label
  67. {
  68. X = Pos.Right (heightEdit) + 2,
  69. Y = Pos.Top (heightEdit),
  70. Text = $"If height is 0, the dimension will be greater than {MessageBox.DefaultMinimumHeight}%."
  71. }
  72. );
  73. label = new ()
  74. {
  75. X = 0,
  76. Y = Pos.Bottom (label),
  77. Width = Dim.Width (label),
  78. Height = 1,
  79. TextAlignment = Alignment.End,
  80. Text = "_Title:"
  81. };
  82. frame.Add (label);
  83. var titleEdit = new TextField
  84. {
  85. X = Pos.Right (label) + 1,
  86. Y = Pos.Top (label),
  87. Width = Dim.Fill (),
  88. Height = 1,
  89. Text = "The title"
  90. };
  91. frame.Add (titleEdit);
  92. label = new ()
  93. {
  94. X = 0,
  95. Y = Pos.Bottom (label),
  96. Width = Dim.Width (label),
  97. Height = 1,
  98. TextAlignment = Alignment.End,
  99. Text = "_Message:"
  100. };
  101. frame.Add (label);
  102. var messageEdit = new TextView
  103. {
  104. 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.",
  105. X = Pos.Right (label) + 1,
  106. Y = Pos.Top (label),
  107. Width = Dim.Fill (),
  108. Height = 5
  109. };
  110. frame.Add (messageEdit);
  111. label = new ()
  112. {
  113. X = 0,
  114. Y = Pos.Bottom (messageEdit),
  115. Width = Dim.Width (label),
  116. Height = 1,
  117. TextAlignment = Alignment.End,
  118. Text = "_Num Buttons:"
  119. };
  120. frame.Add (label);
  121. var numButtonsEdit = new TextField
  122. {
  123. X = Pos.Right (label) + 1,
  124. Y = Pos.Top (label),
  125. Width = 5,
  126. Height = 1,
  127. Text = "3"
  128. };
  129. frame.Add (numButtonsEdit);
  130. label = new ()
  131. {
  132. X = 0,
  133. Y = Pos.Bottom (label),
  134. Width = Dim.Width (label),
  135. Height = 1,
  136. TextAlignment = Alignment.End,
  137. Text = "_Default Button:"
  138. };
  139. frame.Add (label);
  140. var defaultButtonEdit = new TextField
  141. {
  142. X = Pos.Right (label) + 1,
  143. Y = Pos.Top (label),
  144. Width = 5,
  145. Height = 1,
  146. Text = "0"
  147. };
  148. frame.Add (defaultButtonEdit);
  149. label = new ()
  150. {
  151. X = 0,
  152. Y = Pos.Bottom (label),
  153. Width = Dim.Width (label),
  154. Height = 1,
  155. TextAlignment = Alignment.End,
  156. Text = "St_yle:"
  157. };
  158. frame.Add (label);
  159. var styleRadioGroup = new RadioGroup
  160. {
  161. X = Pos.Right (label) + 1, Y = Pos.Top (label), RadioLabels = new [] { "_Query", "_Error" }
  162. };
  163. frame.Add (styleRadioGroup);
  164. label = new ()
  165. {
  166. X = 0,
  167. Y = Pos.Bottom (styleRadioGroup),
  168. Width = Dim.Width (label),
  169. Height = 1,
  170. TextAlignment = Alignment.End,
  171. Text = "Wra_p:"
  172. };
  173. var ckbWrapMessage = new CheckBox
  174. {
  175. X = Pos.Right (label) + 1, Y = Pos.Bottom (styleRadioGroup),
  176. CheckedState = CheckState.Checked,
  177. Text = "_Wrap Message",
  178. };
  179. frame.Add (label, ckbWrapMessage);
  180. frame.ValidatePosDim = true;
  181. label = new ()
  182. {
  183. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, TextAlignment = Alignment.End, Text = "Button Pressed:"
  184. };
  185. app.Add (label);
  186. var buttonPressedLabel = new Label
  187. {
  188. X = Pos.Center (),
  189. Y = Pos.Bottom (label) + 1,
  190. ColorScheme = Colors.ColorSchemes ["Error"],
  191. TextAlignment = Alignment.Center,
  192. Text = " "
  193. };
  194. var showMessageBoxButton = new Button
  195. {
  196. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show MessageBox"
  197. };
  198. app.Accepting += (s, e) =>
  199. {
  200. try
  201. {
  202. int width = int.Parse (widthEdit.Text);
  203. int height = int.Parse (heightEdit.Text);
  204. int numButtons = int.Parse (numButtonsEdit.Text);
  205. int defaultButton = int.Parse (defaultButtonEdit.Text);
  206. List<string> btns = new ();
  207. for (var i = 0; i < numButtons; i++)
  208. {
  209. btns.Add ($"_{NumberToWords.Convert (i)}");
  210. }
  211. if (styleRadioGroup.SelectedItem == 0)
  212. {
  213. buttonPressedLabel.Text =
  214. $"{MessageBox.Query (
  215. width,
  216. height,
  217. titleEdit.Text,
  218. messageEdit.Text,
  219. defaultButton,
  220. ckbWrapMessage.CheckedState == CheckState.Checked,
  221. btns.ToArray ()
  222. )}";
  223. }
  224. else
  225. {
  226. buttonPressedLabel.Text =
  227. $"{MessageBox.ErrorQuery (
  228. width,
  229. height,
  230. titleEdit.Text,
  231. messageEdit.Text,
  232. defaultButton,
  233. ckbWrapMessage.CheckedState == CheckState.Checked,
  234. btns.ToArray ()
  235. )}";
  236. }
  237. }
  238. catch (FormatException)
  239. {
  240. buttonPressedLabel.Text = "Invalid Options";
  241. }
  242. e.Cancel = true;
  243. };
  244. app.Add (showMessageBoxButton);
  245. app.Add (buttonPressedLabel);
  246. Application.Run (app);
  247. app.Dispose ();
  248. Application.Shutdown ();
  249. }
  250. }