Browse Source

added the initial Office 2010 Format style contextual formating bar to RichTextBox. To enable the FormatBar set the RichTextBox.AllowFormatting property to True.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
d7341b8703
  1. 10
      ExtendedWPFToolkitSolution/ExtendedWPFToolkit.vssscc
  2. 186
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/FormatToolbar.xaml
  3. 148
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/FormatToolbar.xaml.cs
  4. 107
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/FormatToolbarManager.cs
  5. BIN
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/Bold16.png
  6. BIN
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/CenterAlign16.png
  7. BIN
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/Italic16.png
  8. BIN
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/JustifyAlign16.png
  9. BIN
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/LeftAlign16.png
  10. BIN
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/RightAlign16.png
  11. BIN
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/Underline16.png
  12. 215
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/UIElementAdorner.cs
  13. 59
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs
  14. 18
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

10
ExtendedWPFToolkitSolution/ExtendedWPFToolkit.vssscc

@ -0,0 +1,10 @@
""
{
"FILE_VERSION" = "9237"
"ENLISTMENT_CHOICE" = "NEVER"
"PROJECT_FILE_RELATIVE_PATH" = ""
"NUMBER_OF_EXCLUDED_FILES" = "0"
"ORIGINAL_PROJECT_FILE_PATH" = ""
"NUMBER_OF_NESTED_PROJECTS" = "0"
"SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROJECT"
}

186
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/FormatToolbar.xaml

