MessageBoxes.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Terminal.Gui;
  6. namespace UICatalog.Scenarios {
  7. [ScenarioMetadata (Name: "MessageBoxes", Description: "Demonstrates how to use the MessageBox class.")]
  8. [ScenarioCategory ("Controls")]
  9. [ScenarioCategory ("Dialogs")]
  10. public class MessageBoxes : Scenario {
  11. public override void Setup ()
  12. {
  13. var frame = new FrameView ("MessageBox Options") {
  14. X = Pos.Center (),
  15. Y = 1,
  16. Width = Dim.Percent (75),
  17. Height = 10
  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 MessageBox will be sized automatically.") {
  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 ("Message:") {
  74. X = 0,
  75. Y = Pos.Bottom (label),
  76. Width = Dim.Width (label),
  77. Height = 1,
  78. TextAlignment = Terminal.Gui.TextAlignment.Right,
  79. };
  80. frame.Add (label);
  81. var messageEdit = new TextView () {
  82. Text = "Message",
  83. X = Pos.Right (label) + 1,
  84. Y = Pos.Top (label),
  85. Width = Dim.Fill (),
  86. Height = 5,
  87. };
  88. frame.Add (messageEdit);
  89. label = new Label ("Num Buttons:") {
  90. X = 0,
  91. Y = Pos.Bottom (messageEdit),
  92. Width = Dim.Width (label),
  93. Height = 1,
  94. TextAlignment = Terminal.Gui.TextAlignment.Right,
  95. };
  96. frame.Add (label);
  97. var numButtonsEdit = new TextField ("3") {
  98. X = Pos.Right (label) + 1,
  99. Y = Pos.Top (label),
  100. Width = 5,
  101. Height = 1
  102. };
  103. frame.Add (numButtonsEdit);
  104. label = new Label ("Default Button:") {
  105. X = 0,
  106. Y = Pos.Bottom (label),
  107. Width = Dim.Width (label),
  108. Height = 1,
  109. TextAlignment = Terminal.Gui.TextAlignment.Right,
  110. };
  111. frame.Add (label);
  112. var defaultButtonEdit = new TextField ("0") {
  113. X = Pos.Right (label) + 1,
  114. Y = Pos.Top (label),
  115. Width = 5,
  116. Height = 1
  117. };
  118. frame.Add (defaultButtonEdit);
  119. var border = new Border () {
  120. Effect3D = true,
  121. BorderStyle = BorderStyle.Single
  122. };
  123. label = new Label ("Style:") {
  124. X = 0,
  125. Y = Pos.Bottom (label),
  126. Width = Dim.Width (label),
  127. Height = 1,
  128. TextAlignment = Terminal.Gui.TextAlignment.Right,
  129. };
  130. frame.Add (label);
  131. var styleRadioGroup = new RadioGroup (new ustring [] { "_Query", "_Error" }) {
  132. X = Pos.Right (label) + 1,
  133. Y = Pos.Top (label),
  134. };
  135. frame.Add (styleRadioGroup);
  136. var ckbEffect3D = new CheckBox ("Effect3D", true) {
  137. X = Pos.Right (label) + 1,
  138. Y = Pos.Top (label) + 2
  139. };
  140. ckbEffect3D.Toggled += (e) => {
  141. border.Effect3D = (bool)!e;
  142. };
  143. frame.Add (ckbEffect3D);
  144. var ckbWrapMessage = new CheckBox ("Wrap Message", true) {
  145. X = Pos.Right (label) + 1,
  146. Y = Pos.Top (label) + 3
  147. };
  148. frame.Add (ckbWrapMessage);
  149. void Top_Loaded ()
  150. {
  151. frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit) + Dim.Height (messageEdit)
  152. + Dim.Height (numButtonsEdit) + Dim.Height (defaultButtonEdit) + Dim.Height (styleRadioGroup) + 2 + Dim.Height (ckbEffect3D) + Dim.Height (ckbWrapMessage);
  153. Application.Top.Loaded -= Top_Loaded;
  154. }
  155. Application.Top.Loaded += Top_Loaded;
  156. label = new Label ("Button Pressed:") {
  157. X = Pos.Center (),
  158. Y = Pos.Bottom (frame) + 4,
  159. Height = 1,
  160. TextAlignment = Terminal.Gui.TextAlignment.Right,
  161. };
  162. Win.Add (label);
  163. var buttonPressedLabel = new Label (" ") {
  164. X = Pos.Center (),
  165. Y = Pos.Bottom (frame) + 5,
  166. Width = 25,
  167. Height = 1,
  168. ColorScheme = Colors.Error,
  169. TextAlignment = Terminal.Gui.TextAlignment.Centered
  170. };
  171. //var btnText = new [] { "_Zero", "_One", "T_wo", "_Three", "_Four", "Fi_ve", "Si_x", "_Seven", "_Eight", "_Nine" };
  172. var showMessageBoxButton = new Button ("Show MessageBox") {
  173. X = Pos.Center (),
  174. Y = Pos.Bottom (frame) + 2,
  175. IsDefault = true,
  176. };
  177. showMessageBoxButton.Clicked += () => {
  178. try {
  179. int width = int.Parse (widthEdit.Text.ToString ());
  180. int height = int.Parse (heightEdit.Text.ToString ());
  181. int numButtons = int.Parse (numButtonsEdit.Text.ToString ());
  182. int defaultButton = int.Parse (defaultButtonEdit.Text.ToString ());
  183. var btns = new List<ustring> ();
  184. for (int i = 0; i < numButtons; i++) {
  185. //btns.Add(btnText[i % 10]);
  186. btns.Add (NumberToWords.Convert (i));
  187. }
  188. if (styleRadioGroup.SelectedItem == 0) {
  189. buttonPressedLabel.Text = $"{MessageBox.Query (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, border, (bool)ckbWrapMessage.Checked, btns.ToArray ())}";
  190. } else {
  191. buttonPressedLabel.Text = $"{MessageBox.ErrorQuery (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, border, (bool)ckbWrapMessage.Checked, btns.ToArray ())}";
  192. }
  193. } catch (FormatException) {
  194. buttonPressedLabel.Text = "Invalid Options";
  195. }
  196. };
  197. Win.Add (showMessageBoxButton);
  198. Win.Add (buttonPressedLabel);
  199. }
  200. }
  201. }