2
// BooleanEditorCell.cs
6
// Michael Hutchinson <m.j.hutchinson@gmail.com>
8
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
10
// Permission is hereby granted, free of charge, to any person obtaining
11
// a copy of this software and associated documentation files (the
12
// "Software"), to deal in the Software without restriction, including
13
// without limitation the rights to use, copy, modify, merge, publish,
14
// distribute, sublicense, and/or sell copies of the Software, and to
15
// permit persons to whom the Software is furnished to do so, subject to
16
// the following conditions:
18
// The above copyright notice and this permission notice shall be
19
// included in all copies or substantial portions of the Software.
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
using System.ComponentModel;
33
namespace MonoDevelop.Components.PropertyGrid.PropertyEditors
35
[PropertyEditorType (typeof (System.Drawing.Color))]
36
public class ColorEditorCell: PropertyEditorCell
38
const int ColorBoxSize = 16;
39
const int ColorBoxSpacing = 3;
41
public override void GetSize (int availableWidth, out int width, out int height)
43
base.GetSize (availableWidth - ColorBoxSize - ColorBoxSpacing, out width, out height);
44
width += ColorBoxSize + ColorBoxSpacing;
45
if (height < ColorBoxSize) height = ColorBoxSize;
48
protected override string GetValueText ()
50
System.Drawing.Color color = (System.Drawing.Color) Value;
51
//TODO: dropdown known color selector so this does something
52
if (color.IsKnownColor)
54
else if (color.IsEmpty)
57
return String.Format("#{0:x2}{1:x2}{2:x2}", color.R, color.G, color.B);
60
public override void Render (Gdk.Drawable window, Gdk.Rectangle bounds, Gtk.StateType state)
62
Gdk.GC gc = new Gdk.GC (window);
63
gc.RgbFgColor = GetColor ();
64
int yd = (bounds.Height - ColorBoxSize) / 2;
65
window.DrawRectangle (gc, true, bounds.X, bounds.Y + yd, ColorBoxSize - 1, ColorBoxSize - 1);
66
window.DrawRectangle (Container.Style.BlackGC, false, bounds.X, bounds.Y + yd, ColorBoxSize - 1, ColorBoxSize - 1);
67
bounds.X += ColorBoxSize + ColorBoxSpacing;
68
bounds.Width -= ColorBoxSize + ColorBoxSpacing;
69
base.Render (window, bounds, state);
72
private Gdk.Color GetColor ()
74
System.Drawing.Color color = (System.Drawing.Color) Value;
75
//TODO: Property.Converter.ConvertTo() fails: why?
76
return new Gdk.Color (color.R, color.G, color.B);
79
protected override IPropertyEditor CreateEditor (Gdk.Rectangle cell_area, Gtk.StateType state)
81
return new ColorEditor ();
85
public class ColorEditor : Gtk.ColorButton, IPropertyEditor
87
public void Initialize (EditSession session)
89
if (session.Property.PropertyType != typeof(System.Drawing.Color))
90
throw new ApplicationException ("Color editor does not support editing values of type " + session.Property.PropertyType);
95
int red = (int) (255 * (float) Color.Red / ushort.MaxValue);
96
int green = (int) (255 * (float) Color.Green / ushort.MaxValue);
97
int blue = (int) (255 * (float) Color.Blue / ushort.MaxValue);
98
return System.Drawing.Color.FromArgb (red, green, blue);
101
System.Drawing.Color color = (System.Drawing.Color) value;
102
Color = new Gdk.Color (color.R, color.G, color.B);
106
protected override void OnColorSet ()
109
if (ValueChanged != null)
110
ValueChanged (this, EventArgs.Empty);
113
public event EventHandler ValueChanged;