Dialogs.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("Dialogs", "Demonstrates how to the Dialog class")]
  6. [ScenarioCategory ("Dialogs")]
  7. public class Dialogs : Scenario
  8. {
  9. private static readonly int CODE_POINT = '你'; // We know this is a wide char
  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. TabStop = TabBehavior.TabStop, // FrameView normally sets to TabGroup
  20. X = Pos.Center (),
  21. Y = 1,
  22. Width = Dim.Percent (75),
  23. Height = Dim.Auto (DimAutoStyle.Content),
  24. Title = "Dialog Options"
  25. };
  26. var numButtonsLabel = new Label
  27. {
  28. X = 0,
  29. TextAlignment = Alignment.End,
  30. Text = "_Number of Buttons:"
  31. };
  32. var label = new Label
  33. {
  34. X = 0,
  35. Y = 0,
  36. Width = Dim.Width (numButtonsLabel),
  37. Height = 1,
  38. TextAlignment = Alignment.End,
  39. Text = "_Width:"
  40. };
  41. frame.Add (label);
  42. var widthEdit = new TextField
  43. {
  44. X = Pos.Right (numButtonsLabel) + 1,
  45. Y = Pos.Top (label),
  46. Width = 5,
  47. Height = 1,
  48. Text = "0"
  49. };
  50. frame.Add (widthEdit);
  51. label = new ()
  52. {
  53. X = 0,
  54. Y = Pos.Bottom (label),
  55. Width = Dim.Width (numButtonsLabel),
  56. Height = 1,
  57. TextAlignment = Alignment.End,
  58. Text = "_Height:"
  59. };
  60. frame.Add (label);
  61. var heightEdit = new TextField
  62. {
  63. X = Pos.Right (numButtonsLabel) + 1,
  64. Y = Pos.Top (label),
  65. Width = 5,
  66. Height = 1,
  67. Text = "0"
  68. };
  69. frame.Add (heightEdit);
  70. frame.Add (
  71. new Label
  72. {
  73. X = Pos.Right (widthEdit) + 2,
  74. Y = Pos.Top (widthEdit),
  75. Text = $"If width is 0, the dimension will be greater than {Dialog.DefaultMinimumWidth}%."
  76. }
  77. );
  78. frame.Add (
  79. new Label
  80. {
  81. X = Pos.Right (heightEdit) + 2,
  82. Y = Pos.Top (heightEdit),
  83. Text = $"If height is 0, the dimension will be greater {Dialog.DefaultMinimumHeight}%."
  84. }
  85. );
  86. label = new ()
  87. {
  88. X = 0,
  89. Y = Pos.Bottom (label),
  90. Width = Dim.Width (numButtonsLabel),
  91. Height = 1,
  92. TextAlignment = Alignment.End,
  93. Text = "_Title:"
  94. };
  95. frame.Add (label);
  96. var titleEdit = new TextField
  97. {
  98. X = Pos.Right (label) + 1,
  99. Y = Pos.Top (label),
  100. Width = Dim.Fill (),
  101. Height = 1,
  102. Text = "Title"
  103. };
  104. frame.Add (titleEdit);
  105. numButtonsLabel.Y = Pos.Bottom (label);
  106. frame.Add (numButtonsLabel);
  107. var numButtonsEdit = new TextField
  108. {
  109. X = Pos.Right (numButtonsLabel) + 1,
  110. Y = Pos.Top (numButtonsLabel),
  111. Width = 5,
  112. Height = 1,
  113. Text = "3"
  114. };
  115. frame.Add (numButtonsEdit);
  116. var glyphsNotWords = new CheckBox
  117. {
  118. X = Pos.Right (numButtonsLabel) + 1,
  119. Y = Pos.Bottom (numButtonsLabel),
  120. TextAlignment = Alignment.End,
  121. Text = $"_Add {char.ConvertFromUtf32 (CODE_POINT)} to button text to stress wide char support",
  122. CheckedState = CheckState.UnChecked
  123. };
  124. frame.Add (glyphsNotWords);
  125. label = new ()
  126. {
  127. X = 0,
  128. Y = Pos.Bottom (glyphsNotWords),
  129. Width = Dim.Width (numButtonsLabel),
  130. Height = 1,
  131. TextAlignment = Alignment.End,
  132. Text = "Button A_lignment:"
  133. };
  134. frame.Add (label);
  135. OptionSelector<Alignment> alignmentOptionSelector = new ()
  136. {
  137. X = Pos.Right (label) + 1,
  138. Y = Pos.Top (label),
  139. Title = "Ali_gn",
  140. AssignHotKeys = true
  141. };
  142. frame.Add (alignmentOptionSelector);
  143. alignmentOptionSelector.Value = Dialog.DefaultButtonAlignment;
  144. frame.ValidatePosDim = true;
  145. app.Add (frame);
  146. label = new ()
  147. {
  148. X = Pos.Center (), Y = Pos.Bottom (frame) + 4, TextAlignment = Alignment.End, Text = "Button Pressed:"
  149. };
  150. app.Add (label);
  151. var buttonPressedLabel = new Label
  152. {
  153. X = Pos.Center (), Y = Pos.Bottom (frame) + 5, SchemeName = "Error", Text = " "
  154. };
  155. var showDialogButton = new Button
  156. {
  157. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show Dialog"
  158. };
  159. app.Accepting += (s, e) =>
  160. {
  161. Dialog dlg = CreateDemoDialog (
  162. widthEdit,
  163. heightEdit,
  164. titleEdit,
  165. numButtonsEdit,
  166. glyphsNotWords,
  167. alignmentOptionSelector,
  168. buttonPressedLabel
  169. );
  170. Application.Run (dlg);
  171. dlg.Dispose ();
  172. e.Handled = true;
  173. };
  174. app.Add (showDialogButton);
  175. app.Add (buttonPressedLabel);
  176. Application.Run (app);
  177. app.Dispose ();
  178. Application.Shutdown ();
  179. }
  180. private Dialog CreateDemoDialog (
  181. TextField widthEdit,
  182. TextField heightEdit,
  183. TextField titleEdit,
  184. TextField numButtonsEdit,
  185. CheckBox glyphsNotWords,
  186. OptionSelector alignmentGroup,
  187. Label buttonPressedLabel
  188. )
  189. {
  190. Dialog dialog = null;
  191. try
  192. {
  193. var width = 0;
  194. int.TryParse (widthEdit.Text, out width);
  195. var height = 0;
  196. int.TryParse (heightEdit.Text, out height);
  197. var numButtons = 3;
  198. int.TryParse (numButtonsEdit.Text, out numButtons);
  199. List<Button> buttons = new ();
  200. int clicked = -1;
  201. for (var i = 0; i < numButtons; i++)
  202. {
  203. int buttonId = i;
  204. Button button = null;
  205. if (glyphsNotWords.CheckedState == CheckState.Checked)
  206. {
  207. buttonId = i;
  208. button = new ()
  209. {
  210. Text = "_" + NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
  211. IsDefault = buttonId == 0
  212. };
  213. }
  214. else
  215. {
  216. button = new () { Text = "_" + NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
  217. }
  218. button.Accepting += (s, e) =>
  219. {
  220. clicked = buttonId;
  221. e.Handled = true;
  222. Application.RequestStop ();
  223. };
  224. buttons.Add (button);
  225. }
  226. // This tests dynamically adding buttons; ensuring the dialog resizes if needed and
  227. // the buttons are laid out correctly
  228. dialog = new ()
  229. {
  230. Title = titleEdit.Text,
  231. Text = "Dialog Text",
  232. ButtonAlignment = (Alignment)Enum.Parse (typeof (Alignment), alignmentGroup.Labels! [(int)alignmentGroup.Value!.Value] [0..]),
  233. Buttons = buttons.ToArray ()
  234. };
  235. if (width != 0)
  236. {
  237. dialog.Width = width;
  238. }
  239. if (height != 0)
  240. {
  241. dialog.Height = height;
  242. }
  243. var add = new Button
  244. {
  245. X = Pos.Center (),
  246. Y = Pos.Center () - 1,
  247. Text = "_Add a button"
  248. };
  249. add.Accepting += (s, e) =>
  250. {
  251. int buttonId = buttons.Count;
  252. Button button;
  253. if (glyphsNotWords.CheckedState == CheckState.Checked)
  254. {
  255. button = new ()
  256. {
  257. Text = "_" + NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
  258. IsDefault = buttonId == 0
  259. };
  260. }
  261. else
  262. {
  263. button = new () { Text = "_" + NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
  264. }
  265. button.Accepting += (s, e) =>
  266. {
  267. clicked = buttonId;
  268. Application.RequestStop ();
  269. e.Handled = true;
  270. };
  271. buttons.Add (button);
  272. dialog.AddButton (button);
  273. //if (buttons.Count > 1)
  274. //{
  275. // button.TabIndex = buttons [buttons.Count - 2].TabIndex + 1;
  276. //}
  277. e.Handled = true;
  278. };
  279. dialog.Add (add);
  280. var addChar = new Button
  281. {
  282. X = Pos.Center (),
  283. Y = Pos.Center () + 1,
  284. Text = $"A_dd a {char.ConvertFromUtf32 (CODE_POINT)} to each button. This text is really long for a reason."
  285. };
  286. addChar.Accepting += (s, e) =>
  287. {
  288. foreach (Button button in buttons)
  289. {
  290. button.Text += char.ConvertFromUtf32 (CODE_POINT);
  291. }
  292. e.Handled = true;
  293. };
  294. dialog.Add (addChar);
  295. dialog.IsRunningChanged += (s, e) =>
  296. {
  297. if (!e.Value)
  298. {
  299. buttonPressedLabel.Text = $"{clicked}";
  300. }
  301. };
  302. }
  303. catch (FormatException)
  304. {
  305. buttonPressedLabel.Text = "Invalid Options";
  306. }
  307. return dialog;
  308. }
  309. public override List<Key> GetDemoKeyStrokes ()
  310. {
  311. var keys = new List<Key> ();
  312. keys.Add (Key.D6);
  313. keys.Add (Key.D5);
  314. keys.Add (Key.Tab);
  315. keys.Add (Key.D2);
  316. keys.Add (Key.D0);
  317. keys.Add (Key.Enter);
  318. for (int i = 0; i < 5; i++)
  319. {
  320. keys.Add (Key.A);
  321. }
  322. keys.Add (Key.Enter);
  323. keys.Add (Key.S.WithAlt);
  324. keys.Add (Key.Enter);
  325. for (int i = 0; i < 5; i++)
  326. {
  327. keys.Add (Key.A);
  328. }
  329. keys.Add (Key.Enter);
  330. keys.Add (Key.E.WithAlt);
  331. keys.Add (Key.Enter);
  332. for (int i = 0; i < 5; i++)
  333. {
  334. keys.Add (Key.A);
  335. }
  336. keys.Add (Key.Enter);
  337. keys.Add (Key.C.WithAlt);
  338. keys.Add (Key.Enter);
  339. for (int i = 0; i < 5; i++)
  340. {
  341. keys.Add (Key.A);
  342. }
  343. keys.Add (Key.Enter);
  344. keys.Add (Key.F.WithAlt);
  345. keys.Add (Key.Enter);
  346. for (int i = 0; i < 5; i++)
  347. {
  348. keys.Add (Key.A);
  349. }
  350. keys.Add (Key.Enter);
  351. return keys;
  352. }
  353. }