Exemplo n.º 1
0
 private static void AssertFormatException(FormattingAction action)
 {
     try {
         action();
         Assert.Fail("Should have thrown an exception.");
     } catch (FormatException) {
         // success
     }
 }
Exemplo n.º 2
0
        internal FormatCommandGroup(string groupString)
        {
            //If this is a command, we need to split it and work out each command separately
            if (groupString[0] == '[')
            {
                //Cut the brackets off the ends
                groupString = groupString.Substring(1, groupString.Length - 2);

                //Split up the different commands
                var parts = groupString.Split(';');

                foreach (var p in parts)
                {
                    FormattingAction fa = null;

                    //Character skipping
                    if (p[0] == '>')
                    {
                        fa = new SkipCharacterFormatAction(p);
                    }
                    //Character range substring
                    else if (Regex.IsMatch(p, "^(_|[^-]+)-(_|.+)$"))
                    {
                        fa = new SubstringRangeFormatAction(p);
                    }

                    //Traditional substring (start, length optionally)
                    else if (Regex.IsMatch(p, "^(_|[^,]+)(,.+)?$"))
                    {
                        fa = new SubstringFormatAction(p);
                    }

                    _formattingActions.Add(fa);
                }
            }

            //If this is not a command block, it is a literal
            else
            {
                _formattingActions.Add(new StringLiteralAction(groupString));
            }
        }
Exemplo n.º 3
0
 private static void AssertFormatException(FormattingAction action)
 {
     try {
     action();
     Assert.Fail("Should have thrown an exception.");
       } catch (FormatException) {
     // success
       }
 }
Exemplo n.º 4
0
 internal void AddCommand(FormattingAction fa)
 {
     _formattingActions.Add(fa);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Sets proper formatting to text in the RichTexBox.
        /// </summary>
        /// <param name="input">Property used by dropdowns passing through selected value.</param>
        /// <param name="action">Type of formatting to be applied to the text.</param>
        /// <param name="isChecked">Property used by button actions indicating whether toggled button is checked or not.</param>
        private void OnTextFormattingChanged(string input, FormattingAction action, bool isChecked = false)
        {
            if (TextBox == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(input))
            {
                return;
            }

            var target = TextBox;

            // Check whether there is text selected or just sitting at cursor
            if (target.Selection.IsEmpty)
            {
                // Check to see if we are at the start of the textbox and nothing has been added yet
                if (target.Selection.Start.Paragraph == null)
                {
                    // Add a new paragraph object to the richtextbox with the fontsize
                    var p = new Paragraph();

                    switch (action)
                    {
                    case FormattingAction.ChangeFontSize:
                        p.FontSize = double.Parse(input);
                        break;

                    case FormattingAction.ChangeFontFamily:
                        p.FontFamily = new FontFamily(input);
                        break;

                    case FormattingAction.ChangeBold:
                        p.FontWeight = isChecked ? FontWeights.Bold : FontWeights.Normal;
                        break;

                    case FormattingAction.ChangeItalic:
                        p.FontStyle = isChecked ? FontStyles.Italic : FontStyles.Normal;
                        break;
                    }

                    target.Document.Blocks.Add(p);
                }
                else
                {
                    // Get current position of cursor
                    var curCaret = target.CaretPosition;
                    // Get the current block object that the cursor is in
                    var curBlock = target.Document.Blocks.FirstOrDefault(x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1);
                    if (curBlock != null)
                    {
                        var curParagraph = curBlock as Paragraph;
                        // Create a new run object with the fontsize, and add it to the current block
                        var newRun = new Run();

                        switch (action)
                        {
                        case FormattingAction.ChangeFontSize:
                            newRun.FontSize = double.Parse(input);
                            break;

                        case FormattingAction.ChangeFontFamily:
                            newRun.FontFamily = new FontFamily(input);
                            break;

                        case FormattingAction.ChangeBold:
                            newRun.FontWeight = isChecked ? FontWeights.Bold : FontWeights.Normal;
                            break;

                        case FormattingAction.ChangeItalic:
                            newRun.FontStyle = isChecked ? FontStyles.Italic : FontStyles.Normal;
                            break;
                        }

                        curParagraph?.Inlines.Add(newRun);
                        // Reset the cursor into the new block.
                        // If we don't do this, the font size will default again when you start typing.
                        target.CaretPosition = newRun.ElementStart;
                    }
                }
            }
            else // There is selected text, so change the fontsize of the selection
            {
                var selectionTextRange = new TextRange(target.Selection.Start, target.Selection.End);

                switch (action)
                {
                case FormattingAction.ChangeFontSize:
                    selectionTextRange.ApplyPropertyValue(TextElement.FontSizeProperty, double.Parse(input));
                    break;

                case FormattingAction.ChangeFontFamily:
                    selectionTextRange.ApplyPropertyValue(TextElement.FontFamilyProperty, new FontFamily(input));
                    break;

                case FormattingAction.ChangeBold:
                    selectionTextRange.ApplyPropertyValue(TextElement.FontWeightProperty,
                                                          isChecked ? FontWeights.Bold : FontWeights.Normal);
                    break;

                case FormattingAction.ChangeItalic:
                    selectionTextRange.ApplyPropertyValue(TextElement.FontStyleProperty,
                                                          isChecked ? FontStyles.Italic : FontStyles.Normal);
                    break;
                }
            }
            // Reset the focus onto the richtextbox after selecting the font in a toolbar etc
            target.Focus();
        }