MessageBoxes.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 MessageBoxes")]
  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. ColorScheme = Colors.Dialog,
  88. };
  89. frame.Add (messageEdit);
  90. label = new Label ("Num Buttons:") {
  91. X = 0,
  92. Y = Pos.Bottom (messageEdit),
  93. Width = Dim.Width (label),
  94. Height = 1,
  95. TextAlignment = Terminal.Gui.TextAlignment.Right,
  96. };
  97. frame.Add (label);
  98. var numButtonsEdit = new TextField ("3") {
  99. X = Pos.Right (label) + 1,
  100. Y = Pos.Top (label),
  101. Width = 5,
  102. Height = 1
  103. };
  104. frame.Add (numButtonsEdit);
  105. label = new Label ("Default Button:") {
  106. X = 0,
  107. Y = Pos.Bottom (label),
  108. Width = Dim.Width (label),
  109. Height = 1,
  110. TextAlignment = Terminal.Gui.TextAlignment.Right,
  111. };
  112. frame.Add (label);
  113. var defaultButtonEdit = new TextField ("0") {
  114. X = Pos.Right (label) + 1,
  115. Y = Pos.Top (label),
  116. Width = 5,
  117. Height = 1
  118. };
  119. frame.Add (defaultButtonEdit);
  120. label = new Label ("Style:") {
  121. X = 0,
  122. Y = Pos.Bottom (label),
  123. Width = Dim.Width (label),
  124. Height = 1,
  125. TextAlignment = Terminal.Gui.TextAlignment.Right,
  126. };
  127. frame.Add (label);
  128. var styleRadioGroup = new RadioGroup (new ustring [] { "_Query", "_Error" }) {
  129. X = Pos.Right (label) + 1,
  130. Y = Pos.Top (label),
  131. };
  132. frame.Add (styleRadioGroup);
  133. var border = new Border () {
  134. Effect3D = true,
  135. BorderStyle = BorderStyle.Single
  136. };
  137. var ckbEffect3D = new CheckBox ("Effect3D", true) {
  138. X = Pos.Right (label) + 1,
  139. Y = Pos.Top (label) + 2
  140. };
  141. ckbEffect3D.Toggled += (e) => {
  142. border.Effect3D = !e;
  143. };
  144. frame.Add (ckbEffect3D);
  145. void Top_Loaded ()
  146. {
  147. frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit) + Dim.Height (messageEdit)
  148. + Dim.Height (numButtonsEdit) + Dim.Height (defaultButtonEdit) + Dim.Height (styleRadioGroup) + 2 + Dim.Height (ckbEffect3D);
  149. Top.Loaded -= Top_Loaded;
  150. }
  151. Top.Loaded += Top_Loaded;
  152. label = new Label ("Button Pressed:") {
  153. X = Pos.Center (),
  154. Y = Pos.Bottom (frame) + 4,
  155. Height = 1,
  156. TextAlignment = Terminal.Gui.TextAlignment.Right,
  157. };
  158. Win.Add (label);
  159. var buttonPressedLabel = new Label (" ") {
  160. X = Pos.Center (),
  161. Y = Pos.Bottom (frame) + 5,
  162. Width = 25,
  163. Height = 1,
  164. ColorScheme = Colors.Error,
  165. TextAlignment = Terminal.Gui.TextAlignment.Centered
  166. };
  167. //var btnText = new [] { "_Zero", "_One", "T_wo", "_Three", "_Four", "Fi_ve", "Si_x", "_Seven", "_Eight", "_Nine" };
  168. var showMessageBoxButton = new Button ("Show MessageBox") {
  169. X = Pos.Center (),
  170. Y = Pos.Bottom (frame) + 2,
  171. IsDefault = true,
  172. };
  173. showMessageBoxButton.Clicked += () => {
  174. try {
  175. int width = int.Parse (widthEdit.Text.ToString ());
  176. int height = int.Parse (heightEdit.Text.ToString ());
  177. int numButtons = int.Parse (numButtonsEdit.Text.ToString ());
  178. int defaultButton = int.Parse (defaultButtonEdit.Text.ToString ());
  179. var btns = new List<ustring> ();
  180. for (int i = 0; i < numButtons; i++) {
  181. //btns.Add(btnText[i % 10]);
  182. btns.Add (NumberToWords.Convert (i));
  183. }
  184. if (styleRadioGroup.SelectedItem == 0) {
  185. buttonPressedLabel.Text = $"{MessageBox.Query (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, border, btns.ToArray ())}";
  186. } else {
  187. buttonPressedLabel.Text = $"{MessageBox.ErrorQuery (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, border, btns.ToArray ())}";
  188. }
  189. } catch (FormatException) {
  190. buttonPressedLabel.Text = "Invalid Options";
  191. }
  192. };
  193. Win.Add (showMessageBoxButton);
  194. Win.Add (buttonPressedLabel);
  195. }
  196. }
  197. }