/// <summary>
        /// Processes dialog-key events (escape, tab, etc.).
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="keyEventArgs">
        /// The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.
        /// </param>
        protected override void ProcessTextAreaKey(object sender, KeyEventArgs keyEventArgs)
        {
            if (!Visible)
            {
                return;
            }

            switch (keyEventArgs.KeyData)
            {
            case Keys.Home:
                _completionListView.SelectItem(0);
                keyEventArgs.Handled = true;
                return;

            case Keys.End:
                _completionListView.SelectItem(_completionListView.ItemCount - 1);
                keyEventArgs.Handled = true;
                return;

            case Keys.PageDown:
                _completionListView.PageDown();
                keyEventArgs.Handled = true;
                return;

            case Keys.PageUp:
                _completionListView.PageUp();
                keyEventArgs.Handled = true;
                return;

            case Keys.Down:
                _completionListView.SelectNextItem();
                keyEventArgs.Handled = true;
                return;

            case Keys.Up:
                _completionListView.SelectPrevItem();
                keyEventArgs.Handled = true;
                return;

            case Keys.Tab:
                InsertSelectedItem('\t');
                keyEventArgs.Handled = true;
                return;

            case Keys.Return:
                InsertSelectedItem('\n');
                keyEventArgs.Handled = true;
                return;
            }
            base.ProcessTextAreaKey(sender, keyEventArgs);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CompletionWindow"/> class.
        /// </summary>
        /// <param name="completionDataProvider">The completion data provider.</param>
        /// <param name="completionData">The completion data.</param>
        /// <param name="parentForm">The parent form.</param>
        /// <param name="control">The text editor control.</param>
        /// <param name="showDeclarationWindow"><c>true</c> to show declaration window; otherwise <c>false</c>.</param>
        /// <param name="fixedListViewWidth"><c>true</c> to use a fixed width in the list view.</param>
        /// <param name="closeAutomatically"><c>true</c> to close the completion window automatically.</param>
        CompletionWindow(ICompletionDataProvider completionDataProvider, ICompletionData[] completionData, Form parentForm, TextEditorControl control, bool showDeclarationWindow, bool fixedListViewWidth, bool closeAutomatically)
            : base(parentForm, control)
        {
            _dataProvider          = completionDataProvider;
            _completionData        = completionData;
            _document              = control.Document;
            _showDeclarationWindow = showDeclarationWindow;
            _fixedListViewWidth    = fixedListViewWidth;
            _closeAutomatically    = closeAutomatically;

            int caretOffset = control.ActiveTextAreaControl.Caret.Offset;

            _startOffset = caretOffset;
            _endOffset   = caretOffset;

            // Move start offset if something is pre-selected.
            if (!String.IsNullOrEmpty(completionDataProvider.PreSelection))
            {
                _startOffset -= completionDataProvider.PreSelection.Length;
            }

            _completionListView                      = new CompletionListView(completionData);
            _completionListView.Dock                 = DockStyle.Fill;
            _completionListView.FilterList           = true;
            _completionListView.ImageList            = completionDataProvider.ImageList;
            _completionListView.Click               += CompletionListViewClick;
            _completionListView.DoubleClick         += CompletionListViewDoubleClick;
            _completionListView.FirstItemChanged    += CompletionListViewFirstItemChanged;
            _completionListView.ItemCountChanged    += CompletionListViewItemCountChanged;
            _completionListView.SelectedItemChanged += CompletionListViewSelectedItemChanged;
            Controls.Add(_completionListView);

            _vScrollBar             = new VScrollBar();
            _vScrollBar.SmallChange = 1;
            _vScrollBar.LargeChange = _maxListLength;
            _vScrollBar.Dock        = DockStyle.Right;
            Controls.Add(_vScrollBar);
            UpdateScrollBar();

            _workingScreen = Screen.GetWorkingArea(Location);
            DrawingSize    = GetListViewSize();
            _textLocation  = TextEditorControl.ActiveTextAreaControl.TextArea.Caret.Position;
            SetLocation(_textLocation);

            if (_declarationViewWindow == null)
            {
                _declarationViewWindow = new DeclarationViewWindow(parentForm);
            }

            SetDeclarationViewLocation();
            _declarationViewWindow.Show();
            _declarationViewWindow.MouseMove += ControlMouseMove;
            control.Focus();
            CompletionListViewSelectedItemChanged(this, EventArgs.Empty);

            if (!String.IsNullOrEmpty(completionDataProvider.PreSelection))
            {
                // Select item based on pre-selection.
                CaretOffsetChanged(this, EventArgs.Empty);
            }
            else if (completionDataProvider.DefaultIndex >= 0)
            {
                // Select default item
                _completionListView.SelectItem(completionDataProvider.DefaultIndex);
            }

            _vScrollBar.ValueChanged           += VScrollBarValueChanged;
            _document.DocumentAboutToBeChanged += DocumentAboutToBeChanged;
        }
    /// <summary>
    /// Initializes a new instance of the <see cref="CompletionWindow"/> class.
    /// </summary>
    /// <param name="completionDataProvider">The completion data provider.</param>
    /// <param name="completionData">The completion data.</param>
    /// <param name="parentForm">The parent form.</param>
    /// <param name="control">The text editor control.</param>
    /// <param name="showDeclarationWindow"><see langword="true"/> to show declaration window; otherwise <see langword="false"/>.</param>
    /// <param name="fixedListViewWidth"><see langword="true"/> to use a fixed width in the list view.</param>
    /// <param name="closeAutomatically"><see langword="true"/> to close the completion window automatically.</param>
    CompletionWindow(ICompletionDataProvider completionDataProvider, ICompletionData[] completionData, Form parentForm, TextEditorControl control, bool showDeclarationWindow, bool fixedListViewWidth, bool closeAutomatically)
      : base(parentForm, control)
    {
      _dataProvider = completionDataProvider;
      _completionData = completionData;
      _document = control.Document;
      _showDeclarationWindow = showDeclarationWindow;
      _fixedListViewWidth = fixedListViewWidth;
      _closeAutomatically = closeAutomatically;

      int caretOffset = control.ActiveTextAreaControl.Caret.Offset;
      _startOffset = caretOffset;
      _endOffset = caretOffset;

      // Move start offset if something is pre-selected.
      if (!String.IsNullOrEmpty(completionDataProvider.PreSelection))
        _startOffset -= completionDataProvider.PreSelection.Length;

      _completionListView = new CompletionListView(completionData);
      _completionListView.Dock = DockStyle.Fill;
      _completionListView.FilterList = true;
      _completionListView.ImageList = completionDataProvider.ImageList;
      _completionListView.Click += CompletionListViewClick;
      _completionListView.DoubleClick += CompletionListViewDoubleClick;
      _completionListView.FirstItemChanged += CompletionListViewFirstItemChanged;
      _completionListView.ItemCountChanged += CompletionListViewItemCountChanged;
      _completionListView.SelectedItemChanged += CompletionListViewSelectedItemChanged;
      Controls.Add(_completionListView);

      _vScrollBar = new VScrollBar();
      _vScrollBar.SmallChange = 1;
      _vScrollBar.LargeChange = _maxListLength;
      _vScrollBar.Dock = DockStyle.Right;
      Controls.Add(_vScrollBar);
      UpdateScrollBar();

      _workingScreen = Screen.GetWorkingArea(Location);
      DrawingSize = GetListViewSize();
      _textLocation = TextEditorControl.ActiveTextAreaControl.TextArea.Caret.Position;
      SetLocation(_textLocation);

      if (_declarationViewWindow == null)
        _declarationViewWindow = new DeclarationViewWindow(parentForm);

      SetDeclarationViewLocation();
      _declarationViewWindow.Show();
      _declarationViewWindow.MouseMove += ControlMouseMove;
      control.Focus();
      CompletionListViewSelectedItemChanged(this, EventArgs.Empty);

      if (!String.IsNullOrEmpty(completionDataProvider.PreSelection))
      {
        // Select item based on pre-selection.
        CaretOffsetChanged(this, EventArgs.Empty);
      }
      else if (completionDataProvider.DefaultIndex >= 0)
      {
        // Select default item
        _completionListView.SelectItem(completionDataProvider.DefaultIndex);
      }

      _vScrollBar.ValueChanged += VScrollBarValueChanged;
      _document.DocumentAboutToBeChanged += DocumentAboutToBeChanged;
    }