Dialogs.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 Main ()
  11. {
  12. Application.Init ();
  13. Window app = new ()
  14. {
  15. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}"
  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 = "Dialog Options"
  24. };
  25. var numButtonsLabel = new Label
  26. {
  27. X = 0,
  28. TextAlignment = TextAlignment.Right,
  29. Text = "_Number of Buttons:"
  30. };
  31. var label = new Label
  32. {
  33. X = 0,
  34. Y = 0,
  35. Width = Dim.Width (numButtonsLabel),
  36. Height = 1,
  37. TextAlignment = TextAlignment.Right,
  38. Text = "_Width:"
  39. };
  40. frame.Add (label);
  41. var widthEdit = new TextField
  42. {
  43. X = Pos.Right (numButtonsLabel) + 1,
  44. Y = Pos.Top (label),
  45. Width = 5,
  46. Height = 1,
  47. Text = "0"
  48. };
  49. frame.Add (widthEdit);
  50. label = new ()
  51. {
  52. X = 0,
  53. Y = Pos.Bottom (label),
  54. Width = Dim.Width (numButtonsLabel),
  55. Height = 1,
  56. TextAlignment = TextAlignment.Right,
  57. Text = "_Height:"
  58. };
  59. frame.Add (label);
  60. var heightEdit = new TextField
  61. {
  62. X = Pos.Right (numButtonsLabel) + 1,
  63. Y = Pos.Top (label),
  64. Width = 5,
  65. Height = 1,
  66. Text = "0"
  67. };
  68. frame.Add (heightEdit);
  69. frame.Add (
  70. new Label { X = Pos.Right (widthEdit) + 2, Y = Pos.Top (widthEdit), Text = "If height & width are both 0," }
  71. );
  72. frame.Add (
  73. new Label
  74. {
  75. X = Pos.Right (heightEdit) + 2,
  76. Y = Pos.Top (heightEdit),
  77. Text = "the Dialog will size to 80% of container."
  78. }
  79. );
  80. label = new ()
  81. {
  82. X = 0,
  83. Y = Pos.Bottom (label),
  84. Width = Dim.Width (numButtonsLabel),
  85. Height = 1,
  86. TextAlignment = TextAlignment.Right,
  87. Text = "_Title:"
  88. };
  89. frame.Add (label);
  90. var titleEdit = new TextField
  91. {
  92. X = Pos.Right (label) + 1,
  93. Y = Pos.Top (label),
  94. Width = Dim.Fill (),
  95. Height = 1,
  96. Text = "Title"
  97. };
  98. frame.Add (titleEdit);
  99. numButtonsLabel.Y = Pos.Bottom (label);
  100. frame.Add (numButtonsLabel);
  101. var numButtonsEdit = new TextField
  102. {
  103. X = Pos.Right (numButtonsLabel) + 1,
  104. Y = Pos.Top (numButtonsLabel),
  105. Width = 5,
  106. Height = 1,
  107. Text = "3"
  108. };
  109. frame.Add (numButtonsEdit);
  110. var glyphsNotWords = new CheckBox
  111. {
  112. X = Pos.Right (numButtonsLabel) + 1,
  113. Y = Pos.Bottom (numButtonsLabel),
  114. TextAlignment = TextAlignment.Right,
  115. Text = $"_Add {char.ConvertFromUtf32 (CODE_POINT)} to button text to stress wide char support",
  116. Checked = false
  117. };
  118. frame.Add (glyphsNotWords);
  119. label = new ()
  120. {
  121. X = 0,
  122. Y = Pos.Bottom (glyphsNotWords),
  123. Width = Dim.Width (numButtonsLabel),
  124. Height = 1,
  125. TextAlignment = TextAlignment.Right,
  126. Text = "Button St_yle:"
  127. };
  128. frame.Add (label);
  129. var styleRadioGroup = new RadioGroup
  130. {
  131. X = Pos.Right (label) + 1,
  132. Y = Pos.Top (label),
  133. RadioLabels = new [] { "_Center", "_Justify", "_Left", "_Right" }
  134. };
  135. frame.Add (styleRadioGroup);
  136. frame.ValidatePosDim = true;
  137. app.Add (frame);
  138. label = new ()
  139. {
  140. X = Pos.Center (), Y = Pos.Bottom (frame) + 4, TextAlignment = TextAlignment.Right, Text = "Button Pressed:"
  141. };
  142. app.Add (label);
  143. var buttonPressedLabel = new Label
  144. {
  145. X = Pos.Center (), Y = Pos.Bottom (frame) + 5, ColorScheme = Colors.ColorSchemes ["Error"], Text = " "
  146. };
  147. // glyphsNotWords
  148. // false:var btnText = new [] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
  149. // true: var btnText = new [] { "0", "\u2780", "➁", "\u2783", "\u2784", "\u2785", "\u2786", "\u2787", "\u2788", "\u2789" };
  150. // \u2781 is ➁ dingbats \ufb70 is
  151. var showDialogButton = new Button
  152. {
  153. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show Dialog"
  154. };
  155. showDialogButton.Accept += (s, e) =>
  156. {
  157. Dialog dlg = CreateDemoDialog (
  158. widthEdit,
  159. heightEdit,
  160. titleEdit,
  161. numButtonsEdit,
  162. glyphsNotWords,
  163. styleRadioGroup,
  164. buttonPressedLabel
  165. );
  166. Application.Run (dlg);
  167. dlg.Dispose ();
  168. };
  169. app.Add (showDialogButton);
  170. app.Add (buttonPressedLabel);
  171. Application.Run (app);
  172. app.Dispose ();
  173. Application.Shutdown ();
  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 ()
  204. {
  205. Text = NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
  206. IsDefault = buttonId == 0
  207. };
  208. }
  209. else
  210. {
  211. button = new () { 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 ()
  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 ()
  248. {
  249. Text = NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
  250. IsDefault = buttonId == 0
  251. };
  252. }
  253. else
  254. {
  255. button = new () { 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. }