RadioGroup.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. namespace Terminal.Gui {
  3. /// <summary>
  4. /// <see cref="RadioGroup"/> shows a group of radio labels, only one of those can be selected at a given time
  5. /// </summary>
  6. public class RadioGroup : View {
  7. int selected, cursor;
  8. /// <summary>
  9. /// Initializes a new instance of the <see cref="RadioGroup"/> class using <see cref="LayoutStyle.Computed"/> layout.
  10. /// </summary>
  11. /// <param name="rect">Boundaries for the radio group.</param>
  12. /// <param name="radioLabels">The radio labels; an array of strings that can contain hotkeys using an underscore before the letter.</param>
  13. /// <param name="selected">The index of item to be selected, the value is clamped to the number of items.</param>
  14. public RadioGroup (Rect rect, string [] radioLabels, int selected = 0) : base (rect)
  15. {
  16. this.selected = selected;
  17. this.radioLabels = radioLabels;
  18. CanFocus = true;
  19. }
  20. /// <summary>
  21. /// The location of the cursor in the <see cref="RadioGroup"/>
  22. /// </summary>
  23. public int Cursor {
  24. get => cursor;
  25. set {
  26. if (cursor < 0 || cursor >= radioLabels.Length)
  27. return;
  28. cursor = value;
  29. SetNeedsDisplay ();
  30. }
  31. }
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="RadioGroup"/> class using <see cref="LayoutStyle.Computed"/> layout.
  34. /// </summary>
  35. public RadioGroup () : this (radioLabels: new string [] { }) { }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="RadioGroup"/> class using <see cref="LayoutStyle.Computed"/> layout.
  38. /// </summary>
  39. /// <param name="radioLabels">The radio labels; an array of strings that can contain hotkeys using an underscore before the letter.</param>
  40. /// <param name="selected">The index of the item to be selected, the value is clamped to the number of items.</param>
  41. public RadioGroup (string [] radioLabels, int selected = 0) : base ()
  42. {
  43. SetWidthHeight(radioLabels);
  44. this.selected = selected;
  45. this.radioLabels = radioLabels;
  46. CanFocus = true;
  47. }
  48. void SetWidthHeight(string[] radioLabels)
  49. {
  50. var r = MakeRect(0, 0, radioLabels);
  51. Width = r.Width;
  52. Height = radioLabels.Length;
  53. }
  54. static Rect MakeRect (int x, int y, string [] radioLabels)
  55. {
  56. int width = 0;
  57. foreach (var s in radioLabels)
  58. width = Math.Max (s.Length + 4, width);
  59. return new Rect (x, y, width, radioLabels.Length);
  60. }
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="RadioGroup"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  63. /// The <see cref="View"/> frame is computed from the provided radio labels.
  64. /// </summary>
  65. /// <param name="x">The x coordinate.</param>
  66. /// <param name="y">The y coordinate.</param>
  67. /// <param name="radioLabels">The radio labels; an array of strings that can contain hotkeys using an underscore before the letter.</param>
  68. /// <param name="selected">The item to be selected, the value is clamped to the number of items.</param>
  69. public RadioGroup (int x, int y, string [] radioLabels, int selected = 0) : this (MakeRect (x, y, radioLabels), radioLabels, selected) { }
  70. string [] radioLabels;
  71. /// <summary>
  72. /// The radio labels to display
  73. /// </summary>
  74. /// <value>The radio labels.</value>
  75. public string [] RadioLabels {
  76. get => radioLabels;
  77. set {
  78. Update(value);
  79. radioLabels = value;
  80. selected = 0;
  81. cursor = 0;
  82. SetNeedsDisplay ();
  83. }
  84. }
  85. void Update(string [] newRadioLabels)
  86. {
  87. for (int i = 0; i < radioLabels.Length; i++) {
  88. Move(0, i);
  89. Driver.SetAttribute(ColorScheme.Normal);
  90. Driver.AddStr(new string(' ', radioLabels[i].Length + 4));
  91. }
  92. if (newRadioLabels.Length != radioLabels.Length) {
  93. SetWidthHeight(newRadioLabels);
  94. }
  95. }
  96. ///<inheritdoc/>
  97. public override void Redraw (Rect bounds)
  98. {
  99. for (int i = 0; i < radioLabels.Length; i++) {
  100. Move (0, i);
  101. Driver.SetAttribute (ColorScheme.Normal);
  102. Driver.AddStr (i == selected ? "(o) " : "( ) ");
  103. DrawHotString (radioLabels [i], HasFocus && i == cursor, ColorScheme);
  104. }
  105. base.Redraw (bounds);
  106. }
  107. ///<inheritdoc/>
  108. public override void PositionCursor ()
  109. {
  110. Move (1, cursor);
  111. }
  112. /// <summary>
  113. /// Invoked when the selected radio label has changed
  114. /// </summary>
  115. public Action<int> SelectionChanged;
  116. /// <summary>
  117. /// The currently selected item from the list of radio labels
  118. /// </summary>
  119. /// <value>The selected.</value>
  120. public int Selected {
  121. get => selected;
  122. set {
  123. selected = value;
  124. SelectionChanged?.Invoke (selected);
  125. SetNeedsDisplay ();
  126. }
  127. }
  128. ///<inheritdoc/>
  129. public override bool ProcessColdKey (KeyEvent kb)
  130. {
  131. var key = kb.KeyValue;
  132. if (key < Char.MaxValue && Char.IsLetterOrDigit ((char)key)) {
  133. int i = 0;
  134. key = Char.ToUpper ((char)key);
  135. foreach (var l in radioLabels) {
  136. bool nextIsHot = false;
  137. foreach (var c in l) {
  138. if (c == '_')
  139. nextIsHot = true;
  140. else {
  141. if (nextIsHot && c == key) {
  142. Selected = i;
  143. cursor = i;
  144. if (!HasFocus)
  145. SuperView.SetFocus (this);
  146. return true;
  147. }
  148. nextIsHot = false;
  149. }
  150. }
  151. i++;
  152. }
  153. }
  154. return false;
  155. }
  156. ///<inheritdoc/>
  157. public override bool ProcessKey (KeyEvent kb)
  158. {
  159. switch (kb.Key) {
  160. case Key.CursorUp:
  161. if (cursor > 0) {
  162. cursor--;
  163. SetNeedsDisplay ();
  164. return true;
  165. }
  166. break;
  167. case Key.CursorDown:
  168. if (cursor + 1 < radioLabels.Length) {
  169. cursor++;
  170. SetNeedsDisplay ();
  171. return true;
  172. }
  173. break;
  174. case Key.Space:
  175. Selected = cursor;
  176. return true;
  177. }
  178. return base.ProcessKey (kb);
  179. }
  180. ///<inheritdoc/>
  181. public override bool MouseEvent (MouseEvent me)
  182. {
  183. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked))
  184. return false;
  185. SuperView.SetFocus (this);
  186. if (me.Y < radioLabels.Length) {
  187. cursor = Selected = me.Y;
  188. SetNeedsDisplay ();
  189. }
  190. return true;
  191. }
  192. }
  193. }