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

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Components/MonoDevelop.Components.PropertyGrid.Editors/FlagsEditorCell.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
 
// FlagsEditorCell.cs
3
 
//
4
 
// Author:
5
 
//   Lluis Sanchez Gual
6
 
//
7
 
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
8
 
//
9
 
// Permission is hereby granted, free of charge, to any person obtaining
10
 
// a copy of this software and associated documentation files (the
11
 
// "Software"), to deal in the Software without restriction, including
12
 
// without limitation the rights to use, copy, modify, merge, publish,
13
 
// distribute, sublicense, and/or sell copies of the Software, and to
14
 
// permit persons to whom the Software is furnished to do so, subject to
15
 
// the following conditions:
16
 
// 
17
 
// The above copyright notice and this permission notice shall be
18
 
// included in all copies or substantial portions of the Software.
19
 
// 
20
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
 
//
28
 
 
29
 
using Gtk;
30
 
using System;
31
 
using System.Collections;
32
 
using System.ComponentModel;
33
 
 
34
 
namespace MonoDevelop.Components.PropertyGrid.PropertyEditors {
35
 
 
36
 
        public class FlagsEditorCell: PropertyEditorCell
37
 
        {
38
 
                protected override string GetValueText ()
39
 
                {
40
 
                        if (Value == null)
41
 
                                return "";
42
 
 
43
 
                        ulong value = Convert.ToUInt64 (Value);
44
 
                        Array values = System.Enum.GetValues (base.Property.PropertyType);
45
 
                        string txt = "";
46
 
                        foreach (object val in values) {
47
 
                                if ((value & Convert.ToUInt64 (value)) != 0) {
48
 
                                        if (txt.Length > 0) txt += ", ";
49
 
                                        txt += val.ToString ();
50
 
                                }
51
 
                        }
52
 
                        return txt;
53
 
                }
54
 
                
55
 
                protected override IPropertyEditor CreateEditor (Gdk.Rectangle cell_area, Gtk.StateType state)
56
 
                {
57
 
                        return new FlagsEditor ();
58
 
                }
59
 
        }
60
 
        
61
 
        public class FlagsEditor : Gtk.HBox, IPropertyEditor
62
 
        {
63
 
                Hashtable flags;
64
 
                Gtk.Entry flagsLabel;
65
 
                string property;
66
 
                Type propType;
67
 
                Array values;
68
 
 
69
 
                public FlagsEditor ()
70
 
                {
71
 
                }
72
 
                
73
 
                public void Initialize (EditSession session)
74
 
                {
75
 
                        PropertyDescriptor prop = session.Property;
76
 
                        
77
 
                        if (!prop.PropertyType.IsEnum)
78
 
                                throw new ApplicationException ("Flags editor does not support editing values of type " + prop.PropertyType);
79
 
                        
80
 
                        Spacing = 3;
81
 
                        propType = prop.PropertyType;
82
 
                        
83
 
                        property = prop.Description;
84
 
                        if (property == null || property.Length == 0)
85
 
                                property = prop.Name;
86
 
 
87
 
                        // For small enums, the editor is a list of checkboxes inside a frame
88
 
                        // For large enums (>5), use a selector dialog.
89
 
 
90
 
                        values = System.Enum.GetValues (prop.PropertyType);
91
 
                        
92
 
                        if (values.Length < 6) 
93
 
                        {
94
 
                                Gtk.VBox vbox = new Gtk.VBox (true, 3);
95
 
 
96
 
                                flags = new Hashtable ();
97
 
 
98
 
                                foreach (object value in values) {
99
 
                                        Gtk.CheckButton check = new Gtk.CheckButton (value.ToString ());
100
 
                                        check.TooltipText = value.ToString ();
101
 
                                        ulong uintVal = Convert.ToUInt64 (value);
102
 
                                        flags[check] = uintVal;
103
 
                                        flags[uintVal] = check;
104
 
                                        
105
 
                                        check.Toggled += FlagToggled;
106
 
                                        vbox.PackStart (check, false, false, 0);
107
 
                                }
108
 
 
109
 
                                Gtk.Frame frame = new Gtk.Frame ();
110
 
                                frame.Add (vbox);
111
 
                                frame.ShowAll ();
112
 
                                PackStart (frame, true, true, 0);
113
 
                        } 
114
 
                        else 
115
 
                        {
116
 
                                flagsLabel = new Gtk.Entry ();
117
 
                                flagsLabel.IsEditable = false;
118
 
                                flagsLabel.HasFrame = false;
119
 
                                flagsLabel.ShowAll ();
120
 
                                PackStart (flagsLabel, true, true, 0);
121
 
                                
122
 
                                Gtk.Button but = new Gtk.Button ("...");
123
 
                                but.Clicked += OnSelectFlags;
124
 
                                but.ShowAll ();
125
 
                                PackStart (but, false, false, 0);
126
 
                        }
127
 
                }
128
 
                
129
 
                protected override void OnDestroyed ()
130
 
                {
131
 
                        base.OnDestroyed ();
132
 
                        ((IDisposable)this).Dispose ();
133
 
                }
134
 
 
135
 
                void IDisposable.Dispose ()
136
 
                {
137
 
                }
138
 
 
139
 
                public object Value {
140
 
                        get {
141
 
                                return Enum.ToObject (propType, UIntValue);
142
 
                        }
143
 
                        set {
144
 
                                ulong newVal = Convert.ToUInt64 (value);
145
 
                                if (flagsLabel != null) {
146
 
                                        string txt = "";
147
 
                                        foreach (object val in values) {
148
 
                                                if ((newVal & Convert.ToUInt64(val)) != 0) {
149
 
                                                        if (txt.Length > 0) txt += ", ";
150
 
                                                        txt += val.ToString ();
151
 
                                                }
152
 
                                        }
153
 
                                        flagsLabel.Text = txt;
154
 
                                        UIntValue = newVal;
155
 
                                }
156
 
                                else {
157
 
                                        for (ulong i = 1; i <= uintValue || i <= newVal; i = i << 1) {
158
 
                                                if ((uintValue & i) != (newVal & i)) {
159
 
                                                        Gtk.CheckButton check = (Gtk.CheckButton)flags[i];
160
 
                                                        if (check != null)
161
 
                                                                check.Active = !check.Active;
162
 
                                                }
163
 
                                        }
164
 
                                }
165
 
                        }
166
 
                }
167
 
 
168
 
                public event EventHandler ValueChanged;
169
 
 
170
 
                ulong uintValue;
171
 
                
172
 
                ulong UIntValue {
173
 
                        get {
174
 
                                return uintValue;
175
 
                        }
176
 
                        set {
177
 
                                if (uintValue != value) {
178
 
                                        uintValue = value;
179
 
                                        if (ValueChanged != null)
180
 
                                                ValueChanged (this, EventArgs.Empty);
181
 
                                }
182
 
                        }
183
 
                }
184
 
 
185
 
                void FlagToggled (object o, EventArgs args)
186
 
                {
187
 
                        Gtk.CheckButton check = (Gtk.CheckButton)o;
188
 
                        ulong val = (ulong)flags[o];
189
 
 
190
 
                        if (check.Active)
191
 
                                UIntValue |= val;
192
 
                        else
193
 
                                UIntValue &= ~val;
194
 
                }
195
 
                
196
 
                void OnSelectFlags (object o, EventArgs args)
197
 
                {
198
 
                        using (FlagsSelectorDialog dialog = new FlagsSelectorDialog (null, propType, UIntValue, property)) {
199
 
                                if (dialog.Run () == (int) ResponseType.Ok) {
200
 
                                        Value = Enum.ToObject (propType, dialog.Value);
201
 
                                }
202
 
                        }
203
 
                }
204
 
        }
205
 
}