Example #1
0
        /// <summary>
        /// Replace selection with insert text.
        /// </summary>
        /// <param name="insert">The text to be inserted.</param>
        /// <returns>The resulting value.</returns>
        public TextSlice Insert(string insert)
        {
            var text   = Head + insert + Tail;
            var result = new TextSlice(text, Start + insert.Length, 0, true);

            return(result);
        }
        /// <summary>
        /// Handle selection changed.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSelectionChanged(RoutedEventArgs e)
        {
            base.OnSelectionChanged(e);

            if (!_isUpdatingText)
            {
                TextSlice = new TextSlice(Text, SelectionStart, SelectionLength, CaretIndex != SelectionStart);
            }

            UpdateSelectionCondition();
        }
        /// <summary>
        /// Handle text changing.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTextChanged(TextChangedEventArgs e)
        {
            base.OnTextChanged(e);

            // The text as it is now.
            var now = Text;

            // Find the common prefix for what was and what is now.
            var head = 0;

            while (head < was.Length && head < now.Length && was[head] == now[head])
            {
                head++;
            }

            // Find the common suffix.
            var wasTail = was.Length;
            var nowTail = now.Length;

            while (head < wasTail && head < nowTail && was[wasTail - 1] == now[nowTail - 1])
            {
                wasTail--;
                nowTail--;
            }

            // Extract the deleted text.
            var nowChanged = now.Substring(head, nowTail - head);
            var wasChanged = was.Substring(head, wasTail - head);

            if (nowChanged == string.Empty && wasChanged.Length != 0 && char.IsUpper(wasChanged[0]))
            {
                // If we've inserted nothing and deleted something that starts with a capital, we're
                // can restore the shift key.
                ShiftToggleState.IsChecked = true;
            }
            else if (wasChanged == string.Empty && nowChanged == " " && head != 0 && now[head - 1].IsSentenceEnding())
            {
                // If we've inserted a space and deleted nothing, check whetehr a fullstop precedes
                // the space and if so press the shift key.
                ShiftToggleState.IsChecked = true;
            }

            // What is becomes what was.
            was = now;

            if (!_isUpdatingText)
            {
                TextSlice = new TextSlice(Text, SelectionStart, SelectionLength, SelectionStart != CaretIndex);
            }
        }