RadioGroup.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Terminal.Gui {
  6. /// <summary>
  7. /// <see cref="RadioGroup"/> shows a group of radio labels, only one of those can be selected at a given time
  8. /// </summary>
  9. public class RadioGroup : View {
  10. int selected = -1;
  11. int cursor;
  12. void Init(Rect rect, ustring [] radioLabels, int selected)
  13. {
  14. if (radioLabels == null) {
  15. this.radioLabels = new List<ustring>();
  16. } else {
  17. this.radioLabels = radioLabels.ToList ();
  18. }
  19. this.selected = selected;
  20. SetWidthHeight (this.radioLabels);
  21. CanFocus = true;
  22. }
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="RadioGroup"/> class using <see cref="LayoutStyle.Computed"/> layout.
  25. /// </summary>
  26. public RadioGroup () : this (radioLabels: new ustring [] { }) { }
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="RadioGroup"/> class using <see cref="LayoutStyle.Computed"/> layout.
  29. /// </summary>
  30. /// <param name="radioLabels">The radio labels; an array of strings that can contain hotkeys using an underscore before the letter.</param>
  31. /// <param name="selected">The index of the item to be selected, the value is clamped to the number of items.</param>
  32. public RadioGroup (ustring [] radioLabels, int selected = 0) : base ()
  33. {
  34. Init (Rect.Empty, radioLabels, selected);
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="RadioGroup"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  38. /// </summary>
  39. /// <param name="rect">Boundaries for the radio group.</param>
  40. /// <param name="radioLabels">The radio labels; an array of strings that can contain hotkeys using an underscore before the letter.</param>
  41. /// <param name="selected">The index of item to be selected, the value is clamped to the number of items.</param>
  42. public RadioGroup (Rect rect, ustring [] radioLabels, int selected = 0) : base (rect)
  43. {
  44. Init (rect, radioLabels, selected);
  45. }
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="RadioGroup"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  48. /// The <see cref="View"/> frame is computed from the provided radio labels.
  49. /// </summary>
  50. /// <param name="x">The x coordinate.</param>
  51. /// <param name="y">The y coordinate.</param>
  52. /// <param name="radioLabels">The radio labels; an array of strings that can contain hotkeys using an underscore before the letter.</param>
  53. /// <param name="selected">The item to be selected, the value is clamped to the number of items.</param>
  54. public RadioGroup (int x, int y, ustring [] radioLabels, int selected = 0) :
  55. this (MakeRect (x, y, radioLabels != null ? radioLabels.ToList() : null), radioLabels, selected) { }
  56. /// <summary>
  57. /// The location of the cursor in the <see cref="RadioGroup"/>
  58. /// </summary>
  59. public int Cursor {
  60. get => cursor;
  61. set {
  62. if (cursor < 0 || cursor >= radioLabels.Count)
  63. return;
  64. cursor = value;
  65. SetNeedsDisplay ();
  66. }
  67. }
  68. void SetWidthHeight (List<ustring> radioLabels)
  69. {
  70. var r = MakeRect(0, 0, radioLabels);
  71. if (LayoutStyle == LayoutStyle.Computed) {
  72. Width = r.Width;
  73. Height = radioLabels.Count;
  74. } else {
  75. Frame = new Rect (Frame.Location, new Size (r.Width, radioLabels.Count));
  76. }
  77. }
  78. static Rect MakeRect (int x, int y, List<ustring> radioLabels)
  79. {
  80. int width = 0;
  81. if (radioLabels == null) {
  82. return new Rect (x, y, width, 0);
  83. }
  84. foreach (var s in radioLabels)
  85. width = Math.Max (s.Length + 3, width);
  86. return new Rect (x, y, width, radioLabels.Count);
  87. }
  88. List<ustring> radioLabels = new List<ustring> ();
  89. /// <summary>
  90. /// The radio labels to display
  91. /// </summary>
  92. /// <value>The radio labels.</value>
  93. public ustring [] RadioLabels {
  94. get => radioLabels.ToArray();
  95. set {
  96. var prevCount = radioLabels.Count;
  97. radioLabels = value.ToList ();
  98. if (prevCount != radioLabels.Count) {
  99. SetWidthHeight (radioLabels);
  100. }
  101. SelectedItem = 0;
  102. cursor = 0;
  103. SetNeedsDisplay ();
  104. }
  105. }
  106. //// Redraws the RadioGroup
  107. //void Update(List<ustring> newRadioLabels)
  108. //{
  109. // for (int i = 0; i < radioLabels.Count; i++) {
  110. // Move(0, i);
  111. // Driver.SetAttribute(ColorScheme.Normal);
  112. // Driver.AddStr(ustring.Make(new string (' ', radioLabels[i].Length + 4)));
  113. // }
  114. // if (newRadioLabels.Count != radioLabels.Count) {
  115. // SetWidthHeight(newRadioLabels);
  116. // }
  117. //}
  118. ///<inheritdoc/>
  119. public override void Redraw (Rect bounds)
  120. {
  121. Driver.SetAttribute (ColorScheme.Normal);
  122. Clear ();
  123. for (int i = 0; i < radioLabels.Count; i++) {
  124. Move (0, i);
  125. Driver.SetAttribute (ColorScheme.Normal);
  126. Driver.AddStr (ustring.Make(new Rune[] { (i == selected ? Driver.Selected : Driver.UnSelected), ' '}));
  127. DrawHotString (radioLabels [i], HasFocus && i == cursor, ColorScheme);
  128. }
  129. }
  130. ///<inheritdoc/>
  131. public override void PositionCursor ()
  132. {
  133. Move (0, cursor);
  134. }
  135. // TODO: Make this a global class
  136. /// <summary>
  137. /// Event arguments for the SelectedItemChagned event.
  138. /// </summary>
  139. public class SelectedItemChangedArgs : EventArgs {
  140. /// <summary>
  141. /// Gets the index of the item that was previously selected. -1 if there was no previous selection.
  142. /// </summary>
  143. public int PreviousSelectedItem { get; }
  144. /// <summary>
  145. /// Gets the index of the item that is now selected. -1 if there is no selection.
  146. /// </summary>
  147. public int SelectedItem { get; }
  148. /// <summary>
  149. /// Initializes a new <see cref="SelectedItemChangedArgs"/> class.
  150. /// </summary>
  151. /// <param name="selectedItem"></param>
  152. /// <param name="previousSelectedItem"></param>
  153. public SelectedItemChangedArgs(int selectedItem, int previousSelectedItem)
  154. {
  155. PreviousSelectedItem = previousSelectedItem;
  156. SelectedItem = selectedItem;
  157. }
  158. }
  159. /// <summary>
  160. /// Invoked when the selected radio label has changed.
  161. /// </summary>
  162. public Action<SelectedItemChangedArgs> SelectedItemChanged;
  163. /// <summary>
  164. /// The currently selected item from the list of radio labels
  165. /// </summary>
  166. /// <value>The selected.</value>
  167. public int SelectedItem {
  168. get => selected;
  169. set {
  170. OnSelectedItemChanged (value, SelectedItem);
  171. SetNeedsDisplay ();
  172. }
  173. }
  174. /// <summary>
  175. /// Called whenever the current selected item changes. Invokes the <see cref="SelectedItemChanged"/> event.
  176. /// </summary>
  177. /// <param name="selectedItem"></param>
  178. /// <param name="previousSelectedItem"></param>
  179. public virtual void OnSelectedItemChanged (int selectedItem, int previousSelectedItem)
  180. {
  181. selected = selectedItem;
  182. SelectedItemChanged?.Invoke (new SelectedItemChangedArgs (selectedItem, previousSelectedItem));
  183. }
  184. ///<inheritdoc/>
  185. public override bool ProcessColdKey (KeyEvent kb)
  186. {
  187. var key = kb.KeyValue;
  188. if (key < Char.MaxValue && Char.IsLetterOrDigit ((char)key)) {
  189. int i = 0;
  190. key = Char.ToUpper ((char)key);
  191. foreach (var l in radioLabels) {
  192. bool nextIsHot = false;
  193. foreach (var c in l) {
  194. if (c == '_')
  195. nextIsHot = true;
  196. else {
  197. if (nextIsHot && c == key) {
  198. SelectedItem = i;
  199. cursor = i;
  200. if (!HasFocus)
  201. SuperView.SetFocus (this);
  202. return true;
  203. }
  204. nextIsHot = false;
  205. }
  206. }
  207. i++;
  208. }
  209. }
  210. return false;
  211. }
  212. ///<inheritdoc/>
  213. public override bool ProcessKey (KeyEvent kb)
  214. {
  215. switch (kb.Key) {
  216. case Key.CursorUp:
  217. if (cursor > 0) {
  218. cursor--;
  219. SetNeedsDisplay ();
  220. return true;
  221. }
  222. break;
  223. case Key.CursorDown:
  224. if (cursor + 1 < radioLabels.Count) {
  225. cursor++;
  226. SetNeedsDisplay ();
  227. return true;
  228. }
  229. break;
  230. case Key.Space:
  231. SelectedItem = cursor;
  232. return true;
  233. }
  234. return base.ProcessKey (kb);
  235. }
  236. ///<inheritdoc/>
  237. public override bool MouseEvent (MouseEvent me)
  238. {
  239. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked))
  240. return false;
  241. SuperView.SetFocus (this);
  242. if (me.Y < radioLabels.Count) {
  243. cursor = SelectedItem = me.Y;
  244. SetNeedsDisplay ();
  245. }
  246. return true;
  247. }
  248. }
  249. }