@ -0,0 +1,186 @@
<UserControl x:Class="Microsoft.Windows.Controls.Formatting.FormatToolbar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Transparent"
IsTabStop="False"
x:Name="_window">
<UserControl.Effect>
<DropShadowEffect BlurRadius="5" Opacity=".25" />
</UserControl.Effect>
<UserControl.Resources>
<ControlTemplate x:Key="ThumbControlTemplate" TargetType="{x:Type Thumb}">
<Border Background="Transparent" Cursor="Hand" ToolTip="Click to Drag">
</Border>
</ControlTemplate>
<Style TargetType="{x:Type Separator}" BasedOn="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}"/>
<SolidColorBrush x:Key="MouseOverBorderBrush" Color="#FFFFB700" />
<LinearGradientBrush x:Key="MouseOverBackgroundBrush" StartPoint="0,0" EndPoint="0,1" >
<GradientStop Offset="0" Color="#FFFEFBF4" />
<GradientStop Offset="0.19" Color="#FFFDE7CE" />
<GradientStop Offset="0.39" Color="#FFFDDEB8" />
<GradientStop Offset="0.39" Color="#FFFFCE6B" />
<GradientStop Offset="0.79" Color="#FFFFDE9A" />
<GradientStop Offset="1" Color="#FFFFEBAA" />
</LinearGradientBrush>
<SolidColorBrush x:Key="CheckedBorderBrush" Color="#FFC29B29" />
<LinearGradientBrush x:Key="CheckedBackgroundBrush" StartPoint="0,0" EndPoint="0,1" >
<GradientStop Offset="0" Color="#FFFFDCA0" />
<GradientStop Offset="0.18" Color="#FFFFD692" />
<GradientStop Offset="0.39" Color="#FFFFC45D" />
<GradientStop Offset="1" Color="#FFFFD178" />
</LinearGradientBrush>
<SolidColorBrush x:Key="PressedBorderBrush" Color="#FFC29B29" />
<LinearGradientBrush x:Key="PressedBackgroundBrush" StartPoint="0,0" EndPoint="0,1" >
<GradientStop Offset="0" Color="#FFE3C085" />
<GradientStop Offset="0.19" Color="#FFF4CC89" />
<GradientStop Offset="0.36" Color="#FFF5C777" />
<GradientStop Offset="0.36" Color="#FFF5BB56" />
<GradientStop Offset="0.79" Color="#FFF4CE9A" />
<GradientStop Offset="1" Color="#FFF3E28D" />
</LinearGradientBrush>
<Style x:Key="FormatBarToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Height" Value="22" />
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="ToolTipService.InitialShowDelay" Value="900"/>
<Setter Property="ToolTipService.ShowDuration" Value="20000"/>
<Setter Property="ToolTipService.BetweenShowDelay" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid SnapsToDevicePixels="True">
<Border x:Name="OuterBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="2"/>
<Border x:Name="MiddleBorder" BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" CornerRadius="2">
<Border x:Name="InnerBorder" BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" CornerRadius="2" Padding="{TemplateBinding Padding}">
<StackPanel x:Name="StackPanel" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}">
<ContentPresenter x:Name="Content" Content="{TemplateBinding Content}" Margin="1"
RenderOptions.BitmapScalingMode="NearestNeighbor"
VerticalAlignment="Center"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
</StackPanel>
</Border>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="OuterBorder" Value="{StaticResource MouseOverBackgroundBrush}"/>
<Setter Property="BorderBrush" TargetName="OuterBorder" Value="{StaticResource MouseOverBorderBrush}"/>
<Setter Property="BorderBrush" TargetName="InnerBorder" Value="#80FFFFFF"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" TargetName="Content" Value="0.5"/>
<Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="#FF9E9E9E"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" TargetName="OuterBorder" Value="{StaticResource CheckedBackgroundBrush}"/>
<Setter Property="BorderBrush" TargetName="OuterBorder" Value="{StaticResource CheckedBorderBrush}"/>
<Setter Property="BorderBrush" TargetName="InnerBorder">
<Setter.Value>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFE7CBAD" Offset="0"/>
<GradientStop Color="#FFF7D7B5" Offset="0.1"/>
<GradientStop Color="#FFFFD38C" Offset="0.36"/>
<GradientStop Color="#FFFFC75A" Offset="0.36"/>
<GradientStop Color="#FFFFEFA5" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="OuterBorder" Value="{StaticResource PressedBackgroundBrush}"/>
<Setter Property="BorderBrush" TargetName="OuterBorder" Value="{StaticResource PressedBorderBrush}"/>
<Setter Property="BorderBrush" TargetName="InnerBorder" Value="Transparent"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="True"/>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="MiddleBorder">
<Setter.Value>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#40FFFEFE" Offset="0"/>
<GradientStop Color="#40FFFEFE" Offset="0.39"/>
<GradientStop Color="#20FFCE68" Offset="0.39"/>
<GradientStop Color="#20FFCE68" Offset="0.69"/>
<GradientStop Color="#10FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Border CornerRadius="3" BorderThickness="1" BorderBrush="Gray" Background="WhiteSmoke">
<Grid Margin="5,0,5,5">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Thumb x:Name="_dragWidget" Height="10"
Template="{StaticResource ThumbControlTemplate}"
DragDelta="DragWidget_DragDelta"/>
<StackPanel Grid.Row="1">
<StackPanel Orientation="Horizontal">
<ComboBox x:Name="_cmbFontFamilies" IsEditable="True" Width="110"
SelectionChanged="FontFamily_SelectionChanged"/>
<ComboBox x:Name="_cmbFontSizes" IsEditable="True" Width="45"
SelectionChanged="FontSize_SelectionChanged"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
<ToggleButton x:Name="_btnBold" Style="{StaticResource FormatBarToggleButtonStyle}"
Command="EditingCommands.ToggleBold" CommandTarget="{Binding ElementName=_window, Path=RichTextBox}">
<Image Source="/WPFToolkit.Extended;component/RichTextBox/FormatToolbar/Images/Bold16.png" />
</ToggleButton>
<ToggleButton x:Name="_btnItalic" Style="{StaticResource FormatBarToggleButtonStyle}"
Command="{x:Static EditingCommands.ToggleItalic}" CommandTarget="{Binding ElementName=_window, Path=RichTextBox}">
<Image Source="/WPFToolkit.Extended;component/RichTextBox/FormatToolbar/Images/Italic16.png" />
</ToggleButton>
<ToggleButton x:Name="_btnUnderline" Style="{StaticResource FormatBarToggleButtonStyle}"
Command="{x:Static EditingCommands.ToggleUnderline}" CommandTarget="{Binding ElementName=_window, Path=RichTextBox}">
<Image Source="/WPFToolkit.Extended;component/RichTextBox/FormatToolbar/Images/Underline16.png" />
</ToggleButton>
<Separator />
<RadioButton x:Name="_btnAlignLeft" Style="{StaticResource FormatBarToggleButtonStyle}"
Command="{x:Static EditingCommands.AlignLeft}" CommandTarget="{Binding ElementName=_window, Path=RichTextBox}" >
<Image Source="/WPFToolkit.Extended;component/RichTextBox/FormatToolbar/Images/LeftAlign16.png" />
</RadioButton>
<RadioButton x:Name="_btnAlignCenter" Style="{StaticResource FormatBarToggleButtonStyle}"
Command="{x:Static EditingCommands.AlignCenter}" CommandTarget="{Binding ElementName=_window, Path=RichTextBox}" >
<Image Source="/WPFToolkit.Extended;component/RichTextBox/FormatToolbar/Images/CenterAlign16.png" />
</RadioButton>
<RadioButton x:Name="_btnAlignRight" Style="{StaticResource FormatBarToggleButtonStyle}"
Command="{x:Static EditingCommands.AlignRight}" CommandTarget="{Binding ElementName=_window, Path=RichTextBox}" >
<Image Source="/WPFToolkit.Extended;component/RichTextBox/FormatToolbar/Images/RightAlign16.png" />
</RadioButton>
</StackPanel>
</StackPanel>
</Grid>
</Border>
</UserControl>

