~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.GtkCore/lib/stetic/libstetic/editor/Flags.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using Gtk;
2
 
using System;
3
 
using System.Collections;
4
 
 
5
 
namespace Stetic.Editor {
6
 
 
7
 
        public class Flags: PropertyEditorCell
8
 
        {
9
 
                protected override string GetValueText ()
10
 
                {
11
 
                        if (Value == null)
12
 
                                return "";
13
 
 
14
 
                        uint value = (uint)(int)Value;
15
 
                        EnumDescriptor enm = Registry.LookupEnum (Property.PropertyType.FullName);
16
 
                        string txt = "";
17
 
                        foreach (Enum val in enm.Values) {
18
 
                                EnumValue eval = enm[val];
19
 
                                if (eval.Label == "")
20
 
                                        continue;
21
 
                                
22
 
                                if ((value & (uint)(int)eval.Value) != 0) {
23
 
                                        if (txt.Length > 0) txt += ", ";
24
 
                                        txt += eval.Label;
25
 
                                }
26
 
                        }
27
 
                        return txt;
28
 
                }
29
 
                
30
 
                protected override IPropertyEditor CreateEditor (Gdk.Rectangle cell_area, Gtk.StateType state)
31
 
                {
32
 
                        return new FlagsEditor ();
33
 
                }
34
 
        }
35
 
        
36
 
        public class FlagsEditor : Gtk.HBox, IPropertyEditor {
37
 
 
38
 
                EnumDescriptor enm;
39
 
                Hashtable flags;
40
 
                Gtk.Tooltips tips;
41
 
                Gtk.Entry flagsLabel;
42
 
                string property;
43
 
 
44
 
                public FlagsEditor ()
45
 
                {
46
 
                }
47
 
                
48
 
                public void Initialize (PropertyDescriptor prop)
49
 
                {
50
 
                        if (!prop.PropertyType.IsEnum)
51
 
                                throw new ApplicationException ("Flags editor does not support editing values of type " + prop.PropertyType);
52
 
                                
53
 
                        property = prop.Label;
54
 
                        Spacing = 3;
55
 
 
56
 
                        // For small enums, the editor is a list of checkboxes inside a frame
57
 
                        // For large enums (>5), use a selector dialog.
58
 
 
59
 
                        enm = Registry.LookupEnum (prop.PropertyType.FullName);
60
 
                        
61
 
                        if (enm.Values.Length < 6) 
62
 
                        {
63
 
                                Gtk.VBox vbox = new Gtk.VBox (true, 3);
64
 
 
65
 
                                tips = new Gtk.Tooltips ();
66
 
                                flags = new Hashtable ();
67
 
 
68
 
                                foreach (Enum value in enm.Values) {
69
 
                                        EnumValue eval = enm[value];
70
 
                                        if (eval.Label == "")
71
 
                                                continue;
72
 
 
73
 
                                        Gtk.CheckButton check = new Gtk.CheckButton (eval.Label);
74
 
                                        tips.SetTip (check, eval.Description, eval.Description);
75
 
                                        uint uintVal = (uint)(int)eval.Value;
76
 
                                        flags[check] = uintVal;
77
 
                                        flags[uintVal] = check;
78
 
                                        
79
 
                                        check.Toggled += FlagToggled;
80
 
                                        vbox.PackStart (check, false, false, 0);
81
 
                                }
82
 
 
83
 
                                Gtk.Frame frame = new Gtk.Frame ();
84
 
                                frame.Add (vbox);
85
 
                                frame.ShowAll ();
86
 
                                PackStart (frame, true, true, 0);
87
 
                        } 
88
 
                        else 
89
 
                        {
90
 
                                flagsLabel = new Gtk.Entry ();
91
 
                                flagsLabel.IsEditable = false;
92
 
                                flagsLabel.HasFrame = false;
93
 
                                flagsLabel.ShowAll ();
94
 
                                PackStart (flagsLabel, true, true, 0);
95
 
                                
96
 
                                Gtk.Button but = new Gtk.Button ("...");
97
 
                                but.Clicked += OnSelectFlags;
98
 
                                but.ShowAll ();
99
 
                                PackStart (but, false, false, 0);
100
 
                        }
101
 
                }
102
 
                
103
 
                public void AttachObject (object ob)
104
 
                {
105
 
                }
106
 
 
107
 
                public override void Dispose ()
108
 
                {
109
 
                        tips.Destroy ();
110
 
                        base.Dispose ();
111
 
                }
112
 
 
113
 
                public object Value {
114
 
                        get {
115
 
                                return Enum.ToObject (enm.EnumType, UIntValue);
116
 
                        }
117
 
                        set {
118
 
                                uint newVal = (uint)(int)value;
119
 
                                if (flagsLabel != null) {
120
 
                                        string txt = "";
121
 
                                        foreach (Enum val in enm.Values) {
122
 
                                                EnumValue eval = enm[val];
123
 
                                                if (eval.Label == "")
124
 
                                                        continue;
125
 
                                                
126
 
                                                if ((newVal & (uint)(int) eval.Value) != 0) {
127
 
                                                        if (txt.Length > 0) txt += ", ";
128
 
                                                        txt += eval.Label;
129
 
                                                }
130
 
                                        }
131
 
                                        flagsLabel.Text = txt;
132
 
                                        UIntValue = newVal;
133
 
                                }
134
 
                                else {
135
 
                                        for (uint i = 1; i <= uintValue || i <= newVal; i = i << 1) {
136
 
                                                if ((uintValue & i) != (newVal & i)) {
137
 
                                                        Gtk.CheckButton check = (Gtk.CheckButton)flags[i];
138
 
                                                        if (check != null)
139
 
                                                                check.Active = !check.Active;
140
 
                                                }
141
 
                                        }
142
 
                                }
143
 
                        }
144
 
                }
145
 
 
146
 
                public event EventHandler ValueChanged;
147
 
 
148
 
                uint uintValue;
149
 
                uint UIntValue {
150
 
                        get {
151
 
                                return uintValue;
152
 
                        }
153
 
                        set {
154
 
                                if (uintValue != value) {
155
 
                                        uintValue = value;
156
 
                                        if (ValueChanged != null)
157
 
                                                ValueChanged (this, EventArgs.Empty);
158
 
                                }
159
 
                        }
160
 
                }
161
 
 
162
 
                void FlagToggled (object o, EventArgs args)
163
 
                {
164
 
                        Gtk.CheckButton check = (Gtk.CheckButton)o;
165
 
                        uint val = (uint)flags[o];
166
 
 
167
 
                        if (check.Active)
168
 
                                UIntValue |= val;
169
 
                        else
170
 
                                UIntValue &= ~val;
171
 
                }
172
 
                
173
 
                void OnSelectFlags (object o, EventArgs args)
174
 
                {
175
 
                        using (FlagsSelectorDialog dialog = new FlagsSelectorDialog (null, enm, UIntValue, property)) {
176
 
                                if (dialog.Run () == (int) ResponseType.Ok) {
177
 
                                        Value = Enum.ToObject (enm.EnumType, dialog.Value);
178
 
                                }
179
 
                        }
180
 
                }
181
 
        }
182
 
}