MessageBoxes.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 styleOptionSelector = new OptionSelector ()
  160. {
  161. X = Pos.Right (label) + 1,
  162. Y = Pos.Top (label),
  163. Labels = ["_Query", "_Error"],
  164. Title = "Sty_le"
  165. };
  166. frame.Add (styleOptionSelector);
  167. label = new ()
  168. {
  169. X = 0,
  170. Y = Pos.Bottom (styleOptionSelector),
  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 (styleOptionSelector),
  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 (styleOptionSelector.Value == 0)
  215. {
  216. buttonPressedLabel.Text =
  217. $"{MessageBox.Query (
  218. Application.Instance, 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 (Application.Instance,
  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. }