148
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/FormatToolbar.xaml.cs

@ -0,0 +1,148 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
namespace Microsoft.Windows.Controls.Formatting
{
/// <summary>
/// Interaction logic for FormatToolbar.xaml
/// </summary>
public partial class FormatToolbar : UserControl
{
#region Properties
public static readonly DependencyProperty RichTextBoxProperty = DependencyProperty.Register("RichTextBox", typeof(RichTextBox), typeof(FormatToolbar));
public RichTextBox RichTextBox
{
get { return (RichTextBox)GetValue(RichTextBoxProperty); }
set { SetValue(RichTextBoxProperty, value); }
}
public 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 FormatToolbar(RichTextBox richTextBox)
{
InitializeComponent();
Loaded += FormatToolbar_Loaded;
RichTextBox = richTextBox;
RichTextBox.SelectionChanged += RichTextBox_SelectionChanged;
}
#endregion //Constructors
#region Event Hanlders
void FormatToolbar_Loaded(object sender, RoutedEventArgs e)
{
_cmbFontFamilies.ItemsSource = System.Windows.Media.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 = RichTextBox.Selection.GetPropertyValue(formattingProperty);
button.IsChecked = (currentValue == DependencyProperty.UnsetValue) ? false : currentValue != null && currentValue.Equals(expectedValue);
}
private void UpdateSelectedFontFamily()
{
object value = RichTextBox.Selection.GetPropertyValue(TextElement.FontFamilyProperty);
FontFamily currentFontFamily = (FontFamily)((value == DependencyProperty.UnsetValue) ? null : value);
if (currentFontFamily != null)
{
_cmbFontFamilies.SelectedItem = currentFontFamily;
}
}
private void UpdateSelectedFontSize()
{
object value = RichTextBox.Selection.GetPropertyValue(TextElement.FontSizeProperty);
_cmbFontSizes.SelectedValue = (value == DependencyProperty.UnsetValue) ? null : value;
}
void ApplyPropertyValueToSelectedText(DependencyProperty formattingProperty, object value)
{
if (value == null)
return;
RichTextBox.Selection.ApplyPropertyValue(formattingProperty, value);
}
private void ProcessMove(DragDeltaEventArgs e)
{
AdornerLayer layer = AdornerLayer.GetAdornerLayer(RichTextBox);
UIElementAdorner<Control> adorner = layer.GetAdorners(RichTextBox)[0] as UIElementAdorner<Control>;
adorner.SetOffsets(adorner.OffsetLeft + e.HorizontalChange, adorner.OffsetTop + e.VerticalChange);
}
#endregion //Methods
}
}

