2 // System.Drawing.Design.ColorEditor.cs
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 // Jonathan Chambers (joncham@gmail.com)
9 // (C) 2003 Martin Willemoes Hansen
10 // (C) 2003 Andreas Nahr
11 // (C) 2006 Jonathan Chambers
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System
.Drawing
.Design
;
38 using System
.ComponentModel
;
39 using System
.Windows
.Forms
;
40 using System
.Windows
.Forms
.Design
;
42 namespace System
.Drawing
.Design
44 public class ColorEditor
: UITypeEditor
46 private IWindowsFormsEditorService editorService
;
47 private Color selected_color
;
48 private bool color_chosen
;
49 private Control editor_control
= null;
55 public override object EditValue (ITypeDescriptorContext context
,
56 IServiceProvider provider
, object value)
58 if (context
!= null && provider
!= null) {
59 editorService
= (IWindowsFormsEditorService
)provider
.GetService(typeof(IWindowsFormsEditorService
));
60 if (editorService
!= null) {
61 if (editor_control
== null)
62 editor_control
= GetEditorControl (value);
63 editorService
.DropDownControl(editor_control
);
65 return selected_color
;
70 return base.EditValue(context
, provider
, value);
73 private Control
GetEditorControl (object value)
75 TabControl tab_control
= new TabControl();
76 tab_control
.Dock
= DockStyle
.Fill
;
77 TabPage custom_tab
= new TabPage("Custom");
78 TabPage web_tab
= new TabPage("Web");
79 TabPage system_tab
= new TabPage("System");
81 ColorListBox web_listbox
= new ColorListBox();
82 ColorListBox system_listbox
= new ColorListBox();
83 web_listbox
.Dock
= DockStyle
.Fill
;
84 system_listbox
.Dock
= DockStyle
.Fill
;
86 web_tab
.Controls
.Add(web_listbox
);
87 system_tab
.Controls
.Add(system_listbox
);
89 SystemColorCompare system_compare
= new SystemColorCompare();
90 System
.Collections
.ArrayList color_list
= new System
.Collections
.ArrayList();
91 foreach (System
.Reflection
.PropertyInfo property
in typeof(SystemColors
).GetProperties(System
.Reflection
.BindingFlags
.Public
|System
.Reflection
.BindingFlags
.Static
)) {
92 Color clr
= (Color
)property
.GetValue(null,null);
95 color_list
.Sort(system_compare
);
96 system_listbox
.Items
.AddRange(color_list
.ToArray());
97 system_listbox
.MouseUp
+=new MouseEventHandler(HandleMouseUp
);
98 system_listbox
.SelectedValueChanged
+=new EventHandler(HandleChange
);
100 WebColorCompare web_compare
= new WebColorCompare();
101 color_list
= new System
.Collections
.ArrayList();
102 foreach (KnownColor known_color
in Enum
.GetValues(typeof(KnownColor
)))
104 Color color
= Color
.FromKnownColor(known_color
);
105 if (color
.IsSystemColor
)
107 color_list
.Add(color
);
109 color_list
.Sort(web_compare
);
110 web_listbox
.Items
.AddRange(color_list
.ToArray());
111 web_listbox
.MouseUp
+=new MouseEventHandler(HandleMouseUp
);
112 web_listbox
.SelectedValueChanged
+=new EventHandler(HandleChange
);
114 CustomColorPicker custom_picker
= new CustomColorPicker ();
115 custom_picker
.Dock
= DockStyle
.Fill
;
116 custom_picker
.ColorChanged
+= new EventHandler (CustomColorPicked
);
117 custom_tab
.Controls
.Add (custom_picker
);
119 tab_control
.TabPages
.Add(custom_tab
);
120 tab_control
.TabPages
.Add(web_tab
);
121 tab_control
.TabPages
.Add(system_tab
);
124 Color current_color
= (Color
)value;
125 if (current_color
.IsSystemColor
)
127 system_listbox
.SelectedValue
= current_color
;
128 tab_control
.SelectedTab
= system_tab
;
130 else if (current_color
.IsKnownColor
)
132 web_listbox
.SelectedValue
= current_color
;
133 tab_control
.SelectedTab
= web_tab
;
135 selected_color
= current_color
;
139 tab_control
.Height
= 216; // the height of the custom colors tab
143 private void HandleChange(object sender
, EventArgs e
)
145 selected_color
= (Color
)((ColorListBox
)sender
).Items
[((ColorListBox
)sender
).SelectedIndex
];
149 private void CustomColorPicked (object sender
, EventArgs e
)
151 selected_color
= (Color
)sender
;
153 if (editorService
!= null)
154 editorService
.CloseDropDown ();
157 private void HandleMouseUp(object sender
, MouseEventArgs e
)
159 if (editorService
!= null)
160 editorService
.CloseDropDown();
163 public override UITypeEditorEditStyle
GetEditStyle (ITypeDescriptorContext context
)
165 return UITypeEditorEditStyle
.DropDown
;
168 public override bool GetPaintValueSupported (ITypeDescriptorContext context
)
173 public override void PaintValue (PaintValueEventArgs e
)
175 Graphics G
= e
.Graphics
;
179 Color C
= (Color
) e
.Value
;
180 using (SolidBrush sb
= new SolidBrush (C
))
181 G
.FillRectangle (sb
, e
.Bounds
);
185 class ColorListBox
: ListBox
{
186 public ColorListBox() {
187 this.DrawMode
= DrawMode
.OwnerDrawFixed
;
189 this.ItemHeight
= 14;
190 this.BorderStyle
= BorderStyle
.FixedSingle
;
193 protected override void OnDrawItem(DrawItemEventArgs e
) {
195 Color color
= (Color
)this.Items
[e
.Index
];
196 using (System
.Drawing
.SolidBrush brush
= new SolidBrush(color
))
197 e
.Graphics
.FillRectangle(brush
, 2,e
.Bounds
.Top
+2,21,9);
198 e
.Graphics
.DrawRectangle(SystemPens
.WindowText
, 2,e
.Bounds
.Top
+2,21,9);
199 e
.Graphics
.DrawString(color
.Name
, this.Font
, SystemBrushes
.WindowText
, 26,e
.Bounds
.Top
);
200 if ((e
.State
& DrawItemState
.Selected
) != 0)
201 e
.DrawFocusRectangle();
206 class SystemColorCompare
: System
.Collections
.IComparer
{
207 #region IComparer Members
209 public int Compare(object x
, object y
) {
212 return String
.Compare(c1
.Name
, c2
.Name
);
217 class WebColorCompare
: System
.Collections
.IComparer
219 #region IComparer Members
221 public int Compare(object x
, object y
)
225 return String
.Compare(c1
.Name
, c2
.Name
);
232 class CustomColorPicker
: UserControl
237 public CustomColorPicker ()
239 colors
= new Color
[8, 8];
240 colors
[0, 0] = Color
.White
;
241 colors
[1, 0] = Color
.FromArgb (224, 224, 224);
242 colors
[2, 0] = Color
.Silver
;
243 colors
[3, 0] = Color
.Gray
;
244 colors
[4, 0] = Color
.FromArgb (64, 64, 64);
245 colors
[5, 0] = Color
.Black
;
246 colors
[6, 0] = Color
.White
;
247 colors
[7, 0] = Color
.White
;
249 colors
[0, 1] = Color
.FromArgb (255, 192, 192);
250 colors
[1, 1] = Color
.FromArgb (255, 128, 128);
251 colors
[2, 1] = Color
.Red
;
252 colors
[3, 1] = Color
.FromArgb (192, 0, 0);
253 colors
[4, 1] = Color
.Maroon
;
254 colors
[5, 1] = Color
.FromArgb (64, 0, 0);
255 colors
[6, 1] = Color
.White
;
256 colors
[7, 1] = Color
.White
;
258 colors
[0, 2] = Color
.FromArgb (255, 224, 192);
259 colors
[1, 2] = Color
.FromArgb (255, 192, 128);
260 colors
[2, 2] = Color
.FromArgb (255, 128, 0);
261 colors
[3, 2] = Color
.FromArgb (192, 64, 0);
262 colors
[4, 2] = Color
.FromArgb (128, 64, 0);
263 colors
[5, 2] = Color
.FromArgb (128, 64, 64);
264 colors
[6, 2] = Color
.White
;
265 colors
[7, 2] = Color
.White
;
267 colors
[0, 3] = Color
.FromArgb (255, 255, 192);
268 colors
[1, 3] = Color
.FromArgb (255, 255, 128);
269 colors
[2, 3] = Color
.Yellow
;
270 colors
[3, 3] = Color
.FromArgb (192, 192, 0);
271 colors
[4, 3] = Color
.Olive
;
272 colors
[5, 3] = Color
.FromArgb (64, 64, 0);
273 colors
[6, 3] = Color
.White
;
274 colors
[7, 3] = Color
.White
;
276 colors
[0, 4] = Color
.FromArgb (192, 255, 192);
277 colors
[1, 4] = Color
.FromArgb (128, 255, 128);
278 colors
[2, 4] = Color
.Lime
;
279 colors
[3, 4] = Color
.FromArgb (0, 192, 0);
280 colors
[4, 4] = Color
.Green
;
281 colors
[5, 4] = Color
.FromArgb (0, 64, 0);
282 colors
[6, 4] = Color
.White
;
283 colors
[7, 4] = Color
.White
;
285 colors
[0, 5] = Color
.FromArgb (192, 255, 255);
286 colors
[1, 5] = Color
.FromArgb (128, 255, 255);
287 colors
[2, 5] = Color
.Cyan
;
288 colors
[3, 5] = Color
.FromArgb (0, 192, 192);
289 colors
[4, 5] = Color
.Teal
;
290 colors
[5, 5] = Color
.FromArgb (0, 64, 64);
291 colors
[6, 5] = Color
.White
;
292 colors
[7, 5] = Color
.White
;
294 colors
[0, 6] = Color
.FromArgb (192, 192, 255);
295 colors
[1, 6] = Color
.FromArgb (128, 128, 255);
296 colors
[2, 6] = Color
.Blue
;
297 colors
[3, 6] = Color
.FromArgb (0, 0, 192);
298 colors
[4, 6] = Color
.Navy
;
299 colors
[5, 6] = Color
.FromArgb (0, 0, 64);
300 colors
[6, 6] = Color
.White
;
301 colors
[7, 6] = Color
.White
;
303 colors
[0, 7] = Color
.FromArgb (255, 192, 255);
304 colors
[1, 7] = Color
.FromArgb (255, 128, 255);
305 colors
[2, 7] = Color
.Fuchsia
;
306 colors
[3, 7] = Color
.FromArgb (192, 0, 192);
307 colors
[4, 7] = Color
.Purple
;
308 colors
[5, 7] = Color
.FromArgb (64, 0, 64);
309 colors
[6, 7] = Color
.White
;
310 colors
[7, 7] = Color
.White
;
313 public event EventHandler ColorChanged
;
315 protected override void OnPaint (PaintEventArgs e
)
317 for (int i
= 0; i
< 8; i
++)
318 for (int j
= 0; j
< 8; j
++) {
319 DrawRect (e
.Graphics
, colors
[i
, j
], j
* 24, i
* 24);
325 ControlPaint
.DrawFocusRectangle (e
.Graphics
, new Rectangle (i
* 24 - 2, j
* 24 - 2, 24, 24));
331 void DrawRect (Graphics g
, Color color
, int x
, int y
)
333 using (SolidBrush brush
= new SolidBrush (color
))
334 g
.FillRectangle (brush
, x
, y
, 20, 20);
335 ControlPaint
.DrawBorder3D (g
, x
, y
, 20, 20);
338 protected override void OnMouseDown (MouseEventArgs e
)
340 if (e
.X
% 24 < 20 && e
.Y
% 24 < 20) {
346 base.OnMouseDown (e
);
349 protected override void OnMouseUp (MouseEventArgs e
)
351 if (highlighting
&& this.ClientRectangle
.Contains (e
.X
, e
.Y
)) {
352 if (ColorChanged
!= null)
353 ColorChanged (colors
[y
/ 24, x
/ 24], EventArgs
.Empty
);
354 highlighting
= false;
359 protected override void OnMouseMove (MouseEventArgs e
)
366 if ((old_x
/ 24 != x
/ 24 || old_y
/ 24 != y
/ 24) &&
367 x
/ 24 < 8 && y
/ 24 < 8) {
368 Region r
= new Region ();
369 r
.Union (new Rectangle (old_x
- 2, old_y
- 2, 24, 24));
370 r
.Union (new Rectangle (x
- 2, y
- 2, 24, 24));
374 base.OnMouseMove (e
);