Dialogs.cs 11 KB

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