using System.IO; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using InABox.WPF; using Syncfusion.Windows.Controls.RichTextBoxAdv; 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(); 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 = ""; } 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 ms = new MemoryStream(Encoding.ASCII.GetBytes(content)); 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 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; } } }