107
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/FormatToolbarManager.cs

@ -0,0 +1,107 @@
using System;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows;
namespace Microsoft.Windows.Controls.Formatting
{
internal class FormatToolbarManager
{
RichTextBox _richTextBox;
readonly UIElementAdorner<Control> _adorner;
FormatToolbar toolbar;
bool AdornerIsVisible
{
get { return _adorner.Visibility == Visibility.Visible; }
}
public FormatToolbarManager(RichTextBox richTextBox)
{
_richTextBox = richTextBox;
_adorner = new UIElementAdorner<Control>(richTextBox);
toolbar = new FormatToolbar(richTextBox);
AttachToRichtextBox();
}
private void AttachToRichtextBox()
{
_richTextBox.Selection.Changed += Selection_Changed;
}
void Selection_Changed(object sender, EventArgs e)
{
TextRange selectedText = new TextRange(_richTextBox.Selection.Start, _richTextBox.Selection.End);
if (selectedText.Text.Length > 0)
{
VerifyAdorner();
}
else
{
HideAdorner();
}
}
//TODO: refactor
void VerifyAdorner()
{
VerifyAdornerLayer();
Control adorningEditor = toolbar;
_adorner.Child = adorningEditor;
_adorner.Visibility = Visibility.Visible;
Rect wordBoundary = _richTextBox.Selection.End.GetPositionAtOffset(0, LogicalDirection.Backward).GetCharacterRect(LogicalDirection.Backward);
double left = wordBoundary.X;
double top = (wordBoundary.Y + wordBoundary.Height) - toolbar.ActualHeight;
//top boundary
if (top < 0)
{
top = wordBoundary.Y + wordBoundary.Height;
}
//right boundary
if (left + toolbar.ActualWidth > _richTextBox.ActualWidth - 20)
{
left = left - toolbar.ActualWidth;
top = wordBoundary.Y + wordBoundary.Height + 5;
}
//bottom boundary
if (top + toolbar.ActualHeight > _richTextBox.ActualHeight - 20)
{
top = wordBoundary.Y - (toolbar.ActualHeight + wordBoundary.Height);
}
_adorner.SetOffsets(left, top);
}
/// <summary>
/// Ensures that the adorner is in the adorner layer.
/// </summary>
/// <returns>True if the adorner is in the adorner layer, else false.</returns>
bool VerifyAdornerLayer()
{
if (_adorner.Parent != null)
return true;
AdornerLayer layer = AdornerLayer.GetAdornerLayer(_richTextBox);
if (layer == null)
return false;
layer.Add(_adorner);
return true;
}
void HideAdorner()
{
if (this.AdornerIsVisible)
{
_adorner.Visibility = Visibility.Collapsed;
_adorner.Child = null;
}
}
}
}

BIN
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/Bold16.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 B

BIN
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/CenterAlign16.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 B

BIN
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/Italic16.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 B

BIN
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/JustifyAlign16.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 169 B

BIN
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/LeftAlign16.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 B

BIN
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/RightAlign16.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 B

BIN
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/Images/Underline16.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 B

215
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/FormatToolbar/UIElementAdorner.cs

