Dialogs.cs 7.1 KB

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