Dialogs.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System.Text;
  2. using System;
  3. using System.Collections.Generic;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios {
  6. [ScenarioMetadata (Name: "Dialogs", Description: "Demonstrates how to the Dialog class")]
  7. [ScenarioCategory ("Dialogs")]
  8. public class Dialogs : Scenario {
  9. static int CODE_POINT = '你'; // We know this is a wide char
  10. public override void Setup ()
  11. {
  12. var frame = new FrameView ("Dialog Options") {
  13. X = Pos.Center (),
  14. Y = 1,
  15. Width = Dim.Percent (75),
  16. };
  17. var label = new Label ("Width:") {
  18. X = 0,
  19. Y = 0,
  20. Width = 15,
  21. Height = 1,
  22. TextAlignment = Terminal.Gui.TextAlignment.Right,
  23. };
  24. frame.Add (label);
  25. var widthEdit = new TextField ("0") {
  26. X = Pos.Right (label) + 1,
  27. Y = Pos.Top (label),
  28. Width = 5,
  29. Height = 1
  30. };
  31. frame.Add (widthEdit);
  32. label = new Label ("Height:") {
  33. X = 0,
  34. Y = Pos.Bottom (label),
  35. Width = Dim.Width (label),
  36. Height = 1,
  37. TextAlignment = Terminal.Gui.TextAlignment.Right,
  38. };
  39. frame.Add (label);
  40. var heightEdit = new TextField ("0") {
  41. X = Pos.Right (label) + 1,
  42. Y = Pos.Top (label),
  43. Width = 5,
  44. Height = 1
  45. };
  46. frame.Add (heightEdit);
  47. frame.Add (new Label ("If height & width are both 0,") {
  48. X = Pos.Right (widthEdit) + 2,
  49. Y = Pos.Top (widthEdit),
  50. });
  51. frame.Add (new Label ("the Dialog will size to 80% of container.") {
  52. X = Pos.Right (heightEdit) + 2,
  53. Y = Pos.Top (heightEdit),
  54. });
  55. label = new Label ("Title:") {
  56. X = 0,
  57. Y = Pos.Bottom (label),
  58. Width = Dim.Width (label),
  59. Height = 1,
  60. TextAlignment = Terminal.Gui.TextAlignment.Right,
  61. };
  62. frame.Add (label);
  63. var titleEdit = new TextField ("Title") {
  64. X = Pos.Right (label) + 1,
  65. Y = Pos.Top (label),
  66. Width = Dim.Fill (),
  67. Height = 1
  68. };
  69. frame.Add (titleEdit);
  70. label = new Label ("Num Buttons:") {
  71. X = 0,
  72. Y = Pos.Bottom (label), // BUGBUG: if this is Pos.Bottom (titleEdit) the initial LayoutSubviews does not work correctly?!?!
  73. Width = Dim.Width (label),
  74. Height = 1,
  75. TextAlignment = Terminal.Gui.TextAlignment.Right,
  76. };
  77. frame.Add (label);
  78. var numButtonsEdit = new TextField ("3") {
  79. X = Pos.Right (label) + 1,
  80. Y = Pos.Top (label),
  81. Width = 5,
  82. Height = 1
  83. };
  84. frame.Add (numButtonsEdit);
  85. var glyphsNotWords = new CheckBox ($"Add {Char.ConvertFromUtf32 (CODE_POINT)} to button text to stress wide char support", false) {
  86. X = Pos.Left (numButtonsEdit),
  87. Y = Pos.Bottom (label),
  88. TextAlignment = Terminal.Gui.TextAlignment.Right,
  89. };
  90. frame.Add (glyphsNotWords);
  91. label = new Label ("Button Style:") {
  92. X = 0,
  93. Y = Pos.Bottom (glyphsNotWords),
  94. TextAlignment = Terminal.Gui.TextAlignment.Right
  95. };
  96. frame.Add (label);
  97. var styleRadioGroup = new RadioGroup (new string [] { "Center", "Justify", "Left", "Right" }) {
  98. X = Pos.Right (label) + 1,
  99. Y = Pos.Top (label),
  100. };
  101. frame.Add (styleRadioGroup);
  102. frame.ForceValidatePosDim = true;
  103. void Top_Loaded (object sender, EventArgs args)
  104. {
  105. frame.Height =
  106. widthEdit.Frame.Height +
  107. heightEdit.Frame.Height +
  108. titleEdit.Frame.Height +
  109. numButtonsEdit.Frame.Height +
  110. glyphsNotWords.Frame.Height +
  111. styleRadioGroup.Frame.Height;
  112. Application.Top.Loaded -= Top_Loaded;
  113. }
  114. Application.Top.Loaded += Top_Loaded;
  115. Win.Add (frame);
  116. label = new Label ("Button Pressed:") {
  117. X = Pos.Center (),
  118. Y = Pos.Bottom (frame) + 4,
  119. Height = 1,
  120. TextAlignment = Terminal.Gui.TextAlignment.Right,
  121. };
  122. Win.Add (label);
  123. var buttonPressedLabel = new Label (" ") {
  124. X = Pos.Center (),
  125. Y = Pos.Bottom (frame) + 5,
  126. Width = 25,
  127. Height = 1,
  128. ColorScheme = Colors.Error,
  129. };
  130. // glyphsNotWords
  131. // false:var btnText = new [] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
  132. // true: var btnText = new [] { "0", "\u2780", "➁", "\u2783", "\u2784", "\u2785", "\u2786", "\u2787", "\u2788", "\u2789" };
  133. // \u2781 is ➁ dingbats \ufb70 is
  134. var showDialogButton = new Button ("Show Dialog") {
  135. X = Pos.Center (),
  136. Y = Pos.Bottom (frame) + 2,
  137. IsDefault = true,
  138. };
  139. showDialogButton.Clicked += (s, e) => {
  140. var dlg = CreateDemoDialog (widthEdit, heightEdit, titleEdit, numButtonsEdit, glyphsNotWords, styleRadioGroup, buttonPressedLabel);
  141. Application.Run (dlg);
  142. };
  143. Win.Add (showDialogButton);
  144. Win.Add (buttonPressedLabel);
  145. }
  146. Dialog CreateDemoDialog (TextField widthEdit, TextField heightEdit, TextField titleEdit, TextField numButtonsEdit, CheckBox glyphsNotWords, RadioGroup styleRadioGroup, Label buttonPressedLabel)
  147. {
  148. Dialog dialog = null;
  149. try {
  150. int width = 0;
  151. int.TryParse (widthEdit.Text, out width);
  152. int height = 0;
  153. int.TryParse (heightEdit.Text, out height);
  154. int numButtons = 3;
  155. int.TryParse (numButtonsEdit.Text, out numButtons);
  156. var buttons = new List<Button> ();
  157. var clicked = -1;
  158. for (int i = 0; i < numButtons; i++) {
  159. int buttonId = i;
  160. Button button = null;
  161. if (glyphsNotWords.Checked == true) {
  162. buttonId = i;
  163. button = new Button (NumberToWords.Convert (buttonId) + " " + Char.ConvertFromUtf32 (buttonId + CODE_POINT),
  164. is_default: buttonId == 0);
  165. } else {
  166. button = new Button (NumberToWords.Convert (buttonId),
  167. is_default: buttonId == 0);
  168. }
  169. button.Clicked += (s, e) => {
  170. clicked = buttonId;
  171. Application.RequestStop ();
  172. };
  173. buttons.Add (button);
  174. }
  175. //if (buttons.Count > 1) {
  176. // buttons [1].Text = "Accept";
  177. // buttons [1].IsDefault = true;
  178. // buttons [0].Visible = false;
  179. // buttons [0].Text = "_Back";
  180. // buttons [0].IsDefault = false;
  181. //}
  182. // This tests dynamically adding buttons; ensuring the dialog resizes if needed and
  183. // the buttons are laid out correctly
  184. dialog = new Dialog (buttons.ToArray ()) {
  185. Title = titleEdit.Text,
  186. ButtonAlignment = (Dialog.ButtonAlignments)styleRadioGroup.SelectedItem
  187. };
  188. if (height != 0 || width != 0) {
  189. dialog.Height = height;
  190. dialog.Width = width;
  191. }
  192. var add = new Button ("Add a button") {
  193. X = Pos.Center (),
  194. Y = Pos.Center ()
  195. };
  196. add.Clicked += (s, e) => {
  197. var buttonId = buttons.Count;
  198. Button button;
  199. if (glyphsNotWords.Checked == true) {
  200. button = new Button (NumberToWords.Convert (buttonId) + " " + Char.ConvertFromUtf32 (buttonId + CODE_POINT),
  201. is_default: buttonId == 0);
  202. } else {
  203. button = new Button (NumberToWords.Convert (buttonId),
  204. is_default: buttonId == 0);
  205. }
  206. button.Clicked += (s, e) => {
  207. clicked = buttonId;
  208. Application.RequestStop ();
  209. };
  210. buttons.Add (button);
  211. dialog.AddButton (button);
  212. if (buttons.Count > 1) {
  213. button.TabIndex = buttons [buttons.Count - 2].TabIndex + 1;
  214. }
  215. };
  216. dialog.Add (add);
  217. var addChar = new Button ($"Add a {Char.ConvertFromUtf32 (CODE_POINT)} to each button") {
  218. X = Pos.Center (),
  219. Y = Pos.Center () + 1
  220. };
  221. addChar.Clicked += (s, e) => {
  222. foreach (var button in buttons) {
  223. button.Text += Char.ConvertFromUtf32 (CODE_POINT);
  224. }
  225. dialog.LayoutSubviews ();
  226. };
  227. dialog.Closed += (s, e) => {
  228. buttonPressedLabel.Text = $"{clicked}";
  229. };
  230. dialog.Add (addChar);
  231. } catch (FormatException) {
  232. buttonPressedLabel.Text = "Invalid Options";
  233. }
  234. return dialog;
  235. }
  236. }
  237. }