@ -0,0 +1,215 @@
using System;
using System.Collections;
using System.Windows.Media;
using System.Windows.Documents;
using System.Windows;
namespace Microsoft.Windows.Controls.Formatting
{
/// <summary>
/// An adorner that can display one and only one UIElement.
/// That element can be a panel, which contains multiple other elements.
/// The element is added to the adorner's visual and logical trees, enabling it to
/// particpate in dependency property value inheritance, amongst other things.
/// </summary>
internal class UIElementAdorner<TElement>
: Adorner
where TElement : UIElement
{
#region Fields
TElement _child = null;
double _offsetLeft = 0;
double _offsetTop = 0;
#endregion // Fields
#region Constructor
/// <summary>
/// Constructor.
/// </summary>
/// <param name="adornedElement">The element to which the adorner will be bound.</param>
public UIElementAdorner(UIElement adornedElement)
: base(adornedElement)
{
}
#endregion // Constructor
#region Public Interface
#region Child
/// <summary>
/// Gets/sets the child element hosted in the adorner.
/// </summary>
public TElement Child
{
get { return _child; }
set
{
if (value == _child)
return;
if (_child != null)
{
base.RemoveLogicalChild(_child);
base.RemoveVisualChild(_child);
}
_child = value;
if (_child != null)
{
base.AddLogicalChild(_child);
base.AddVisualChild(_child);
}
}
}
#endregion // Child
#region GetDesiredTransform
/// <summary>
/// Override.
/// </summary>
/// <param name="transform"></param>
/// <returns></returns>
public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
{
GeneralTransformGroup result = new GeneralTransformGroup();
result.Children.Add(base.GetDesiredTransform(transform));
result.Children.Add(new TranslateTransform(_offsetLeft, _offsetTop));
return result;
}
#endregion // GetDesiredTransform
#region OffsetLeft
/// <summary>
/// Gets/sets the horizontal offset of the adorner.
/// </summary>
public double OffsetLeft
{
get { return _offsetLeft; }
set
{
_offsetLeft = value;
UpdateLocation();
}
}
#endregion // OffsetLeft
#region SetOffsets
/// <summary>
/// Updates the location of the adorner in one atomic operation.
/// </summary>
public void SetOffsets(double left, double top)
{
_offsetLeft = left;
_offsetTop = top;
this.UpdateLocation();
}
#endregion // SetOffsets
#region OffsetTop
/// <summary>
/// Gets/sets the vertical offset of the adorner.
/// </summary>
public double OffsetTop
{
get { return _offsetTop; }
set
{
_offsetTop = value;
UpdateLocation();
}
}
#endregion // OffsetTop
#endregion // Public Interface
#region Protected Overrides
/// <summary>
/// Override.
/// </summary>
/// <param name="constraint"></param>
/// <returns></returns>
protected override Size MeasureOverride(Size constraint)
{
if (_child == null)
return base.MeasureOverride(constraint);
_child.Measure(constraint);
return _child.DesiredSize;
}
/// <summary>
/// Override.
/// </summary>
/// <param name="finalSize"></param>
/// <returns></returns>
protected override Size ArrangeOverride(Size finalSize)
{
if (_child == null)
return base.ArrangeOverride(finalSize);
_child.Arrange(new Rect(finalSize));
return finalSize;
}
/// <summary>
/// Override.
/// </summary>
protected override IEnumerator LogicalChildren
{
get
{
ArrayList list = new ArrayList();
if (_child != null)
list.Add(_child);
return list.GetEnumerator();
}
}
/// <summary>
/// Override.
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
protected override Visual GetVisualChild(int index)
{
return _child;
}
/// <summary>
/// Override.
/// </summary>
protected override int VisualChildrenCount
{
get { return _child == null ? 0 : 1; }
}
#endregion // Protected Overrides
#region Private Helpers
void UpdateLocation()
{
AdornerLayer adornerLayer = base.Parent as AdornerLayer;
if (adornerLayer != null)
adornerLayer.Update(base.AdornedElement);
}
#endregion // Private Helpers
}
}

59
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs

