diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor.cs deleted file mode 100644 index 881b243f97..0000000000 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor.cs +++ /dev/null @@ -1,84 +0,0 @@ -// -// -// -// -// $Revision$ -// - -using System; -using System.Diagnostics; -using System.Windows; -using System.Windows.Data; -using System.Windows.Controls; -using System.Windows.Documents; -using System.Windows.Media; -using ICSharpCode.WpfDesign.PropertyEditor; - -namespace ICSharpCode.WpfDesign.Designer.Controls.TypeEditors -{ - /// - /// Type editor used to edit Brush properties. - /// - [TypeEditor(typeof(Brush))] - public sealed class BrushEditor : DockPanel - { - readonly IPropertyEditorDataProperty property; - - Border brushShowingBorder = new Border { - SnapsToDevicePixels = true, - BorderThickness = new Thickness(1) - }; - DropDownButton ddb = new DropDownButton { - HorizontalAlignment = HorizontalAlignment.Right - }; - - /// - /// Creates a new BooleanEditor instance. - /// - public BrushEditor(IPropertyEditorDataProperty property) - { - this.property = property; - - PropertyEditorBindingHelper.AddValueChangedEventHandler(this, property, OnValueChanged); - OnValueChanged(null, null); - - ddb.Click += new RoutedEventHandler(DropDownButtonClick); - SetDock(ddb, Dock.Right); - this.Children.Add(ddb); - this.Children.Add(brushShowingBorder); - - this.Unloaded += delegate { - if (dlg != null) - dlg.Close(); - }; - } - - BrushEditorDialog dlg; - - void DropDownButtonClick(object sender, RoutedEventArgs e) - { - dlg = new BrushEditorDialog(property); - Point pos = ddb.PointToScreen(new Point(ddb.ActualWidth, ddb.ActualHeight)); - dlg.Left = pos.X - dlg.Width; - dlg.Top = pos.Y; - dlg.SelectedBrush = property.Value as Brush; - dlg.SelectedBrushChanged += delegate { - property.Value = dlg.SelectedBrush; - }; - dlg.Show(); - } - - void OnValueChanged(object sender, EventArgs e) - { - Brush val = property.Value as Brush; - brushShowingBorder.Background = val; - if (val == null) { - brushShowingBorder.BorderBrush = null; - } else if (property.IsSet) { - brushShowingBorder.BorderBrush = Brushes.Black; - } else { - brushShowingBorder.BorderBrush = Brushes.Gray; - } - } - } -} diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor/BrushEditor.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor/BrushEditor.cs new file mode 100644 index 0000000000..b6a3fa2abd --- /dev/null +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor/BrushEditor.cs @@ -0,0 +1,234 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.ComponentModel; +using ICSharpCode.WpfDesign.PropertyEditor; +using System.Windows.Media; +using System.Reflection; +using System.Windows; + +namespace ICSharpCode.WpfDesign.Designer.Controls.TypeEditors.BrushEditor +{ + public enum BrushEditorKind + { + None, + Solid, + Linear, + Radial, + List + } + + public class BrushItem + { + public string Name { get; set; } + public Brush Brush { get; set; } + } + + public class BrushEditor : INotifyPropertyChanged + { + public BrushEditor() + { + GradientStopCollection stops = new GradientStopCollection(); + stops.Add(new GradientStop(Colors.Black, 0)); + stops.Add(new GradientStop(Colors.White, 1)); + + linearGradientBrush = new LinearGradientBrush(stops); + linearGradientBrush.EndPoint = new Point(1, 0); + radialGradientBrush = new RadialGradientBrush(stops); + } + + public static BrushEditor Instance = new BrushEditor(); + + public static BrushItem[] SystemBrushes = typeof(SystemColors) + .GetProperties(BindingFlags.Static | BindingFlags.Public) + .Where(p => p.PropertyType == typeof(SolidColorBrush)) + .Select(p => new BrushItem() { Name = p.Name, Brush = (Brush)p.GetValue(null, null) }) + .ToArray(); + + public static BrushItem[] SystemColors = typeof(SystemColors) + .GetProperties(BindingFlags.Static | BindingFlags.Public) + .Where(p => p.PropertyType == typeof(Color)) + .Select(p => new BrushItem() + { + Name = p.Name, + Brush = new SolidColorBrush((Color)p.GetValue(null, null)) + }) + .ToArray(); + + SolidColorBrush solidColorBrush = new SolidColorBrush(Colors.White); + LinearGradientBrush linearGradientBrush; + RadialGradientBrush radialGradientBrush; + + IPropertyEditorDataProperty property; + + public IPropertyEditorDataProperty Property + { + get + { + return property; + } + set + { + property = value; + if (property != null) + { + var f = property.Value as Freezable; + if (f != null && f.IsFrozen) property.Value = f.Clone(); + } + DetermineCurrentKind(); + RaisePropertyChanged("Property"); + RaisePropertyChanged("Brush"); + } + } + + public Brush Brush + { + get + { + if (property != null) + { + return property.Value as Brush; + } + return null; + } + set + { + if (property != null && property.Value != value) + { + if (value != null && value.IsFrozen) + { + value = value.Clone(); + } + property.Value = value; + DetermineCurrentKind(); + RaisePropertyChanged("Brush"); + } + } + } + + void DetermineCurrentKind() + { + if (Brush == null) + { + CurrentKind = BrushEditorKind.None; + } + else if (Brush is SolidColorBrush) + { + solidColorBrush = Brush as SolidColorBrush; + CurrentKind = BrushEditorKind.Solid; + } + else if (Brush is LinearGradientBrush) + { + linearGradientBrush = Brush as LinearGradientBrush; + radialGradientBrush.GradientStops = linearGradientBrush.GradientStops; + CurrentKind = BrushEditorKind.Linear; + } + else if (Brush is RadialGradientBrush) + { + radialGradientBrush = Brush as RadialGradientBrush; + linearGradientBrush.GradientStops = linearGradientBrush.GradientStops; + CurrentKind = BrushEditorKind.Radial; + } + } + + BrushEditorKind currentKind; + + public BrushEditorKind CurrentKind + { + get + { + return currentKind; + } + set + { + currentKind = value; + RaisePropertyChanged("CurrentKind"); + + switch (CurrentKind) + { + case BrushEditorKind.None: + Brush = null; + break; + + case BrushEditorKind.Solid: + Brush = solidColorBrush; + break; + + case BrushEditorKind.Linear: + Brush = linearGradientBrush; + break; + + case BrushEditorKind.Radial: + Brush = radialGradientBrush; + break; + + case BrushEditorKind.List: + Brush = solidColorBrush; + break; + } + } + } + + public double GradientAngle + { + get + { + var x = linearGradientBrush.EndPoint.X - linearGradientBrush.StartPoint.X; + var y = linearGradientBrush.EndPoint.Y - linearGradientBrush.StartPoint.Y; + return Vector.AngleBetween(new Vector(1, 0), new Vector(x, -y)); + } + set + { + var d = value * Math.PI / 180; + var p = new Point(Math.Cos(d), -Math.Sin(d)); + var k = 1 / Math.Max(Math.Abs(p.X), Math.Abs(p.Y)); + p.X *= k; + p.Y *= k; + var p2 = new Point(-p.X, -p.Y); + linearGradientBrush.StartPoint = new Point((p2.X + 1) / 2, (p2.Y + 1) / 2); + linearGradientBrush.EndPoint = new Point((p.X + 1) / 2, (p.Y + 1) / 2); + RaisePropertyChanged("GradientAngle"); + } + } + + public IEnumerable AvailableColors + { + get { return SystemColors; } + } + + public IEnumerable AvailableBrushes + { + get { return SystemBrushes; } + } + + public void MakeGradientHorizontal() + { + GradientAngle = 0; + } + + public void MakeGradientVertical() + { + GradientAngle = -90; + } + + public void Commit() + { + Property.Value = Property.Value; + } + + #region INotifyPropertyChanged Members + + public event PropertyChangedEventHandler PropertyChanged; + + void RaisePropertyChanged(string name) + { + if (PropertyChanged != null) + { + PropertyChanged(this, new PropertyChangedEventArgs(name)); + } + } + + #endregion + } +} diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor/BrushEditorPopup.xaml b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor/BrushEditorPopup.xaml new file mode 100644 index 0000000000..ca14c05174 --- /dev/null +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor/BrushEditorPopup.xaml @@ -0,0 +1,10 @@ + + + diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor/BrushEditorPopup.xaml.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor/BrushEditorPopup.xaml.cs new file mode 100644 index 0000000000..cb67d876c8 --- /dev/null +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor/BrushEditorPopup.xaml.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using System.Diagnostics; + +namespace ICSharpCode.WpfDesign.Designer.Controls.TypeEditors.BrushEditor +{ + public partial class BrushEditorPopup + { + public BrushEditorPopup() + { + InitializeComponent(); + } + + public static BrushEditorPopup Instance = new BrushEditorPopup(); + + protected override void OnClosed(EventArgs e) + { + base.OnClosed(e); + BrushEditor.Instance.Commit(); + } + + protected override void OnKeyDown(KeyEventArgs e) + { + if (e.Key == Key.Escape) IsOpen = false; + } + } +} diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor/BrushEditorView.xaml b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor/BrushEditorView.xaml new file mode 100644 index 0000000000..ad30afbf41 --- /dev/null +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/TypeEditors/BrushEditor/BrushEditorView.xaml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +