Dialogs.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using System;
  2. using System.Collections.Generic;
  3. using Terminal.Gui;
  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 Setup ()
  11. {
  12. var frame = new FrameView { X = Pos.Center (), Y = 1, Width = Dim.Percent (75), Title = "Dialog Options" };
  13. var numButtonsLabel = new Label
  14. {
  15. X = 0,
  16. TextAlignment = TextAlignment.Right,
  17. Text = "_Number of Buttons:"
  18. };
  19. var label = new Label
  20. {
  21. X = 0,
  22. Y = 0,
  23. Width = Dim.Width (numButtonsLabel),
  24. Height = 1,
  25. TextAlignment = TextAlignment.Right,
  26. Text = "_Width:"
  27. };
  28. frame.Add (label);
  29. var widthEdit = new TextField
  30. {
  31. X = Pos.Right (numButtonsLabel) + 1,
  32. Y = Pos.Top (label),
  33. Width = 5,
  34. Height = 1,
  35. Text = "0"
  36. };
  37. frame.Add (widthEdit);
  38. label = new Label
  39. {
  40. X = 0,
  41. Y = Pos.Bottom (label),
  42. Width = Dim.Width (numButtonsLabel),
  43. Height = 1,
  44. TextAlignment = TextAlignment.Right,
  45. Text = "_Height:"
  46. };
  47. frame.Add (label);
  48. var heightEdit = new TextField
  49. {
  50. X = Pos.Right (numButtonsLabel) + 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 { X = Pos.Right (widthEdit) + 2, Y = Pos.Top (widthEdit), Text = "If height & width are both 0," }
  59. );
  60. frame.Add (
  61. new Label
  62. {
  63. X = Pos.Right (heightEdit) + 2,
  64. Y = Pos.Top (heightEdit),
  65. Text = "the Dialog will size to 80% of container."
  66. }
  67. );
  68. label = new Label
  69. {
  70. AutoSize = false,
  71. X = 0,
  72. Y = Pos.Bottom (label),
  73. Width = Dim.Width (numButtonsLabel),
  74. Height = 1,
  75. TextAlignment = TextAlignment.Right,
  76. Text = "_Title:"
  77. };
  78. frame.Add (label);
  79. var titleEdit = new TextField
  80. {
  81. X = Pos.Right (label) + 1,
  82. Y = Pos.Top (label),
  83. Width = Dim.Fill (),
  84. Height = 1,
  85. Text = "Title"
  86. };
  87. frame.Add (titleEdit);
  88. numButtonsLabel.Y = Pos.Bottom (label);
  89. frame.Add (numButtonsLabel);
  90. var numButtonsEdit = new TextField
  91. {
  92. X = Pos.Right (numButtonsLabel) + 1,
  93. Y = Pos.Top (numButtonsLabel),
  94. Width = 5,
  95. Height = 1,
  96. Text = "3"
  97. };
  98. frame.Add (numButtonsEdit);
  99. var glyphsNotWords = new CheckBox
  100. {
  101. X = Pos.Right (numButtonsLabel) + 1,
  102. Y = Pos.Bottom (numButtonsLabel),
  103. TextAlignment = TextAlignment.Right,
  104. Text = $"_Add {char.ConvertFromUtf32 (CODE_POINT)} to button text to stress wide char support",
  105. Checked = false
  106. };
  107. frame.Add (glyphsNotWords);
  108. label = new Label
  109. {
  110. AutoSize = false,
  111. X = 0,
  112. Y = Pos.Bottom (glyphsNotWords),
  113. Width = Dim.Width (numButtonsLabel),
  114. Height = 1,
  115. TextAlignment = TextAlignment.Right,
  116. Text = "Button St_yle:"
  117. };
  118. frame.Add (label);
  119. var styleRadioGroup = new RadioGroup
  120. {
  121. X = Pos.Right (label) + 1,
  122. Y = Pos.Top (label),
  123. RadioLabels = new [] { "_Center", "_Justify", "_Left", "_Right" }
  124. };
  125. frame.Add (styleRadioGroup);
  126. frame.ValidatePosDim = true;
  127. void Top_LayoutComplete (object sender, EventArgs args)
  128. {
  129. frame.Height =
  130. widthEdit.Frame.Height
  131. + heightEdit.Frame.Height
  132. + titleEdit.Frame.Height
  133. + numButtonsEdit.Frame.Height
  134. + glyphsNotWords.Frame.Height
  135. + styleRadioGroup.Frame.Height
  136. + frame.GetAdornmentsThickness ().Vertical;
  137. }
  138. Top.LayoutComplete += Top_LayoutComplete;
  139. Win.Add (frame);
  140. label = new Label
  141. {
  142. X = Pos.Center (), Y = Pos.Bottom (frame) + 4, TextAlignment = TextAlignment.Right, Text = "Button Pressed:"
  143. };
  144. Win.Add (label);
  145. var buttonPressedLabel = new Label
  146. {
  147. X = Pos.Center (), Y = Pos.Bottom (frame) + 5, ColorScheme = Colors.ColorSchemes ["Error"], Text = " "
  148. };
  149. // glyphsNotWords
  150. // false:var btnText = new [] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
  151. // true: var btnText = new [] { "0", "\u2780", "➁", "\u2783", "\u2784", "\u2785", "\u2786", "\u2787", "\u2788", "\u2789" };
  152. // \u2781 is ➁ dingbats \ufb70 is
  153. var showDialogButton = new Button
  154. {
  155. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show Dialog"
  156. };
  157. showDialogButton.Accept += (s, e) =>
  158. {
  159. Dialog dlg = CreateDemoDialog (
  160. widthEdit,
  161. heightEdit,
  162. titleEdit,
  163. numButtonsEdit,
  164. glyphsNotWords,
  165. styleRadioGroup,
  166. buttonPressedLabel
  167. );
  168. Application.Run (dlg);
  169. dlg.Dispose ();
  170. };
  171. Win.Add (showDialogButton);
  172. Win.Add (buttonPressedLabel);
  173. }
  174. private Dialog CreateDemoDialog (
  175. TextField widthEdit,
  176. TextField heightEdit,
  177. TextField titleEdit,
  178. TextField numButtonsEdit,
  179. CheckBox glyphsNotWords,
  180. RadioGroup styleRadioGroup,
  181. Label buttonPressedLabel
  182. )
  183. {
  184. Dialog dialog = null;
  185. try
  186. {
  187. var width = 0;
  188. int.TryParse (widthEdit.Text, out width);
  189. var height = 0;
  190. int.TryParse (heightEdit.Text, out height);
  191. var numButtons = 3;
  192. int.TryParse (numButtonsEdit.Text, out numButtons);
  193. List<Button> buttons = new ();
  194. int clicked = -1;
  195. for (var i = 0; i < numButtons; i++)
  196. {
  197. int buttonId = i;
  198. Button button = null;
  199. if (glyphsNotWords.Checked == true)
  200. {
  201. buttonId = i;
  202. button = new Button
  203. {
  204. Text = NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
  205. IsDefault = buttonId == 0
  206. };
  207. }
  208. else
  209. {
  210. button = new Button { Text = NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
  211. }
  212. button.Accept += (s, e) =>
  213. {
  214. clicked = buttonId;
  215. Application.RequestStop ();
  216. };
  217. buttons.Add (button);
  218. }
  219. //if (buttons.Count > 1) {
  220. // buttons [1].Text = "Accept";
  221. // buttons [1].IsDefault = true;
  222. // buttons [0].Visible = false;
  223. // buttons [0].Text = "_Back";
  224. // buttons [0].IsDefault = false;
  225. //}
  226. // This tests dynamically adding buttons; ensuring the dialog resizes if needed and
  227. // the buttons are laid out correctly
  228. dialog = new Dialog
  229. {
  230. Title = titleEdit.Text,
  231. ButtonAlignment = (Dialog.ButtonAlignments)styleRadioGroup.SelectedItem,
  232. Buttons = buttons.ToArray ()
  233. };
  234. if (height != 0 || width != 0)
  235. {
  236. dialog.Height = height;
  237. dialog.Width = width;
  238. }
  239. var add = new Button { X = Pos.Center (), Y = Pos.Center (), Text = "_Add a button" };
  240. add.Accept += (s, e) =>
  241. {
  242. int buttonId = buttons.Count;
  243. Button button;
  244. if (glyphsNotWords.Checked == true)
  245. {
  246. button = new Button
  247. {
  248. Text = NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
  249. IsDefault = buttonId == 0
  250. };
  251. }
  252. else
  253. {
  254. button = new Button { Text = NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
  255. }
  256. button.Accept += (s, e) =>
  257. {
  258. clicked = buttonId;
  259. Application.RequestStop ();
  260. };
  261. buttons.Add (button);
  262. dialog.AddButton (button);
  263. if (buttons.Count > 1)
  264. {
  265. button.TabIndex = buttons [buttons.Count - 2].TabIndex + 1;
  266. }
  267. };
  268. dialog.Add (add);
  269. var addChar = new Button
  270. {
  271. X = Pos.Center (),
  272. Y = Pos.Center () + 1,
  273. Text = $"A_dd a {char.ConvertFromUtf32 (CODE_POINT)} to each button"
  274. };
  275. addChar.Accept += (s, e) =>
  276. {
  277. foreach (Button button in buttons)
  278. {
  279. button.Text += char.ConvertFromUtf32 (CODE_POINT);
  280. }
  281. dialog.LayoutSubviews ();
  282. };
  283. dialog.Closed += (s, e) => { buttonPressedLabel.Text = $"{clicked}"; };
  284. dialog.Add (addChar);
  285. }
  286. catch (FormatException)
  287. {
  288. buttonPressedLabel.Text = "Invalid Options";
  289. }
  290. return dialog;
  291. }
  292. }