1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections.Generic;
8
using System.ComponentModel;
9
using ICSharpCode.WpfDesign.PropertyGrid;
10
using System.Windows.Media;
11
using System.Reflection;
14
namespace ICSharpCode.WpfDesign.Designer.PropertyGrid.Editors.BrushEditor
16
public class BrushEditor : INotifyPropertyChanged
20
GradientStopCollection stops = new GradientStopCollection();
21
stops.Add(new GradientStop(Colors.Black, 0));
22
stops.Add(new GradientStop(Colors.White, 1));
24
linearGradientBrush = new LinearGradientBrush(stops);
25
linearGradientBrush.EndPoint = new Point(1, 0);
26
radialGradientBrush = new RadialGradientBrush(stops);
29
public static BrushItem[] SystemBrushes = typeof(SystemColors)
30
.GetProperties(BindingFlags.Static | BindingFlags.Public)
31
.Where(p => p.PropertyType == typeof(SolidColorBrush))
32
.Select(p => new BrushItem() { Name = p.Name, Brush = (Brush)p.GetValue(null, null) })
35
public static BrushItem[] SystemColors = typeof(SystemColors)
36
.GetProperties(BindingFlags.Static | BindingFlags.Public)
37
.Where(p => p.PropertyType == typeof(Color))
38
.Select(p => new BrushItem()
41
Brush = new SolidColorBrush((Color)p.GetValue(null, null))
45
SolidColorBrush solidColorBrush = new SolidColorBrush(Colors.White);
46
LinearGradientBrush linearGradientBrush;
47
RadialGradientBrush radialGradientBrush;
49
PropertyNode property;
51
public PropertyNode Property {
57
if (property != null) {
58
var f = property.Value as Freezable;
59
if (f != null && f.IsFrozen) property.Value = f.Clone();
61
DetermineCurrentKind();
62
RaisePropertyChanged("Property");
63
RaisePropertyChanged("Brush");
69
if (property != null) {
70
return property.Value as Brush;
75
if (property != null && property.Value != value) {
76
property.Value = value;
77
DetermineCurrentKind();
78
RaisePropertyChanged("Brush");
83
void DetermineCurrentKind()
86
CurrentKind = BrushEditorKind.None;
88
else if (Brush is SolidColorBrush) {
89
solidColorBrush = Brush as SolidColorBrush;
90
CurrentKind = BrushEditorKind.Solid;
92
else if (Brush is LinearGradientBrush) {
93
linearGradientBrush = Brush as LinearGradientBrush;
94
radialGradientBrush.GradientStops = linearGradientBrush.GradientStops;
95
CurrentKind = BrushEditorKind.Linear;
97
else if (Brush is RadialGradientBrush) {
98
radialGradientBrush = Brush as RadialGradientBrush;
99
linearGradientBrush.GradientStops = linearGradientBrush.GradientStops;
100
CurrentKind = BrushEditorKind.Radial;
104
BrushEditorKind currentKind;
106
public BrushEditorKind CurrentKind {
112
RaisePropertyChanged("CurrentKind");
114
switch (CurrentKind) {
115
case BrushEditorKind.None:
119
case BrushEditorKind.Solid:
120
Brush = solidColorBrush;
123
case BrushEditorKind.Linear:
124
Brush = linearGradientBrush;
127
case BrushEditorKind.Radial:
128
Brush = radialGradientBrush;
131
case BrushEditorKind.List:
132
Brush = solidColorBrush;
138
public double GradientAngle {
140
var x = linearGradientBrush.EndPoint.X - linearGradientBrush.StartPoint.X;
141
var y = linearGradientBrush.EndPoint.Y - linearGradientBrush.StartPoint.Y;
142
return Vector.AngleBetween(new Vector(1, 0), new Vector(x, -y));
145
var d = value * Math.PI / 180;
146
var p = new Point(Math.Cos(d), -Math.Sin(d));
147
var k = 1 / Math.Max(Math.Abs(p.X), Math.Abs(p.Y));
150
var p2 = new Point(-p.X, -p.Y);
151
linearGradientBrush.StartPoint = new Point((p2.X + 1) / 2, (p2.Y + 1) / 2);
152
linearGradientBrush.EndPoint = new Point((p.X + 1) / 2, (p.Y + 1) / 2);
153
RaisePropertyChanged("GradientAngle");
157
public IEnumerable<BrushItem> AvailableColors {
158
get { return SystemColors; }
161
public IEnumerable<BrushItem> AvailableBrushes {
162
get { return SystemBrushes; }
165
public void MakeGradientHorizontal()
170
public void MakeGradientVertical()
178
Property.Value = Brush.Clone();
182
#region INotifyPropertyChanged Members
184
public event PropertyChangedEventHandler PropertyChanged;
186
void RaisePropertyChanged(string name)
188
if (PropertyChanged != null) {
189
PropertyChanged(this, new PropertyChangedEventArgs(name));
196
public enum BrushEditorKind
205
public class BrushItem
207
public string Name { get; set; }
208
public Brush Brush { get; set; }