MessageBoxes.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. label = new Label ("Style:") {
  120. X = 0,
  121. Y = Pos.Bottom (label),
  122. Width = Dim.Width (label),
  123. Height = 1,
  124. TextAlignment = Terminal.Gui.TextAlignment.Right,
  125. };
  126. frame.Add (label);
  127. var styleRadioGroup = new RadioGroup (new ustring [] { "_Query", "_Error" }) {
  128. X = Pos.Right (label) + 1,
  129. Y = Pos.Top (label),
  130. };
  131. frame.Add (styleRadioGroup);
  132. var border = new Border () {
  133. Effect3D = true,
  134. BorderStyle = BorderStyle.Single
  135. };
  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 = !e;
  142. };
  143. frame.Add (ckbEffect3D);
  144. void Top_Loaded ()
  145. {
  146. frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit) + Dim.Height (messageEdit)
  147. + Dim.Height (numButtonsEdit) + Dim.Height (defaultButtonEdit) + Dim.Height (styleRadioGroup) + 2 + Dim.Height (ckbEffect3D);
  148. Application.Top.Loaded -= Top_Loaded;
  149. }
  150. Application.Top.Loaded += Top_Loaded;
  151. label = new Label ("Button Pressed:") {
  152. X = Pos.Center (),
  153. Y = Pos.Bottom (frame) + 4,
  154. Height = 1,
  155. TextAlignment = Terminal.Gui.TextAlignment.Right,
  156. };
  157. Win.Add (label);
  158. var buttonPressedLabel = new Label (" ") {
  159. X = Pos.Center (),
  160. Y = Pos.Bottom (frame) + 5,
  161. Width = 25,
  162. Height = 1,
  163. ColorScheme = Colors.Error,
  164. TextAlignment = Terminal.Gui.TextAlignment.Centered
  165. };
  166. //var btnText = new [] { "_Zero", "_One", "T_wo", "_Three", "_Four", "Fi_ve", "Si_x", "_Seven", "_Eight", "_Nine" };
  167. var showMessageBoxButton = new Button ("Show MessageBox") {
  168. X = Pos.Center (),
  169. Y = Pos.Bottom (frame) + 2,
  170. IsDefault = true,
  171. };
  172. showMessageBoxButton.Clicked += () => {
  173. try {
  174. int width = int.Parse (widthEdit.Text.ToString ());
  175. int height = int.Parse (heightEdit.Text.ToString ());
  176. int numButtons = int.Parse (numButtonsEdit.Text.ToString ());
  177. int defaultButton = int.Parse (defaultButtonEdit.Text.ToString ());
  178. var btns = new List<ustring> ();
  179. for (int i = 0; i < numButtons; i++) {
  180. //btns.Add(btnText[i % 10]);
  181. btns.Add (NumberToWords.Convert (i));
  182. }
  183. if (styleRadioGroup.SelectedItem == 0) {
  184. buttonPressedLabel.Text = $"{MessageBox.Query (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, border, btns.ToArray ())}";
  185. } else {
  186. buttonPressedLabel.Text = $"{MessageBox.ErrorQuery (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, border, btns.ToArray ())}";
  187. }
  188. } catch (FormatException) {
  189. buttonPressedLabel.Text = "Invalid Options";
  190. }
  191. };
  192. Win.Add (showMessageBoxButton);
  193. Win.Add (buttonPressedLabel);
  194. }
  195. }
  196. }