ComboBox.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. search = new TextField ("");
  71. listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true };
  72. Initialize ();
  73. }
  74. ///// <summary>
  75. ///// Public constructor
  76. ///// </summary>
  77. ///// <param name="rect"></param>
  78. ///// <param name="source"></param>
  79. //public ComboBox (Rect rect, IList source) : base (rect)
  80. //{
  81. // SetSource (source);
  82. // this.x = rect.X;
  83. // this.y = rect.Y;
  84. // this.height = rect.Height;
  85. // this.width = rect.Width;
  86. // search = new TextField ("") { X = rect.X, Y = rect.Y, Width = width };
  87. // listview = new ListView (new Rect (rect.X, rect.Y + 1, width, 0), source) { LayoutStyle = LayoutStyle.Computed};
  88. // Initialize ();
  89. //}
  90. static IListDataSource MakeWrapper (IList source)
  91. {
  92. return new ListWrapper (source);
  93. }
  94. private void Initialize()
  95. {
  96. search.TextChanged += Search_Changed;
  97. // On resize
  98. LayoutComplete += (LayoutEventArgs a) => {
  99. search.Width = Bounds.Width;
  100. listview.Width = autoHide ? Bounds.Width - 1 : Bounds.Width;
  101. };
  102. listview.SelectedItemChanged += (ListViewItemEventArgs e) => {
  103. if(searchset.Count > 0)
  104. SetValue ((string)searchset [listview.SelectedItem]);
  105. };
  106. Application.Loaded += (Application.ResizedEventArgs a) => {
  107. // Determine if this view is hosted inside a dialog
  108. for (View view = this.SuperView; view != null; view = view.SuperView) {
  109. if (view is Dialog) {
  110. autoHide = false;
  111. break;
  112. }
  113. }
  114. ResetSearchSet ();
  115. // Needs to be re-applied for LayoutStyle.Computed
  116. // If Dim or Pos are null, these are the from the parametrized constructor
  117. if (X == null)
  118. listview.X = x;
  119. if (Y == null)
  120. listview.Y = y + 1;
  121. else
  122. listview.Y = Pos.Bottom (search);
  123. if (Width == null) {
  124. listview.Width = CalculateWidth ();
  125. search.Width = width;
  126. } else {
  127. width = GetDimAsInt (Width, a, vertical: false);
  128. search.Width = width;
  129. listview.Width = CalculateWidth ();
  130. }
  131. if (Height == null) {
  132. var h = CalculatetHeight ();
  133. listview.Height = h;
  134. this.Height = h + 1; // adjust view to account for search box
  135. } else {
  136. if (height == 0)
  137. height = GetDimAsInt (Height, a, vertical: true);
  138. listview.Height = CalculatetHeight ();
  139. this.Height = height + 1; // adjust view to account for search box
  140. }
  141. if (this.Text != null)
  142. Search_Changed (Text);
  143. if (autoHide)
  144. listview.ColorScheme = Colors.Menu;
  145. else
  146. search.ColorScheme = Colors.Menu;
  147. };
  148. search.MouseClick += Search_MouseClick;
  149. this.Add(listview, search);
  150. this.SetFocus(search);
  151. }
  152. private void Search_MouseClick (MouseEventArgs e)
  153. {
  154. if (e.MouseEvent.Flags != MouseFlags.Button1Clicked)
  155. return;
  156. SuperView.SetFocus (search);
  157. }
  158. ///<inheritdoc/>
  159. public override bool OnEnter ()
  160. {
  161. if (!search.HasFocus)
  162. this.SetFocus (search);
  163. search.CursorPosition = search.Text.Length;
  164. return true;
  165. }
  166. /// <summary>
  167. /// Invokes the SelectedChanged event if it is defined.
  168. /// </summary>
  169. /// <returns></returns>
  170. public virtual bool OnSelectedChanged ()
  171. {
  172. // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic.
  173. // So we cannot optimize. Ie: Don't call if not changed
  174. SelectedItemChanged?.Invoke (this, search.Text);
  175. return true;
  176. }
  177. ///<inheritdoc/>
  178. public override bool ProcessKey(KeyEvent e)
  179. {
  180. if (e.Key == Key.Tab) {
  181. base.ProcessKey(e);
  182. return false; // allow tab-out to next control
  183. }
  184. if (e.Key == Key.Enter && listview.HasFocus) {
  185. if (listview.Source.Count == 0 || searchset.Count == 0) {
  186. text = "";
  187. return true;
  188. }
  189. SetValue((string)searchset [listview.SelectedItem]);
  190. search.CursorPosition = search.Text.Length;
  191. Search_Changed (search.Text);
  192. OnSelectedChanged ();
  193. searchset.Clear();
  194. listview.Clear ();
  195. listview.Height = 0;
  196. this.SetFocus(search);
  197. return true;
  198. }
  199. if (e.Key == Key.CursorDown && search.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) { // jump to list
  200. this.SetFocus (listview);
  201. SetValue ((string)searchset [listview.SelectedItem]);
  202. return true;
  203. }
  204. if (e.Key == Key.CursorUp && search.HasFocus) // stop odd behavior on KeyUp when search has focus
  205. return true;
  206. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) // jump back to search
  207. {
  208. search.CursorPosition = search.Text.Length;
  209. this.SetFocus (search);
  210. return true;
  211. }
  212. if (e.Key == Key.Esc) {
  213. this.SetFocus (search);
  214. search.Text = text = "";
  215. OnSelectedChanged ();
  216. return true;
  217. }
  218. // Unix emulation
  219. if (e.Key == Key.ControlU)
  220. {
  221. Reset();
  222. return true;
  223. }
  224. return base.ProcessKey(e);
  225. }
  226. /// <summary>
  227. /// The currently selected list item
  228. /// </summary>
  229. public ustring Text
  230. {
  231. get
  232. {
  233. return text;
  234. }
  235. set {
  236. search.Text = text = value;
  237. }
  238. }
  239. private void SetValue(ustring text)
  240. {
  241. search.TextChanged -= Search_Changed;
  242. this.text = search.Text = text;
  243. search.CursorPosition = 0;
  244. search.TextChanged += Search_Changed;
  245. }
  246. /// <summary>
  247. /// Reset to full original list
  248. /// </summary>
  249. private void Reset()
  250. {
  251. search.Text = text = "";
  252. OnSelectedChanged();
  253. ResetSearchSet ();
  254. listview.SetSource(searchset);
  255. listview.Height = CalculatetHeight ();
  256. this.SetFocus(search);
  257. }
  258. private void ResetSearchSet()
  259. {
  260. if (autoHide) {
  261. if (searchset == null)
  262. searchset = new List<string> ();
  263. else
  264. searchset.Clear ();
  265. } else
  266. searchset = source.ToList ();
  267. }
  268. private void Search_Changed (ustring text)
  269. {
  270. if (source == null) // Object initialization
  271. return;
  272. if (string.IsNullOrEmpty (search.Text.ToString ()))
  273. ResetSearchSet ();
  274. else
  275. searchset = source.ToList().Cast<string>().Where (x => x.StartsWith (search.Text.ToString (), StringComparison.CurrentCultureIgnoreCase)).ToList();
  276. listview.SetSource (searchset);
  277. listview.Height = CalculatetHeight ();
  278. listview.Redraw (new Rect (0, 0, width, height)); // for any view behind this
  279. this.SuperView?.BringSubviewToFront (this);
  280. }
  281. /// <summary>
  282. /// Internal height of dynamic search list
  283. /// </summary>
  284. /// <returns></returns>
  285. private int CalculatetHeight ()
  286. {
  287. return Math.Min (height, searchset.Count);
  288. }
  289. /// <summary>
  290. /// Internal width of search list
  291. /// </summary>
  292. /// <returns></returns>
  293. private int CalculateWidth ()
  294. {
  295. return autoHide ? Math.Max (1, width - 1) : width;
  296. }
  297. /// <summary>
  298. /// Get DimAbsolute as integer value
  299. /// </summary>
  300. /// <param name="dim"></param>
  301. /// <param name="a"></param>
  302. /// <param name="vertical"></param>
  303. /// <returns></returns>
  304. private int GetDimAsInt (Dim dim, Application.ResizedEventArgs a, bool vertical)
  305. {
  306. if (dim is Dim.DimAbsolute)
  307. return dim.Anchor (0);
  308. else
  309. return vertical ? dim.Anchor (a.Rows) : dim.Anchor (a.Cols);
  310. }
  311. }
  312. }