RadioGroup.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. var r = MakeRect (0, 0, radioLabels);
  43. Width = r.Width;
  44. Height = radioLabels.Length;
  45. this.selected = selected;
  46. this.radioLabels = radioLabels;
  47. CanFocus = true;
  48. }
  49. static Rect MakeRect (int x, int y, string [] radioLabels)
  50. {
  51. int width = 0;
  52. foreach (var s in radioLabels)
  53. width = Math.Max (s.Length + 4, width);
  54. return new Rect (x, y, width, radioLabels.Length);
  55. }
  56. /// <summary>
  57. /// Initializes a new instance of the <see cref="T:Terminal.Gui.RadioGroup"/> class
  58. /// setting up the initial set of radio labels and the item that should be selected,
  59. /// the view frame is computed from the provided radioLabels.
  60. /// </summary>
  61. /// <param name="x">The x coordinate.</param>
  62. /// <param name="y">The y coordinate.</param>
  63. /// <param name="radioLabels">Radio labels, the strings can contain hotkeys using an undermine before the letter.</param>
  64. /// <param name="selected">The item to be selected, the value is clamped to the number of items.</param>
  65. public RadioGroup (int x, int y, string [] radioLabels, int selected = 0) : this (MakeRect (x, y, radioLabels), radioLabels, selected)
  66. {
  67. }
  68. string [] radioLabels;
  69. /// <summary>
  70. /// The radio labels to display
  71. /// </summary>
  72. /// <value>The radio labels.</value>
  73. public string [] RadioLabels {
  74. get => radioLabels;
  75. set {
  76. radioLabels = value;
  77. selected = 0;
  78. cursor = 0;
  79. SetNeedsDisplay ();
  80. }
  81. }
  82. public override void Redraw (Rect region)
  83. {
  84. base.Redraw (region);
  85. for (int i = 0; i < radioLabels.Length; i++) {
  86. Move (0, i);
  87. Driver.SetAttribute (ColorScheme.Normal);
  88. Driver.AddStr (i == selected ? "(o) " : "( ) ");
  89. DrawHotString (radioLabels [i], HasFocus && i == cursor, ColorScheme);
  90. }
  91. }
  92. public override void PositionCursor ()
  93. {
  94. Move (1, cursor);
  95. }
  96. public Action<int> SelectionChanged;
  97. /// <summary>
  98. /// The currently selected item from the list of radio labels
  99. /// </summary>
  100. /// <value>The selected.</value>
  101. public int Selected {
  102. get => selected;
  103. set {
  104. selected = value;
  105. SelectionChanged?.Invoke (selected);
  106. SetNeedsDisplay ();
  107. }
  108. }
  109. public override bool ProcessColdKey (KeyEvent kb)
  110. {
  111. var key = kb.KeyValue;
  112. if (key < Char.MaxValue && Char.IsLetterOrDigit ((char)key)) {
  113. int i = 0;
  114. key = Char.ToUpper ((char)key);
  115. foreach (var l in radioLabels) {
  116. bool nextIsHot = false;
  117. foreach (var c in l) {
  118. if (c == '_')
  119. nextIsHot = true;
  120. else {
  121. if (nextIsHot && c == key) {
  122. Selected = i;
  123. cursor = i;
  124. if (!HasFocus)
  125. SuperView.SetFocus (this);
  126. return true;
  127. }
  128. nextIsHot = false;
  129. }
  130. }
  131. i++;
  132. }
  133. }
  134. return false;
  135. }
  136. public override bool ProcessKey (KeyEvent kb)
  137. {
  138. switch (kb.Key) {
  139. case Key.CursorUp:
  140. if (cursor > 0) {
  141. cursor--;
  142. SetNeedsDisplay ();
  143. return true;
  144. }
  145. break;
  146. case Key.CursorDown:
  147. if (cursor + 1 < radioLabels.Length) {
  148. cursor++;
  149. SetNeedsDisplay ();
  150. return true;
  151. }
  152. break;
  153. case Key.Space:
  154. Selected = cursor;
  155. return true;
  156. }
  157. return base.ProcessKey (kb);
  158. }
  159. public override bool MouseEvent (MouseEvent me)
  160. {
  161. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked))
  162. return false;
  163. SuperView.SetFocus (this);
  164. if (me.Y < radioLabels.Length) {
  165. cursor = Selected = me.Y;
  166. SetNeedsDisplay ();
  167. }
  168. return true;
  169. }
  170. }
  171. }