using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Documents; using System.Windows.Media; using HTMLConverter; using InABox.WPF; using NPOI.OpenXmlFormats.Spreadsheet; using NPOI.OpenXmlFormats.Wordprocessing; using Syncfusion.Windows.Controls.RichTextBoxAdv; using Color = System.Windows.Media.Color; using Image = System.Windows.Controls.Image; using Inline = System.Windows.Documents.Inline; namespace InABox.DynamicGrid { public delegate void RichTextEditorChanged(object sender); /// /// Interaction logic for RichTextEditorControl.xaml /// public partial class RichTextEditor : UserControl { public static readonly DependencyProperty HideToolbarProperty = DependencyProperty.Register(nameof(HideToolbar), typeof(bool), typeof(RichTextEditor)); public static readonly DependencyProperty ZoomFactorProperty = DependencyProperty.Register(nameof(ZoomFactor), typeof(double), typeof(RichTextEditor)); public static readonly DependencyProperty ColorProperty = DependencyProperty.Register( "Color", typeof(Color), typeof(RichTextEditor), new PropertyMetadata(default(Color), OnBackgroundPropertyChanged)); private bool bdirty; private bool bLoaded; private bool bReady; private string curvalue = ""; public RichTextEditor() { InitializeComponent(); DataContext = new RichTextEditorViewModel(); ConfigureButton(BoldBtn, Properties.Resources.Bold16); ConfigureButton(ItalicBtn, Properties.Resources.Italic16); ConfigureButton(UnderlineBtn, Properties.Resources.Underline16); ConfigureButton(AlignLeftBtn, Properties.Resources.AlignTextLeft16); ConfigureButton(AlignCenterBtn, Properties.Resources.AlignTextCenter16); ConfigureButton(AlignRightBtn, Properties.Resources.AlignTextRight16); ConfigureButton(AlignJustifyBtn, Properties.Resources.AlignTextJustify16); ConfigureButton(HyperlinkBtn, Properties.Resources.Hyperlink16); /*Bold.SmallIcon = Properties.Resources.Bold16.AsBitmapImage(16, 16); Italic.SmallIcon = Properties.Resources.Italic16.AsBitmapImage(16, 16); Underline.SmallIcon = Properties.Resources.Underline16.AsBitmapImage(16, 16); AlignLeft.SmallIcon = Properties.Resources.AlignTextLeft16.AsBitmapImage(16, 16); AlignCentre.SmallIcon = Properties.Resources.AlignTextCenter16.AsBitmapImage(16, 16); AlignRight.SmallIcon = Properties.Resources.AlignTextRight16.AsBitmapImage(16, 16); AlignJustify.SmallIcon = Properties.Resources.AlignTextJustify16.AsBitmapImage(16, 16); Hyperlink.SmallIcon = Properties.Resources.Hyperlink16.AsBitmapImage(16, 16); Picture.SmallIcon = Properties.Resources.Picture16.AsBitmapImage(16, 16); Table.SmallIcon = Properties.Resources.Table16.AsBitmapImage(16, 16); ZoomIn.SmallIcon = Properties.Resources.zoomin.AsBitmapImage(16, 16); ZoomOut.SmallIcon = Properties.Resources.zoomout.AsBitmapImage(16, 16);*/ /*Editor.CaretBrush = new SolidColorBrush(Colors.Black); Editor.FontFamily = new FontFamily("Calibri"); Editor.FontSize = 12.0F; Editor.Foreground = new SolidColorBrush(Colors.Black);*/ ZoomFactor = 150; //Text = ""; } private void ConfigureButton(ButtonBase button, Bitmap image) { button.Content = new Image { Source = image.AsBitmapImage(16, 16), Width = 16, Height = 16 }; } public bool HideToolbar { get => (bool)GetValue(HideToolbarProperty); set { SetValue(HideToolbarProperty, value); Toolbar.Visibility = value ? Visibility.Collapsed : Visibility.Visible; } } public double ZoomFactor { get => (double)GetValue(ZoomFactorProperty); set { SetValue(ZoomFactorProperty, value); //Editor.ZoomFactor = value; } } public string Text { get => Save(); set => Load(value); } public RichTextEditorChanged OnChanged { get; set; } public Color Color { get => (Color)GetValue(ColorProperty); set { SetValue(ColorProperty, value); //Editor.Background = new SolidColorBrush(value); } } public void Load(string content) { if (content == null) content = ""; content = content.Replace("background:#000000", "").Replace("background:NoColor;", ""); var xaml = content; if (xaml.StartsWith("")) { xaml = HtmlToXamlConverter.ConvertHtmlToXaml(xaml, false); } var ms = new MemoryStream(Encoding.ASCII.GetBytes(xaml)); var range = new TextRange(RtbEditor.Document.ContentStart, RtbEditor.Document.ContentEnd); range.Load(ms, DataFormats.Text); //Editor.Load(ms, FormatType.Html); VerticalAlignment = VerticalAlignment.Top; VerticalAlignment = VerticalAlignment.Stretch; curvalue = content; bdirty = false; bLoaded = true; } public string Save() { if (bdirty) { var ms = new MemoryStream(); //Editor.Save(ms, FormatType.Html); var range = new TextRange(RtbEditor.Document.ContentStart, RtbEditor.Document.ContentEnd); range.Save(ms, DataFormats.Xaml); var reader = new StreamReader(ms); curvalue = Encoding.UTF8.GetString(ms.GetBuffer()); curvalue = curvalue.Replace("background:#000000", ""); bdirty = false; } return curvalue; } private void RichTextBoxAdv_ContentChanged(object obj, ContentChangedEventArgs args) { bdirty = true; } public void SetColor(Color color) { //Editor.Background = new SolidColorBrush(color); } private static void OnBackgroundPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var source = d as RichTextEditor; source.SetColor((Color)e.NewValue); } private void RichTextBoxAdv_LostFocus(object sender, RoutedEventArgs e) { if (bReady) OnChanged?.Invoke(this); bReady = true; } private void ZoomIn_Click(object sender, RoutedEventArgs e) { //Editor.ZoomFactor = Editor.ZoomFactor * 1.5F; } private void ZoomOut_Click(object sender, RoutedEventArgs e) { //Editor.ZoomFactor = Editor.ZoomFactor / 1.5F; } private void Editor_RequestNavigate(object obj, RequestNavigateEventArgs args) { var processStartInfo = new ProcessStartInfo(args.Hyperlink.NavigationLink); processStartInfo.UseShellExecute = true; Process.Start(processStartInfo); } private bool SelectionHasProperty(DependencyProperty property, object? value) { var temp = RtbEditor.Selection.GetPropertyValue(property); return temp != DependencyProperty.UnsetValue && temp.Equals(value); } private object? SelectionProperty(DependencyProperty property) { return RtbEditor.Selection.GetPropertyValue(property); } private void UpdateButtons() { BoldBtn.IsChecked = SelectionHasProperty(Inline.FontWeightProperty, FontWeights.Bold); ItalicBtn.IsChecked = SelectionHasProperty(Inline.FontStyleProperty, FontStyles.Italic); UnderlineBtn.IsChecked = SelectionHasProperty(Inline.TextDecorationsProperty, TextDecorations.Underline); AlignLeftBtn.IsChecked = SelectionHasProperty(FlowDocument.TextAlignmentProperty, TextAlignment.Left); AlignCenterBtn.IsChecked = SelectionHasProperty(FlowDocument.TextAlignmentProperty, TextAlignment.Center); AlignRightBtn.IsChecked = SelectionHasProperty(FlowDocument.TextAlignmentProperty, TextAlignment.Right); AlignJustifyBtn.IsChecked = SelectionHasProperty(FlowDocument.TextAlignmentProperty, TextAlignment.Justify); FontFamilyComboBox.SelectedItem = SelectionProperty(FlowDocument.FontFamilyProperty); FontSizeComboBox.Text = SelectionProperty(FlowDocument.FontSizeProperty)?.ToString(); } private void RtbEditor_SelectionChanged(object sender, RoutedEventArgs e) { UpdateButtons(); } private void fontFamilyCombo_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { if(FontFamilyComboBox.SelectedItem != null) { RtbEditor.Selection.ApplyPropertyValue(FlowDocument.FontFamilyProperty, FontFamilyComboBox.SelectedItem); } } private void fontSizeCombo_TextChanged(object sender, TextChangedEventArgs e) { var currentValue = SelectionProperty(FlowDocument.FontSizeProperty)?.ToString(); var fontSize = FontSizeComboBox.Text; if(double.TryParse(fontSize, out var size)) { RtbEditor.Selection.ApplyPropertyValue(FlowDocument.FontSizeProperty, size); } else { FontSizeComboBox.Text = currentValue; } } private void RtbEditor_TextChanged(object sender, TextChangedEventArgs e) { UpdateButtons(); } private void HyperlinkBtn_Click(object sender, RoutedEventArgs e) { var selection = RtbEditor.Selection.Text; } } }