Dialogs.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using NStack;
  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 ustring [] { "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. try {
  141. Dialog dialog = null;
  142. int width = 0;
  143. int.TryParse (widthEdit.Text.ToString (), out width);
  144. int height = 0;
  145. int.TryParse (heightEdit.Text.ToString (), out height);
  146. int numButtons = 3;
  147. int.TryParse (numButtonsEdit.Text.ToString (), out numButtons);
  148. var buttons = new List<Button> ();
  149. var clicked = -1;
  150. for (int i = 0; i < numButtons; i++) {
  151. int buttonId = i;
  152. Button button = null;
  153. if (glyphsNotWords.Checked == true) {
  154. buttonId = i;
  155. button = new Button (NumberToWords.Convert (buttonId) + " " + Char.ConvertFromUtf32 (buttonId + CODE_POINT),
  156. is_default: buttonId == 0);
  157. } else {
  158. button = new Button (NumberToWords.Convert (buttonId),
  159. is_default: buttonId == 0);
  160. }
  161. button.Clicked += (s, e) => {
  162. clicked = buttonId;
  163. Application.RequestStop ();
  164. };
  165. buttons.Add (button);
  166. }
  167. //if (buttons.Count > 1) {
  168. // buttons [1].Text = "Accept";
  169. // buttons [1].IsDefault = true;
  170. // buttons [0].Visible = false;
  171. // buttons [0].Text = "_Back";
  172. // buttons [0].IsDefault = false;
  173. //}
  174. // This tests dynamically adding buttons; ensuring the dialog resizes if needed and
  175. // the buttons are laid out correctly
  176. dialog = new Dialog (buttons.ToArray ()) {
  177. Title = titleEdit.Text,
  178. ButtonAlignment = (Dialog.ButtonAlignments)styleRadioGroup.SelectedItem
  179. };
  180. if (height != 0 || width != 0) {
  181. dialog.Height = height;
  182. dialog.Width = width;
  183. }
  184. var add = new Button ("Add a button") {
  185. X = Pos.Center (),
  186. Y = Pos.Center ()
  187. };
  188. add.Clicked += (s, e) => {
  189. var buttonId = buttons.Count;
  190. Button button;
  191. if (glyphsNotWords.Checked == true) {
  192. button = new Button (NumberToWords.Convert (buttonId) + " " + Char.ConvertFromUtf32 (buttonId + CODE_POINT),
  193. is_default: buttonId == 0);
  194. } else {
  195. button = new Button (NumberToWords.Convert (buttonId),
  196. is_default: buttonId == 0);
  197. }
  198. button.Clicked += (s, e) => {
  199. clicked = buttonId;
  200. Application.RequestStop ();
  201. };
  202. buttons.Add (button);
  203. dialog.AddButton (button);
  204. if (buttons.Count > 1) {
  205. button.TabIndex = buttons [buttons.Count - 2].TabIndex + 1;
  206. }
  207. };
  208. dialog.Add (add);
  209. var addChar = new Button ($"Add a {Char.ConvertFromUtf32 (CODE_POINT)} to each button") {
  210. X = Pos.Center (),
  211. Y = Pos.Center () + 1
  212. };
  213. addChar.Clicked += (s, e) => {
  214. foreach (var button in buttons) {
  215. button.Text += Char.ConvertFromUtf32 (CODE_POINT);
  216. }
  217. dialog.LayoutSubviews ();
  218. };
  219. dialog.Closed += (s, e) => {
  220. buttonPressedLabel.Text = $"{clicked}";
  221. };
  222. dialog.Add (addChar);
  223. Application.Run (dialog);
  224. } catch (FormatException) {
  225. buttonPressedLabel.Text = "Invalid Options";
  226. }
  227. };
  228. Win.Add (showDialogButton);
  229. Win.Add (buttonPressedLabel);
  230. }
  231. }
  232. }