using System; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Media; using JetBrains.Annotations; using JetBrains.Util; namespace JetBrains.UI.Avalon { /// /// Extension methods for Avalon. /// public static class AvalonEx { #region Operations /// /// Adds a new child to the panel control. /// [NotNull] public static TPanel AddChild([NotNull] this TPanel panel, UIElement child) where TPanel : Panel { if(panel == null) throw new ArgumentNullException("panel"); if(child == null) throw new ArgumentNullException("child"); panel.Children.Add(child); return panel; } /// /// Adds a new column to the grid, placing a child in the newly-added column. /// [NotNull] public static Grid AddColumnChild([NotNull] this Grid grid, [NotNull] string size, [NotNull] UIElement child) { if(grid == null) throw new ArgumentNullException("grid"); if(size.IsEmpty()) throw new ArgumentNullException("size"); if(child == null) throw new ArgumentNullException("child"); // Create a new row for the child grid.ColumnDefinitions.Add(new ColumnDefinition {Width = (GridLength)TypeDescriptor.GetConverter(typeof(GridLength)).ConvertFromInvariantString(size)}); // Specify child location Grid.SetColumn(child, grid.ColumnDefinitions.Count - 1); Grid.SetRow(child, 0); // Add child grid.Children.Add(child); return grid; } [NotNull] public static Paragraph AddPara([NotNull] this FlowDocument document) { if(document == null) throw new ArgumentNullException("document"); var para = new Paragraph(); document.Blocks.Add(para); return para; } /// /// Adds a new row to the grid, placing a child in the newly-added row. /// [NotNull] public static Grid AddRowChild([NotNull] this Grid grid, [NotNull] string size, [NotNull] UIElement child) { if(grid == null) throw new ArgumentNullException("grid"); if(size.IsEmpty()) throw new ArgumentNullException("size"); if(child == null) throw new ArgumentNullException("child"); // Create a new row for the child grid.RowDefinitions.Add(new RowDefinition {Height = (GridLength)TypeDescriptor.GetConverter(typeof(GridLength)).ConvertFromInvariantString(size)}); // Specify child location Grid.SetColumn(child, 0); Grid.SetRow(child, grid.RowDefinitions.Count - 1); // Add child grid.Children.Add(child); return grid; } /// /// Adds one more to the . /// public static TextBlock Append([NotNull] this TextBlock block, [NotNull] string text) { return Append(block, text, FontStyles.Normal, FontWeights.Normal); } /// /// Adds one more to the . /// public static TextBlock Append([NotNull] this TextBlock block, [NotNull] string text, FontStyle style) { return Append(block, text, style, FontWeights.Normal); } /// /// Adds one more to the . /// public static TextBlock Append([NotNull] this TextBlock block, [NotNull] string text, FontWeight weight) { return Append(block, text, FontStyles.Normal, weight); } /// /// Adds one more to the . /// public static TextBlock Append([NotNull] this TextBlock block, [NotNull] Run run) { if(block == null) throw new ArgumentNullException("block"); if(run == null) throw new ArgumentNullException("run"); block.Inlines.Add(run); return block; } /// /// Adds one more to the . /// public static TextBlock Append([NotNull] this TextBlock block, [NotNull] Inline run) { if(block == null) throw new ArgumentNullException("block"); if(run == null) throw new ArgumentNullException("run"); block.Inlines.Add(run); return block; } /// /// Adds one more to the . /// public static TextBlock Append([NotNull] this TextBlock block, [NotNull] string text, FontStyle style, FontWeight weight) { if(block == null) throw new ArgumentNullException("block"); if(text == null) throw new ArgumentNullException("text"); block.Inlines.Add(new Run(text) {FontStyle = style, FontWeight = weight}); return block; } /// /// Adds one more to the . /// [NotNull] public static Paragraph Append([NotNull] this Paragraph para, [NotNull] string text) { return Append(para, text, FontStyles.Normal, FontWeights.Normal); } /// /// Adds one more to the . /// [NotNull] public static Paragraph Append([NotNull] this Paragraph para, [NotNull] string text, FontStyle style, FontWeight weight) { if(para == null) throw new ArgumentNullException("para"); if(text == null) throw new ArgumentNullException("text"); para.Inlines.Add(new Run(text) {FontStyle = style, FontWeight = weight}); return para; } /// /// Establishes a two-way property binding on the element. /// The “Update Source” is triggered according to the default scenario (ie on focus loss for an edit box). /// [NotNull] public static TElement Bind([NotNull] this TElement element, [NotNull] DependencyProperty property, [NotNull] string path) where TElement : FrameworkElement { element.SetBinding(property, path); return element; } /// /// Establishes a two-way property binding on the element. /// The “Update Source” is triggered according to the default scenario (ie on focus loss for an edit box). /// [NotNull] public static TElement Bind([NotNull] this TElement element, [NotNull] DependencyProperty property, [NotNull] BindingBase binding) where TElement : FrameworkElement { element.SetBinding(property, binding); return element; } /// /// Establishes a two-way property binding on the element. /// The “Update Source” is triggeret immediately when the property changes (ie on typing for an edit box). /// public static TElement BindOnChange(this TElement element, DependencyProperty property, string path) where TElement : FrameworkElement { element.SetBinding(property, new Binding(path) {UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged}); return element; } /// /// Adds columns to the grid. Each sizes element is a Star grid-length value. /// public static Grid Cols([NotNull] this Grid grid, [NotNull] params string[] sizes) { if(grid == null) throw new ArgumentNullException("grid"); if(sizes == null) throw new ArgumentNullException("sizes"); foreach(string size in sizes) grid.ColumnDefinitions.Add(new ColumnDefinition {Width = (GridLength)TypeDescriptor.GetConverter(typeof(GridLength)).ConvertFromInvariantString(size)}); return grid; } /// /// Constraints the to be no more than on each of the dimensions independently. /// public static Size Constrain(this Size size, Size constraint) { if(constraint.Width == double.NaN) constraint.Width = double.PositiveInfinity; if(constraint.Height == double.NaN) constraint.Height = double.PositiveInfinity; return new Size(size.Width <= constraint.Width ? size.Width : constraint.Width, size.Height <= constraint.Height ? size.Height : constraint.Height); } /// /// A converter. /// public static Visibility ConvertBoolToVisibility(bool visible) { return visible ? Visibility.Visible : Visibility.Collapsed; } /// /// Sets the extension property on the specified UI element. /// public static TElement Dock(this TElement element, Dock dock) where TElement : UIElement { return element.Set(DockPanel.DockProperty, dock); } /// /// Sets the and extension properties on the specified UI element. /// public static TElement InGrid(this TElement element, int col, int row) where TElement : UIElement { return element.Set(Grid.ColumnProperty, col).Set(Grid.RowProperty, row); } /// /// Sets the and extension properties on the specified UI element. /// In addition, specifies the and extension properties. /// public static TElement InGrid(this TElement element, int col, int row, int colspan, int rowspan) where TElement : UIElement { return element.Set(Grid.ColumnProperty, col).Set(Grid.RowProperty, row).Set(Grid.ColumnSpanProperty, colspan).Set(Grid.RowSpanProperty, rowspan); } /// /// Mixes two colors together. /// public static Color MixWith(this Color first, Color second, float firstpercentage) { return Color.Add(Color.Multiply(first, firstpercentage), Color.Multiply(second, 1 - firstpercentage)); } /// /// Registers a name for the object in the host's name scope. /// The name scope must first be registered for the host or one of its parents. /// public static TObject Name(this TObject @object, FrameworkElement host, string name) where TObject : DependencyObject { host.RegisterName(name, @object); return @object; } /// /// Sinks the specified on the . /// public static TElement OnEvent(this TElement element, RoutedEvent @event, RoutedEventHandler handler) where TElement : UIElement { element.AddHandler(@event, handler); return element; } /// /// Sinks the specified on the . /// public static TElement OnEventC(this TElement element, RoutedEvent @event, RoutedEventHandler handler) where TElement : ContentElement { element.AddHandler(@event, handler); return element; } /// /// Adds rows to the grid. Each sizes element is a Star grid-length value. /// public static Grid Rows([NotNull] this Grid grid, [NotNull] params string[] sizes) { if(grid == null) throw new ArgumentNullException("grid"); if(sizes == null) throw new ArgumentNullException("sizes"); foreach(string size in sizes) grid.RowDefinitions.Add(new RowDefinition {Height = (GridLength)TypeDescriptor.GetConverter(typeof(GridLength)).ConvertFromInvariantString(size)}); return grid; } /// /// Applies a scale layout transformation to the element. /// public static TElement Scale(this TElement element, double factor) where TElement : FrameworkElement { var transform = new ScaleTransform(factor, factor); // Combine with existing? if(element.LayoutTransform != null) { var group = element.LayoutTransform as TransformGroup; if(group == null) { group = new TransformGroup(); group.Children.Add(element.LayoutTransform); element.LayoutTransform = group; } group.Children.Add(transform); } else element.LayoutTransform = transform; return element; } /// /// Sets a dependency property on a dependency object, allows to pipe such settings. /// Especially useful for setting extension properties. /// public static TDependencyObject Set([NotNull] this TDependencyObject dependencyobject, [NotNull] DependencyProperty dependencyproperty, object value) where TDependencyObject : DependencyObject { if(dependencyobject == null) throw new ArgumentNullException("dependencyobject"); if(dependencyproperty == null) throw new ArgumentNullException("dependencyproperty"); dependencyobject.SetValue(dependencyproperty, value); return dependencyobject; } /// /// Applies dialog font to a control. /// public static TElement SetDialogFont(this TElement element) where TElement : Control { element.FontFamily = new FontFamily("Corbel"); element.FontSize = 13; element.FontWeight = FontWeights.Normal; element.FontStyle = FontStyles.Normal; return element; } /// /// Applies editor font to a control. /// public static TElement SetEditorFont(this TElement element) where TElement : Control { element.FontFamily = new FontFamily("Consolas"); element.FontSize = 14; element.FontWeight = FontWeights.Normal; element.FontStyle = FontStyles.Normal; return element; } /// /// By default, a document has some large serif font applied. /// Makes the document follow the system font family and sizes. /// [NotNull] public static FlowDocument SetSystemFont([NotNull] this FlowDocument document) { document.FontFamily = SystemFonts.MessageFontFamily; document.FontSize = SystemFonts.MessageFontSize; document.FontStyle = SystemFonts.MessageFontStyle; document.FontWeight = SystemFonts.MessageFontWeight; return document; } #endregion } }