using System; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Media; using System.Windows.Controls.Primitives; using Microsoft.Windows.Controls.Core; namespace Microsoft.Windows.Controls { /// /// Interaction logic for RichTextBoxFormatBar.xaml /// public partial class RichTextBoxFormatBar : UserControl, IRichTextBoxFormatBar { #region Properties #region RichTextBox public static readonly DependencyProperty TargetProperty = DependencyProperty.Register("Target", typeof(global::System.Windows.Controls.RichTextBox), typeof(RichTextBoxFormatBar), new PropertyMetadata(null, OnRichTextBoxPropertyChanged)); public global::System.Windows.Controls.RichTextBox Target { get { return (global::System.Windows.Controls.RichTextBox)GetValue(TargetProperty); } set { SetValue(TargetProperty, value); } } private static void OnRichTextBoxPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { RichTextBoxFormatBar formatBar = d as RichTextBoxFormatBar; formatBar.HookupRichTextBoxEvents(); } private void HookupRichTextBoxEvents() { Target.SelectionChanged += RichTextBox_SelectionChanged; } #endregion //RichTextBox public static double[] FontSizes { get { return new double[] { 3.0, 4.0, 5.0, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 22.0, 24.0, 26.0, 28.0, 30.0, 32.0, 34.0, 36.0, 38.0, 40.0, 44.0, 48.0, 52.0, 56.0, 60.0, 64.0, 68.0, 72.0, 76.0, 80.0, 88.0, 96.0, 104.0, 112.0, 120.0, 128.0, 136.0, 144.0 }; } } #endregion #region Constructors public RichTextBoxFormatBar() { InitializeComponent(); Loaded += FormatToolbar_Loaded; } #endregion //Constructors #region Event Hanlders void FormatToolbar_Loaded(object sender, RoutedEventArgs e) { _cmbFontFamilies.ItemsSource = Fonts.SystemFontFamilies; _cmbFontSizes.ItemsSource = FontSizes; } private void FontFamily_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count == 0) return; FontFamily editValue = (FontFamily)e.AddedItems[0]; ApplyPropertyValueToSelectedText(TextElement.FontFamilyProperty, editValue); } private void FontSize_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count == 0) return; ApplyPropertyValueToSelectedText(TextElement.FontSizeProperty, e.AddedItems[0]); } void RichTextBox_SelectionChanged(object sender, RoutedEventArgs e) { UpdateVisualState(); } private void DragWidget_DragDelta(object sender, DragDeltaEventArgs e) { ProcessMove(e); } #endregion //Event Hanlders #region Methods private void UpdateVisualState() { UpdateToggleButtonState(); UpdateSelectedFontFamily(); UpdateSelectedFontSize(); } private void UpdateToggleButtonState() { UpdateItemCheckedState(_btnBold, TextElement.FontWeightProperty, FontWeights.Bold); UpdateItemCheckedState(_btnItalic, TextElement.FontStyleProperty, FontStyles.Italic); UpdateItemCheckedState(_btnUnderline, Inline.TextDecorationsProperty, TextDecorations.Underline); UpdateItemCheckedState(_btnAlignLeft, Paragraph.TextAlignmentProperty, TextAlignment.Left); UpdateItemCheckedState(_btnAlignCenter, Paragraph.TextAlignmentProperty, TextAlignment.Center); UpdateItemCheckedState(_btnAlignRight, Paragraph.TextAlignmentProperty, TextAlignment.Right); } void UpdateItemCheckedState(ToggleButton button, DependencyProperty formattingProperty, object expectedValue) { object currentValue = Target.Selection.GetPropertyValue(formattingProperty); button.IsChecked = (currentValue == DependencyProperty.UnsetValue) ? false : currentValue != null && currentValue.Equals(expectedValue); } private void UpdateSelectedFontFamily() { object value = Target.Selection.GetPropertyValue(TextElement.FontFamilyProperty); FontFamily currentFontFamily = (FontFamily)((value == DependencyProperty.UnsetValue) ? null : value); if (currentFontFamily != null) { _cmbFontFamilies.SelectedItem = currentFontFamily; } } private void UpdateSelectedFontSize() { object value = Target.Selection.GetPropertyValue(TextElement.FontSizeProperty); _cmbFontSizes.SelectedValue = (value == DependencyProperty.UnsetValue) ? null : value; } void ApplyPropertyValueToSelectedText(DependencyProperty formattingProperty, object value) { if (value == null) return; Target.Selection.ApplyPropertyValue(formattingProperty, value); } private void ProcessMove(DragDeltaEventArgs e) { AdornerLayer layer = AdornerLayer.GetAdornerLayer(Target); UIElementAdorner adorner = layer.GetAdorners(Target)[0] as UIElementAdorner; adorner.SetOffsets(adorner.OffsetLeft + e.HorizontalChange, adorner.OffsetTop + e.VerticalChange); } #endregion //Methods } }