RadioGroup.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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
  10. /// setting up the initial set of radio labels and the item that should be selected and uses
  11. /// an absolute layout for the result.
  12. /// </summary>
  13. /// <param name="rect">Boundaries for the radio group.</param>
  14. /// <param name="radioLabels">The radio labels; an array of strings that can contain hotkeys using an underscore before the letter.</param>
  15. /// <param name="selected">The index of item to be selected, the value is clamped to the number of items.</param>
  16. public RadioGroup (Rect rect, string [] radioLabels, int selected = 0) : base (rect)
  17. {
  18. this.selected = selected;
  19. this.radioLabels = radioLabels;
  20. CanFocus = true;
  21. }
  22. /// <summary>
  23. /// The location of the cursor in the <see cref="RadioGroup"/>
  24. /// </summary>
  25. public int Cursor {
  26. get => cursor;
  27. set {
  28. if (cursor < 0 || cursor >= radioLabels.Length)
  29. return;
  30. cursor = value;
  31. SetNeedsDisplay ();
  32. }
  33. }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="RadioGroup"/> class
  36. /// setting up the initial set of radio labels and the item that should be selected.
  37. /// </summary>
  38. /// <param name="radioLabels">The radio labels; an array of strings that can contain hotkeys using an underscore before the letter.</param>
  39. /// <param name="selected">The index of the item to be selected, the value is clamped to the number of items.</param>
  40. public RadioGroup (string [] radioLabels, int selected = 0) : base ()
  41. {
  42. SetWidthHeight(radioLabels);
  43. this.selected = selected;
  44. this.radioLabels = radioLabels;
  45. CanFocus = true;
  46. }
  47. void SetWidthHeight(string[] radioLabels)
  48. {
  49. var r = MakeRect(0, 0, radioLabels);
  50. Width = r.Width;
  51. Height = radioLabels.Length;
  52. }
  53. static Rect MakeRect (int x, int y, string [] radioLabels)
  54. {
  55. int width = 0;
  56. foreach (var s in radioLabels)
  57. width = Math.Max (s.Length + 4, width);
  58. return new Rect (x, y, width, radioLabels.Length);
  59. }
  60. /// <summary>
  61. /// Initializes a new instance of the <see cref="RadioGroup"/> class
  62. /// setting up the initial set of radio labels and the item that should be selected.
  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. {
  71. }
  72. string [] radioLabels;
  73. /// <summary>
  74. /// The radio labels to display
  75. /// </summary>
  76. /// <value>The radio labels.</value>
  77. public string [] RadioLabels {
  78. get => radioLabels;
  79. set {
  80. Update(value);
  81. radioLabels = value;
  82. selected = 0;
  83. cursor = 0;
  84. SetNeedsDisplay ();
  85. }
  86. }
  87. void Update(string [] newRadioLabels)
  88. {
  89. for (int i = 0; i < radioLabels.Length; i++) {
  90. Move(0, i);
  91. Driver.SetAttribute(ColorScheme.Normal);
  92. Driver.AddStr(new string(' ', radioLabels[i].Length + 4));
  93. }
  94. if (newRadioLabels.Length != radioLabels.Length) {
  95. SetWidthHeight(newRadioLabels);
  96. }
  97. }
  98. ///<inheritdoc cref="Redraw(Rect)"/>
  99. public override void Redraw (Rect region)
  100. {
  101. for (int i = 0; i < radioLabels.Length; i++) {
  102. Move (0, i);
  103. Driver.SetAttribute (ColorScheme.Normal);
  104. Driver.AddStr (i == selected ? "(o) " : "( ) ");
  105. DrawHotString (radioLabels [i], HasFocus && i == cursor, ColorScheme);
  106. }
  107. base.Redraw (region);
  108. }
  109. ///<inheritdoc cref="PositionCursor"/>
  110. public override void PositionCursor ()
  111. {
  112. Move (1, cursor);
  113. }
  114. ///<inheritdoc cref="SelectionChanged"/>
  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 cref="ProcessColdKey"/>
  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 cref="ProcessKey(KeyEvent)"/>
  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 cref="MouseEvent(Gui.MouseEvent)"/>
  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. }