2 // System.ComponentModel.Design.MenuCommandService.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Ivan N. Zlatev <contact@i-nz.net>
8 // Copyright (C) 2007 Novell, Inc
9 // Copyright (C) Ivan N. Zlatev
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
37 using System.Collections.Generic;
38 using System.ComponentModel.Design;
39 using System.ComponentModel;
41 namespace System.ComponentModel.Design
43 public class MenuCommandService : IMenuCommandService, IDisposable
45 private IServiceProvider _serviceProvider;
46 private DesignerVerbCollection _globalVerbs;
47 private DesignerVerbCollection _verbs;
48 private Dictionary <CommandID, MenuCommand> _commands;
50 public MenuCommandService (IServiceProvider serviceProvider)
52 if (serviceProvider == null)
53 throw new ArgumentNullException ("serviceProvider");
54 _serviceProvider = serviceProvider;
55 ISelectionService selectionSvc = _serviceProvider.GetService (typeof (ISelectionService)) as ISelectionService;
56 if (selectionSvc != null)
57 selectionSvc.SelectionChanged += OnSelectionChanged;
60 private void OnSelectionChanged (object sender, EventArgs arg)
62 this.OnCommandsChanged (new MenuCommandsChangedEventArgs (MenuCommandsChangedType.CommandChanged, null));
63 // The commands will be updated whenever they are requested/modified.
66 public event MenuCommandsChangedEventHandler MenuCommandsChanged;
68 public virtual DesignerVerbCollection Verbs {
75 public virtual void AddCommand (MenuCommand command)
78 throw new ArgumentNullException ("command");
80 if (_commands == null)
81 _commands = new Dictionary <CommandID, MenuCommand> ();
82 _commands.Add (command.CommandID, command);
83 this.OnCommandsChanged (new MenuCommandsChangedEventArgs (MenuCommandsChangedType.CommandAdded, command));
86 public virtual void AddVerb (DesignerVerb verb)
89 throw new ArgumentNullException ("verb");
91 if (!_verbs.Contains (verb)) {
92 if (_globalVerbs == null)
93 _globalVerbs = new DesignerVerbCollection ();
94 _globalVerbs.Add (verb);
96 this.OnCommandsChanged (new MenuCommandsChangedEventArgs (MenuCommandsChangedType.CommandAdded, verb));
99 public void Dispose ()
104 protected virtual void Dispose (bool disposing)
107 if (_globalVerbs != null) {
108 _globalVerbs.Clear ();
111 if (_verbs != null) {
115 if (_commands != null) {
119 if (_serviceProvider != null) {
120 ISelectionService selectionSvc = _serviceProvider.GetService (typeof (ISelectionService)) as ISelectionService;
121 if (selectionSvc != null)
122 selectionSvc.SelectionChanged -= OnSelectionChanged;
123 _serviceProvider = null;
128 protected void EnsureVerbs ()
130 DesignerVerbCollection selectionVerbs = null;
132 ISelectionService selectionSvc = this.GetService (typeof (ISelectionService)) as ISelectionService;
133 IDesignerHost host = this.GetService (typeof (IDesignerHost)) as IDesignerHost;
134 if (selectionSvc != null && host != null && selectionSvc.SelectionCount == 1) {
135 IComponent primarySelection = selectionSvc.PrimarySelection as IComponent;
136 if (primarySelection != null) {
137 IDesigner designer = host.GetDesigner (primarySelection);
138 if (designer != null)
139 selectionVerbs = designer.Verbs;
143 // Designer provided verbs have the higher precedence than the global
145 Dictionary <string, DesignerVerb> allVerbs = new Dictionary <string, DesignerVerb> ();
146 if (_globalVerbs != null) {
147 foreach (DesignerVerb verb in _globalVerbs)
148 allVerbs[verb.Text] = verb;
150 if (selectionVerbs != null) {
151 foreach (DesignerVerb verb in selectionVerbs)
152 allVerbs[verb.Text] = verb;
156 _verbs = new DesignerVerbCollection ();
160 foreach (DesignerVerb verb in allVerbs.Values)
164 protected MenuCommand FindCommand (Guid guid, int id)
166 return this.FindCommand (new CommandID (guid, id));
169 public MenuCommand FindCommand (CommandID commandID)
171 if (commandID == null)
172 throw new ArgumentNullException ("commandID");
174 MenuCommand command = null;
175 if (_commands != null)
176 _commands.TryGetValue (commandID, out command);
177 if (command == null) {
179 foreach (DesignerVerb verb in _verbs) {
180 if (verb.CommandID.Equals (commandID)) {
181 command = (MenuCommand) verb;
189 protected ICollection GetCommandList (Guid guid)
191 List<MenuCommand> list = new List<MenuCommand> ();
192 if (_commands != null) {
193 foreach (MenuCommand command in _commands.Values) {
194 if (command.CommandID.Guid == guid)
201 public virtual bool GlobalInvoke (CommandID commandID)
203 if (commandID == null)
204 throw new ArgumentNullException ("commandID");
206 MenuCommand command = this.FindCommand (commandID);
207 if (command != null) {
214 public virtual bool GlobalInvoke (CommandID commandID, object arg)
216 if (commandID == null)
217 throw new ArgumentNullException ("commandID");
219 MenuCommand command = this.FindCommand (commandID);
220 if (command != null) {
221 command.Invoke (arg);
227 protected virtual void OnCommandsChanged (MenuCommandsChangedEventArgs e)
229 if (MenuCommandsChanged != null)
230 MenuCommandsChanged (this, e);
233 public virtual void RemoveCommand (MenuCommand command)
236 throw new ArgumentNullException ("command");
237 if (_commands != null)
238 _commands.Remove (command.CommandID);
240 this.OnCommandsChanged (new MenuCommandsChangedEventArgs (MenuCommandsChangedType.CommandRemoved, null));
243 public virtual void RemoveVerb (DesignerVerb verb)
246 throw new ArgumentNullException ("verb");
248 if (_globalVerbs.Contains (verb))
249 _globalVerbs.Remove (verb);
251 this.OnCommandsChanged (new MenuCommandsChangedEventArgs (MenuCommandsChangedType.CommandRemoved, verb));
254 public virtual void ShowContextMenu (CommandID menuID, int x, int y)
258 protected object GetService (Type serviceType)
260 if (_serviceProvider != null)
261 return _serviceProvider.GetService (serviceType);