Example #1
0
        private void FindAndHighlightText(string textToFind)
        {
            ClearAllHighlightedWords();

            ITextRange searchRange = editor.Document.GetRange(0, TextConstants.MaxUnitCount);

            searchRange.Move(0, 0);

            bool textFound = true;

            do
            {
                if (searchRange.FindText(textToFind, TextConstants.MaxUnitCount, FindOptions.None) < 1)
                {
                    textFound = false;
                }
                else
                {
                    ITextRange ss = searchRange.GetClone();
                    m_highlightedWords.Add(searchRange.GetClone());

                    ITextCharacterFormat charFormatting = searchRange.CharacterFormat;
                    charFormatting.BackgroundColor = Colors.Yellow;
                    searchRange.CharacterFormat    = charFormatting;
                }
            } while (textFound);
        }
Example #2
0
        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            string text = "";

            editor.Document.GetText(Windows.UI.Text.TextGetOptions.None, out text);
            ITextRange allText = editor.Document.GetRange(0, text.Length - 1);

            if (allText != null)
            {
                // Reset Character Formats
                ITextCharacterFormat charFormatting = allText.CharacterFormat;
                charFormatting.Name            = "Segoe UI";
                charFormatting.Size            = 15f;
                charFormatting.ForegroundColor = Colors.Black;
                charFormatting.Bold            = charFormatting.Bold = FormatEffect.Off;
                charFormatting.Italic          = charFormatting.Bold = FormatEffect.Off;
                charFormatting.Underline       = Windows.UI.Text.UnderlineType.None;
                allText.CharacterFormat        = charFormatting;

                // Reset Paragraph Formats and Style
                ITextParagraphFormat paraFormatting = allText.ParagraphFormat;
                paraFormatting.ListType = MarkerType.None;
                //paraFormatting.Style = ParagraphStyle.Undefined;
                allText.ParagraphFormat = paraFormatting;
            }
        }
Example #3
0
 protected bool IsEqual(ITextCharacterFormat format, ITextCharacterFormat document)
 {
     return(document.AllCaps == format.AllCaps &&
            document.BackgroundColor == format.BackgroundColor &&
            document.Bold == format.Bold &&
            document.FontStretch == format.FontStretch &&
            document.FontStyle == format.FontStyle &&
            document.ForegroundColor == format.ForegroundColor &&
            document.Hidden == format.Hidden &&
            document.Italic == format.Italic &&
            document.Kerning == format.Kerning &&
            //document.LanguageTag == format.LanguageTag &&
            document.LinkType == format.LinkType &&
            document.Name == format.Name &&
            document.Outline == format.Outline &&
            document.Position == format.Position &&
            document.ProtectedText == format.ProtectedText &&
            document.Size == format.Size &&
            document.SmallCaps == format.SmallCaps &&
            document.Spacing == format.Spacing &&
            document.Strikethrough == format.Strikethrough &&
            document.Subscript == format.Subscript &&
            document.Superscript == format.Superscript &&
            //document.TextScript == format.TextScript &&
            document.Underline == format.Underline &&
            document.Weight == format.Weight);
 }
Example #4
0
        public void UndoVisualChangeAnno(ITextSelection selectedTxt)
        {
            ITextCharacterFormat charFormatting = selectedTxt.CharacterFormat;

            charFormatting.Bold            = FormatEffect.Off;
            charFormatting.Underline       = UnderlineType.None;
            charFormatting.ForegroundColor = GetColor("");
            selectedTxt.CharacterFormat    = charFormatting;
        }
Example #5
0
        public void ApplyVisualChangeAnno(ITextSelection selectedTxt, string color)
        {
            ITextCharacterFormat charFormatting = selectedTxt.CharacterFormat;

            charFormatting.Bold            = FormatEffect.On;
            charFormatting.Underline       = UnderlineType.Single;
            charFormatting.ForegroundColor = GetColor(color);
            selectedTxt.CharacterFormat    = charFormatting;
        }
