Text.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using NStack;
  2. using System;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using Terminal.Gui;
  7. using Terminal.Gui.TextValidateProviders;
  8. namespace UICatalog.Scenarios {
  9. [ScenarioMetadata (Name: "Text Input Controls", Description: "Tests all text input controls")]
  10. [ScenarioCategory ("Controls")]
  11. [ScenarioCategory ("Mouse and Keyboard")]
  12. [ScenarioCategory ("Text and Formatting")]
  13. public class Text : Scenario {
  14. public override void Setup ()
  15. {
  16. var s = "TAB to jump between text fields.";
  17. var textField = new TextField (s) {
  18. X = 1,
  19. Y = 1,
  20. Width = Dim.Percent (50),
  21. //ColorScheme = Colors.Dialog
  22. };
  23. textField.TextChanging += TextField_TextChanging;
  24. void TextField_TextChanging (TextChangingEventArgs e)
  25. {
  26. textField.Autocomplete.AllSuggestions = Regex.Matches (e.NewText.ToString (), "\\w+")
  27. .Select (s => s.Value)
  28. .Distinct ().ToList ();
  29. }
  30. Win.Add (textField);
  31. var labelMirroringTextField = new Label (textField.Text) {
  32. X = Pos.Right (textField) + 1,
  33. Y = Pos.Top (textField),
  34. Width = Dim.Fill (1)
  35. };
  36. Win.Add (labelMirroringTextField);
  37. textField.TextChanged += (prev) => {
  38. labelMirroringTextField.Text = textField.Text;
  39. };
  40. var textView = new TextView () {
  41. X = 1,
  42. Y = 3,
  43. Width = Dim.Percent (50),
  44. Height = Dim.Percent (30),
  45. ColorScheme = Colors.Dialog
  46. };
  47. textView.Text = s;
  48. textView.DrawContent += TextView_DrawContent;
  49. void TextView_DrawContent (Rect e)
  50. {
  51. textView.Autocomplete.AllSuggestions = Regex.Matches (textView.Text.ToString (), "\\w+")
  52. .Select (s => s.Value)
  53. .Distinct ().ToList ();
  54. }
  55. Win.Add (textView);
  56. var labelMirroringTextView = new Label (textView.Text) {
  57. X = Pos.Right (textView) + 1,
  58. Y = Pos.Top (textView),
  59. Width = Dim.Fill (1),
  60. Height = Dim.Height (textView),
  61. };
  62. Win.Add (labelMirroringTextView);
  63. textView.TextChanged += () => {
  64. labelMirroringTextView.Text = textView.Text;
  65. };
  66. var btnMultiline = new Button ("Toggle Multiline") {
  67. X = Pos.Right (textView) + 1,
  68. Y = Pos.Top (textView) + 1
  69. };
  70. btnMultiline.Clicked += () => textView.Multiline = !textView.Multiline;
  71. Win.Add (btnMultiline);
  72. // BUGBUG: 531 - TAB doesn't go to next control from HexView
  73. var hexView = new HexView (new System.IO.MemoryStream (Encoding.ASCII.GetBytes (s))) {
  74. X = 1,
  75. Y = Pos.Bottom (textView) + 1,
  76. Width = Dim.Fill (1),
  77. Height = Dim.Percent (30),
  78. //ColorScheme = Colors.Dialog
  79. };
  80. Win.Add (hexView);
  81. var dateField = new DateField (System.DateTime.Now) {
  82. X = 1,
  83. Y = Pos.Bottom (hexView) + 1,
  84. Width = 20,
  85. //ColorScheme = Colors.Dialog,
  86. IsShortFormat = false
  87. };
  88. Win.Add (dateField);
  89. var labelMirroringDateField = new Label (dateField.Text) {
  90. X = Pos.Right (dateField) + 1,
  91. Y = Pos.Top (dateField),
  92. Width = Dim.Width (dateField),
  93. Height = Dim.Height (dateField),
  94. };
  95. Win.Add (labelMirroringDateField);
  96. dateField.TextChanged += (prev) => {
  97. labelMirroringDateField.Text = dateField.Text;
  98. };
  99. _timeField = new TimeField (DateTime.Now.TimeOfDay) {
  100. X = Pos.Right (labelMirroringDateField) + 5,
  101. Y = Pos.Bottom (hexView) + 1,
  102. Width = 20,
  103. //ColorScheme = Colors.Dialog,
  104. IsShortFormat = false
  105. };
  106. Win.Add (_timeField);
  107. _labelMirroringTimeField = new Label (_timeField.Text) {
  108. X = Pos.Right (_timeField) + 1,
  109. Y = Pos.Top (_timeField),
  110. Width = Dim.Width (_timeField),
  111. Height = Dim.Height (_timeField),
  112. };
  113. Win.Add (_labelMirroringTimeField);
  114. _timeField.TimeChanged += TimeChanged;
  115. // MaskedTextProvider
  116. var netProviderLabel = new Label (".Net MaskedTextProvider [ 999 000 LLL >LLL| AAA aaa ]") {
  117. X = Pos.Left (dateField),
  118. Y = Pos.Bottom (dateField) + 1
  119. };
  120. Win.Add (netProviderLabel);
  121. var netProvider = new NetMaskedTextProvider ("999 000 LLL > LLL | AAA aaa");
  122. var netProviderField = new TextValidateField (netProvider) {
  123. X = Pos.Right (netProviderLabel) + 1,
  124. Y = Pos.Y (netProviderLabel)
  125. };
  126. Win.Add (netProviderField);
  127. // TextRegexProvider
  128. var regexProvider = new Label ("Gui.cs TextRegexProvider [ ^([0-9]?[0-9]?[0-9]|1000)$ ]") {
  129. X = Pos.Left (netProviderLabel),
  130. Y = Pos.Bottom (netProviderLabel) + 1
  131. };
  132. Win.Add (regexProvider);
  133. var provider2 = new TextRegexProvider ("^([0-9]?[0-9]?[0-9]|1000)$") { ValidateOnInput = false };
  134. var regexProviderField = new TextValidateField (provider2) {
  135. X = Pos.Right (regexProvider) + 1,
  136. Y = Pos.Y (regexProvider),
  137. Width = 30,
  138. TextAlignment = TextAlignment.Centered
  139. };
  140. Win.Add (regexProviderField);
  141. }
  142. TimeField _timeField;
  143. Label _labelMirroringTimeField;
  144. private void TimeChanged (DateTimeEventArgs<TimeSpan> e)
  145. {
  146. _labelMirroringTimeField.Text = _timeField.Text;
  147. }
  148. }
  149. }