ComboBox.cs 7.5 KB

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