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

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.GtkCore/lib/stetic/libstetic/ClassDescriptor.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 System;
2
 
using System.Collections;
3
 
using System.Reflection;
4
 
using System.Runtime.InteropServices;
5
 
using System.Xml;
6
 
 
7
 
namespace Stetic {
8
 
 
9
 
        
10
 
        public abstract class ClassDescriptor
11
 
        {
12
 
                protected string label, category, cname;
13
 
                protected bool deprecated, hexpandable, vexpandable, allowChildren = true;
14
 
                
15
 
                protected ItemGroupCollection groups = new ItemGroupCollection ();
16
 
                protected ItemGroupCollection signals = new ItemGroupCollection ();
17
 
 
18
 
                protected int importantGroups;
19
 
                protected ItemGroup contextMenu;
20
 
                protected ItemGroup internalChildren;
21
 
                protected string baseType;
22
 
                protected bool isInternal;
23
 
                protected string targetGtkVersion;
24
 
                
25
 
                WidgetLibrary library;
26
 
                PropertyDescriptor[] initializationProperties;
27
 
                static PropertyDescriptor[] emptyPropArray = new PropertyDescriptor[0];
28
 
 
29
 
                int counter;
30
 
 
31
 
                protected void Load (XmlElement elem)
32
 
                {
33
 
                        if (elem.HasAttribute ("cname"))
34
 
                                cname = elem.GetAttribute ("cname");
35
 
                        else if (cname == null)
36
 
                                cname = elem.GetAttribute ("type");
37
 
 
38
 
                        label = elem.GetAttribute ("label");
39
 
                        if (label == "") {
40
 
                                label = WrappedTypeName;
41
 
                                int i = label.LastIndexOf ('.');
42
 
                                if (i != -1)
43
 
                                        label = label.Substring (i+1);
44
 
                        }
45
 
                        
46
 
                        if (elem.HasAttribute ("allow-children"))
47
 
                                allowChildren = elem.GetAttribute ("allow-children") == "yes" || elem.GetAttribute ("allow-children") == "true";
48
 
                                
49
 
                        category = elem.GetAttribute ("palette-category");
50
 
 
51
 
                        if (elem.HasAttribute ("deprecated"))
52
 
                                deprecated = true;
53
 
                        if (elem.HasAttribute ("hexpandable"))
54
 
                                hexpandable = true;
55
 
                        if (elem.HasAttribute ("vexpandable"))
56
 
                                vexpandable = true;
57
 
                        if (elem.GetAttribute ("internal") == "true")
58
 
                                isInternal = true;
59
 
                        
60
 
                        contextMenu = ItemGroup.Empty;
61
 
                        
62
 
                        baseType = elem.GetAttribute ("base-type");
63
 
                        if (baseType.Length > 0) {
64
 
                                ClassDescriptor basec = Registry.LookupClassByName (baseType);
65
 
                                if (basec == null)
66
 
                                        throw new InvalidOperationException ("Base type '" + baseType + "' not found.");
67
 
                                foreach (ItemGroup group in basec.ItemGroups)
68
 
                                        groups.Add (group);
69
 
                                foreach (ItemGroup group in basec.SignalGroups)
70
 
                                        signals.Add (group);
71
 
                                contextMenu = basec.ContextMenu;
72
 
                        } else
73
 
                                baseType = null;
74
 
 
75
 
                        XmlElement groupsElem = elem["itemgroups"];
76
 
                        if (groupsElem != null) {
77
 
                                foreach (XmlElement groupElem in groupsElem.SelectNodes ("itemgroup")) {
78
 
                                        ItemGroup itemgroup;
79
 
 
80
 
                                        if (groupElem.HasAttribute ("ref")) {
81
 
                                                string refname = groupElem.GetAttribute ("ref");
82
 
                                                itemgroup = Registry.LookupItemGroup (refname);
83
 
                                        } else
84
 
                                                itemgroup = new ItemGroup (groupElem, this);
85
 
                                        groups.Add (itemgroup);
86
 
 
87
 
                                        if (groupElem.HasAttribute ("important")) {
88
 
                                                if (groupElem.GetAttribute ("important") == "true")
89
 
                                                        importantGroups++;
90
 
                                        } else if (groups.Count == 1)
91
 
                                                importantGroups++;
92
 
                                }
93
 
                        }
94
 
 
95
 
                        XmlElement signalsElem = elem["signals"];
96
 
                        if (signalsElem != null) {
97
 
                                foreach (XmlElement groupElem in signalsElem.SelectNodes ("itemgroup")) {
98
 
                                        ItemGroup itemgroup;
99
 
                                        if (groupElem.HasAttribute ("ref")) {
100
 
                                                string refname = groupElem.GetAttribute ("ref");
101
 
                                                itemgroup = Registry.LookupSignalGroup (refname);
102
 
                                        } else
103
 
                                                itemgroup = new ItemGroup (groupElem, this);
104
 
                                        signals.Add (itemgroup);
105
 
                                }
106
 
                        }
107
 
 
108
 
                        XmlElement contextElem = elem["contextmenu"];
109
 
                        if (contextElem != null) {
110
 
                                if (contextElem.HasAttribute ("ref")) {
111
 
                                        string refname = contextElem.GetAttribute ("ref");
112
 
                                        contextMenu = Registry.LookupContextMenu (refname);
113
 
                                } else
114
 
                                        contextMenu = new ItemGroup (contextElem, this);
115
 
                        }
116
 
 
117
 
                        XmlElement ichildElem = elem["internal-children"];
118
 
                        if (ichildElem != null)
119
 
                                internalChildren = new ItemGroup (ichildElem, this);
120
 
                        else
121
 
                                internalChildren = ItemGroup.Empty;
122
 
 
123
 
                        string initProps = elem.GetAttribute ("init-properties");
124
 
                        if (initProps.Length > 0) {
125
 
                                string[] props = initProps.Split (' ');
126
 
                                ArrayList list = new ArrayList ();
127
 
                                foreach (string prop in props) {
128
 
                                        PropertyDescriptor idesc = this [prop] as PropertyDescriptor;
129
 
                                        if (idesc == null)
130
 
                                                throw new InvalidOperationException ("Initialization property not found: " + prop);
131
 
                                        list.Add (idesc);
132
 
                                }
133
 
                                initializationProperties = (PropertyDescriptor[]) list.ToArray (typeof(PropertyDescriptor));
134
 
                        } else
135
 
                                initializationProperties = emptyPropArray;
136
 
                        
137
 
                        targetGtkVersion = elem.GetAttribute ("gtk-version");
138
 
                        if (targetGtkVersion.Length == 0)
139
 
                                targetGtkVersion = null;
140
 
                }
141
 
                
142
 
                public virtual string Name {
143
 
                        get {
144
 
                                return WrappedTypeName;
145
 
                        }
146
 
                }
147
 
                
148
 
                public virtual bool IsInternal {
149
 
                        get { return isInternal; }
150
 
                }
151
 
 
152
 
                public abstract string WrappedTypeName {
153
 
                        get;
154
 
                }
155
 
 
156
 
                public string CName {
157
 
                        get {
158
 
                                return cname;
159
 
                        }
160
 
                }
161
 
 
162
 
                public bool Deprecated {
163
 
                        get {
164
 
                                return deprecated;
165
 
                        }
166
 
                }
167
 
 
168
 
                public bool HExpandable {
169
 
                        get {
170
 
                                return hexpandable;
171
 
                        }
172
 
                }
173
 
 
174
 
                public bool VExpandable {
175
 
                        get {
176
 
                                return vexpandable;
177
 
                        }
178
 
                }
179
 
 
180
 
                public string Label {
181
 
                        get {
182
 
                                return label;
183
 
                        }
184
 
                }
185
 
 
186
 
                public abstract Gdk.Pixbuf Icon {
187
 
                        get;
188
 
                }
189
 
 
190
 
                public string Category {
191
 
                        get {
192
 
                                return category;
193
 
                        }
194
 
                }
195
 
                
196
 
                public virtual string TargetGtkVersion {
197
 
                        get {
198
 
                                if (targetGtkVersion == null)
199
 
                                        return library.TargetGtkVersion;
200
 
                                else
201
 
                                        return targetGtkVersion; 
202
 
                        }
203
 
                }
204
 
                
205
 
                public bool SupportsGtkVersion (string targetVersion)
206
 
                {
207
 
                        return WidgetUtils.CompareVersions (TargetGtkVersion, targetVersion) >= 0;
208
 
                }
209
 
 
210
 
                public PropertyDescriptor[] InitializationProperties {
211
 
                        get { return initializationProperties; }
212
 
                }
213
 
                
214
 
                public object NewInstance (IProject proj)
215
 
                {
216
 
                        return NewInstance (proj, true);
217
 
                }
218
 
                
219
 
                public object NewInstance (IProject proj, bool initialize)
220
 
                {
221
 
                        object ob = CreateInstance (proj);
222
 
                        
223
 
                        string name = WrappedTypeName.ToLower () + (++counter).ToString ();
224
 
                        int i = name.LastIndexOf ('.');
225
 
                        if (i != -1) {
226
 
                                if (i < name.Length)
227
 
                                        name = name.Substring (i+1);
228
 
                                else
229
 
                                        name = name.Replace (".", "");
230
 
                        }
231
 
                        
232
 
                        foreach (ItemGroup group in groups) {
233
 
                                foreach (ItemDescriptor item in group) {
234
 
                                        PropertyDescriptor prop = item as PropertyDescriptor;
235
 
                                        if (prop != null && prop.InitWithName)
236
 
                                                prop.SetValue (ob, name);
237
 
                                }
238
 
                        }
239
 
                        
240
 
                        ObjectWrapper ow = CreateWrapper ();
241
 
                        ObjectWrapper.Bind (proj, this, ow, ob, !initialize);
242
 
                        return ob;
243
 
                }
244
 
                
245
 
                // Sets the default values for an instance
246
 
                public virtual void ResetInstance (object obj)
247
 
                {
248
 
                        foreach (ItemGroup group in groups) {
249
 
                                foreach (ItemDescriptor item in group) {
250
 
                                        PropertyDescriptor prop = item as PropertyDescriptor;
251
 
                                        if (prop != null)
252
 
                                                prop.ResetValue (obj);
253
 
                                }
254
 
                        }
255
 
                }
256
 
                
257
 
                public abstract object CreateInstance (IProject proj);
258
 
                
259
 
                public abstract ObjectWrapper CreateWrapper ();
260
 
 
261
 
                public ItemDescriptor this[string name] {
262
 
                        get {
263
 
                                if (groups != null) {
264
 
                                        foreach (ItemGroup group in groups) {
265
 
                                                ItemDescriptor item = group[name];
266
 
                                                if (item != null)
267
 
                                                        return item;
268
 
                                        }
269
 
                                }
270
 
 
271
 
                                return null;
272
 
                        }
273
 
                }
274
 
 
275
 
                public ItemGroupCollection ItemGroups {
276
 
                        get {
277
 
                                return groups;
278
 
                        }
279
 
                }
280
 
 
281
 
                public ItemGroupCollection SignalGroups {
282
 
                        get {
283
 
                                return signals;
284
 
                        }
285
 
                }
286
 
 
287
 
                public int ImportantGroups {
288
 
                        get {
289
 
                                return importantGroups;
290
 
                        }
291
 
                }
292
 
 
293
 
                public ItemGroup ContextMenu {
294
 
                        get {
295
 
                                return contextMenu;
296
 
                        }
297
 
                }
298
 
 
299
 
                public ItemGroup InternalChildren {
300
 
                        get {
301
 
                                return internalChildren;
302
 
                        }
303
 
                }
304
 
                
305
 
                public WidgetLibrary Library {
306
 
                        get { return library; }
307
 
                }
308
 
                
309
 
                public virtual bool AllowChildren {
310
 
                        get { return allowChildren; }
311
 
                }
312
 
                
313
 
                internal protected virtual ItemDescriptor CreateItemDescriptor (XmlElement elem, ItemGroup group)
314
 
                {
315
 
                        if (elem.Name == "command")
316
 
                                return new CommandDescriptor (elem, group, this);
317
 
                        else
318
 
                                throw new ApplicationException ("Bad item name " + elem.Name + " in " + WrappedTypeName);
319
 
                }
320
 
                
321
 
                internal void SetLibrary (WidgetLibrary library)
322
 
                {
323
 
                        this.library = library;
324
 
                }
325
 
        }
326
 
}