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

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.GtkCore/libstetic/wrapper/RadioActionGroupManager.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.Collections;
 
4
using System.CodeDom;
 
5
 
 
6
namespace Stetic.Wrapper
 
7
{
 
8
        public class RadioActionGroupManager: IRadioGroupManager
 
9
        {
 
10
                public event GroupsChangedDelegate GroupsChanged;
 
11
                Hashtable actions = new Hashtable ();
 
12
                ArrayList groups = new ArrayList ();
 
13
                
 
14
                public IEnumerable GroupNames {
 
15
                        get {
 
16
                                foreach (string grp in groups)
 
17
                                        yield return grp;
 
18
                        }
 
19
                }
 
20
                
 
21
                public void Rename (string oldName, string newName)
 
22
                {
 
23
                        int i = groups.IndexOf (oldName);
 
24
                        if (i == -1)
 
25
                                return;
 
26
 
 
27
                        groups [i] = newName;
 
28
 
 
29
                        ArrayList list = new ArrayList ();
 
30
                        foreach (Action a in FindActionsInGroup (oldName))
 
31
                                list.Add (a);
 
32
        
 
33
                        foreach (Action a in list)
 
34
                                actions [a] = newName;
 
35
                                
 
36
                        EmitGroupsChanged ();
 
37
                }
 
38
                
 
39
                public void Add (string name)
 
40
                {
 
41
                        groups.Add (name);
 
42
                        EmitGroupsChanged ();
 
43
                }
 
44
                
 
45
                public RadioGroup FindGroup (string name)
 
46
                {
 
47
                        for (int i = 0; i < groups.Count; i++) {
 
48
                                RadioGroup group = groups[i] as RadioGroup;
 
49
                                if (group.Name == name)
 
50
                                        return group;
 
51
                        }
 
52
                        return null;
 
53
                }
 
54
 
 
55
                public string GetGroup (Action action)
 
56
                {
 
57
                        return actions [action] as string;
 
58
                }
 
59
                
 
60
                public void SetGroup (Action action, string group)
 
61
                {
 
62
                        if (group == null) {
 
63
                                if (actions.Contains (action)) {
 
64
                                        actions.Remove (action);
 
65
                                        action.Disposed -= OnActionDisposed;
 
66
                                }
 
67
                                return;
 
68
                        }
 
69
                        
 
70
                        if (!actions.Contains (action))
 
71
                                action.Disposed += OnActionDisposed;
 
72
                        actions [action] = group;
 
73
                        if (!groups.Contains (group))
 
74
                                groups.Add (group);
 
75
                }
 
76
                
 
77
                void OnActionDisposed (object s, EventArgs a)
 
78
                {
 
79
                        Action ac = (Action) s;
 
80
                        if (ac != null) {
 
81
                                ac.Disposed -= OnActionDisposed;
 
82
                                actions.Remove (ac);
 
83
                        }
 
84
                }
 
85
                
 
86
                public string LastGroup {
 
87
                        get {
 
88
                                if (groups.Count == 0)
 
89
                                        Add ("group1");
 
90
                                return groups [groups.Count - 1] as string;
 
91
                        }
 
92
                }
 
93
                
 
94
                void EmitGroupsChanged ()
 
95
                {
 
96
                        if (GroupsChanged != null)
 
97
                                GroupsChanged ();
 
98
                }
 
99
                
 
100
                IEnumerable FindActionsInGroup (string grp)
 
101
                {
 
102
                        foreach (DictionaryEntry e in actions)
 
103
                                if (((string)e.Value) == grp)
 
104
                                        yield return e.Key;
 
105
                }
 
106
                
 
107
                public CodeExpression GenerateGroupExpression (GeneratorContext ctx, Action action)
 
108
                {
 
109
                        // Returns and expression that represents the group to which the radio belongs.
 
110
                        // This expression can be an empty SList, if this is the first radio of the
 
111
                        // group that has been generated, or an SList taken from previously generated
 
112
                        // radios from the same group.
 
113
                        
 
114
                        string group = actions [action] as string;
 
115
                        if (group == null)
 
116
                                return new CodePrimitiveExpression (null);
 
117
 
 
118
                        CodeExpression var = null;
 
119
                        
 
120
                        foreach (Action a in FindActionsInGroup (group)) {
 
121
                                if (a == action)
 
122
                                        continue;
 
123
                                var = ctx.WidgetMap.GetWidgetExp (a);
 
124
                                if (var != null)
 
125
                                        break;
 
126
                        }
 
127
                        
 
128
                        if (var == null) {
 
129
                                return new CodeObjectCreateExpression (
 
130
                                        "GLib.SList",
 
131
                                        new CodePropertyReferenceExpression (
 
132
                                                new CodeTypeReferenceExpression (typeof(IntPtr)),
 
133
                                                "Zero"
 
134
                                        )
 
135
                                );
 
136
                        } else {
 
137
                                return new CodePropertyReferenceExpression (
 
138
                                        var,
 
139
                                        "Group"
 
140
                                );
 
141
                        }
 
142
                }
 
143
        }
 
144
}