RadioGroup.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. namespace Terminal.Gui {
  3. /// <summary>
  4. /// Radio group shows a group of 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="T:Terminal.Gui.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">Radio labels, the strings can contain hotkeys using an undermine before the letter.</param>
  15. /// <param name="selected">The 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 radio group
  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="T:Terminal.Gui.RadioGroup"/> class
  36. /// setting up the initial set of radio labels and the item that should be selected.
  37. /// </summary>
  38. /// <param name="radioLabels">Radio labels, the strings can contain hotkeys using an undermine before the letter.</param>
  39. /// <param name="selected">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="T:Terminal.Gui.RadioGroup"/> class
  62. /// setting up the initial set of radio labels and the item that should be selected,
  63. /// the view frame is computed from the provided radioLabels.
  64. /// </summary>
  65. /// <param name="x">The x coordinate.</param>
  66. /// <param name="y">The y coordinate.</param>
  67. /// <param name="radioLabels">Radio labels, the strings can contain hotkeys using an undermine 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. public override void Redraw (Rect region)
  99. {
  100. base.Redraw (region);
  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. }
  108. public override void PositionCursor ()
  109. {
  110. Move (1, cursor);
  111. }
  112. public Action<int> SelectionChanged;
  113. /// <summary>
  114. /// The currently selected item from the list of radio labels
  115. /// </summary>
  116. /// <value>The selected.</value>
  117. public int Selected {
  118. get => selected;
  119. set {
  120. selected = value;
  121. SelectionChanged?.Invoke (selected);
  122. SetNeedsDisplay ();
  123. }
  124. }
  125. public override bool ProcessColdKey (KeyEvent kb)
  126. {
  127. var key = kb.KeyValue;
  128. if (key < Char.MaxValue && Char.IsLetterOrDigit ((char)key)) {
  129. int i = 0;
  130. key = Char.ToUpper ((char)key);
  131. foreach (var l in radioLabels) {
  132. bool nextIsHot = false;
  133. foreach (var c in l) {
  134. if (c == '_')
  135. nextIsHot = true;
  136. else {
  137. if (nextIsHot && c == key) {
  138. Selected = i;
  139. cursor = i;
  140. if (!HasFocus)
  141. SuperView.SetFocus (this);
  142. return true;
  143. }
  144. nextIsHot = false;
  145. }
  146. }
  147. i++;
  148. }
  149. }
  150. return false;
  151. }
  152. public override bool ProcessKey (KeyEvent kb)
  153. {
  154. switch (kb.Key) {
  155. case Key.CursorUp:
  156. if (cursor > 0) {
  157. cursor--;
  158. SetNeedsDisplay ();
  159. return true;
  160. }
  161. break;
  162. case Key.CursorDown:
  163. if (cursor + 1 < radioLabels.Length) {
  164. cursor++;
  165. SetNeedsDisplay ();
  166. return true;
  167. }
  168. break;
  169. case Key.Space:
  170. Selected = cursor;
  171. return true;
  172. }
  173. return base.ProcessKey (kb);
  174. }
  175. public override bool MouseEvent (MouseEvent me)
  176. {
  177. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked))
  178. return false;
  179. SuperView.SetFocus (this);
  180. if (me.Y < radioLabels.Length) {
  181. cursor = Selected = me.Y;
  182. SetNeedsDisplay ();
  183. }
  184. return true;
  185. }
  186. }
  187. }