~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/PropertyGrid/Editors/BrushEditor/BrushEditor.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Linq;
 
7
using System.Text;
 
8
using System.ComponentModel;
 
9
using ICSharpCode.WpfDesign.PropertyGrid;
 
10
using System.Windows.Media;
 
11
using System.Reflection;
 
12
using System.Windows;
 
13
 
 
14
namespace ICSharpCode.WpfDesign.Designer.PropertyGrid.Editors.BrushEditor
 
15
{
 
16
        public class BrushEditor : INotifyPropertyChanged
 
17
        {
 
18
                public BrushEditor()
 
19
                {
 
20
                        GradientStopCollection stops = new GradientStopCollection();
 
21
                        stops.Add(new GradientStop(Colors.Black, 0));
 
22
                        stops.Add(new GradientStop(Colors.White, 1));
 
23
 
 
24
                        linearGradientBrush = new LinearGradientBrush(stops);
 
25
                        linearGradientBrush.EndPoint = new Point(1, 0);
 
26
                        radialGradientBrush = new RadialGradientBrush(stops);
 
27
                }
 
28
 
 
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) })
 
33
                        .ToArray();
 
34
 
 
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()
 
39
                        {
 
40
                        Name = p.Name,
 
41
                        Brush = new SolidColorBrush((Color)p.GetValue(null, null))
 
42
                        })
 
43
                        .ToArray();
 
44
 
 
45
                SolidColorBrush solidColorBrush = new SolidColorBrush(Colors.White);
 
46
                LinearGradientBrush linearGradientBrush;
 
47
                RadialGradientBrush radialGradientBrush;
 
48
 
 
49
                PropertyNode property;
 
50
 
 
51
                public PropertyNode Property {
 
52
                        get {
 
53
                                return property;
 
54
                        }
 
55
                        set {
 
56
                                property = value;
 
57
                                if (property != null) {
 
58
                                        var f = property.Value as Freezable;
 
59
                                        if (f != null && f.IsFrozen) property.Value = f.Clone();
 
60
                                }
 
61
                                DetermineCurrentKind();
 
62
                                RaisePropertyChanged("Property");
 
63
                                RaisePropertyChanged("Brush");
 
64
                        }
 
65
                }
 
66
 
 
67
                public Brush Brush {
 
68
                        get {
 
69
                                if (property != null) {
 
70
                                        return property.Value as Brush;
 
71
                                }
 
72
                                return null;
 
73
                        }
 
74
                        set {
 
75
                                if (property != null && property.Value != value) {
 
76
                                        property.Value = value;
 
77
                                        DetermineCurrentKind();
 
78
                                        RaisePropertyChanged("Brush");
 
79
                                }
 
80
                        }
 
81
                }
 
82
 
 
83
                void DetermineCurrentKind()
 
84
                {
 
85
                        if (Brush == null) {
 
86
                                CurrentKind = BrushEditorKind.None;
 
87
                        }
 
88
                        else if (Brush is SolidColorBrush) {
 
89
                                solidColorBrush = Brush as SolidColorBrush;
 
90
                                CurrentKind = BrushEditorKind.Solid;
 
91
                        }
 
92
                        else if (Brush is LinearGradientBrush) {
 
93
                                linearGradientBrush = Brush as LinearGradientBrush;
 
94
                                radialGradientBrush.GradientStops = linearGradientBrush.GradientStops;
 
95
                                CurrentKind = BrushEditorKind.Linear;
 
96
                        }
 
97
                        else if (Brush is RadialGradientBrush) {
 
98
                                radialGradientBrush = Brush as RadialGradientBrush;
 
99
                                linearGradientBrush.GradientStops = linearGradientBrush.GradientStops;
 
100
                                CurrentKind = BrushEditorKind.Radial;
 
101
                        }
 
102
                }
 
103
 
 
104
                BrushEditorKind currentKind;
 
105
 
 
106
                public BrushEditorKind CurrentKind {
 
107
                        get {
 
108
                                return currentKind;
 
109
                        }
 
110
                        set {
 
111
                                currentKind = value;
 
112
                                RaisePropertyChanged("CurrentKind");
 
113
 
 
114
                                switch (CurrentKind) {
 
115
                                        case BrushEditorKind.None:
 
116
                                                Brush = null;
 
117
                                                break;
 
118
 
 
119
                                        case BrushEditorKind.Solid:
 
120
                                                Brush = solidColorBrush;
 
121
                                                break;
 
122
 
 
123
                                        case BrushEditorKind.Linear:
 
124
                                                Brush = linearGradientBrush;
 
125
                                                break;
 
126
 
 
127
                                        case BrushEditorKind.Radial:
 
128
                                                Brush = radialGradientBrush;
 
129
                                                break;
 
130
 
 
131
                                        case BrushEditorKind.List:
 
132
                                                Brush = solidColorBrush;
 
133
                                                break;
 
134
                                }
 
135
                        }
 
136
                }
 
137
 
 
138
                public double GradientAngle {
 
139
                        get {
 
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));
 
143
                        }
 
144
                        set {
 
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));
 
148
                                p.X *= k;
 
149
                                p.Y *= k;
 
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");
 
154
                        }
 
155
                }
 
156
 
 
157
                public IEnumerable<BrushItem> AvailableColors {
 
158
                        get { return SystemColors; }
 
159
                }
 
160
 
 
161
                public IEnumerable<BrushItem> AvailableBrushes {
 
162
                        get { return SystemBrushes; }
 
163
                }
 
164
 
 
165
                public void MakeGradientHorizontal()
 
166
                {
 
167
                        GradientAngle = 0;
 
168
                }
 
169
 
 
170
                public void MakeGradientVertical()
 
171
                {
 
172
                        GradientAngle = -90;
 
173
                }
 
174
 
 
175
                public void Commit()
 
176
                {
 
177
                        if (Brush != null) {
 
178
                                Property.Value = Brush.Clone();
 
179
                        }
 
180
                }
 
181
 
 
182
                #region INotifyPropertyChanged Members
 
183
 
 
184
                public event PropertyChangedEventHandler PropertyChanged;
 
185
 
 
186
                void RaisePropertyChanged(string name)
 
187
                {
 
188
                        if (PropertyChanged != null) {
 
189
                                PropertyChanged(this, new PropertyChangedEventArgs(name));
 
190
                        }
 
191
                }
 
192
 
 
193
                #endregion
 
194
        }
 
195
 
 
196
        public enum BrushEditorKind
 
197
        {
 
198
                None,
 
199
                Solid,
 
200
                Linear,
 
201
                Radial,
 
202
                List
 
203
        }
 
204
 
 
205
        public class BrushItem
 
206
        {
 
207
                public string Name { get; set; }
 
208
                public Brush Brush { get; set; }
 
209
        }
 
210
}