Dialogs.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios {
  8. [ScenarioMetadata (Name: "Dialogs", Description: "Demonstrates how to the Dialog class")]
  9. [ScenarioCategory ("Dialogs")]
  10. public class Dialogs : Scenario {
  11. static int CODE_POINT = '你'; // We know this is a wide char
  12. public override void Setup ()
  13. {
  14. var frame = new FrameView ("Dialog Options") {
  15. X = Pos.Center (),
  16. Y = 0,
  17. Width = Dim.Percent (75)
  18. };
  19. Win.Add (frame);
  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 (titleEdit),
  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. void Top_Loaded ()
  106. {
  107. frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit)
  108. + Dim.Height (numButtonsEdit) + Dim.Height (styleRadioGroup) + Dim.Height(glyphsNotWords) + 2;
  109. Application.Top.Loaded -= Top_Loaded;
  110. }
  111. Application.Top.Loaded += Top_Loaded;
  112. label = new Label ("Button Pressed:") {
  113. X = Pos.Center (),
  114. Y = Pos.Bottom (frame) + 4,
  115. Height = 1,
  116. TextAlignment = Terminal.Gui.TextAlignment.Right,
  117. };
  118. Win.Add (label);
  119. var buttonPressedLabel = new Label (" ") {
  120. X = Pos.Center (),
  121. Y = Pos.Bottom (frame) + 5,
  122. Width = 25,
  123. Height = 1,
  124. ColorScheme = Colors.Error,
  125. };
  126. // glyphsNotWords
  127. // false:var btnText = new [] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
  128. // true: var btnText = new [] { "0", "\u2780", "➁", "\u2783", "\u2784", "\u2785", "\u2786", "\u2787", "\u2788", "\u2789" };
  129. // \u2781 is ➁ dingbats \ufb70 is
  130. var showDialogButton = new Button ("Show Dialog") {
  131. X = Pos.Center (),
  132. Y = Pos.Bottom (frame) + 2,
  133. IsDefault = true,
  134. };
  135. showDialogButton.Clicked += () => {
  136. try {
  137. Dialog dialog = null;
  138. int width = 0;
  139. int.TryParse (widthEdit.Text.ToString (), out width);
  140. int height = 0;
  141. int.TryParse (heightEdit.Text.ToString (), out height);
  142. int numButtons = 3;
  143. int.TryParse (numButtonsEdit.Text.ToString (), out numButtons);
  144. var buttons = new List<Button> ();
  145. var clicked = -1;
  146. for (int i = 0; i < numButtons; i++) {
  147. int buttonId = i;
  148. Button button = null;
  149. if (glyphsNotWords.Checked) {
  150. buttonId = i;
  151. button = new Button (NumberToWords.Convert (buttonId) + " " + Char.ConvertFromUtf32 (buttonId + CODE_POINT),
  152. is_default: buttonId == 0);
  153. } else {
  154. button = new Button (NumberToWords.Convert (buttonId),
  155. is_default: buttonId == 0);
  156. }
  157. button.Clicked += () => {
  158. clicked = buttonId;
  159. Application.RequestStop ();
  160. };
  161. buttons.Add (button);
  162. }
  163. //if (buttons.Count > 1) {
  164. // buttons [1].Text = "Accept";
  165. // buttons [1].IsDefault = true;
  166. // buttons [0].Visible = false;
  167. // buttons [0].Text = "_Back";
  168. // buttons [0].IsDefault = false;
  169. //}
  170. // This tests dynamically adding buttons; ensuring the dialog resizes if needed and
  171. // the buttons are laid out correctly
  172. dialog = new Dialog (titleEdit.Text, width, height,
  173. buttons.ToArray ()) {
  174. ButtonAlignment = (Dialog.ButtonAlignments)styleRadioGroup.SelectedItem
  175. };
  176. var add = new Button ("Add a button") {
  177. X = Pos.Center (),
  178. Y = Pos.Center ()
  179. };
  180. add.Clicked += () => {
  181. var buttonId = buttons.Count;
  182. Button button;
  183. if (glyphsNotWords.Checked) {
  184. button = new Button (NumberToWords.Convert (buttonId) + " " + Char.ConvertFromUtf32 (buttonId + CODE_POINT),
  185. is_default: buttonId == 0);
  186. } else {
  187. button = new Button (NumberToWords.Convert (buttonId),
  188. is_default: buttonId == 0);
  189. }
  190. button.Clicked += () => {
  191. clicked = buttonId;
  192. Application.RequestStop ();
  193. };
  194. buttons.Add (button);
  195. dialog.AddButton (button);
  196. if (buttons.Count > 1) {
  197. button.TabIndex = buttons [buttons.Count - 2].TabIndex + 1;
  198. }
  199. };
  200. dialog.Add (add);
  201. var addChar = new Button ($"Add a {Char.ConvertFromUtf32(CODE_POINT)} to each button") {
  202. X = Pos.Center (),
  203. Y = Pos.Center () + 1
  204. };
  205. addChar.Clicked += () => {
  206. foreach (var button in buttons) {
  207. button.Text += Char.ConvertFromUtf32 (CODE_POINT);
  208. }
  209. dialog.LayoutSubviews ();
  210. };
  211. dialog.Closed += (args) => {
  212. buttonPressedLabel.Text = $"{clicked}";
  213. };
  214. dialog.Add (addChar);
  215. Application.Run (dialog);
  216. } catch (FormatException) {
  217. buttonPressedLabel.Text = "Invalid Options";
  218. }
  219. };
  220. Win.Add (showDialogButton);
  221. Win.Add (buttonPressedLabel);
  222. }
  223. }
  224. }