RadioGroup.cs 4.8 KB

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