~ubuntu-branches/ubuntu/saucy/monodevelop/saucy-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Components/MonoDevelop.Components.Commands/CommandMenuItem.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-09-10 16:54:48 UTC
  • mfrom: (19.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100910165448-0rybfk25zd4o9431
Tags: 2.4+dfsg-2
* debian/patches/inject_Mono.Debugger.Soft_source.patch,
  debian/patches/use_system_Mono.Debugger.Soft.patch,
  debian/control:
  + Build against system Soft Debugger, since we now have a new
    enough Mono to match MonoDevelop's required API

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// CommandMenuItem.cs
3
 
//
4
 
// Author:
5
 
//   Lluis Sanchez Gual
6
 
//
7
 
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8
 
//
9
 
// Permission is hereby granted, free of charge, to any person obtaining
10
 
// a copy of this software and associated documentation files (the
11
 
// "Software"), to deal in the Software without restriction, including
12
 
// without limitation the rights to use, copy, modify, merge, publish,
13
 
// distribute, sublicense, and/or sell copies of the Software, and to
14
 
// permit persons to whom the Software is furnished to do so, subject to
15
 
// the following conditions:
16
 
// 
17
 
// The above copyright notice and this permission notice shall be
18
 
// included in all copies or substantial portions of the Software.
19
 
// 
20
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
 
//
28
 
 
29
 
using System;
30
 
using System.Collections;
31
 
 
32
 
namespace MonoDevelop.Components.Commands
33
 
{
34
 
        public class CommandMenuItem: Gtk.ImageMenuItem, ICommandMenuItem
35
 
        {
36
 
                CommandManager commandManager;
37
 
                object commandId;
38
 
                bool isArray;
39
 
                bool isArrayItem;
40
 
                object arrayDataItem;
41
 
                ArrayList itemArray;
42
 
                string lastIcon;
43
 
                string overrideLabel;
44
 
                bool wasButtonActivation;
45
 
                object initialTarget;
46
 
                bool disabledVisible = true;
47
 
                CommandInfo lastCmdInfo;
48
 
                
49
 
                public CommandMenuItem (object commandId, CommandManager commandManager, string overrideLabel, bool disabledVisible): base ("")
50
 
                {
51
 
                        this.commandId = commandId;
52
 
                        this.commandManager = commandManager;
53
 
                        this.overrideLabel = overrideLabel;
54
 
                        this.disabledVisible = disabledVisible;
55
 
                        ActionCommand cmd = commandManager.GetCommand (commandId) as ActionCommand;
56
 
                        if (cmd != null)
57
 
                                isArray = cmd.CommandArray;
58
 
                }
59
 
                
60
 
                public CommandMenuItem (object commandId, CommandManager commandManager): this (commandId, commandManager, null, true)
61
 
                {
62
 
                }
63
 
 
64
 
                void ICommandUserItem.Update (object initialTarget)
65
 
                {
66
 
                        if (commandManager != null && !isArrayItem) {
67
 
                                CommandInfo cinfo = commandManager.GetCommandInfo (commandId, initialTarget);
68
 
                                this.initialTarget = initialTarget;
69
 
                                Update (cinfo);
70
 
                        }
71
 
                }
72
 
                
73
 
                void ICommandMenuItem.SetUpdateInfo (CommandInfo cmdInfo, object initialTarget)
74
 
                {
75
 
                        isArrayItem = true;
76
 
                        this.initialTarget = initialTarget;
77
 
                        arrayDataItem = cmdInfo.DataItem;
78
 
                        Update (cmdInfo);
79
 
                }
80
 
                
81
 
                protected override void OnParentSet (Gtk.Widget parent)
82
 
                {
83
 
                        base.OnParentSet (parent);
84
 
                        if (Parent == null)
85
 
                                return;
86
 
                        
87
 
                        ((ICommandUserItem)this).Update (null);
88
 
                        
89
 
                        if (!isArrayItem) {
90
 
                                // Make sure the accelerators always work for this item
91
 
                                // while the menu is hidden
92
 
                                Sensitive = true;
93
 
                                Visible = true;
94
 
                        }
95
 
                }
96
 
                
97
 
                protected override bool OnButtonReleaseEvent (Gdk.EventButton ev)
98
 
                {
99
 
                        wasButtonActivation = true;
100
 
                        return base.OnButtonReleaseEvent (ev);
101
 
                }
102
 
                
103
 
                protected override void OnActivated ()
104
 
                {
105
 
                        base.OnActivated ();
106
 
 
107
 
                        if (commandManager == null)
108
 
                                throw new InvalidOperationException ();
109
 
                        
110
 
                        if (!wasButtonActivation) {
111
 
                                // It's being activated by an accelerator.
112
 
                                if (Submenu == null)
113
 
                                        commandManager.DispatchCommandFromAccel (commandId, arrayDataItem, initialTarget);
114
 
                        } else {
115
 
                                wasButtonActivation = false;
116
 
                                commandManager.DispatchCommand (commandId, arrayDataItem, initialTarget);
117
 
                        }
118
 
                }
119
 
                
120
 
                protected override void OnSelected ()
121
 
                {
122
 
                        if (commandManager != null)
123
 
                                commandManager.NotifySelected (lastCmdInfo);
124
 
                        base.OnSelected ();
125
 
                }
126
 
                
127
 
                protected override void OnDeselected ()
128
 
                {
129
 
                        if (commandManager != null)
130
 
                                commandManager.NotifyDeselected ();
131
 
                        base.OnDeselected ();
132
 
                }
133
 
                
134
 
                void Update (CommandInfo cmdInfo)
135
 
                {
136
 
                        lastCmdInfo = cmdInfo;
137
 
                        if (isArray && !isArrayItem) {
138
 
                                this.Visible = false;
139
 
                                Gtk.Menu menu = (Gtk.Menu) Parent;  
140
 
                                
141
 
                                if (itemArray != null) {
142
 
                                        foreach (Gtk.MenuItem item in itemArray)
143
 
                                                menu.Remove (item);
144
 
                                }
145
 
                                
146
 
                                itemArray = new ArrayList ();
147
 
                                int i = Array.IndexOf (menu.Children, this);
148
 
                                
149
 
                                if (cmdInfo.ArrayInfo != null) {
150
 
                                        foreach (CommandInfo info in cmdInfo.ArrayInfo) {
151
 
                                                Gtk.MenuItem item;
152
 
                                                if (info.IsArraySeparator) {
153
 
                                                        item = new Gtk.SeparatorMenuItem ();
154
 
                                                        item.Show ();
155
 
                                                } else {
156
 
                                                        item = CommandEntry.CreateMenuItem (commandManager, commandId, false);
157
 
                                                        ICommandMenuItem mi = (ICommandMenuItem) item; 
158
 
                                                        mi.SetUpdateInfo (info, initialTarget);
159
 
                                                }
160
 
                                                menu.Insert (item, ++i);
161
 
                                                itemArray.Add (item);
162
 
                                        }
163
 
                                }
164
 
                        } else {
165
 
                                Gtk.Widget child = Child;
166
 
                                if (child == null)
167
 
                                        return;
168
 
                                
169
 
                                Gtk.Label accel_label = null;
170
 
                                Gtk.Label label = null;
171
 
                                
172
 
                                if (!(child is Gtk.HBox)) {
173
 
                                        child = new Gtk.HBox (false, 0);
174
 
                                        accel_label = new Gtk.Label ("");
175
 
                                        accel_label.UseUnderline = false;
176
 
                                        accel_label.Xalign = 1.0f;
177
 
                                        accel_label.Show ();
178
 
                                        
179
 
                                        label = new Gtk.Label ("");
180
 
                                        label.UseUnderline = true;
181
 
                                        label.Xalign = 0.0f;
182
 
                                        label.Show ();
183
 
                                        
184
 
                                        ((Gtk.Box) child).PackStart (label);
185
 
                                        ((Gtk.Box) child).PackStart (accel_label);
186
 
                                        child.Show ();
187
 
                                        
188
 
                                        this.Remove (Child);
189
 
                                        this.Add (child);
190
 
                                } else {
191
 
                                        accel_label = (Gtk.Label) ((Gtk.Box) child).Children[1];
192
 
                                        label = (Gtk.Label) ((Gtk.Box) child).Children[0];
193
 
                                }
194
 
                                
195
 
                                if (cmdInfo.AccelKey != null)
196
 
                                        accel_label.Text = "    " + KeyBindingManager.BindingToDisplayLabel (cmdInfo.AccelKey, true);
197
 
                                else
198
 
                                        accel_label.Text = String.Empty;
199
 
                                
200
 
                                if (cmdInfo.UseMarkup) {
201
 
                                        label.Markup = overrideLabel ?? cmdInfo.Text;
202
 
                                        label.UseMarkup = true;
203
 
                                } else {
204
 
                                        label.Text = overrideLabel ?? cmdInfo.Text;
205
 
                                        label.UseMarkup = false;
206
 
                                }
207
 
                                
208
 
                                label.UseUnderline = true;
209
 
                                
210
 
                                this.Sensitive = cmdInfo.Enabled;
211
 
                                this.Visible = cmdInfo.Visible && (disabledVisible || cmdInfo.Enabled);
212
 
                                
213
 
                                if (cmdInfo.Icon != null && cmdInfo.Icon != "" && cmdInfo.Icon != lastIcon) {
214
 
                                        Image = new Gtk.Image (cmdInfo.Icon, Gtk.IconSize.Menu);
215
 
                                        lastIcon = cmdInfo.Icon;
216
 
                                }
217
 
                                
218
 
                                if (cmdInfo is CommandInfoSet) {
219
 
                                        CommandInfoSet ciset = (CommandInfoSet) cmdInfo;
220
 
                                        Gtk.Menu smenu = new Gtk.Menu ();
221
 
                                        Submenu = smenu;
222
 
                                        foreach (CommandInfo info in ciset.CommandInfos) {
223
 
                                                Gtk.MenuItem item;
224
 
                                                if (info.IsArraySeparator) {
225
 
                                                        item = new Gtk.SeparatorMenuItem ();
226
 
                                                        item.Show ();
227
 
                                                } else {
228
 
                                                        item = CommandEntry.CreateMenuItem (commandManager, commandId, false);
229
 
                                                        ICommandMenuItem mi = (ICommandMenuItem) item; 
230
 
                                                        mi.SetUpdateInfo (info, initialTarget);
231
 
                                                }
232
 
                                                smenu.Add (item);
233
 
                                        }
234
 
                                }
235
 
                        }
236
 
                }
237
 
        }
238
 
}