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; } }
protected override void OnClick() { base.OnClick(); MainWindow mainWindow = (MainWindow)Application.Current.MainWindow; if (this.Parent != StackPanelRecentColor) { ColorButton colorButton = new ColorButton(CustomRichTextBox, GridProp, 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; DropButton.IsOpen = false; if (GridProp == null) { 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; 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; return; } else if (textElement is ListItem) { CustomRichTextBox.IsCreateRunNull = true; CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition; 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; 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, Brush); } else { if (CustomRichTextBox.Run.Foreground is SolidColorBrush && Brush is SolidColorBrush) { SolidColorBrush runBrush = CustomRichTextBox.Run.Foreground as SolidColorBrush; SolidColorBrush newBrush = Brush as SolidColorBrush; if (runBrush.Color.A != newBrush.Color.A || runBrush.Color.B != newBrush.Color.B || runBrush.Color.G != newBrush.Color.G || runBrush.Color.R != newBrush.Color.R || runBrush.Color.ScA != newBrush.Color.ScA || runBrush.Color.ScB != newBrush.Color.ScB || runBrush.Color.ScG != newBrush.Color.ScG || runBrush.Color.ScR != newBrush.Color.ScR || runBrush.Opacity != newBrush.Opacity) { CustomRichTextBox.IsCreateRunColor = true; CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition; } else { CustomRichTextBox.IsCreateRunColor = false; } } else if (CustomRichTextBox.Run.Foreground is LinearGradientBrush && Brush is LinearGradientBrush) { LinearGradientBrush runBrush = CustomRichTextBox.Run.Foreground as LinearGradientBrush; LinearGradientBrush newBrush = Brush as LinearGradientBrush; int count = 0; if (runBrush.ColorInterpolationMode != newBrush.ColorInterpolationMode || runBrush.EndPoint != newBrush.EndPoint || runBrush.Opacity != newBrush.Opacity || runBrush.MappingMode != newBrush.MappingMode || runBrush.SpreadMethod != newBrush.SpreadMethod || runBrush.StartPoint != newBrush.StartPoint || runBrush.GradientStops.Count != newBrush.GradientStops.Count) { CustomRichTextBox.IsCreateRunColor = true; CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition; } else { GradientStop newGradientStop; foreach (GradientStop runGradientStop in runBrush.GradientStops) { newGradientStop = newBrush.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; return; } else { CustomRichTextBox.IsCreateRunColor = false; } count++; } } } else if (CustomRichTextBox.Run.Foreground is RadialGradientBrush && Brush is RadialGradientBrush) { RadialGradientBrush runBrush = CustomRichTextBox.Run.Foreground as RadialGradientBrush; RadialGradientBrush newBrush = Brush as RadialGradientBrush; int count = 0; if (runBrush.Center != newBrush.Center || runBrush.ColorInterpolationMode != newBrush.ColorInterpolationMode || runBrush.GradientOrigin != newBrush.GradientOrigin || runBrush.MappingMode != newBrush.MappingMode || runBrush.Opacity != newBrush.Opacity || runBrush.RadiusX != newBrush.RadiusX || runBrush.RadiusY != newBrush.RadiusY || runBrush.SpreadMethod != newBrush.SpreadMethod || runBrush.GradientStops.Count != newBrush.GradientStops.Count) { CustomRichTextBox.IsCreateRunColor = true; CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition; } else { GradientStop newGradientStop; foreach (GradientStop runGradientStop in runBrush.GradientStops) { newGradientStop = newBrush.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; return; } else { CustomRichTextBox.IsCreateRunColor = false; } count++; } } } else { CustomRichTextBox.IsCreateRunColor = true; CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition; } } } else { CustomRichTextBox.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brush); } } else { GridProp.NewBrush = Brush; } }
protected override void OnInitialized(EventArgs e) { base.OnInitialized(e); this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => { wrapPanelColor = GetTemplateChild("WrapPanelColor") as WrapPanel; stackPanelRecentColor = GetTemplateChild("StackPanelRecentColor") as StackPanel; advanceColor = GetTemplateChild("AdvanceColor") as Button; advanceColor.Click += advanceColor_Click; ColorButton white = new ColorButton(CustomRichTextBox, Grid, Brushes.White, stackPanelRecentColor, DropButton, RectangleColor); ColorButton black = new ColorButton(CustomRichTextBox, Grid, Brushes.Black, stackPanelRecentColor, DropButton, RectangleColor); ColorButton red = new ColorButton(CustomRichTextBox, Grid, Brushes.Red, stackPanelRecentColor, DropButton, RectangleColor); ColorButton green = new ColorButton(CustomRichTextBox, Grid, Brushes.Green, stackPanelRecentColor, DropButton, RectangleColor); ColorButton orange = new ColorButton(CustomRichTextBox, Grid, Brushes.Orange, stackPanelRecentColor, DropButton, RectangleColor); ColorButton yellow = new ColorButton(CustomRichTextBox, Grid, Brushes.Yellow, stackPanelRecentColor, DropButton, RectangleColor); ColorButton blue = new ColorButton(CustomRichTextBox, Grid, Brushes.Blue, stackPanelRecentColor, DropButton, RectangleColor); ColorButton pink = new ColorButton(CustomRichTextBox, Grid, Brushes.Pink, stackPanelRecentColor, DropButton, RectangleColor); ColorButton gray = new ColorButton(CustomRichTextBox, Grid, Brushes.Gray, stackPanelRecentColor, DropButton, RectangleColor); ColorButton gold = new ColorButton(CustomRichTextBox, Grid, Brushes.Gold, stackPanelRecentColor, DropButton, RectangleColor); ColorButton greenYellow = new ColorButton(CustomRichTextBox, Grid, Brushes.GreenYellow, stackPanelRecentColor, DropButton, RectangleColor); ColorButton purple = new ColorButton(CustomRichTextBox, Grid, Brushes.Purple, stackPanelRecentColor, DropButton, RectangleColor); ColorButton indigo = new ColorButton(CustomRichTextBox, Grid, Brushes.Indigo, stackPanelRecentColor, DropButton, RectangleColor); ColorButton navy = new ColorButton(CustomRichTextBox, Grid, Brushes.Navy, stackPanelRecentColor, DropButton, RectangleColor); ColorButton olive = new ColorButton(CustomRichTextBox, Grid, Brushes.Olive, stackPanelRecentColor, DropButton, RectangleColor); ColorButton tomato = new ColorButton(CustomRichTextBox, Grid, Brushes.Tomato, stackPanelRecentColor, DropButton, RectangleColor); ColorButton tan = new ColorButton(CustomRichTextBox, Grid, Brushes.Tan, stackPanelRecentColor, DropButton, RectangleColor); ColorButton orchid = new ColorButton(CustomRichTextBox, Grid, Brushes.Orchid, stackPanelRecentColor, DropButton, RectangleColor); wrapPanelColor.Children.Add(white); wrapPanelColor.Children.Add(black); wrapPanelColor.Children.Add(red); wrapPanelColor.Children.Add(green); wrapPanelColor.Children.Add(orange); wrapPanelColor.Children.Add(yellow); wrapPanelColor.Children.Add(blue); wrapPanelColor.Children.Add(pink); wrapPanelColor.Children.Add(gray); wrapPanelColor.Children.Add(gold); wrapPanelColor.Children.Add(greenYellow); wrapPanelColor.Children.Add(purple); wrapPanelColor.Children.Add(indigo); wrapPanelColor.Children.Add(navy); wrapPanelColor.Children.Add(olive); wrapPanelColor.Children.Add(tomato); wrapPanelColor.Children.Add(tan); wrapPanelColor.Children.Add(orchid); MainWindow mainWindow = (MainWindow)Application.Current.MainWindow; if (mainWindow.RecentColorCollection.Count > 0) { foreach (ColorButton colorButton in mainWindow.RecentColorCollection) { ColorButton newColorButton = new ColorButton(CustomRichTextBox, Grid, colorButton.Brush, stackPanelRecentColor, DropButton, RectangleColor); stackPanelRecentColor.Children.Add(newColorButton); } } })); }