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

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.GtkCore/lib/stetic/libstetic/wrapper/ActionGroup.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
 
 
2
 
using System;
3
 
using System.CodeDom;
4
 
using System.Xml;
5
 
using System.Collections;
6
 
using Stetic.Undo;
7
 
 
8
 
namespace Stetic.Wrapper
9
 
{
10
 
        public sealed class ActionGroup: ObjectWrapper
11
 
        {
12
 
                string name;
13
 
                ActionCollection actions;
14
 
                ObjectWrapper owner;
15
 
                bool generatePublic = true;
16
 
                
17
 
                public event ActionEventHandler ActionAdded;
18
 
                public event ActionEventHandler ActionRemoved;
19
 
                public event ActionEventHandler ActionChanged;
20
 
                
21
 
                public ActionGroup ()
22
 
                {
23
 
                        actions = new ActionCollection (this);
24
 
                }
25
 
                
26
 
                public ActionGroup (string name): this ()
27
 
                {
28
 
                        this.name = name;
29
 
                }
30
 
                
31
 
                public override void Dispose ()
32
 
                {
33
 
                        foreach (Action a in actions)
34
 
                                a.Dispose ();
35
 
                        base.Dispose ();
36
 
                }
37
 
                
38
 
                public ActionCollection Actions {
39
 
                        get { return actions; }
40
 
                }
41
 
                
42
 
                public string Name {
43
 
                        get { return name; }
44
 
                        set { 
45
 
                                name = value;
46
 
                                NotifyChanged ();
47
 
                        }
48
 
                }
49
 
                
50
 
                public bool GeneratePublic {
51
 
                        get { return generatePublic; }
52
 
                        set { generatePublic = value; }
53
 
                }
54
 
                
55
 
                public Action GetAction (string name)
56
 
                {
57
 
                        foreach (Action ac in actions)
58
 
                                if (ac.Name == name)
59
 
                                        return ac;
60
 
                        return null;
61
 
                }
62
 
                
63
 
                internal string GetValidName (Action reqAction, string name)
64
 
                {
65
 
                        int max = 0;
66
 
                        bool found = false;
67
 
                        foreach (Action ac in Actions) {
68
 
                                if (ac == reqAction)
69
 
                                        continue;
70
 
                                        
71
 
                                string bname;
72
 
                                int index;
73
 
                                WidgetUtils.ParseWidgetName (ac.Name, out bname, out index);
74
 
                                
75
 
                                if (name == ac.Name)
76
 
                                        found = true;
77
 
                                if (name == bname && index > max)
78
 
                                        max = index;
79
 
                        }
80
 
                        if (found)
81
 
                                return name + (max+1);
82
 
                        else
83
 
                                return name;
84
 
                }
85
 
                
86
 
                public override XmlElement Write (ObjectWriter writer)
87
 
                {
88
 
                        XmlElement group = writer.XmlDocument.CreateElement ("action-group");
89
 
                        group.SetAttribute ("name", name);
90
 
                        if (writer.CreateUndoInfo)
91
 
                                group.SetAttribute ("undoId", UndoId);
92
 
                        foreach (Action ac in actions) {
93
 
                                if (ac.Name.Length > 0)
94
 
                                        group.AppendChild (writer.WriteObject (ac));
95
 
                        }
96
 
                        return group;
97
 
                }
98
 
                
99
 
                public override void Read (ObjectReader reader, XmlElement elem)
100
 
                {
101
 
                        name = elem.GetAttribute ("name");
102
 
                        string uid = elem.GetAttribute ("undoId");
103
 
                        if (uid.Length > 0)
104
 
                                UndoId = uid;
105
 
                        foreach (XmlElement child in elem.SelectNodes ("action")) {
106
 
                                Action ac = new Action ();
107
 
                                ac.Read (reader, child);
108
 
                                actions.Add (ac);
109
 
                        }
110
 
                }
111
 
                
112
 
                internal protected override CodeExpression GenerateObjectCreation (GeneratorContext ctx)
113
 
                {
114
 
                        return new CodeObjectCreateExpression (
115
 
                                typeof(Gtk.ActionGroup),
116
 
                                new CodePrimitiveExpression (Name)
117
 
                        );
118
 
                }
119
 
                
120
 
                internal protected override void GenerateBuildCode (GeneratorContext ctx, CodeExpression var)
121
 
                {
122
 
                        foreach (Action action in Actions) {
123
 
                                // Create the action
124
 
                                CodeExpression acVarExp = ctx.GenerateInstanceExpression (action, action.GenerateObjectCreation (ctx));
125
 
                                ctx.GenerateBuildCode (action, acVarExp);
126
 
                                ctx.Statements.Add (
127
 
                                        new CodeMethodInvokeExpression (
128
 
                                                var,
129
 
                                                "Add",
130
 
                                                acVarExp,
131
 
                                                new CodePrimitiveExpression (action.Accelerator)
132
 
                                        )
133
 
                                );
134
 
                        }
135
 
                }
136
 
                
137
 
                internal void SetOwner (ObjectWrapper owner)
138
 
                {
139
 
                        this.owner = owner;
140
 
                }
141
 
                
142
 
                internal override UndoManager GetUndoManagerInternal ()
143
 
                {
144
 
                        if (owner != null)
145
 
                                return owner.UndoManager;
146
 
                        else
147
 
                                return base.GetUndoManagerInternal ();
148
 
                }
149
 
                
150
 
                public override ObjectWrapper FindObjectByUndoId (string id)
151
 
                {
152
 
                        ObjectWrapper ow = base.FindObjectByUndoId (id);
153
 
                        if (ow != null) return ow;
154
 
                        
155
 
                        foreach (Action ac in Actions) {
156
 
                                ow = ac.FindObjectByUndoId (id);
157
 
                                if (ow != null)
158
 
                                        return ow;
159
 
                        }
160
 
                        return null;
161
 
                }
162
 
                
163
 
                DiffGenerator GetDiffGenerator ()
164
 
                {
165
 
                        DiffGenerator gen = new DiffGenerator ();
166
 
                        gen.CurrentStatusAdaptor = new ActionDiffAdaptor (Project);
167
 
                        XmlDiffAdaptor xad = new XmlDiffAdaptor ();
168
 
                        xad.ChildElementName = "action";
169
 
                        gen.NewStatusAdaptor = xad;
170
 
                        return gen;
171
 
                }
172
 
                
173
 
                public override object GetUndoDiff ()
174
 
                {
175
 
                        XmlElement oldElem = UndoManager.GetObjectStatus (this);
176
 
                        UndoWriter writer = new UndoWriter (oldElem.OwnerDocument, UndoManager);
177
 
                        
178
 
                        XmlElement newElem = Write (writer);
179
 
                        ObjectDiff actionsDiff = GetDiffGenerator().GetDiff (this, oldElem);
180
 
                        UndoManager.UpdateObjectStatus (this, newElem);
181
 
                        return actionsDiff;
182
 
                }
183
 
                
184
 
                public override object ApplyUndoRedoDiff (object diff)
185
 
                {
186
 
                        ObjectDiff actionsDiff = (ObjectDiff) diff;
187
 
                        
188
 
                        XmlElement status = UndoManager.GetObjectStatus (this);
189
 
                        
190
 
                        DiffGenerator differ = GetDiffGenerator();
191
 
                        differ.ApplyDiff (this, actionsDiff);
192
 
                        actionsDiff = differ.GetDiff (this, status);
193
 
                        
194
 
                        UndoWriter writer = new UndoWriter (status.OwnerDocument, UndoManager);
195
 
                        XmlElement newElem = Write (writer);
196
 
                        UndoManager.UpdateObjectStatus (this, newElem);
197
 
                        
198
 
                        return actionsDiff;
199
 
                }
200
 
                
201
 
                internal void NotifyActionAdded (Action ac)
202
 
                {
203
 
                        ac.SetActionGroup (this);
204
 
                        ac.ObjectChanged += OnActionChanged;
205
 
                        ac.SignalAdded += OnSignalAdded;
206
 
                        ac.SignalRemoved += OnSignalRemoved;
207
 
                        ac.SignalChanged += OnSignalChanged;
208
 
                        
209
 
                        ac.UpdateNameIndex ();
210
 
                        
211
 
                        NotifyChanged ();
212
 
                        
213
 
                        if (ActionAdded != null)
214
 
                                ActionAdded (this, new ActionEventArgs (ac));
215
 
                }
216
 
                
217
 
                internal void NotifyActionRemoved (Action ac)
218
 
                {
219
 
                        ac.SetActionGroup (null);
220
 
                        ac.ObjectChanged -= OnActionChanged;
221
 
                        ac.SignalAdded -= OnSignalAdded;
222
 
                        ac.SignalRemoved -= OnSignalRemoved;
223
 
                        ac.SignalChanged -= OnSignalChanged;
224
 
 
225
 
                        NotifyChanged ();
226
 
                        
227
 
                        if (ActionRemoved != null)
228
 
                                ActionRemoved (this, new ActionEventArgs (ac));
229
 
                }
230
 
                
231
 
                void OnActionChanged (object s, ObjectWrapperEventArgs args)
232
 
                {
233
 
                        NotifyChanged ();
234
 
                        if (ActionChanged != null)
235
 
                                ActionChanged (this, new ActionEventArgs ((Action) args.Wrapper));
236
 
                }
237
 
                
238
 
                void OnSignalAdded (object s, SignalEventArgs args)
239
 
                {
240
 
                        OnSignalAdded (args);
241
 
                }
242
 
                
243
 
                void OnSignalRemoved (object s, SignalEventArgs args)
244
 
                {
245
 
                        OnSignalRemoved (args);
246
 
                }
247
 
                
248
 
                void OnSignalChanged (object s, SignalChangedEventArgs args)
249
 
                {
250
 
                        OnSignalChanged (args);
251
 
                }
252
 
        }
253
 
        
254
 
        public class ActionGroupCollection: CollectionBase
255
 
        {
256
 
                ActionGroup[] toClear;
257
 
                ObjectWrapper owner;
258
 
                
259
 
                internal void SetOwner (ObjectWrapper owner)
260
 
                {
261
 
                        this.owner = owner;
262
 
                }
263
 
                
264
 
                public void Add (ActionGroup group)
265
 
                {
266
 
                        List.Add (group);
267
 
                }
268
 
                
269
 
                public void Insert (int n, ActionGroup group)
270
 
                {
271
 
                        List.Insert (n, group);
272
 
                }
273
 
                
274
 
                public ActionGroup this [int n] {
275
 
                        get { return (ActionGroup) List [n]; }
276
 
                }
277
 
                
278
 
                public ActionGroup this [string name] {
279
 
                        get {
280
 
                                foreach (ActionGroup grp in List)
281
 
                                        if (grp.Name == name)
282
 
                                                return grp;
283
 
                                return null;
284
 
                        }
285
 
                }
286
 
                
287
 
                internal ObjectWrapper FindObjectByUndoId (string id)
288
 
                {
289
 
                        foreach (ActionGroup ag in List) {
290
 
                                ObjectWrapper ow = ag.FindObjectByUndoId (id);
291
 
                                if (ow != null)
292
 
                                        return ow;
293
 
                        }
294
 
                        return null;
295
 
                }
296
 
                
297
 
                DiffGenerator GetDiffGenerator (IProject prj)
298
 
                {
299
 
                        DiffGenerator gen = new DiffGenerator ();
300
 
                        gen.CurrentStatusAdaptor = new ActionDiffAdaptor (prj);
301
 
                        XmlDiffAdaptor xad = new XmlDiffAdaptor ();
302
 
                        xad.ChildElementName = "action-group";
303
 
                        xad.ProcessProperties = false;
304
 
                        xad.ChildAdaptor = new XmlDiffAdaptor ();
305
 
                        xad.ChildAdaptor.ChildElementName = "action";
306
 
                        gen.NewStatusAdaptor = xad;
307
 
                        return gen;
308
 
                }
309
 
                
310
 
                internal ObjectDiff GetDiff (IProject prj, XmlElement elem)
311
 
                {
312
 
                        return GetDiffGenerator (prj).GetDiff (this, elem);
313
 
                }
314
 
                
315
 
                internal void ApplyDiff (IProject prj, ObjectDiff diff)
316
 
                {
317
 
                        GetDiffGenerator (prj).ApplyDiff (this, diff);
318
 
                }
319
 
                
320
 
                public int IndexOf (ActionGroup group)
321
 
                {
322
 
                        return List.IndexOf (group);
323
 
                }
324
 
                
325
 
                public void Remove (ActionGroup group)
326
 
                {
327
 
                        List.Remove (group);
328
 
                }
329
 
 
330
 
                protected override void OnInsertComplete (int index, object val)
331
 
                {
332
 
                        NotifyGroupAdded ((ActionGroup) val);
333
 
                }
334
 
                
335
 
                protected override void OnRemoveComplete (int index, object val)
336
 
                {
337
 
                        NotifyGroupRemoved ((ActionGroup)val);
338
 
                }
339
 
                
340
 
                protected override void OnSetComplete (int index, object oldv, object newv)
341
 
                {
342
 
                        NotifyGroupRemoved ((ActionGroup) oldv);
343
 
                        NotifyGroupAdded ((ActionGroup) newv);
344
 
                }
345
 
                
346
 
                protected override void OnClear ()
347
 
                {
348
 
                        toClear = new ActionGroup [Count];
349
 
                        List.CopyTo (toClear, 0);
350
 
                }
351
 
                
352
 
                protected override void OnClearComplete ()
353
 
                {
354
 
                        foreach (ActionGroup a in toClear)
355
 
                                NotifyGroupRemoved (a);
356
 
                        toClear = null;
357
 
                }
358
 
                
359
 
                void NotifyGroupAdded (ActionGroup grp)
360
 
                {
361
 
                        grp.SetOwner (owner);
362
 
                        grp.ObjectChanged += OnGroupChanged;
363
 
                        if (ActionGroupAdded != null)
364
 
                                ActionGroupAdded (this, new ActionGroupEventArgs (grp));
365
 
                }
366
 
                
367
 
                void NotifyGroupRemoved (ActionGroup grp)
368
 
                {
369
 
                        grp.SetOwner (null);
370
 
                        grp.ObjectChanged -= OnGroupChanged;
371
 
                        if (ActionGroupRemoved != null)
372
 
                                ActionGroupRemoved (this, new ActionGroupEventArgs (grp));
373
 
                }
374
 
                
375
 
                void OnGroupChanged (object s, ObjectWrapperEventArgs a)
376
 
                {
377
 
                        if (ActionGroupChanged != null)
378
 
                                ActionGroupChanged (this, new ActionGroupEventArgs ((ActionGroup)s));
379
 
                }
380
 
                
381
 
                public ActionGroup[] ToArray ()
382
 
                {
383
 
                        ActionGroup[] groups = new ActionGroup [Count];
384
 
                        List.CopyTo (groups, 0);
385
 
                        return groups;
386
 
                }
387
 
                
388
 
                public event ActionGroupEventHandler ActionGroupAdded;
389
 
                public event ActionGroupEventHandler ActionGroupRemoved;
390
 
                public event ActionGroupEventHandler ActionGroupChanged;
391
 
        }
392
 
        
393
 
        
394
 
        public delegate void ActionEventHandler (object sender, ActionEventArgs args);
395
 
        
396
 
        public class ActionEventArgs: EventArgs
397
 
        {
398
 
                readonly Action action;
399
 
                
400
 
                public ActionEventArgs (Action ac)
401
 
                {
402
 
                        action = ac;
403
 
                }
404
 
                
405
 
                public Action Action {
406
 
                        get { return action; }
407
 
                }
408
 
        }
409
 
        
410
 
        public delegate void ActionGroupEventHandler (object sender, ActionGroupEventArgs args);
411
 
        
412
 
        public class ActionGroupEventArgs: EventArgs
413
 
        {
414
 
                readonly ActionGroup action;
415
 
                
416
 
                public ActionGroupEventArgs (ActionGroup ac)
417
 
                {
418
 
                        action = ac;
419
 
                }
420
 
                
421
 
                public ActionGroup ActionGroup {
422
 
                        get { return action; }
423
 
                }
424
 
        }
425
 
}