ComboBox.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. //
  2. // ComboBox.cs: ComboBox control
  3. //
  4. // Authors:
  5. // Ross Ferguson ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using NStack;
  13. namespace Terminal.Gui {
  14. /// <summary>
  15. /// ComboBox control
  16. /// </summary>
  17. public class ComboBox : View {
  18. IListDataSource source;
  19. /// <summary>
  20. /// Gets or sets the <see cref="IListDataSource"/> backing this <see cref="ComboBox"/>, enabling custom rendering.
  21. /// </summary>
  22. /// <value>The source.</value>
  23. /// <remarks>
  24. /// Use <see cref="SetSource"/> to set a new <see cref="IList"/> source.
  25. /// </remarks>
  26. public IListDataSource Source {
  27. get => source;
  28. set {
  29. source = value;
  30. SetNeedsDisplay ();
  31. }
  32. }
  33. /// <summary>
  34. /// Sets the source of the <see cref="ComboBox"/> to an <see cref="IList"/>.
  35. /// </summary>
  36. /// <value>An object implementing the IList interface.</value>
  37. /// <remarks>
  38. /// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custome rendering.
  39. /// </remarks>
  40. public void SetSource (IList source)
  41. {
  42. if (source == null)
  43. Source = null;
  44. else {
  45. Source = MakeWrapper (source);
  46. }
  47. }
  48. /// <summary>
  49. /// Changed event, raised when the selection has been confirmed.
  50. /// </summary>
  51. /// <remarks>
  52. /// Client code can hook up to this event, it is
  53. /// raised when the selection has been confirmed.
  54. /// </remarks>
  55. public event EventHandler<ustring> SelectedItemChanged;
  56. IList searchset;
  57. ustring text = "";
  58. TextField search;
  59. ListView listview;
  60. int x = 0;
  61. int y = 0;
  62. int height;
  63. int width;
  64. bool autoHide = true;
  65. /// <summary>
  66. /// Public constructor
  67. /// </summary>
  68. public ComboBox () : base()
  69. {
  70. ColorScheme = Colors.Base;
  71. search = new TextField ("");
  72. listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true };
  73. Initialize ();
  74. }
  75. ///// <summary>
  76. ///// Public constructor
  77. ///// </summary>
  78. ///// <param name="rect"></param>
  79. ///// <param name="source"></param>
  80. //public ComboBox (Rect rect, IList source) : base (rect)
  81. //{
  82. // SetSource (source);
  83. // this.x = rect.X;
  84. // this.y = rect.Y;
  85. // this.height = rect.Height;
  86. // this.width = rect.Width;
  87. // search = new TextField ("") { X = rect.X, Y = rect.Y, Width = width };
  88. // listview = new ListView (new Rect (rect.X, rect.Y + 1, width, 0), source) { LayoutStyle = LayoutStyle.Computed};
  89. // Initialize ();
  90. //}
  91. static IListDataSource MakeWrapper (IList source)
  92. {
  93. return new ListWrapper (source);
  94. }
  95. private void Initialize()
  96. {
  97. search.TextChanged += Search_Changed;
  98. // On resize
  99. LayoutComplete += (LayoutEventArgs a) => {
  100. search.Width = Bounds.Width;
  101. listview.Width = autoHide ? Bounds.Width - 1 : Bounds.Width;
  102. };
  103. listview.SelectedItemChanged += (ListViewItemEventArgs e) => {
  104. if(searchset.Count > 0)
  105. SetValue ((string)searchset [listview.SelectedItem]);
  106. };
  107. Application.Loaded += (Application.ResizedEventArgs a) => {
  108. // Determine if this view is hosted inside a dialog
  109. for (View view = this.SuperView; view != null; view = view.SuperView) {
  110. if (view is Dialog) {
  111. autoHide = false;
  112. break;
  113. }
  114. }
  115. ResetSearchSet ();
  116. // Needs to be re-applied for LayoutStyle.Computed
  117. // If Dim or Pos are null, these are the from the parametrized constructor
  118. if (X == null)
  119. listview.X = x;
  120. if (Y == null)
  121. listview.Y = y + 1;
  122. else
  123. listview.Y = Pos.Bottom (search);
  124. if (Width == null) {
  125. listview.Width = CalculateWidth ();
  126. search.Width = width;
  127. } else {
  128. width = GetDimAsInt (Width, a, vertical: false);
  129. search.Width = width;
  130. listview.Width = CalculateWidth ();
  131. }
  132. if (Height == null) {
  133. var h = CalculatetHeight ();
  134. listview.Height = h;
  135. this.Height = h + 1; // adjust view to account for search box
  136. } else {
  137. if (height == 0)
  138. height = GetDimAsInt (Height, a, vertical: true);
  139. listview.Height = CalculatetHeight ();
  140. this.Height = height + 1; // adjust view to account for search box
  141. }
  142. if (this.Text != null)
  143. Search_Changed (Text);
  144. if (autoHide)
  145. listview.ColorScheme = Colors.Menu;
  146. else
  147. search.ColorScheme = Colors.Menu;
  148. };
  149. search.MouseClick += Search_MouseClick;
  150. this.Add(listview, search);
  151. this.SetFocus(search);
  152. }
  153. private void Search_MouseClick (MouseEventArgs e)
  154. {
  155. if (e.MouseEvent.Flags != MouseFlags.Button1Clicked)
  156. return;
  157. SuperView.SetFocus (search);
  158. }
  159. ///<inheritdoc/>
  160. public override bool OnEnter ()
  161. {
  162. if (!search.HasFocus)
  163. this.SetFocus (search);
  164. search.CursorPosition = search.Text.Length;
  165. return true;
  166. }
  167. /// <summary>
  168. /// Invokes the SelectedChanged event if it is defined.
  169. /// </summary>
  170. /// <returns></returns>
  171. public virtual bool OnSelectedChanged ()
  172. {
  173. // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic.
  174. // So we cannot optimize. Ie: Don't call if not changed
  175. SelectedItemChanged?.Invoke (this, search.Text);
  176. return true;
  177. }
  178. ///<inheritdoc/>
  179. public override bool ProcessKey(KeyEvent e)
  180. {
  181. if (e.Key == Key.Tab) {
  182. base.ProcessKey(e);
  183. return false; // allow tab-out to next control
  184. }
  185. if (e.Key == Key.Enter && listview.HasFocus) {
  186. if (listview.Source.Count == 0 || searchset.Count == 0) {
  187. text = "";
  188. return true;
  189. }
  190. SetValue((string)searchset [listview.SelectedItem]);
  191. search.CursorPosition = search.Text.Length;
  192. Search_Changed (search.Text);
  193. OnSelectedChanged ();
  194. searchset.Clear();
  195. listview.Clear ();
  196. listview.Height = 0;
  197. this.SetFocus(search);
  198. return true;
  199. }
  200. if (e.Key == Key.CursorDown && search.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) { // jump to list
  201. this.SetFocus (listview);
  202. SetValue ((string)searchset [listview.SelectedItem]);
  203. return true;
  204. }
  205. if (e.Key == Key.CursorUp && search.HasFocus) // stop odd behavior on KeyUp when search has focus
  206. return true;
  207. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) // jump back to search
  208. {
  209. search.CursorPosition = search.Text.Length;
  210. this.SetFocus (search);
  211. return true;
  212. }
  213. if (e.Key == Key.Esc) {
  214. this.SetFocus (search);
  215. search.Text = text = "";
  216. OnSelectedChanged ();
  217. return true;
  218. }
  219. // Unix emulation
  220. if (e.Key == Key.ControlU)
  221. {
  222. Reset();
  223. return true;
  224. }
  225. return base.ProcessKey(e);
  226. }
  227. /// <summary>
  228. /// The currently selected list item
  229. /// </summary>
  230. public ustring Text
  231. {
  232. get
  233. {
  234. return text;
  235. }
  236. set {
  237. search.Text = text = value;
  238. }
  239. }
  240. private void SetValue(ustring text)
  241. {
  242. search.TextChanged -= Search_Changed;
  243. this.text = search.Text = text;
  244. search.CursorPosition = 0;
  245. search.TextChanged += Search_Changed;
  246. }
  247. /// <summary>
  248. /// Reset to full original list
  249. /// </summary>
  250. private void Reset()
  251. {
  252. search.Text = text = "";
  253. OnSelectedChanged();
  254. ResetSearchSet ();
  255. listview.SetSource(searchset);
  256. listview.Height = CalculatetHeight ();
  257. this.SetFocus(search);
  258. }
  259. private void ResetSearchSet()
  260. {
  261. if (autoHide) {
  262. if (searchset == null)
  263. searchset = new List<string> ();
  264. else
  265. searchset.Clear ();
  266. } else
  267. searchset = source.ToList ();
  268. }
  269. private void Search_Changed (ustring text)
  270. {
  271. if (source == null) // Object initialization
  272. return;
  273. if (string.IsNullOrEmpty (search.Text.ToString ()))
  274. ResetSearchSet ();
  275. else
  276. searchset = source.ToList().Cast<string>().Where (x => x.StartsWith (search.Text.ToString (), StringComparison.CurrentCultureIgnoreCase)).ToList();
  277. listview.SetSource (searchset);
  278. listview.Height = CalculatetHeight ();
  279. listview.Redraw (new Rect (0, 0, width, height)); // for any view behind this
  280. this.SuperView?.BringSubviewToFront (this);
  281. }
  282. /// <summary>
  283. /// Internal height of dynamic search list
  284. /// </summary>
  285. /// <returns></returns>
  286. private int CalculatetHeight ()
  287. {
  288. return Math.Min (height, searchset.Count);
  289. }
  290. /// <summary>
  291. /// Internal width of search list
  292. /// </summary>
  293. /// <returns></returns>
  294. private int CalculateWidth ()
  295. {
  296. return autoHide ? Math.Max (1, width - 1) : width;
  297. }
  298. /// <summary>
  299. /// Get DimAbsolute as integer value
  300. /// </summary>
  301. /// <param name="dim"></param>
  302. /// <param name="a"></param>
  303. /// <param name="vertical"></param>
  304. /// <returns></returns>
  305. private int GetDimAsInt (Dim dim, Application.ResizedEventArgs a, bool vertical)
  306. {
  307. if (dim is Dim.DimAbsolute)
  308. return dim.Anchor (0);
  309. else
  310. return vertical ? dim.Anchor (a.Rows) : dim.Anchor (a.Cols);
  311. }
  312. }
  313. }