MessageBoxes.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  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,
  162. Y = Pos.Top (label),
  163. RadioLabels = ["_Query", "_Error"],
  164. };
  165. frame.Add (styleRadioGroup);
  166. label = new ()
  167. {
  168. X = 0,
  169. Y = Pos.Bottom (styleRadioGroup),
  170. Width = Dim.Width (label),
  171. Height = 1,
  172. TextAlignment = Alignment.End,
  173. Text = "Wra_p:"
  174. };
  175. var ckbWrapMessage = new CheckBox
  176. {
  177. X = Pos.Right (label) + 1, Y = Pos.Bottom (styleRadioGroup),
  178. CheckedState = CheckState.Checked,
  179. Text = "_Wrap Message",
  180. };
  181. frame.Add (label, ckbWrapMessage);
  182. frame.ValidatePosDim = true;
  183. label = new ()
  184. {
  185. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, TextAlignment = Alignment.End, Text = "Button Pressed:"
  186. };
  187. app.Add (label);
  188. var buttonPressedLabel = new Label
  189. {
  190. X = Pos.Center (),
  191. Y = Pos.Bottom (label) + 1,
  192. SchemeName = "Error",
  193. TextAlignment = Alignment.Center,
  194. Text = " "
  195. };
  196. var showMessageBoxButton = new Button
  197. {
  198. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show MessageBox"
  199. };
  200. app.Accepting += (s, e) =>
  201. {
  202. try
  203. {
  204. int width = int.Parse (widthEdit.Text);
  205. int height = int.Parse (heightEdit.Text);
  206. int numButtons = int.Parse (numButtonsEdit.Text);
  207. int defaultButton = int.Parse (defaultButtonEdit.Text);
  208. List<string> btns = new ();
  209. for (var i = 0; i < numButtons; i++)
  210. {
  211. btns.Add ($"_{NumberToWords.Convert (i)}");
  212. }
  213. if (styleRadioGroup.SelectedItem == 0)
  214. {
  215. buttonPressedLabel.Text =
  216. $"{MessageBox.Query (
  217. width,
  218. height,
  219. titleEdit.Text,
  220. messageEdit.Text,
  221. defaultButton,
  222. ckbWrapMessage.CheckedState == CheckState.Checked,
  223. btns.ToArray ()
  224. )}";
  225. }
  226. else
  227. {
  228. buttonPressedLabel.Text =
  229. $"{MessageBox.ErrorQuery (
  230. width,
  231. height,
  232. titleEdit.Text,
  233. messageEdit.Text,
  234. defaultButton,
  235. ckbWrapMessage.CheckedState == CheckState.Checked,
  236. btns.ToArray ()
  237. )}";
  238. }
  239. }
  240. catch (FormatException)
  241. {
  242. buttonPressedLabel.Text = "Invalid Options";
  243. }
  244. e.Handled = true;
  245. };
  246. app.Add (showMessageBoxButton);
  247. app.Add (buttonPressedLabel);
  248. Application.Run (app);
  249. app.Dispose ();
  250. Application.Shutdown ();
  251. }
  252. public override List<Key> GetDemoKeyStrokes ()
  253. {
  254. var keys = new List<Key> ();
  255. keys.Add (Key.S.WithAlt);
  256. keys.Add (Key.Esc);
  257. keys.Add (Key.E.WithAlt);
  258. keys.Add (Key.S.WithAlt);
  259. keys.Add (Key.Esc);
  260. keys.Add (Key.N.WithAlt);
  261. keys.Add (Key.D5);
  262. keys.Add (Key.S.WithAlt);
  263. keys.Add (Key.Enter);
  264. keys.Add (Key.T.WithAlt);
  265. keys.Add (Key.T.WithCtrl);
  266. keys.AddRange (GetKeysFromText ("This is a really long title"));
  267. keys.Add (Key.M.WithAlt);
  268. keys.Add (Key.T.WithCtrl);
  269. keys.AddRange (GetKeysFromText ("This is a long,\nmulti-line message.\nThis is a test of the emergency\nbroadcast\nsystem."));
  270. keys.Add (Key.S.WithAlt);
  271. for (int i = 0; i < 10; i++)
  272. {
  273. keys.Add (Key.Tab);
  274. }
  275. keys.Add (Key.Enter);
  276. keys.Add (Key.W.WithAlt);
  277. keys.Add (Key.S.WithAlt);
  278. for (int i = 0; i < 10; i++)
  279. {
  280. keys.Add (Key.Tab);
  281. }
  282. keys.Add (Key.Enter);
  283. return keys;
  284. }
  285. List<Key> GetKeysFromText (string text)
  286. {
  287. List<Key> keys = new ();
  288. foreach (var r in text)
  289. {
  290. keys.Add (r);
  291. }
  292. return keys;
  293. }
  294. }