Dialogs.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 {
  8. [ScenarioMetadata (Name: "Dialogs", Description: "Demonstrates how to the Dialog class")]
  9. [ScenarioCategory ("Controls")]
  10. [ScenarioCategory ("Dialogs")]
  11. class Dialogs : Scenario {
  12. public override void Setup ()
  13. {
  14. var frame = new FrameView ("Dialog Options") {
  15. X = Pos.Center (),
  16. Y = 1,
  17. Width = Dim.Percent (75),
  18. Height = 10
  19. };
  20. Win.Add (frame);
  21. var label = new Label ("width:") {
  22. X = 0,
  23. Y = 0,
  24. Width = 15,
  25. Height = 1,
  26. TextAlignment = Terminal.Gui.TextAlignment.Right,
  27. };
  28. frame.Add (label);
  29. var widthEdit = new TextField ("0") {
  30. X = Pos.Right (label) + 1,
  31. Y = Pos.Top (label),
  32. Width = 5,
  33. Height = 1
  34. };
  35. frame.Add (widthEdit);
  36. label = new Label ("height:") {
  37. X = 0,
  38. Y = Pos.Bottom (label),
  39. Width = Dim.Width (label),
  40. Height = 1,
  41. TextAlignment = Terminal.Gui.TextAlignment.Right,
  42. };
  43. frame.Add (label);
  44. var heightEdit = new TextField ("0") {
  45. X = Pos.Right (label) + 1,
  46. Y = Pos.Top (label),
  47. Width = 5,
  48. Height = 1
  49. };
  50. frame.Add (heightEdit);
  51. frame.Add (new Label ("If height & width are both 0,") {
  52. X = Pos.Right (widthEdit) + 2,
  53. Y = Pos.Top (widthEdit),
  54. });
  55. frame.Add (new Label ("the Dialog will size to 80% of container.") {
  56. X = Pos.Right (heightEdit) + 2,
  57. Y = Pos.Top (heightEdit),
  58. });
  59. label = new Label ("Title:") {
  60. X = 0,
  61. Y = Pos.Bottom (label),
  62. Width = Dim.Width (label),
  63. Height = 1,
  64. TextAlignment = Terminal.Gui.TextAlignment.Right,
  65. };
  66. frame.Add (label);
  67. var titleEdit = new TextField ("Title") {
  68. X = Pos.Right (label) + 1,
  69. Y = Pos.Top (label),
  70. Width = Dim.Fill (),
  71. Height = 1
  72. };
  73. frame.Add (titleEdit);
  74. label = new Label ("Num Buttons:") {
  75. X = 0,
  76. Y = Pos.Bottom (titleEdit),
  77. Width = Dim.Width (label),
  78. Height = 1,
  79. TextAlignment = Terminal.Gui.TextAlignment.Right,
  80. };
  81. frame.Add (label);
  82. var numButtonsEdit = new TextField ("3") {
  83. X = Pos.Right (label) + 1,
  84. Y = Pos.Top (label),
  85. Width = 5,
  86. Height = 1
  87. };
  88. frame.Add (numButtonsEdit);
  89. void Top_Loaded ()
  90. {
  91. frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit)
  92. + Dim.Height (numButtonsEdit) + 2;
  93. Top.Loaded -= Top_Loaded;
  94. }
  95. Top.Loaded += Top_Loaded;
  96. label = new Label ("Button Pressed:") {
  97. X = Pos.Center (),
  98. Y = Pos.Bottom (frame) + 4,
  99. Height = 1,
  100. TextAlignment = Terminal.Gui.TextAlignment.Right,
  101. };
  102. Win.Add (label);
  103. var buttonPressedLabel = new Label (" ") {
  104. X = Pos.Center (),
  105. Y = Pos.Bottom (frame) + 5,
  106. Width = 25,
  107. Height = 1,
  108. ColorScheme = Colors.Error,
  109. };
  110. //var btnText = new [] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
  111. var showDialogButton = new Button ("Show Dialog") {
  112. X = Pos.Center (),
  113. Y = Pos.Bottom (frame) + 2,
  114. IsDefault = true,
  115. };
  116. showDialogButton.Clicked += () => {
  117. try {
  118. int width = int.Parse (widthEdit.Text.ToString ());
  119. int height = int.Parse (heightEdit.Text.ToString ());
  120. int numButtons = int.Parse (numButtonsEdit.Text.ToString ());
  121. var buttons = new List<Button> ();
  122. var clicked = -1;
  123. for (int i = 0; i < numButtons; i++) {
  124. var buttonId = i;
  125. //var button = new Button (btnText [buttonId % 10],
  126. // is_default: buttonId == 0);
  127. var button = new Button (NumberToWords.Convert (buttonId),
  128. is_default: buttonId == 0);
  129. button.Clicked += () => {
  130. clicked = buttonId;
  131. Application.RequestStop ();
  132. };
  133. buttons.Add (button);
  134. }
  135. // This tests dynamically adding buttons; ensuring the dialog resizes if needed and
  136. // the buttons are laid out correctly
  137. var dialog = new Dialog (titleEdit.Text, width, height,
  138. buttons.ToArray ());
  139. var add = new Button ("Add a button") {
  140. X = Pos.Center (),
  141. Y = Pos.Center ()
  142. };
  143. add.Clicked += () => {
  144. var buttonId = buttons.Count;
  145. //var button = new Button (btnText [buttonId % 10],
  146. // is_default: buttonId == 0);
  147. var button = new Button (NumberToWords.Convert (buttonId),
  148. is_default: buttonId == 0);
  149. button.Clicked += () => {
  150. clicked = buttonId;
  151. Application.RequestStop ();
  152. };
  153. buttons.Add (button);
  154. dialog.AddButton (button);
  155. button.TabIndex = buttons [buttons.Count - 2].TabIndex + 1;
  156. };
  157. dialog.Add (add);
  158. Application.Run (dialog);
  159. buttonPressedLabel.Text = $"{clicked}";
  160. } catch (FormatException) {
  161. buttonPressedLabel.Text = "Invalid Options";
  162. }
  163. };
  164. Win.Add (showDialogButton);
  165. Win.Add (buttonPressedLabel);
  166. }
  167. }
  168. }