Example #6
0
 public void Update(ITextCharacterFormat format)
 {
     Bold.IsChecked          = format.Bold == FormatEffect.On;
     Italic.IsChecked        = format.Italic == FormatEffect.On;
     Strikethrough.IsChecked = format.Strikethrough == FormatEffect.On;
     Underline.IsChecked     = format.Underline == UnderlineType.Single;
     Monospace.IsChecked     = string.Equals(format.Name, "Consolas", StringComparison.OrdinalIgnoreCase);
     Spoiler.IsChecked       = format.BackgroundColor == Colors.Gray;
 }
Example #7
0
        private void ColorButton_Click(object sender, RoutedEventArgs e)
        {
            Button clickedColor = (Button)sender;

            ITextCharacterFormat charFormatting = editor.Document.Selection.CharacterFormat;

            switch (clickedColor.Name)
            {
            case "black":
            {
                charFormatting.ForegroundColor = Colors.Black;
                break;
            }

            case "gray":
            {
                charFormatting.ForegroundColor = Colors.Gray;
                break;
            }

            case "greenyellow":
            {
                charFormatting.ForegroundColor = Colors.GreenYellow;
                break;
            }

            case "green":
            {
                charFormatting.ForegroundColor = Colors.Green;
                break;
            }

            case "blue":
            {
                charFormatting.ForegroundColor = Colors.Blue;
                break;
            }

            case "red":
            {
                charFormatting.ForegroundColor = Colors.Red;
                break;
            }

            default:
            {
                charFormatting.ForegroundColor = Colors.Black;
                break;
            }
            }
            editor.Document.Selection.CharacterFormat = charFormatting;

            editor.Focus(Windows.UI.Xaml.FocusState.Keyboard);
            FontColorButton.Flyout.Hide();
        }
Example #8
0
        private void ItalicButton_Click(object sender, RoutedEventArgs e)
        {
            ITextSelection selectedText = editor.Document.Selection;

            if (selectedText != null)
            {
                ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
                charFormatting.Italic        = FormatEffect.Toggle;
                selectedText.CharacterFormat = charFormatting;
            }
        }
Example #9
0
        private void Underline_Click(object sender, RoutedEventArgs e)
        {
            ITextSelection selectedText = EventDescription.Document.Selection;

            if (selectedText != null)
            {
                ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
                selectedText.CharacterFormat.Underline = (charFormatting.Underline == UnderlineType.None) ? UnderlineType.Single : UnderlineType.None;
            }
            EventDescription.Focus(FocusState.Programmatic);
        }
        private void SetFontSize(int size)
        {
            ITextSelection selectedText = editor.Document.Selection;

            if (selectedText != null)
            {
                ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
                charFormatting.Size          = size;
                selectedText.CharacterFormat = charFormatting;
            }
        }
Example #11
0
        private void ItalicSelection(KeyRoutedEventArgs e)
        {
            ITextSelection selectedText = richEditBox.Document.Selection;

            if (selectedText != null)
            {
                ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
                charFormatting.Italic        = FormatEffect.Toggle;
                selectedText.CharacterFormat = charFormatting;
                e.Handled = true;
            }
        }
        private void add(object sender, RoutedEventArgs e)
        {
            ITextCharacterFormat format = editor.Document.GetDefaultCharacterFormat();

            format.Bold            = FormatEffect.Off;
            format.ForegroundColor = Colors.Black;
            editor.Document.SetDefaultCharacterFormat(format);
            editor.Document.SetText(TextSetOptions.ApplyRtfDocumentDefaults, "Second Line of Text \r\n");
            format.Bold            = FormatEffect.On;
            format.ForegroundColor = Colors.Red;
            editor.Document.Selection.CharacterFormat = format;
            editor.Document.Selection.SetText(TextSetOptions.Unhide, "First Line of Text \r\n");
        }
        private void BoldButton_Click(object sender, RoutedEventArgs e)
        {
            ITextSelection selectedText = Editor.Document.Selection;

            if (selectedText == null)
            {
                return;
            }

            ITextCharacterFormat charFormatting = selectedText.CharacterFormat;

            charFormatting.Bold          = FormatEffect.Toggle;
            selectedText.CharacterFormat = charFormatting;
        }
        /// <summary>
        /// Pad range with Zero-Width-Spaces.
        /// </summary>
        /// <param name="range">Range to pad.</param>
        /// <param name="format">Character format to apply to the padding.</param>
        private static void PadRange(ITextRange range, ITextCharacterFormat format)
        {
            var startPosition = range.StartPosition;
            var endPosition   = range.EndPosition + 1;
            var clone         = range.GetClone();

            clone.Collapse(true);
            clone.SetText(TextSetOptions.Unhide, "\u200B");
            clone.CharacterFormat.SetClone(format);
            clone.SetRange(endPosition, endPosition);
            clone.SetText(TextSetOptions.Unhide, "\u200B");
            clone.CharacterFormat.SetClone(format);
            range.SetRange(startPosition, endPosition + 1);
        }
