/
MenuCommand.cs
186 lines (161 loc) · 5.61 KB
/
MenuCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
namespace System.ComponentModel.Design
{
/// <summary>
/// Represents a Windows menu or toolbar item.
/// </summary>
public class MenuCommand
{
// Events that we suface or call on
private readonly EventHandler? _execHandler;
private int _status;
private IDictionary? _properties;
/// <summary>
/// Indicates that the given command is enabled. An enabled command may
/// be selected by the user (it's not greyed out).
/// </summary>
private const int ENABLED = 0x02; //tagOLECMDF.OLECMDF_ENABLED;
/// <summary>
/// Indicates that the given command is not visible on the command bar.
/// </summary>
private const int INVISIBLE = 0x10;
/// <summary>
/// Indicates that the given command is checked in the "on" state.
/// </summary>
private const int CHECKED = 0x04; // tagOLECMDF.OLECMDF_LATCHED;
/// <summary>
/// Indicates that the given command is supported. Marking a command
/// as supported indicates that the shell will not look any further up
/// the command target chain.
/// </summary>
private const int SUPPORTED = 0x01; // tagOLECMDF.OLECMDF_SUPPORTED
/// <summary>
/// Initializes a new instance of <see cref='System.ComponentModel.Design.MenuCommand'/>.
/// </summary>
public MenuCommand(EventHandler? handler, CommandID? command)
{
_execHandler = handler;
CommandID = command;
_status = SUPPORTED | ENABLED;
}
/// <summary>
/// Gets or sets a value indicating whether this menu item is checked.
/// </summary>
public virtual bool Checked
{
get => (_status & CHECKED) != 0;
set => SetStatus(CHECKED, value);
}
/// <summary>
/// Gets or sets a value indicating whether this menu item is available.
/// </summary>
public virtual bool Enabled
{
get => (_status & ENABLED) != 0;
set => SetStatus(ENABLED, value);
}
private void SetStatus(int mask, bool value)
{
int newStatus = _status;
if (value)
{
newStatus |= mask;
}
else
{
newStatus &= ~mask;
}
if (newStatus != _status)
{
_status = newStatus;
OnCommandChanged(EventArgs.Empty);
}
}
public virtual IDictionary Properties => _properties ??= new HybridDictionary();
/// <summary>
/// Gets or sets a value indicating whether this menu item is supported.
/// </summary>
public virtual bool Supported
{
get => (_status & SUPPORTED) != 0;
set => SetStatus(SUPPORTED, value);
}
/// <summary>
/// Gets or sets a value indicating if this menu item is visible.
/// </summary>
public virtual bool Visible
{
get => (_status & INVISIBLE) == 0;
set => SetStatus(INVISIBLE, !value);
}
/// <summary>
/// Occurs when the menu command changes.
/// </summary>
public event EventHandler? CommandChanged;
/// <summary>
/// Gets the <see cref='System.ComponentModel.Design.CommandID'/> associated with this menu command.
/// </summary>
public virtual CommandID? CommandID { get; }
/// <summary>
/// Invokes a menu item.
/// </summary>
public virtual void Invoke()
{
if (_execHandler != null)
{
try
{
_execHandler(this, EventArgs.Empty);
}
catch (CheckoutException cxe)
{
if (cxe == CheckoutException.Canceled)
return;
throw;
}
}
}
/// <summary>
/// Invokes a menu item. The default implementation of this method ignores
/// the argument, but deriving classes may override this method.
/// </summary>
public virtual void Invoke(object arg) => Invoke();
/// <summary>
/// Gets the OLE command status code for this menu item.
/// </summary>
public virtual int OleStatus => _status;
/// <summary>
/// Provides notification and is called in response to
/// a <see cref='System.ComponentModel.Design.MenuCommand.CommandChanged'/> event.
/// </summary>
protected virtual void OnCommandChanged(EventArgs e) => CommandChanged?.Invoke(this, e);
/// <summary>
/// Overrides object's ToString().
/// </summary>
public override string ToString()
{
string str = (CommandID?.ToString() ?? "") + " : ";
if ((_status & SUPPORTED) != 0)
{
str += "Supported";
}
if ((_status & ENABLED) != 0)
{
str += "|Enabled";
}
if ((_status & INVISIBLE) == 0)
{
str += "|Visible";
}
if ((_status & CHECKED) != 0)
{
str += "|Checked";
}
return str;
}
}
}