private List <Tuple <DocumentPosition, string> > GetPreviousWords(DocumentPosition caretPosition, List <Tuple <DocumentPosition, string> > result = null)
        {
            if (result == null)
            {
                result = new List <Tuple <DocumentPosition, string> >();
            }

            DocumentPosition pos = new DocumentPosition(richTextBox.Document);

            pos.MoveToPosition(caretPosition);
            pos.MoveToPreviousWordStart();
            var text = pos.GetCurrentInlineBox().Text;

            if (text.Contains("_"))
            {
                pos.MoveToPreviousWordStart();
            }
            var previousOfMainCaret = new DocumentPosition(pos);

            if (previousOfMainCaret != caretPosition)
            {
                result.Add(new Tuple <DocumentPosition, string>(previousOfMainCaret, text));

                GetPreviousWords(previousOfMainCaret, result);
            }

            return(result);
            //////var word = GetText(previousOfMainCaret, caretPosition);// pos.Get
            //////return new Tuple<DocumentPosition, string>(previousOfMainCaret, word);
        }
        private void RepositionDialog(DocumentPosition targetPosition)
        {
            var targetLocation = new Point();
            var point          = this.richTextBox.ActiveEditorPresenter.GetViewPointFromDocumentPosition(targetPosition);

            targetLocation.X = point.X;
            targetLocation.Y = point.Y;

#if WPF
            if (System.Windows.Interop.BrowserInteropHelper.IsBrowserHosted)
            {
                targetLocation = this.richTextBox.TransformToVisual(Application.Current.MainWindow).Transform(targetLocation);
            }
            else
            {
                targetLocation = this.richTextBox.PointToScreen(targetLocation);
            }
#else
            targetLocation = this.richTextBox.TransformToVisual(Application.Current.RootVisual).Transform(targetLocation);
#endif

            Rect dialogRect = new Rect()
            {
                X      = this.Left,
                Y      = this.Top,
                Width  = this.ActualWidth,
                Height = this.ActualHeight
            };

            // TODO: smarter move
            if (dialogRect.Contains(targetLocation))
            {
                this.Top = targetLocation.Y - this.ActualHeight - 5;
                if (this.Top < 0)
                {
                    this.Top = targetLocation.Y + (targetPosition.GetCurrentInlineBox().ControlBoundingRectangle.Height *this.richTextBox.ScaleFactor.Height) + 5;
                }
            }
        }
        private void FlushBoxes(DocumentPosition startPosition, DocumentPosition endPosition)
        {
            if (startPosition == endPosition)
            {
                return;
            }
            RectangleF rect = new RectangleF();

            rect.X     = startPosition.Location.X;
            rect.Width = endPosition.Location.X - startPosition.Location.X;

            InlineLayoutBox startBox = startPosition.GetCurrentInlineBox();
            InlineLayoutBox endBox   = endPosition.GetCurrentInlineBox();

            float top    = startBox.ControlBoundingRectangle.Top;
            float bottom = startBox.ControlBoundingRectangle.Bottom;

            while (startBox != endBox)
            {
                startBox = DocumentStructureCollection.GetNextSiblingForDocumentElementOnSameLevel(startBox, startBox.AssociatedDocumentElement) as InlineLayoutBox;
                if (startBox == null)
                {
                    break;
                }
                if (startBox.ControlBoundingRectangle.Top < top)
                {
                    top = startBox.ControlBoundingRectangle.Top;
                }
                if (startBox.ControlBoundingRectangle.Bottom < bottom)
                {
                    bottom = startBox.ClippedControlBoundingRectangle.Bottom;
                }
            }
            rect.Y      = top;
            rect.Height = bottom - top;

            this.AddRectangle(rect);
        }
        private void FlushBoxes(DocumentPosition startPosition, DocumentPosition endPosition)
        {
            if (startPosition == endPosition)
            {
                return;
            }
            RectangleF rect = new RectangleF();

            rect.X = startPosition.Location.X;
            rect.Width = endPosition.Location.X - startPosition.Location.X;

            InlineLayoutBox startBox = startPosition.GetCurrentInlineBox();
            InlineLayoutBox endBox = endPosition.GetCurrentInlineBox();

            float top = startBox.ControlBoundingRectangle.Top;
            float bottom = startBox.ControlBoundingRectangle.Bottom;

            while (startBox != endBox)
            {
                startBox = DocumentStructureCollection.GetNextSiblingForDocumentElementOnSameLevel(startBox, startBox.AssociatedDocumentElement) as InlineLayoutBox;
                if (startBox == null)
                {
                    break;
                }
                if (startBox.ControlBoundingRectangle.Top < top)
                {
                    top = startBox.ControlBoundingRectangle.Top;
                }
                if (startBox.ControlBoundingRectangle.Bottom < bottom)
                {
                    bottom = startBox.ClippedControlBoundingRectangle.Bottom;
                }
            }
            rect.Y = top;
            rect.Height = bottom - top;

            this.AddRectangle(rect);
        }
        private void RepositionDialog(DocumentPosition targetPosition)
        {
            var targetLocation = new Point();
            var point = this.richTextBox.ActiveEditorPresenter.GetViewPointFromDocumentPosition(targetPosition);
            targetLocation.X = point.X;
            targetLocation.Y = point.Y;

            #if WPF
            if (System.Windows.Interop.BrowserInteropHelper.IsBrowserHosted)
            {
                targetLocation = this.richTextBox.TransformToVisual(Application.Current.MainWindow).Transform(targetLocation);
            }
            else
            {
                targetLocation = this.richTextBox.PointToScreen(targetLocation);
            }
            #else
            targetLocation = this.richTextBox.TransformToVisual(Application.Current.RootVisual).Transform(targetLocation);
            #endif

            Rect dialogRect = new Rect()
            {
                X = this.Left,
                Y = this.Top,
                Width = this.ActualWidth,
                Height = this.ActualHeight
            };

            // TODO: smarter move
            if (dialogRect.Contains(targetLocation))
            {
                this.Top = targetLocation.Y - this.ActualHeight - 5;
                if (this.Top < 0)
                {
                    this.Top = targetLocation.Y + (targetPosition.GetCurrentInlineBox().ControlBoundingRectangle.Height * this.richTextBox.ScaleFactor.Height) + 5;
                }
            }
        }