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

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.GtkCore/lib/stetic/libsteticui/ActionGroupToolbar.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 Stetic.Wrapper;
5
 
using Mono.Unix;
6
 
 
7
 
namespace Stetic
8
 
{
9
 
        internal class ActionGroupToolbar: Gtk.Toolbar
10
 
        {
11
 
                Wrapper.ActionGroupCollection actionGroups;
12
 
                Gtk.ComboBox combo;
13
 
                bool updating;
14
 
                ActionGroup currentGroup;
15
 
                ArrayList internalButtons = new ArrayList ();
16
 
                bool singleGroupMode;
17
 
                bool allowBinding;
18
 
                ActionGroupDesignerFrontend frontend;
19
 
                Editor.ActionGroupEditor agroupEditor;
20
 
                Gtk.ToolButton addButton;
21
 
                Gtk.ToolButton removeButton;
22
 
                
23
 
                public event ActionGroupEventHandler ActiveGroupChanged;
24
 
                public event ActionGroupEventHandler ActiveGroupCreated;
25
 
                
26
 
                public ActionGroupToolbar (ActionGroupDesignerFrontend frontend, bool singleGroupMode)
27
 
                {
28
 
                        Initialize (frontend, null, singleGroupMode);
29
 
                }
30
 
                
31
 
                public ActionGroupToolbar (ActionGroupDesignerFrontend frontend, Wrapper.ActionGroup actionGroup)
32
 
                {
33
 
                        currentGroup = actionGroup;
34
 
                        Initialize (frontend, null, true);
35
 
                }
36
 
                
37
 
                public ActionGroupToolbar (ActionGroupDesignerFrontend frontend, Wrapper.ActionGroupCollection actionGroups)
38
 
                {
39
 
                        Initialize (frontend, actionGroups, false);
40
 
                }
41
 
                
42
 
                public bool AllowActionBinding {
43
 
                        get { return allowBinding; }
44
 
                        set { allowBinding = value; }
45
 
                }
46
 
                
47
 
                void Initialize (ActionGroupDesignerFrontend frontend, Wrapper.ActionGroupCollection actionGroups, bool singleGroupMode)
48
 
                {
49
 
                        this.frontend = frontend;
50
 
                        this.singleGroupMode = singleGroupMode;
51
 
                        IconSize = Gtk.IconSize.SmallToolbar;
52
 
                        Orientation = Gtk.Orientation.Horizontal;
53
 
                        ToolbarStyle = Gtk.ToolbarStyle.BothHoriz;
54
 
                        
55
 
                        combo = Gtk.ComboBox.NewText ();
56
 
                        
57
 
                        if (!singleGroupMode) {
58
 
                                combo.Changed += OnActiveChanged;
59
 
 
60
 
                                Gtk.ToolItem comboItem = new Gtk.ToolItem ();
61
 
                                Gtk.HBox cbox = new Gtk.HBox ();
62
 
                                cbox.PackStart (new Gtk.Label (Catalog.GetString ("Action Group:") + " "), false, false, 3);
63
 
                                cbox.PackStart (combo, true, true, 3);
64
 
                                comboItem.Add (cbox);
65
 
                                comboItem.ShowAll ();
66
 
                                Insert (comboItem, -1);
67
 
                                internalButtons.Add (comboItem);
68
 
                                
69
 
                                addButton = new Gtk.ToolButton (Gtk.Stock.Add);
70
 
                                addButton.Clicked += OnAddGroup;
71
 
                                Insert (addButton, -1);
72
 
                                internalButtons.Add (addButton);
73
 
                                
74
 
                                removeButton = new Gtk.ToolButton (Gtk.Stock.Remove);
75
 
                                removeButton.Clicked += OnRemoveGroup;
76
 
                                Insert (removeButton, -1);
77
 
                                internalButtons.Add (removeButton);
78
 
                                
79
 
                                ActionGroups = actionGroups;
80
 
                                
81
 
                                if (actionGroups != null && actionGroups.Count > 0)
82
 
                                        combo.Active = 0;
83
 
                        } else {
84
 
                                UpdateActionCommands (null);
85
 
                        }
86
 
 
87
 
                        ShowAll ();
88
 
                }
89
 
                
90
 
                public override void Dispose ()
91
 
                {
92
 
                        combo.Changed -= OnActiveChanged;
93
 
                        if (addButton != null) {
94
 
                                addButton.Clicked -= OnAddGroup;
95
 
                                removeButton.Clicked -= OnRemoveGroup;
96
 
                        }
97
 
                                
98
 
                        if (agroupEditor != null) {
99
 
                                agroupEditor.SelectionChanged -= OnEditorSelectionChanged;
100
 
                                agroupEditor = null;
101
 
                        }
102
 
                        
103
 
                        if (!singleGroupMode)
104
 
                                ActionGroups = null;
105
 
                        base.Dispose ();
106
 
                }
107
 
                
108
 
                public Wrapper.ActionGroupCollection ActionGroups {
109
 
                        get { return actionGroups; }
110
 
                        set {
111
 
                                if (singleGroupMode)
112
 
                                        throw new InvalidOperationException ("ActionGroups can't be set in single group mode");
113
 
 
114
 
                                if (actionGroups != null) {
115
 
                                        actionGroups.ActionGroupAdded -= OnCollectionChanged;
116
 
                                        actionGroups.ActionGroupRemoved -= OnCollectionChanged;
117
 
                                        actionGroups.ActionGroupChanged -= OnCollectionChanged;
118
 
                                }
119
 
                                
120
 
                                this.actionGroups = value;
121
 
                                
122
 
                                if (actionGroups != null) {
123
 
                                        actionGroups.ActionGroupAdded += OnCollectionChanged;
124
 
                                        actionGroups.ActionGroupRemoved += OnCollectionChanged;
125
 
                                        actionGroups.ActionGroupChanged += OnCollectionChanged;
126
 
                                }
127
 
                                Refresh ();
128
 
                        }
129
 
                }
130
 
                
131
 
                public void Bind (Editor.ActionGroupEditor agroupEditor)
132
 
                {
133
 
                        this.agroupEditor = agroupEditor;
134
 
                        agroupEditor.SelectionChanged += OnEditorSelectionChanged;
135
 
                        agroupEditor.ActionGroup = ActiveGroup;
136
 
                }
137
 
                
138
 
                public void OnEditorSelectionChanged (object s, EventArgs a)
139
 
                {
140
 
                        UpdateActionCommands (agroupEditor.SelectedAction);
141
 
                }
142
 
                
143
 
                public ActionGroup ActiveGroup {
144
 
                        get {
145
 
                                return currentGroup;
146
 
                        }
147
 
                        set {
148
 
                                if (singleGroupMode) {
149
 
                                        currentGroup = value;
150
 
                                        UpdateActionCommands (null);
151
 
                                        NotifyActiveGroupChanged ();
152
 
                                } else {
153
 
                                        int i = actionGroups.IndexOf (value);
154
 
                                        if (i != -1)
155
 
                                                combo.Active = i;
156
 
                                }
157
 
                        }
158
 
                }
159
 
                
160
 
                void Refresh ()
161
 
                {
162
 
                        if (singleGroupMode)
163
 
                                return;
164
 
 
165
 
                        while (combo.Model.IterNChildren () > 0)
166
 
                                combo.RemoveText (0);
167
 
                        if (actionGroups != null) {
168
 
                                foreach (ActionGroup group in actionGroups)
169
 
                                        combo.AppendText (group.Name);
170
 
                        }
171
 
                }
172
 
                
173
 
                void OnCollectionChanged (object s, ActionGroupEventArgs args)
174
 
                {
175
 
                        // Avoid firing the selection change event if the selected
176
 
                        // group is the same after the refresh
177
 
                        ActionGroup oldGroup = currentGroup;
178
 
                        updating = true;
179
 
                        
180
 
                        int i = combo.Active;
181
 
                        Refresh ();
182
 
                        if (actionGroups.Count == 0) {
183
 
                                combo.Sensitive = false;
184
 
                                currentGroup = null;
185
 
                        }
186
 
                        else {
187
 
                                combo.Sensitive = true;
188
 
                                if (i == -1)
189
 
                                        i = 0;
190
 
                                if (i < actionGroups.Count)
191
 
                                        combo.Active = i;
192
 
                                else
193
 
                                        combo.Active = actionGroups.Count - 1;
194
 
                                currentGroup = (ActionGroup) actionGroups [combo.Active];
195
 
                        }
196
 
                        updating = false;
197
 
                        if (currentGroup != oldGroup)
198
 
                                OnActiveChanged (null, null);
199
 
                        frontend.NotifyModified ();
200
 
                }
201
 
                
202
 
                void OnAddGroup (object s, EventArgs args)
203
 
                {
204
 
                        ActionGroup group = new ActionGroup ();
205
 
                        group.Name = Catalog.GetString ("New Action Group");
206
 
                        actionGroups.Add (group);
207
 
                        combo.Active = actionGroups.Count - 1;
208
 
                        if (ActiveGroupCreated != null)
209
 
                                ActiveGroupCreated (this, new ActionGroupEventArgs (ActiveGroup));
210
 
                        
211
 
                        if (agroupEditor != null)
212
 
                                agroupEditor.StartEditing ();
213
 
                }
214
 
                
215
 
                void OnRemoveGroup (object s, EventArgs args)
216
 
                {
217
 
                        if (combo.Active != -1)
218
 
                                actionGroups.RemoveAt (combo.Active);
219
 
                }
220
 
                
221
 
                void OnActiveChanged (object s, EventArgs args)
222
 
                {
223
 
                        if (!updating) {
224
 
                                UpdateActionCommands (null);
225
 
                                if (combo.Active != -1)
226
 
                                        currentGroup = (ActionGroup) actionGroups [combo.Active];
227
 
                                else
228
 
                                        currentGroup = null;
229
 
                                NotifyActiveGroupChanged ();
230
 
                        }
231
 
                }
232
 
                
233
 
                void NotifyActiveGroupChanged ()
234
 
                {
235
 
                        if (agroupEditor != null)
236
 
                                agroupEditor.ActionGroup = ActiveGroup;
237
 
                        if (ActiveGroupChanged != null)
238
 
                                ActiveGroupChanged (this, new ActionGroupEventArgs (ActiveGroup));
239
 
                }
240
 
                
241
 
                void UpdateActionCommands (Wrapper.Action action)
242
 
                {
243
 
                        foreach (Gtk.Widget w in Children) {
244
 
                                if (!internalButtons.Contains (w)) {
245
 
                                        Remove (w);
246
 
                                        w.Destroy ();
247
 
                                }
248
 
                        }
249
 
                        AddActionCommands (action);
250
 
                                
251
 
                        if (internalButtons.Count > 0 && internalButtons.Count != Children.Length) {
252
 
                                Insert (new Gtk.SeparatorToolItem (), internalButtons.Count);
253
 
                        }
254
 
                        ShowAll ();
255
 
                }
256
 
                
257
 
                protected virtual void AddActionCommands (Wrapper.Action action)
258
 
                {
259
 
                        if (allowBinding) {
260
 
                                Gtk.ToolButton bindButton = new Gtk.ToolButton (null, Catalog.GetString ("Bind to Field"));
261
 
                                bindButton.IsImportant = true;
262
 
                                bindButton.Show ();
263
 
                                Insert (bindButton, -1);
264
 
                                if (action == null)
265
 
                                        bindButton.Sensitive = false;
266
 
                                        
267
 
                                bindButton.Clicked += delegate { frontend.NotifyBindField (); };
268
 
                        }
269
 
                }
270
 
        }
271
 
}