MessageBoxes.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata ("MessageBoxes", "Demonstrates how to use the MessageBox class.")]
  7. [ScenarioCategory ("Controls")]
  8. [ScenarioCategory ("Dialogs")]
  9. public class MessageBoxes : Scenario
  10. {
  11. public override void Main ()
  12. {
  13. Application.Init ();
  14. Window app = new ()
  15. {
  16. Title = GetQuitKeyAndName (),
  17. };
  18. var frame = new FrameView
  19. {
  20. X = Pos.Center (),
  21. Y = 1,
  22. Width = Dim.Percent (75),
  23. Height = Dim.Auto (DimAutoStyle.Content),
  24. Title = "MessageBox Options"
  25. };
  26. app.Add (frame);
  27. // TODO: Use Pos.Align her to demo aligning labels and fields
  28. var label = new Label { X = 0, Y = 0, Width = 15, TextAlignment = Alignment.End, Text = "W_idth:" };
  29. frame.Add (label);
  30. var widthEdit = new TextField
  31. {
  32. X = Pos.Right (label) + 1,
  33. Y = Pos.Top (label),
  34. Width = 5,
  35. Height = 1,
  36. Text = "0"
  37. };
  38. frame.Add (widthEdit);
  39. label = new ()
  40. {
  41. X = 0,
  42. Y = Pos.Bottom (label),
  43. Width = Dim.Width (label),
  44. Height = 1,
  45. TextAlignment = Alignment.End,
  46. Text = "_Height:"
  47. };
  48. frame.Add (label);
  49. var heightEdit = new TextField
  50. {
  51. X = Pos.Right (label) + 1,
  52. Y = Pos.Top (label),
  53. Width = 5,
  54. Height = 1,
  55. Text = "0"
  56. };
  57. frame.Add (heightEdit);
  58. frame.Add (
  59. new Label
  60. {
  61. X = Pos.Right (widthEdit) + 2,
  62. Y = Pos.Top (widthEdit),
  63. Text = $"If width is 0, the dimension will be greater than {MessageBox.DefaultMinimumWidth}%."
  64. }
  65. );
  66. frame.Add (
  67. new Label
  68. {
  69. X = Pos.Right (heightEdit) + 2,
  70. Y = Pos.Top (heightEdit),
  71. Text = $"If height is 0, the dimension will be greater than {MessageBox.DefaultMinimumHeight}%."
  72. }
  73. );
  74. label = new ()
  75. {
  76. X = 0,
  77. Y = Pos.Bottom (label),
  78. Width = Dim.Width (label),
  79. Height = 1,
  80. TextAlignment = Alignment.End,
  81. Text = "_Title:"
  82. };
  83. frame.Add (label);
  84. var titleEdit = new TextField
  85. {
  86. X = Pos.Right (label) + 1,
  87. Y = Pos.Top (label),
  88. Width = Dim.Fill (),
  89. Height = 1,
  90. Text = "The title"
  91. };
  92. frame.Add (titleEdit);
  93. label = new ()
  94. {
  95. X = 0,
  96. Y = Pos.Bottom (label),
  97. Width = Dim.Width (label),
  98. Height = 1,
  99. TextAlignment = Alignment.End,
  100. Text = "_Message:"
  101. };
  102. frame.Add (label);
  103. var messageEdit = new TextView
  104. {
  105. 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.",
  106. X = Pos.Right (label) + 1,
  107. Y = Pos.Top (label),
  108. Width = Dim.Fill (),
  109. Height = 5
  110. };
  111. frame.Add (messageEdit);
  112. label = new ()
  113. {
  114. X = 0,
  115. Y = Pos.Bottom (messageEdit),
  116. Width = Dim.Width (label),
  117. Height = 1,
  118. TextAlignment = Alignment.End,
  119. Text = "_Num Buttons:"
  120. };
  121. frame.Add (label);
  122. var numButtonsEdit = new TextField
  123. {
  124. X = Pos.Right (label) + 1,
  125. Y = Pos.Top (label),
  126. Width = 5,
  127. Height = 1,
  128. Text = "3"
  129. };
  130. frame.Add (numButtonsEdit);
  131. label = new ()
  132. {
  133. X = 0,
  134. Y = Pos.Bottom (label),
  135. Width = Dim.Width (label),
  136. Height = 1,
  137. TextAlignment = Alignment.End,
  138. Text = "_Default Button:"
  139. };
  140. frame.Add (label);
  141. var defaultButtonEdit = new TextField
  142. {
  143. X = Pos.Right (label) + 1,
  144. Y = Pos.Top (label),
  145. Width = 5,
  146. Height = 1,
  147. Text = "0"
  148. };
  149. frame.Add (defaultButtonEdit);
  150. label = new ()
  151. {
  152. X = 0,
  153. Y = Pos.Bottom (label),
  154. Width = Dim.Width (label),
  155. Height = 1,
  156. TextAlignment = Alignment.End,
  157. Text = "St_yle:"
  158. };
  159. frame.Add (label);
  160. var styleRadioGroup = new RadioGroup
  161. {
  162. X = Pos.Right (label) + 1,
  163. Y = Pos.Top (label),
  164. RadioLabels = ["_Query", "_Error"],
  165. };
  166. frame.Add (styleRadioGroup);
  167. label = new ()
  168. {
  169. X = 0,
  170. Y = Pos.Bottom (styleRadioGroup),
  171. Width = Dim.Width (label),
  172. Height = 1,
  173. TextAlignment = Alignment.End,
  174. Text = "Wra_p:"
  175. };
  176. var ckbWrapMessage = new CheckBox
  177. {
  178. X = Pos.Right (label) + 1, Y = Pos.Bottom (styleRadioGroup),
  179. CheckedState = CheckState.Checked,
  180. Text = "_Wrap Message",
  181. };
  182. frame.Add (label, ckbWrapMessage);
  183. frame.ValidatePosDim = true;
  184. label = new ()
  185. {
  186. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, TextAlignment = Alignment.End, Text = "Button Pressed:"
  187. };
  188. app.Add (label);
  189. var buttonPressedLabel = new Label
  190. {
  191. X = Pos.Center (),
  192. Y = Pos.Bottom (label) + 1,
  193. SchemeName = "Error",
  194. TextAlignment = Alignment.Center,
  195. Text = " "
  196. };
  197. var showMessageBoxButton = new Button
  198. {
  199. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show MessageBox"
  200. };
  201. app.Accepting += (s, e) =>
  202. {
  203. try
  204. {
  205. int width = int.Parse (widthEdit.Text);
  206. int height = int.Parse (heightEdit.Text);
  207. int numButtons = int.Parse (numButtonsEdit.Text);
  208. int defaultButton = int.Parse (defaultButtonEdit.Text);
  209. List<string> btns = new ();
  210. for (var i = 0; i < numButtons; i++)
  211. {
  212. btns.Add ($"_{NumberToWords.Convert (i)}");
  213. }
  214. if (styleRadioGroup.SelectedItem == 0)
  215. {
  216. buttonPressedLabel.Text =
  217. $"{MessageBox.Query (
  218. width,
  219. height,
  220. titleEdit.Text,
  221. messageEdit.Text,
  222. defaultButton,
  223. ckbWrapMessage.CheckedState == CheckState.Checked,
  224. btns.ToArray ()
  225. )}";
  226. }
  227. else
  228. {
  229. buttonPressedLabel.Text =
  230. $"{MessageBox.ErrorQuery (
  231. width,
  232. height,
  233. titleEdit.Text,
  234. messageEdit.Text,
  235. defaultButton,
  236. ckbWrapMessage.CheckedState == CheckState.Checked,
  237. btns.ToArray ()
  238. )}";
  239. }
  240. }
  241. catch (FormatException)
  242. {
  243. buttonPressedLabel.Text = "Invalid Options";
  244. }
  245. e.Handled = true;
  246. };
  247. app.Add (showMessageBoxButton);
  248. app.Add (buttonPressedLabel);
  249. Application.Run (app);
  250. app.Dispose ();
  251. Application.Shutdown ();
  252. }
  253. public override List<Key> GetDemoKeyStrokes ()
  254. {
  255. var keys = new List<Key> ();
  256. keys.Add (Key.S.WithAlt);
  257. keys.Add (Key.Esc);
  258. keys.Add (Key.E.WithAlt);
  259. keys.Add (Key.S.WithAlt);
  260. keys.Add (Key.Esc);
  261. keys.Add (Key.N.WithAlt);
  262. keys.Add (Key.D5);
  263. keys.Add (Key.S.WithAlt);
  264. keys.Add (Key.Enter);
  265. keys.Add (Key.T.WithAlt);
  266. keys.Add (Key.T.WithCtrl);
  267. keys.AddRange (GetKeysFromText ("This is a really long title"));
  268. keys.Add (Key.M.WithAlt);
  269. keys.Add (Key.T.WithCtrl);
  270. keys.AddRange (GetKeysFromText ("This is a long,\nmulti-line message.\nThis is a test of the emergency\nbroadcast\nsystem."));
  271. keys.Add (Key.S.WithAlt);
  272. for (int i = 0; i < 10; i++)
  273. {
  274. keys.Add (Key.Tab);
  275. }
  276. keys.Add (Key.Enter);
  277. keys.Add (Key.W.WithAlt);
  278. keys.Add (Key.S.WithAlt);
  279. for (int i = 0; i < 10; i++)
  280. {
  281. keys.Add (Key.Tab);
  282. }
  283. keys.Add (Key.Enter);
  284. return keys;
  285. }
  286. List<Key> GetKeysFromText (string text)
  287. {
  288. List<Key> keys = new ();
  289. foreach (var r in text)
  290. {
  291. keys.Add (r);
  292. }
  293. return keys;
  294. }
  295. }