Dialogs.cs 7.4 KB

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