PageRenderTime 25ms CodeModel.GetById 20ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 19using System;
 20using System.Windows;
 21using System.Windows.Controls;
 22using ICSharpCode.AvalonEdit.Utils;
 23
 24namespace ICSharpCode.AvalonEdit.CodeCompletion
 25{
 26	/// <summary>
 27	/// The list box used inside the CompletionList.
 28	/// </summary>
 29	public class CompletionListBox : ListBox
 30	{
 31		internal ScrollViewer scrollViewer;
 32		
 33		/// <inheritdoc/>
 34		public override void OnApplyTemplate()
 35		{
 36			base.OnApplyTemplate();
 37			
 38			// Find the scroll viewer:
 39			scrollViewer = null;
 40			if (this.VisualChildrenCount > 0) {
 41				Border border = this.GetVisualChild(0) as Border;
 42				if (border != null)
 43					scrollViewer = border.Child as ScrollViewer;
 44			}
 45		}
 46		
 47		/// <summary>
 48		/// Gets the number of the first visible item.
 49		/// </summary>
 50		public int FirstVisibleItem {
 51			get {
 52				if (scrollViewer == null || scrollViewer.ExtentHeight == 0) {
 53					return 0;
 54				} else {
 55					return (int)(this.Items.Count * scrollViewer.VerticalOffset / scrollViewer.ExtentHeight);
 56				}
 57			}
 58			set {
 59				value = value.CoerceValue(0, this.Items.Count - this.VisibleItemCount);
 60				if (scrollViewer != null) {
 61					scrollViewer.ScrollToVerticalOffset((double)value / this.Items.Count * scrollViewer.ExtentHeight);
 62				}
 63			}
 64		}
 65		
 66		/// <summary>
 67		/// Gets the number of visible items.
 68		/// </summary>
 69		public int VisibleItemCount {
 70			get {
 71				if (scrollViewer == null || scrollViewer.ExtentHeight == 0) {
 72					return 10;
 73				} else {
 74					return Math.Max(
 75						3,
 76						(int)Math.Ceiling(this.Items.Count * scrollViewer.ViewportHeight
 77						                  / scrollViewer.ExtentHeight));
 78				}
 79			}
 80		}
 81		
 82		/// <summary>
 83		/// Removes the selection.
 84		/// </summary>
 85		public void ClearSelection()
 86		{
 87			this.SelectedIndex = -1;
 88		}
 89		
 90		/// <summary>
 91		/// Selects the item with the specified index and scrolls it into view.
 92		/// </summary>
 93		public void SelectIndex(int index)
 94		{
 95			if (index >= this.Items.Count)
 96				index = this.Items.Count - 1;
 97			if (index < 0)
 98				index = 0;
 99			this.SelectedIndex = index;
100			this.ScrollIntoView(this.SelectedItem);
101		}
102		
103		/// <summary>
104		/// Centers the view on the item with the specified index.
105		/// </summary>
106		public void CenterViewOn(int index)
107		{
108			this.FirstVisibleItem = index - VisibleItemCount / 2;
109		}
110	}
111}