Example #15
0
        private void ColorButtonClick(object sender, RoutedEventArgs e)
        {
            Button clickedColor = (Button)sender;

            ITextCharacterFormat charFormatting = editor.Document.Selection.CharacterFormat;

            switch (clickedColor.Name)
            {
            case "black":
            {
                charFormatting.ForegroundColor = Colors.Black;
                break;
            }

            case "gray":
            {
                charFormatting.ForegroundColor = Colors.Gray;
                break;
            }

            case "darkgreen":
            {
                charFormatting.ForegroundColor = Colors.DarkGreen;
                break;
            }

            case "green":
            {
                charFormatting.ForegroundColor = Colors.Green;
                break;
            }

            case "blue":
            {
                charFormatting.ForegroundColor = Colors.Blue;
                break;
            }

            default:
            {
                charFormatting.ForegroundColor = Colors.Black;
                break;
            }
            }
            editor.Document.Selection.CharacterFormat = charFormatting;

            editor.Focus(Windows.UI.Xaml.FocusState.Keyboard);
            fontColorPopup.IsOpen = false;
        }
Example #16
0
        // 使选中的文字加粗
        private void btnBold_Click(object sender, RoutedEventArgs e)
        {
            // 获取选中的文本
            ITextSelection selectedText = txtEditor.Document.Selection;

            if (selectedText != null)
            {
                // 实体化一个 ITextCharacterFormat,指定字符格式为加粗
                ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
                charFormatting.Bold = FormatEffect.Toggle;

                // 设置选中文本的字符格式
                selectedText.CharacterFormat = charFormatting;
            }
        }
        private void UnderlineButton_Click(object sender, RoutedEventArgs e)
        {
            ITextSelection selectedText = editor.Document.Selection;

            if (selectedText != null)
            {
                ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
                if (charFormatting.Underline == Windows.UI.Text.UnderlineType.None)
                {
                    charFormatting.Underline = Windows.UI.Text.UnderlineType.Single;
                }
                else
                {
                    charFormatting.Underline = Windows.UI.Text.UnderlineType.None;
                }
                selectedText.CharacterFormat = charFormatting;
            }
        }
Example #18
0
        private void UnderlineSelection(KeyRoutedEventArgs e)
        {
            ITextSelection selectedText = richEditBox.Document.Selection;

            if (selectedText != null)
            {
                ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
                if (charFormatting.Underline == UnderlineType.None)
                {
                    charFormatting.Underline = UnderlineType.Single;
                }
                else
                {
                    charFormatting.Underline = UnderlineType.None;
                }
                selectedText.CharacterFormat = charFormatting;
                e.Handled = true;
            }
        }
        void OnTopAppBarOpened(object sender, object args)
        {
            // Get the character formatting at the current selection
            ITextCharacterFormat charFormat = richEditBox.Document.Selection.CharacterFormat;

            // Set the CheckBox app bar buttons
            boldAppBarCheckBox.IsChecked      = charFormat.Bold == FormatEffect.On;
            italicAppBarCheckBox.IsChecked    = charFormat.Italic == FormatEffect.On;
            underlineAppBarCheckBox.IsChecked = charFormat.Underline == UnderlineType.Single;

            // Set the two ComboBox's
            fontSizeComboBox.SelectedItem   = (int)charFormat.Size;
            fontFamilyComboBox.SelectedItem = charFormat.Name;

            // Get the paragraph alignment and set the RadioButton's
            ParagraphAlignment paragraphAlign = richEditBox.Document.Selection.ParagraphFormat.Alignment;

            alignLeftAppBarRadioButton.IsChecked   = paragraphAlign == ParagraphAlignment.Left;
            alignCenterAppBarRadioButton.IsChecked = paragraphAlign == ParagraphAlignment.Center;
            alignRightAppBarRadioButton.IsChecked  = paragraphAlign == ParagraphAlignment.Right;
        }
