/AvalonEdit/ICSharpCode.AvalonEdit/CodeCompletion/CompletionListBox.cs

http://github.com/icsharpcode/ILSpy · C# · 111 lines · 65 code · 9 blank · 37 comment · 17 complexity · 92d7ecd8e78b9cfdea9e08716958ee56 MD5 · raw file

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using ICSharpCode.AvalonEdit.Utils;
  22. namespace ICSharpCode.AvalonEdit.CodeCompletion
  23. {
  24. /// <summary>
  25. /// The list box used inside the CompletionList.
  26. /// </summary>
  27. public class CompletionListBox : ListBox
  28. {
  29. internal ScrollViewer scrollViewer;
  30. /// <inheritdoc/>
  31. public override void OnApplyTemplate()
  32. {
  33. base.OnApplyTemplate();
  34. // Find the scroll viewer:
  35. scrollViewer = null;
  36. if (this.VisualChildrenCount > 0) {
  37. Border border = this.GetVisualChild(0) as Border;
  38. if (border != null)
  39. scrollViewer = border.Child as ScrollViewer;
  40. }
  41. }
  42. /// <summary>
  43. /// Gets the number of the first visible item.
  44. /// </summary>
  45. public int FirstVisibleItem {
  46. get {
  47. if (scrollViewer == null || scrollViewer.ExtentHeight == 0) {
  48. return 0;
  49. } else {
  50. return (int)(this.Items.Count * scrollViewer.VerticalOffset / scrollViewer.ExtentHeight);
  51. }
  52. }
  53. set {
  54. value = value.CoerceValue(0, this.Items.Count - this.VisibleItemCount);
  55. if (scrollViewer != null) {
  56. scrollViewer.ScrollToVerticalOffset((double)value / this.Items.Count * scrollViewer.ExtentHeight);
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// Gets the number of visible items.
  62. /// </summary>
  63. public int VisibleItemCount {
  64. get {
  65. if (scrollViewer == null || scrollViewer.ExtentHeight == 0) {
  66. return 10;
  67. } else {
  68. return Math.Max(
  69. 3,
  70. (int)Math.Ceiling(this.Items.Count * scrollViewer.ViewportHeight
  71. / scrollViewer.ExtentHeight));
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// Removes the selection.
  77. /// </summary>
  78. public void ClearSelection()
  79. {
  80. this.SelectedIndex = -1;
  81. }
  82. /// <summary>
  83. /// Selects the item with the specified index and scrolls it into view.
  84. /// </summary>
  85. public void SelectIndex(int index)
  86. {
  87. if (index >= this.Items.Count)
  88. index = this.Items.Count - 1;
  89. if (index < 0)
  90. index = 0;
  91. this.SelectedIndex = index;
  92. this.ScrollIntoView(this.SelectedItem);
  93. }
  94. /// <summary>
  95. /// Centers the view on the item with the specified index.
  96. /// </summary>
  97. public void CenterViewOn(int index)
  98. {
  99. this.FirstVisibleItem = index - VisibleItemCount / 2;
  100. }
  101. }
  102. }