7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
31
using System.Collections;
32
using System.ComponentModel;
34
namespace MonoDevelop.Components.PropertyGrid.PropertyEditors {
36
public class FlagsEditorCell: PropertyEditorCell
38
protected override string GetValueText ()
43
ulong value = Convert.ToUInt64 (Value);
44
Array values = System.Enum.GetValues (base.Property.PropertyType);
46
foreach (object val in values) {
47
if ((value & Convert.ToUInt64 (value)) != 0) {
48
if (txt.Length > 0) txt += ", ";
49
txt += val.ToString ();
55
protected override IPropertyEditor CreateEditor (Gdk.Rectangle cell_area, Gtk.StateType state)
57
return new FlagsEditor ();
61
public class FlagsEditor : Gtk.HBox, IPropertyEditor
73
public void Initialize (EditSession session)
75
PropertyDescriptor prop = session.Property;
77
if (!prop.PropertyType.IsEnum)
78
throw new ApplicationException ("Flags editor does not support editing values of type " + prop.PropertyType);
81
propType = prop.PropertyType;
83
property = prop.Description;
84
if (property == null || property.Length == 0)
87
// For small enums, the editor is a list of checkboxes inside a frame
88
// For large enums (>5), use a selector dialog.
90
values = System.Enum.GetValues (prop.PropertyType);
92
if (values.Length < 6)
94
Gtk.VBox vbox = new Gtk.VBox (true, 3);
96
flags = new Hashtable ();
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;
105
check.Toggled += FlagToggled;
106
vbox.PackStart (check, false, false, 0);
109
Gtk.Frame frame = new Gtk.Frame ();
112
PackStart (frame, true, true, 0);
116
flagsLabel = new Gtk.Entry ();
117
flagsLabel.IsEditable = false;
118
flagsLabel.HasFrame = false;
119
flagsLabel.ShowAll ();
120
PackStart (flagsLabel, true, true, 0);
122
Gtk.Button but = new Gtk.Button ("...");
123
but.Clicked += OnSelectFlags;
125
PackStart (but, false, false, 0);
129
protected override void OnDestroyed ()
132
((IDisposable)this).Dispose ();
135
void IDisposable.Dispose ()
139
public object Value {
141
return Enum.ToObject (propType, UIntValue);
144
ulong newVal = Convert.ToUInt64 (value);
145
if (flagsLabel != null) {
147
foreach (object val in values) {
148
if ((newVal & Convert.ToUInt64(val)) != 0) {
149
if (txt.Length > 0) txt += ", ";
150
txt += val.ToString ();
153
flagsLabel.Text = txt;
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];
161
check.Active = !check.Active;
168
public event EventHandler ValueChanged;
177
if (uintValue != value) {
179
if (ValueChanged != null)
180
ValueChanged (this, EventArgs.Empty);
185
void FlagToggled (object o, EventArgs args)
187
Gtk.CheckButton check = (Gtk.CheckButton)o;
188
ulong val = (ulong)flags[o];
196
void OnSelectFlags (object o, EventArgs args)
198
using (FlagsSelectorDialog dialog = new FlagsSelectorDialog (null, propType, UIntValue, property)) {
199
if (dialog.Run () == (int) ResponseType.Ok) {
200
Value = Enum.ToObject (propType, dialog.Value);