void advanceColor_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;

            DialogWindowBrushColor dialoWindow = new DialogWindowBrushColor();

            dialoWindow.Owner = Application.Current.MainWindow;

            Lovatts.ColorEditor.BrushEditor brushEditor = (Lovatts.ColorEditor.BrushEditor)dialoWindow.Content;
            brushEditor.ParentWindow = dialoWindow;

            if (dialoWindow.ShowDialog() == true)
            {
                Brush = brushEditor.BrushEditorViewModel.Brush;

                Grid.NewBrush = Brush;

                MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;

                ColorButton colorButton = new ColorButton(CustomRichTextBox, Grid, Brush, stackPanelRecentColor, DropButton, RectangleColor);

                if (mainWindow.RecentColorCollection.Count < 7)
                {
                    stackPanelRecentColor.Children.Add(colorButton);

                    mainWindow.RecentColorCollection.Add(colorButton);
                }
                else
                {
                    mainWindow.RecentColorCollection.RemoveAt(0);
                    mainWindow.RecentColorCollection.Add(colorButton);

                    stackPanelRecentColor.Children.RemoveAt(0);
                    stackPanelRecentColor.Children.Add(colorButton);
                }

                RectangleColor.Fill = Brush;
            }
        }
Example #2
0
        void advanceColor_Click(object sender, RoutedEventArgs e)
        {
            DialogWindowBrushColor dialoWindow = new DialogWindowBrushColor();

            dialoWindow.Owner = Application.Current.MainWindow;

            Lovatts.ColorEditor.BrushEditor brushEditor = (Lovatts.ColorEditor.BrushEditor)dialoWindow.Content;
            brushEditor.ParentWindow = dialoWindow;

            if (dialoWindow.ShowDialog() == true)
            {
                Brush newBrush = brushEditor.BrushEditorViewModel.Brush;

                MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;

                ColorButton colorButton = new ColorButton(CustomRichTextBox, Grid, newBrush, stackPanelRecentColor, DropButton, RectangleColor);

                if (mainWindow.RecentColorCollection.Count < 7)
                {
                    stackPanelRecentColor.Children.Add(colorButton);

                    mainWindow.RecentColorCollection.Add(colorButton);
                }
                else
                {
                    mainWindow.RecentColorCollection.RemoveAt(0);
                    mainWindow.RecentColorCollection.Add(colorButton);

                    stackPanelRecentColor.Children.RemoveAt(0);
                    stackPanelRecentColor.Children.Add(colorButton);
                }

                RectangleColor.Fill = newBrush;

                TextPointer textPoint = CustomRichTextBox.CaretPosition;
                CustomRichTextBox.startTextPointer = null;
                CustomRichTextBox.endTextPointer   = null;

                if (CustomRichTextBox.Selection.IsEmpty)
                {
                    TextElement textElement = null;

                    if (CustomRichTextBox.CaretPosition.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.None)
                    {
                        CustomRichTextBox.IsCreateRunNull = true;

                        CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;

                        e.Handled = true;

                        return;
                    }
                    else if (CustomRichTextBox.CaretPosition.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart)
                    {
                        textElement = (TextElement)CustomRichTextBox.CaretPosition.GetAdjacentElement(LogicalDirection.Backward);

                        if (textElement is Paragraph)
                        {
                            CustomRichTextBox.IsCreateRunNull = true;

                            CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;

                            e.Handled = true;

                            return;
                        }
                        else if (textElement is ListItem)
                        {
                            CustomRichTextBox.IsCreateRunNull = true;

                            CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;

                            e.Handled = true;

                            return;
                        }
                    }
                    else if (CustomRichTextBox.CaretPosition.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementEnd)
                    {
                        textElement = (TextElement)CustomRichTextBox.CaretPosition.GetAdjacentElement(LogicalDirection.Backward);

                        if (textElement is LineBreak)
                        {
                            CustomRichTextBox.IsCreateRunNull = true;

                            CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;

                            e.Handled = true;

                            return;
                        }
                    }

                    if (textPoint.GetTextRunLength(LogicalDirection.Backward) > 0) // Если позади каретки в текстовом элементе есть текст
                    {
                        TextPointer textPointer = textPoint.GetNextContextPosition(LogicalDirection.Backward);

                        Run run = (Run)textPointer.GetAdjacentElement(LogicalDirection.Backward);

                        TextElement nextBackTextElement = (TextElement)run.ElementStart.GetAdjacentElement(LogicalDirection.Backward);

                        int i = CustomRichTextBox.CaretPosition.GetTextRunLength(LogicalDirection.Backward);

                        TextRange textRange = new TextRange(textPointer, CustomRichTextBox.CaretPosition);

                        int index = textRange.Text.LastIndexOfAny(new char[] { ' ', ',', '.', '?', '!', ':', ';' });

                        if (index != -1)
                        {
                            if (textRange.Text[i - 1] != ' ' && textRange.Text[i - 1] != ',' && textRange.Text[i - 1] != '.' && textRange.Text[i - 1] != '?' && textRange.Text[i - 1] != '!' && textRange.Text[i - 1] != ':' && textRange.Text[i - 1] != ';')
                            {
                                CustomRichTextBox.startTextPointer = CustomRichTextBox.CaretPosition.GetPositionAtOffset(index + 1 - i);
                            }
                            else
                            {
                                CustomRichTextBox.startTextPointer = null;
                            }
                        }
                        else
                        {
                            CustomRichTextBox.IsText = true;

                            CustomRichTextBox.StartTextPointer(nextBackTextElement);
                        }
                    }
                    else
                    {
                        CustomRichTextBox.IsTextNull = true;

                        TextElement nextBackTextElement = (TextElement)CustomRichTextBox.CaretPosition.GetAdjacentElement(LogicalDirection.Backward);

                        if (nextBackTextElement is Run)
                        {
                            nextBackTextElement = (TextElement)nextBackTextElement.ElementStart.GetAdjacentElement(LogicalDirection.Backward);
                        }

                        CustomRichTextBox.StartTextPointer(nextBackTextElement);
                    }

                    CustomRichTextBox.IsTextNull = false;

                    CustomRichTextBox.IsText = false;

                    if (textPoint.GetTextRunLength(LogicalDirection.Forward) > 0) // Если в текстовом элементе впереди каретки есть текст
                    {
                        TextPointer textPointer = textPoint.GetNextContextPosition(LogicalDirection.Forward);

                        Run run = (Run)textPointer.GetAdjacentElement(LogicalDirection.Forward);

                        TextElement nextForwardTextElement = (TextElement)run.ElementEnd.GetAdjacentElement(LogicalDirection.Forward);

                        int i = CustomRichTextBox.CaretPosition.GetTextRunLength(LogicalDirection.Forward);

                        TextRange textRange = new TextRange(CustomRichTextBox.CaretPosition, textPointer);

                        int index = textRange.Text.IndexOfAny(new char[] { ' ', ',', '.', '?', '!', ':', ';' });

                        if (index != -1)
                        {
                            if (textRange.Text[0] != ' ' && textRange.Text[0] != ',' && textRange.Text[0] != '.' && textRange.Text[0] != '?' && textRange.Text[0] != '!' && textRange.Text[0] != ':' && textRange.Text[0] != ';')
                            {
                                CustomRichTextBox.endTextPointer = CustomRichTextBox.CaretPosition.GetPositionAtOffset(index);
                            }
                            else
                            {
                                CustomRichTextBox.endTextPointer = null;
                            }
                        }
                        else
                        {
                            CustomRichTextBox.IsText = true;

                            CustomRichTextBox.EndTextPointer(nextForwardTextElement);
                        }
                    }
                    else
                    {
                        CustomRichTextBox.IsTextNull = true;

                        TextElement nextForwardTextElement = (TextElement)CustomRichTextBox.CaretPosition.GetAdjacentElement(LogicalDirection.Forward);

                        if (nextForwardTextElement is Run)
                        {
                            nextForwardTextElement = (TextElement)nextForwardTextElement.ElementEnd.GetAdjacentElement(LogicalDirection.Forward);
                        }

                        CustomRichTextBox.EndTextPointer(nextForwardTextElement);
                    }

                    CustomRichTextBox.IsText = false;

                    CustomRichTextBox.IsTextNull = false;

                    if (CustomRichTextBox.startTextPointer != null && CustomRichTextBox.endTextPointer != null)
                    {
                        TextRange textRange = new TextRange(CustomRichTextBox.startTextPointer, CustomRichTextBox.endTextPointer);

                        textRange.ApplyPropertyValue(TextElement.ForegroundProperty, newBrush);
                    }
                    else
                    {
                        if (CustomRichTextBox.Run.Foreground is SolidColorBrush && newBrush is SolidColorBrush)
                        {
                            SolidColorBrush runBrush           = CustomRichTextBox.Run.Foreground as SolidColorBrush;
                            SolidColorBrush newSolidColorBrush = newBrush as SolidColorBrush;

                            if (runBrush.Color.A != newSolidColorBrush.Color.A || runBrush.Color.B != newSolidColorBrush.Color.B || runBrush.Color.G != newSolidColorBrush.Color.G || runBrush.Color.R != newSolidColorBrush.Color.R || runBrush.Color.ScA != newSolidColorBrush.Color.ScA || runBrush.Color.ScB != newSolidColorBrush.Color.ScB || runBrush.Color.ScG != newSolidColorBrush.Color.ScG || runBrush.Color.ScR != newSolidColorBrush.Color.ScR || runBrush.Opacity != newSolidColorBrush.Opacity)
                            {
                                CustomRichTextBox.IsCreateRunColor = true;

                                CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;
                            }
                            else
                            {
                                CustomRichTextBox.IsCreateRunColor = false;
                            }
                        }
                        else if (CustomRichTextBox.Run.Foreground is LinearGradientBrush && newBrush is LinearGradientBrush)
                        {
                            LinearGradientBrush runBrush = CustomRichTextBox.Run.Foreground as LinearGradientBrush;
                            LinearGradientBrush newLinearGradientBrush = newBrush as LinearGradientBrush;

                            int count = 0;

                            if (runBrush.ColorInterpolationMode != newLinearGradientBrush.ColorInterpolationMode || runBrush.EndPoint != newLinearGradientBrush.EndPoint || runBrush.Opacity != newLinearGradientBrush.Opacity || runBrush.MappingMode != newLinearGradientBrush.MappingMode || runBrush.SpreadMethod != newLinearGradientBrush.SpreadMethod || runBrush.StartPoint != newLinearGradientBrush.StartPoint || runBrush.GradientStops.Count != newLinearGradientBrush.GradientStops.Count)
                            {
                                CustomRichTextBox.IsCreateRunColor = true;

                                CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;
                            }
                            else
                            {
                                GradientStop newGradientStop;

                                foreach (GradientStop runGradientStop in runBrush.GradientStops)
                                {
                                    newGradientStop = newLinearGradientBrush.GradientStops[count];

                                    if (runGradientStop.Color.A != newGradientStop.Color.A || runGradientStop.Color.B != newGradientStop.Color.B || runGradientStop.Color.G != newGradientStop.Color.G || runGradientStop.Color.R != newGradientStop.Color.R || runGradientStop.Color.ScA != newGradientStop.Color.ScA || runGradientStop.Color.ScB != newGradientStop.Color.ScB || runGradientStop.Color.ScG != newGradientStop.Color.ScG || runGradientStop.Color.ScR != newGradientStop.Color.ScR)
                                    {
                                        CustomRichTextBox.IsCreateRunColor = true;

                                        CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;

                                        e.Handled = true;

                                        return;
                                    }
                                    else
                                    {
                                        CustomRichTextBox.IsCreateRunColor = false;
                                    }

                                    count++;
                                }
                            }
                        }
                        else if (CustomRichTextBox.Run.Foreground is RadialGradientBrush && newBrush is RadialGradientBrush)
                        {
                            RadialGradientBrush runBrush = CustomRichTextBox.Run.Foreground as RadialGradientBrush;
                            RadialGradientBrush newRadialGradientBrush = newBrush as RadialGradientBrush;

                            int count = 0;

                            if (runBrush.Center != newRadialGradientBrush.Center || runBrush.ColorInterpolationMode != newRadialGradientBrush.ColorInterpolationMode || runBrush.GradientOrigin != newRadialGradientBrush.GradientOrigin || runBrush.MappingMode != newRadialGradientBrush.MappingMode || runBrush.Opacity != newRadialGradientBrush.Opacity || runBrush.RadiusX != newRadialGradientBrush.RadiusX || runBrush.RadiusY != newRadialGradientBrush.RadiusY || runBrush.SpreadMethod != newRadialGradientBrush.SpreadMethod || runBrush.GradientStops.Count != newRadialGradientBrush.GradientStops.Count)
                            {
                                CustomRichTextBox.IsCreateRunColor = true;

                                CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;
                            }
                            else
                            {
                                GradientStop newGradientStop;

                                foreach (GradientStop runGradientStop in runBrush.GradientStops)
                                {
                                    newGradientStop = newRadialGradientBrush.GradientStops[count];

                                    if (runGradientStop.Color.A != newGradientStop.Color.A || runGradientStop.Color.B != newGradientStop.Color.B || runGradientStop.Color.G != newGradientStop.Color.G || runGradientStop.Color.R != newGradientStop.Color.R || runGradientStop.Color.ScA != newGradientStop.Color.ScA || runGradientStop.Color.ScB != newGradientStop.Color.ScB || runGradientStop.Color.ScG != newGradientStop.Color.ScG || runGradientStop.Color.ScR != newGradientStop.Color.ScR)
                                    {
                                        CustomRichTextBox.IsCreateRunColor = true;

                                        CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;

                                        e.Handled = true;

                                        return;
                                    }
                                    else
                                    {
                                        CustomRichTextBox.IsCreateRunColor = false;
                                    }

                                    count++;
                                }
                            }
                        }
                        else
                        {
                            CustomRichTextBox.IsCreateRunColor = true;

                            CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;
                        }
                    }
                }
                else
                {
                    CustomRichTextBox.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, newBrush);
                }
            }

            e.Handled = true;
        }