RichTextEditor.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System.Diagnostics;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Controls.Primitives;
  9. using System.Windows.Documents;
  10. using System.Windows.Media;
  11. using HTMLConverter;
  12. using InABox.WPF;
  13. using NPOI.OpenXmlFormats.Spreadsheet;
  14. using NPOI.OpenXmlFormats.Wordprocessing;
  15. using Syncfusion.Windows.Controls.RichTextBoxAdv;
  16. using Color = System.Windows.Media.Color;
  17. using Image = System.Windows.Controls.Image;
  18. using Inline = System.Windows.Documents.Inline;
  19. namespace InABox.DynamicGrid
  20. {
  21. public delegate void RichTextEditorChanged(object sender);
  22. /// <summary>
  23. /// Interaction logic for RichTextEditorControl.xaml
  24. /// </summary>
  25. public partial class RichTextEditor : UserControl
  26. {
  27. public static readonly DependencyProperty HideToolbarProperty =
  28. DependencyProperty.Register(nameof(HideToolbar), typeof(bool), typeof(RichTextEditor));
  29. public static readonly DependencyProperty ZoomFactorProperty =
  30. DependencyProperty.Register(nameof(ZoomFactor), typeof(double), typeof(RichTextEditor));
  31. public static readonly DependencyProperty ColorProperty =
  32. DependencyProperty.Register(
  33. "Color",
  34. typeof(Color),
  35. typeof(RichTextEditor),
  36. new PropertyMetadata(default(Color), OnBackgroundPropertyChanged));
  37. private bool bdirty;
  38. private bool bLoaded;
  39. private bool bReady;
  40. private string curvalue = "";
  41. public RichTextEditor()
  42. {
  43. InitializeComponent();
  44. DataContext = new RichTextEditorViewModel();
  45. ConfigureButton(BoldBtn, Properties.Resources.Bold16);
  46. ConfigureButton(ItalicBtn, Properties.Resources.Italic16);
  47. ConfigureButton(UnderlineBtn, Properties.Resources.Underline16);
  48. ConfigureButton(AlignLeftBtn, Properties.Resources.AlignTextLeft16);
  49. ConfigureButton(AlignCenterBtn, Properties.Resources.AlignTextCenter16);
  50. ConfigureButton(AlignRightBtn, Properties.Resources.AlignTextRight16);
  51. ConfigureButton(AlignJustifyBtn, Properties.Resources.AlignTextJustify16);
  52. ConfigureButton(HyperlinkBtn, Properties.Resources.Hyperlink16);
  53. /*Bold.SmallIcon = Properties.Resources.Bold16.AsBitmapImage(16, 16);
  54. Italic.SmallIcon = Properties.Resources.Italic16.AsBitmapImage(16, 16);
  55. Underline.SmallIcon = Properties.Resources.Underline16.AsBitmapImage(16, 16);
  56. AlignLeft.SmallIcon = Properties.Resources.AlignTextLeft16.AsBitmapImage(16, 16);
  57. AlignCentre.SmallIcon = Properties.Resources.AlignTextCenter16.AsBitmapImage(16, 16);
  58. AlignRight.SmallIcon = Properties.Resources.AlignTextRight16.AsBitmapImage(16, 16);
  59. AlignJustify.SmallIcon = Properties.Resources.AlignTextJustify16.AsBitmapImage(16, 16);
  60. Hyperlink.SmallIcon = Properties.Resources.Hyperlink16.AsBitmapImage(16, 16);
  61. Picture.SmallIcon = Properties.Resources.Picture16.AsBitmapImage(16, 16);
  62. Table.SmallIcon = Properties.Resources.Table16.AsBitmapImage(16, 16);
  63. ZoomIn.SmallIcon = Properties.Resources.zoomin.AsBitmapImage(16, 16);
  64. ZoomOut.SmallIcon = Properties.Resources.zoomout.AsBitmapImage(16, 16);*/
  65. /*Editor.CaretBrush = new SolidColorBrush(Colors.Black);
  66. Editor.FontFamily = new FontFamily("Calibri");
  67. Editor.FontSize = 12.0F;
  68. Editor.Foreground = new SolidColorBrush(Colors.Black);*/
  69. ZoomFactor = 150;
  70. //Text = "";
  71. }
  72. private void ConfigureButton(ButtonBase button, Bitmap image)
  73. {
  74. button.Content = new Image { Source = image.AsBitmapImage(16, 16), Width = 16, Height = 16 };
  75. }
  76. public bool HideToolbar
  77. {
  78. get => (bool)GetValue(HideToolbarProperty);
  79. set
  80. {
  81. SetValue(HideToolbarProperty, value);
  82. Toolbar.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
  83. }
  84. }
  85. public double ZoomFactor
  86. {
  87. get => (double)GetValue(ZoomFactorProperty);
  88. set
  89. {
  90. SetValue(ZoomFactorProperty, value);
  91. //Editor.ZoomFactor = value;
  92. }
  93. }
  94. public string Text
  95. {
  96. get => Save();
  97. set => Load(value);
  98. }
  99. public RichTextEditorChanged OnChanged { get; set; }
  100. public Color Color
  101. {
  102. get => (Color)GetValue(ColorProperty);
  103. set
  104. {
  105. SetValue(ColorProperty, value);
  106. //Editor.Background = new SolidColorBrush(value);
  107. }
  108. }
  109. public void Load(string content)
  110. {
  111. if (content == null)
  112. content = "";
  113. content = content.Replace("background:#000000", "").Replace("background:NoColor;", "");
  114. var xaml = content;
  115. if (xaml.StartsWith("<html>"))
  116. {
  117. xaml = HtmlToXamlConverter.ConvertHtmlToXaml(xaml, false);
  118. }
  119. var ms = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
  120. var range = new TextRange(RtbEditor.Document.ContentStart, RtbEditor.Document.ContentEnd);
  121. range.Load(ms, DataFormats.Text);
  122. //Editor.Load(ms, FormatType.Html);
  123. VerticalAlignment = VerticalAlignment.Top;
  124. VerticalAlignment = VerticalAlignment.Stretch;
  125. curvalue = content;
  126. bdirty = false;
  127. bLoaded = true;
  128. }
  129. public string Save()
  130. {
  131. if (bdirty)
  132. {
  133. var ms = new MemoryStream();
  134. //Editor.Save(ms, FormatType.Html);
  135. var range = new TextRange(RtbEditor.Document.ContentStart, RtbEditor.Document.ContentEnd);
  136. range.Save(ms, DataFormats.Xaml);
  137. var reader = new StreamReader(ms);
  138. curvalue = Encoding.UTF8.GetString(ms.GetBuffer());
  139. curvalue = curvalue.Replace("background:#000000", "");
  140. bdirty = false;
  141. }
  142. return curvalue;
  143. }
  144. private void RichTextBoxAdv_ContentChanged(object obj, ContentChangedEventArgs args)
  145. {
  146. bdirty = true;
  147. }
  148. public void SetColor(Color color)
  149. {
  150. //Editor.Background = new SolidColorBrush(color);
  151. }
  152. private static void OnBackgroundPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  153. {
  154. var source = d as RichTextEditor;
  155. source.SetColor((Color)e.NewValue);
  156. }
  157. private void RichTextBoxAdv_LostFocus(object sender, RoutedEventArgs e)
  158. {
  159. if (bReady)
  160. OnChanged?.Invoke(this);
  161. bReady = true;
  162. }
  163. private void ZoomIn_Click(object sender, RoutedEventArgs e)
  164. {
  165. //Editor.ZoomFactor = Editor.ZoomFactor * 1.5F;
  166. }
  167. private void ZoomOut_Click(object sender, RoutedEventArgs e)
  168. {
  169. //Editor.ZoomFactor = Editor.ZoomFactor / 1.5F;
  170. }
  171. private void Editor_RequestNavigate(object obj, RequestNavigateEventArgs args)
  172. {
  173. var processStartInfo = new ProcessStartInfo(args.Hyperlink.NavigationLink);
  174. processStartInfo.UseShellExecute = true;
  175. Process.Start(processStartInfo);
  176. }
  177. private bool SelectionHasProperty(DependencyProperty property, object? value)
  178. {
  179. var temp = RtbEditor.Selection.GetPropertyValue(property);
  180. return temp != DependencyProperty.UnsetValue && temp.Equals(value);
  181. }
  182. private object? SelectionProperty(DependencyProperty property)
  183. {
  184. return RtbEditor.Selection.GetPropertyValue(property);
  185. }
  186. private void UpdateButtons()
  187. {
  188. BoldBtn.IsChecked = SelectionHasProperty(Inline.FontWeightProperty, FontWeights.Bold);
  189. ItalicBtn.IsChecked = SelectionHasProperty(Inline.FontStyleProperty, FontStyles.Italic);
  190. UnderlineBtn.IsChecked = SelectionHasProperty(Inline.TextDecorationsProperty, TextDecorations.Underline);
  191. AlignLeftBtn.IsChecked = SelectionHasProperty(FlowDocument.TextAlignmentProperty, TextAlignment.Left);
  192. AlignCenterBtn.IsChecked = SelectionHasProperty(FlowDocument.TextAlignmentProperty, TextAlignment.Center);
  193. AlignRightBtn.IsChecked = SelectionHasProperty(FlowDocument.TextAlignmentProperty, TextAlignment.Right);
  194. AlignJustifyBtn.IsChecked = SelectionHasProperty(FlowDocument.TextAlignmentProperty, TextAlignment.Justify);
  195. FontFamilyComboBox.SelectedItem = SelectionProperty(FlowDocument.FontFamilyProperty);
  196. FontSizeComboBox.Text = SelectionProperty(FlowDocument.FontSizeProperty)?.ToString();
  197. }
  198. private void RtbEditor_SelectionChanged(object sender, RoutedEventArgs e)
  199. {
  200. UpdateButtons();
  201. }
  202. private void fontFamilyCombo_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  203. {
  204. if(FontFamilyComboBox.SelectedItem != null)
  205. {
  206. RtbEditor.Selection.ApplyPropertyValue(FlowDocument.FontFamilyProperty, FontFamilyComboBox.SelectedItem);
  207. }
  208. }
  209. private void fontSizeCombo_TextChanged(object sender, TextChangedEventArgs e)
  210. {
  211. var currentValue = SelectionProperty(FlowDocument.FontSizeProperty)?.ToString();
  212. var fontSize = FontSizeComboBox.Text;
  213. if(double.TryParse(fontSize, out var size))
  214. {
  215. RtbEditor.Selection.ApplyPropertyValue(FlowDocument.FontSizeProperty, size);
  216. }
  217. else
  218. {
  219. FontSizeComboBox.Text = currentValue;
  220. }
  221. }
  222. private void RtbEditor_TextChanged(object sender, TextChangedEventArgs e)
  223. {
  224. UpdateButtons();
  225. }
  226. private void HyperlinkBtn_Click(object sender, RoutedEventArgs e)
  227. {
  228. var selection = RtbEditor.Selection.Text;
  229. }
  230. }
  231. }