Example #20
0
        // 高亮显示用户搜索的字符
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            // 清除高亮字符的高亮效果
            ITextCharacterFormat charFormat;

            for (int i = 0; i < _highlightedWords.Count; i++)
            {
                charFormat = _highlightedWords[i].CharacterFormat;
                charFormat.BackgroundColor           = Colors.Transparent;
                _highlightedWords[i].CharacterFormat = charFormat;
            }
            _highlightedWords.Clear();

            // 获取全部文本,并将操作点移动到文本的起点
            ITextRange searchRange = txtEditor.Document.GetRange(0, TextConstants.MaxUnitCount);

            searchRange.Move(0, 0);

            bool textFound = true;

            do
            {
                // 在全部文本中搜索指定的字符串
                if (searchRange.FindText(txtSearch.Text, TextConstants.MaxUnitCount, FindOptions.None) < 1)
                {
                    textFound = false;
                }
                else
                {
                    _highlightedWords.Add(searchRange.GetClone());

                    // 实体化一个 ITextCharacterFormat,指定字符背景颜色为黄色
                    ITextCharacterFormat charFormatting = searchRange.CharacterFormat;
                    charFormatting.BackgroundColor = Colors.Orange;

                    // 设置指定文本的字符格式(高亮效果)
                    searchRange.CharacterFormat = charFormatting;
                }
            } while (textFound);
        }
Example #21
0
 protected bool IsDefault(ITextCharacterFormat format)
 {
     return(IsEqual(format, Document.GetDefaultCharacterFormat()));
 }
Example #22
0
 /// <summary>
 /// Stub default formatting
 /// </summary>
 /// <param name="format">Not used</param>
 protected override void DefaultFormatting(ITextCharacterFormat format)
 {
     // Do nothing - base implementation messes up TextCharacterFormatMock properties
 }
Example #23
0
 /// <summary>
 /// Performs default formatting (non-highlighted text)
 /// </summary>
 /// <param name="format">Format of selection</param>
 protected abstract void DefaultFormatting(ITextCharacterFormat format);
Example #24
0
 /// <summary>
 /// Performs default formatting (non-highlighted text)
 /// </summary>
 /// <param name="format">Format of selection</param>
 protected override void DefaultFormatting(ITextCharacterFormat format)
 {
     format.Bold = FormatEffect.Off;
     format.Italic = FormatEffect.Off;
 }
Example #25
0
        private bool TryCommitSuggestionIntoDocument(ITextRange range, string displayText, Guid id, ITextCharacterFormat format, bool addTrailingSpace = true)
        {
            // We don't want to set text when the display text doesn't change since it may lead to unexpected caret move.
            range.GetText(TextGetOptions.NoHidden, out var existingText);
            if (existingText != displayText)
            {
                range.SetText(TextSetOptions.Unhide, displayText);
            }

            var formatBefore = range.CharacterFormat.GetClone();

            range.CharacterFormat.SetClone(format);
            PadRange(range, formatBefore);
            range.Link = $"\"{id}\"";

            // In some rare case, setting Link can fail. Only observed when interacting with Undo/Redo feature.
            if (range.Link != $"\"{id}\"")
            {
                range.Delete(TextRangeUnit.Story, -1);
                return(false);
            }

            if (addTrailingSpace)
            {
                var clone = range.GetClone();
                clone.Collapse(false);
                clone.SetText(TextSetOptions.Unhide, " ");
                clone.Collapse(false);
                TextDocument.Selection.SetRange(clone.EndPosition, clone.EndPosition);
            }

            return(true);
        }
Example #26
0
 public bool IsEqual(ITextCharacterFormat format)
 {
     throw new NotImplementedException();
 }
Example #27
0
 public void SetClone(ITextCharacterFormat value)
 {
     throw new NotImplementedException();
 }