2 using System
.Runtime
.InteropServices
;
3 using System
.Security
.Permissions
;
4 using Microsoft
.VisualStudio
.OLE
.Interop
;
6 namespace Microsoft
.VisualStudio
.Shell
8 using System
.ComponentModel
;
9 using System
.ComponentModel
.Design
;
11 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCmdEventArgs"]/*' />
13 /// This is the set of arguments passed to a OleMenuCommand object when the
14 /// Invoke function is called
17 public class OleMenuCmdEventArgs
: System
.EventArgs
19 private object inParam
;
20 private IntPtr outParam
;
21 private OLECMDEXECOPT execOptions
;
23 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCmdEventArgs.OleMenuCmdEventArgs"]/*' />
25 /// Builds the OleMenuCmdEventArgs
27 /// <param name="inParam">The input parameter to the command function.</param>
28 /// <param name="outParam">A pointer to the parameter returned by the function</param>
29 public OleMenuCmdEventArgs(object inParam
, IntPtr outParam
) :
30 this(inParam
, outParam
, OLECMDEXECOPT
.OLECMDEXECOPT_DODEFAULT
)
34 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCmdEventArgs.OleMenuCmdEventArgs"]/*' />
36 /// Builds the OleMenuCmdEventArgs
38 /// <param name="inParam">The input parameter to the command function.</param>
39 /// <param name="outParam">A pointer to the parameter returned by the function</param>
40 /// <param name="options">Execution options for the command.</param>
41 public OleMenuCmdEventArgs(object inParam
, IntPtr outParam
, OLECMDEXECOPT options
) : base()
43 this.execOptions
= options
;
44 this.inParam
= inParam
;
45 this.outParam
= outParam
;
48 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCmdEventArgs.InValue"]/*' />
50 /// Gets the parameter passed as input to the command function
54 get { return inParam; }
57 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCmdEventArgs.Options"]/*' />
59 /// Gets the execution options for the command.
61 public OLECMDEXECOPT Options
63 get { return execOptions; }
66 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCmdEventArgs.OutValue"]/*' />
68 /// Gets a pointer to the parameter used as output by the command function
70 public IntPtr OutValue
72 get { return outParam; }
76 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand"]/*' />
78 /// This class is an expansion of MenuCommand.
80 [System
.Runtime
.InteropServices
.ComVisible(true)]
81 public class OleMenuCommand
: MenuCommand
, IOleMenuCommand
, IMenuCommandInvokeEx
83 /// <summary>The event handler called to execute the command.</summary>
84 private EventHandler execHandler
;
86 /// The event handler caller before getting the command status; it can be used to
87 /// implement a command with a dynamic status.
89 private EventHandler beforeQueryStatusHandler
;
91 // Used in the case of dynamic menu (created with the DYNAMICITEMSTART option)
92 private int matchedCommandId
;
93 // If the command supports parameters, then this string will contain the description
95 private string parametersDescription
;
97 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.OleMenuCommand"]/*' />
99 /// Builds a new OleMenuCommand.
101 /// <param name="invokeHandler">The event handler called to execute the command.</param>
102 /// <param name="id">ID of the command.</param>
103 public OleMenuCommand(EventHandler invokeHandler
, CommandID id
) :
104 base(invokeHandler
, id
)
106 PrivateInit(invokeHandler
, null, null, String
.Empty
);
109 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.OleMenuCommand1"]/*' />
111 /// Builds a new OleMenuCommand.
113 /// <param name="invokeHandler">The event handler called to execute the command.</param>
114 /// <param name="id">ID of the command.</param>
115 /// <param name="Text">The text of the command.</param>
116 public OleMenuCommand(EventHandler invokeHandler
, CommandID id
, string Text
) :
117 base(invokeHandler
, id
)
119 PrivateInit(invokeHandler
, null, null, Text
);
122 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.OleMenuCommand2"]/*' />
124 /// Builds a new OleMenuCommand.
126 /// <param name="invokeHandler">The event handler called to execute the command.</param>
127 /// <param name="changeHandler">The event handler called when the command's status changes.</param>
128 /// <param name="id">ID of the command.</param>
129 public OleMenuCommand(EventHandler invokeHandler
, EventHandler changeHandler
, CommandID id
) :
130 base(invokeHandler
, id
)
132 PrivateInit(invokeHandler
, changeHandler
, null, String
.Empty
);
135 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.OleMenuCommand3"]/*' />
137 /// Builds a new OleMenuCommand.
139 /// <param name="invokeHandler">The event handler called to execute the command.</param>
140 /// <param name="changeHandler">The event handler called when the command's status changes.</param>
141 /// <param name="id">ID of the command.</param>
142 /// <param name="Text">The text of the command.</param>
143 public OleMenuCommand(EventHandler invokeHandler
, EventHandler changeHandler
, CommandID id
, string Text
) :
144 base(invokeHandler
, id
)
146 PrivateInit(invokeHandler
, changeHandler
, null, Text
);
149 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.OleMenuCommand4"]/*' />
151 /// Builds a new OleMenuCommand.
153 /// <param name="invokeHandler">The event handler called to execute the command.</param>
154 /// <param name="changeHandler">The event handler called when the command's status changes.</param>
155 /// <param name="beforeQueryStatus">Event handler called when a lient asks for the command status.</param>
156 /// <param name="id">ID of the command.</param>
157 public OleMenuCommand(EventHandler invokeHandler
, EventHandler changeHandler
, EventHandler beforeQueryStatus
, CommandID id
) :
158 base(invokeHandler
, id
)
160 PrivateInit(invokeHandler
, changeHandler
, beforeQueryStatus
, String
.Empty
);
163 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.OleMenuCommand5"]/*' />
165 /// Builds a new OleMenuCommand.
167 /// <param name="invokeHandler">The event handler called to execute the command.</param>
168 /// <param name="changeHandler">The event handler called when the command's status changes.</param>
169 /// <param name="beforeQueryStatus">Event handler called when a lient asks for the command status.</param>
170 /// <param name="id">ID of the command.</param>
171 /// <param name="Text">The text of the command.</param>
172 public OleMenuCommand(EventHandler invokeHandler
, EventHandler changeHandler
, EventHandler beforeQueryStatus
, CommandID id
, string Text
) :
173 base(invokeHandler
, id
)
175 PrivateInit(invokeHandler
, changeHandler
, beforeQueryStatus
, Text
);
178 private void PrivateInit(EventHandler handler
, EventHandler changeHandler
, EventHandler beforeQS
, string Text
)
180 execHandler
= handler
;
181 if (changeHandler
!= null)
183 this.CommandChanged
+= changeHandler
;
185 beforeQueryStatusHandler
= beforeQS
;
187 parametersDescription
= null;
190 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.BeforeQueryStatus"]/*' />
192 /// Event fired when a client asks for the status of the command.
195 public event EventHandler BeforeQueryStatus
197 add { beforeQueryStatusHandler += value; }
198 remove { beforeQueryStatusHandler -= value; }
201 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.OleStatus"]/*' />
202 public override int OleStatus
204 [PermissionSet(SecurityAction
.LinkDemand
, Name
= "FullTrust")]
207 if (null != beforeQueryStatusHandler
)
209 beforeQueryStatusHandler(this, EventArgs
.Empty
);
211 return base.OleStatus
;
215 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.ParametersDescription"]/*' />
217 /// Get or set the string that describes the paraeters accepted by the command.
219 public string ParametersDescription
221 get { return parametersDescription; }
222 set { parametersDescription = value; }
225 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.Invoke"]/*' />
227 /// Executes the command.
229 /// <param name="inArg">The parameter passed to the command.</param>
230 [PermissionSet(SecurityAction
.LinkDemand
, Name
= "FullTrust")]
231 public override void Invoke(object inArg
)
235 OleMenuCmdEventArgs args
= new OleMenuCmdEventArgs(inArg
, NativeMethods
.InvalidIntPtr
);
236 execHandler(this, args
);
238 catch (CheckoutException ex
)
240 if (CheckoutException
.Canceled
!= ex
)
245 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.Invoke1"]/*' />
247 /// Executes the command.
249 /// <param name="inArg">The parameter passed to the command.</param>
250 /// <param name="outArg">The parameter returned by the command.</param>
251 [EnvironmentPermission(SecurityAction
.LinkDemand
, Unrestricted
= true)]
252 public virtual void Invoke(object inArg
, IntPtr outArg
)
254 Invoke(inArg
, outArg
, OLECMDEXECOPT
.OLECMDEXECOPT_DODEFAULT
);
257 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.Invoke2"]/*' />
259 /// Executes the command with execution options.
261 /// <param name="inArg">The parameter passed to the command.</param>
262 /// <param name="outArg">The parameter returned by the command.</param>
263 /// <param name="options">The execution options for the command.</param>
264 [EnvironmentPermission(SecurityAction
.LinkDemand
, Unrestricted
= true)]
265 [CLSCompliant(false)]
266 public virtual void Invoke(object inArg
, IntPtr outArg
, OLECMDEXECOPT options
)
270 OleMenuCmdEventArgs args
= new OleMenuCmdEventArgs(inArg
, outArg
, options
);
271 execHandler(this, args
);
273 catch (CheckoutException ex
)
275 if (CheckoutException
.Canceled
!= ex
)
280 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.Text"]/*' />
282 /// Gets or sets the text for the command.
285 public virtual string Text
288 set { if (text != value) { text = value; OnCommandChanged(EventArgs.Empty); }
}
291 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.DynamicItemMatch"]/*' />
293 /// Allows a dynamic item command to match the subsequent items in its list. This must be overriden
294 /// when implementing a menu via DYNAMICITEMSTART.
296 /// <param name="cmdId"></param>
297 /// <returns></returns>
298 public virtual bool DynamicItemMatch(int cmdId
)
303 /// <include file='doc\OleMenuCommand.uex' path='docs/doc[@for="OleMenuCommand.MatchedCommandId"]/*' />
305 /// The command id that was most recently used to match this command. This must be set by the sub-class
306 /// when a match occurs and can be used to identify the actual command being invoked.
309 public int MatchedCommandId
311 get { return matchedCommandId; }
312 set { matchedCommandId = value; }