~ubuntu-branches/ubuntu/saucy/monodevelop/saucy-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components.PropertyGrid.Editors/ColorEditorCell.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-09-10 16:54:48 UTC
  • mfrom: (19.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100910165448-0rybfk25zd4o9431
Tags: 2.4+dfsg-2
* debian/patches/inject_Mono.Debugger.Soft_source.patch,
  debian/patches/use_system_Mono.Debugger.Soft.patch,
  debian/control:
  + Build against system Soft Debugger, since we now have a new
    enough Mono to match MonoDevelop's required API

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// BooleanEditorCell.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//   Michael Hutchinson <m.j.hutchinson@gmail.com>
 
7
//
 
8
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
9
//
 
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:
 
17
// 
 
18
// The above copyright notice and this permission notice shall be
 
19
// included in all copies or substantial portions of the Software.
 
20
// 
 
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.
 
28
//
 
29
 
 
30
using System;
 
31
using System.ComponentModel;
 
32
 
 
33
namespace MonoDevelop.Components.PropertyGrid.PropertyEditors
 
34
{
 
35
        [PropertyEditorType (typeof (System.Drawing.Color))]
 
36
        public class ColorEditorCell: PropertyEditorCell 
 
37
        {
 
38
                const int ColorBoxSize = 16;
 
39
                const int ColorBoxSpacing = 3;
 
40
                
 
41
                public override void GetSize (int availableWidth, out int width, out int height)
 
42
                {
 
43
                        base.GetSize (availableWidth - ColorBoxSize - ColorBoxSpacing, out width, out height);
 
44
                        width += ColorBoxSize + ColorBoxSpacing;
 
45
                        if (height < ColorBoxSize) height = ColorBoxSize;
 
46
                }
 
47
                
 
48
                protected override string GetValueText ()
 
49
                {
 
50
                        System.Drawing.Color color = (System.Drawing.Color) Value;
 
51
                        //TODO: dropdown known color selector so this does something
 
52
                        if (color.IsKnownColor)
 
53
                                return color.Name;
 
54
                        else if (color.IsEmpty)
 
55
                                return "";
 
56
                        else
 
57
                                return String.Format("#{0:x2}{1:x2}{2:x2}", color.R, color.G, color.B);
 
58
                }
 
59
                
 
60
                public override void Render (Gdk.Drawable window, Gdk.Rectangle bounds, Gtk.StateType state)
 
61
                {
 
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);
 
70
                }
 
71
                
 
72
                private Gdk.Color GetColor ()
 
73
                {
 
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);
 
77
                }
 
78
 
 
79
                protected override IPropertyEditor CreateEditor (Gdk.Rectangle cell_area, Gtk.StateType state)
 
80
                {
 
81
                        return new ColorEditor ();
 
82
                }
 
83
        }
 
84
        
 
85
        public class ColorEditor : Gtk.ColorButton, IPropertyEditor
 
86
        {
 
87
                public void Initialize (EditSession session)
 
88
                {
 
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);
 
91
                }
 
92
                
 
93
                public object Value { 
 
94
                        get {
 
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);
 
99
                        }
 
100
                        set {
 
101
                                System.Drawing.Color color = (System.Drawing.Color) value;
 
102
                                Color = new Gdk.Color (color.R, color.G, color.B);
 
103
                        }
 
104
                }
 
105
                
 
106
                protected override void OnColorSet ()
 
107
                {
 
108
                        base.OnColorSet ();
 
109
                        if (ValueChanged != null)
 
110
                                ValueChanged (this, EventArgs.Empty);
 
111
                }
 
112
 
 
113
                public event EventHandler ValueChanged;
 
114
        }
 
115
}