@ -2,6 +2,7 @@
using System.Windows;
using System.Windows.Data;
using System.Windows.Threading;
using Microsoft.Windows.Controls.Formatting;
namespace Microsoft.Windows.Controls
{
@ -10,7 +11,8 @@ namespace Microsoft.Windows.Controls
#region Private Members
private bool _textHasLoaded;
bool isInvokePending;
private bool isInvokePending;
private FormatToolbarManager _manager;
#endregion //Private Members
@ -31,26 +33,25 @@ namespace Microsoft.Windows.Controls
#region Properties
private ITextFormatter _textFormatter;
/// <summary>
/// The ITextFormatter the is used to format the text of the RichTextBox.
/// Deafult formatter is the RtfFormatter
/// </summary>
public ITextFormatter TextFormatter
#region AllowFormatting
public static readonly DependencyProperty AllowFormatingProperty = DependencyProperty.Register("AllowFormating", typeof(bool), typeof(RichTextBox), new PropertyMetadata(false, new PropertyChangedCallback(OnAllowFormatingPropertyChanged)));
public bool AllowFormating
{
get
{
if (_textFormatter == null)
_textFormatter = new RtfFormatter(); //default is rtf
get { return (bool)GetValue(AllowFormatingProperty); }
set { SetValue(AllowFormatingProperty, value); }
}
return _textFormatter;
}
set
{
_textFormatter = value;
}
private static void OnAllowFormatingPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
RichTextBox rtb = (RichTextBox)d;
if ((bool)e.NewValue)
rtb._manager = new FormatToolbarManager(rtb);
}
#endregion //AllowFormatting
#region Text
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(RichTextBox), new FrameworkPropertyMetadata(String.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnTextPropertyChanged), new CoerceValueCallback(CoerceTextProperty), true, System.Windows.Data.UpdateSourceTrigger.LostFocus));
@ -78,6 +79,30 @@ namespace Microsoft.Windows.Controls
#endregion //Text
#region TextFormatter
private ITextFormatter _textFormatter;
/// <summary>
/// The ITextFormatter the is used to format the text of the RichTextBox.
/// Deafult formatter is the RtfFormatter
/// </summary>
public ITextFormatter TextFormatter
{
get
{
if (_textFormatter == null)
_textFormatter = new RtfFormatter(); //default is rtf
return _textFormatter;
}
set
{
_textFormatter = value;
}
}
#endregion //TextFormatter
#endregion //Properties
#region Methods

18
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

@ -53,6 +53,10 @@
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<Page Include="RichTextBox\FormatToolbar\FormatToolbar.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Themes\Generic.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@ -82,6 +86,11 @@
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<Compile Include="BusyIndicator\VisualStates.BusyIndicator.cs" />
<Compile Include="RichTextBox\FormatToolbar\FormatToolbarManager.cs" />
<Compile Include="RichTextBox\FormatToolbar\FormatToolbar.xaml.cs">
<DependentUpon>FormatToolbar.xaml</DependentUpon>
</Compile>
<Compile Include="RichTextBox\FormatToolbar\UIElementAdorner.cs" />
<Compile Include="RichTextBox\Formatters\ITextFormatter.cs" />
<Compile Include="RichTextBox\Formatters\PlainTextFormatter.cs" />
<Compile Include="RichTextBox\Formatters\RtfFormatter.cs" />
@ -105,6 +114,15 @@
<Resource Include="MessageBox\Icons\Question48.png" />
<Resource Include="MessageBox\Icons\Warning48.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="RichTextBox\FormatToolbar\Images\Bold16.png" />
<Resource Include="RichTextBox\FormatToolbar\Images\CenterAlign16.png" />
<Resource Include="RichTextBox\FormatToolbar\Images\Italic16.png" />
<Resource Include="RichTextBox\FormatToolbar\Images\JustifyAlign16.png" />
<Resource Include="RichTextBox\FormatToolbar\Images\LeftAlign16.png" />
<Resource Include="RichTextBox\FormatToolbar\Images\RightAlign16.png" />
<Resource Include="RichTextBox\FormatToolbar\Images\Underline16.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

Loading…
Cancel
Save