-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathBuiltInContextMenu.cs
261 lines (247 loc) · 11.2 KB
/
BuiltInContextMenu.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
namespace Be.Windows.Forms
{
/// <summary>
/// Defines a build-in ContextMenuStrip manager for HexBox control to show Copy, Cut, Paste menu in contextmenu of the control.
/// </summary>
[TypeConverterAttribute(typeof(ExpandableObjectConverter))]
public sealed class BuiltInContextMenu : Component
{
/// <summary>
/// Contains the HexBox control.
/// </summary>
HexBox _hexBox;
/// <summary>
/// Contains the ContextMenuStrip control.
/// </summary>
ContextMenuStrip _contextMenuStrip;
/// <summary>
/// Contains the "Cut"-ToolStripMenuItem object.
/// </summary>
ToolStripMenuItem _cutToolStripMenuItem;
/// <summary>
/// Contains the "Copy"-ToolStripMenuItem object.
/// </summary>
ToolStripMenuItem _copyToolStripMenuItem;
/// <summary>
/// Contains the "Copy as Hex"-ToolStripMenuItem object.
/// </summary>
ToolStripMenuItem _copyAsHexToolStripMenuItem;
/// <summary>
/// Contains the "Paste"-ToolStripMenuItem object.
/// </summary>
ToolStripMenuItem _pasteToolStripMenuItem;
/// <summary>
/// Contains the "Select All"-ToolStripMenuItem object.
/// </summary>
ToolStripMenuItem _selectAllToolStripMenuItem;
/// <summary>
/// Initializes a new instance of BuildInContextMenu class.
/// </summary>
/// <param name="hexBox">the HexBox control</param>
internal BuiltInContextMenu(HexBox hexBox)
{
_hexBox = hexBox;
_hexBox.ByteProviderChanged += new EventHandler(HexBox_ByteProviderChanged);
}
/// <summary>
/// If ByteProvider
/// </summary>
/// <param name="sender">the sender object</param>
/// <param name="e">the event data</param>
void HexBox_ByteProviderChanged(object sender, EventArgs e)
{
CheckBuiltInContextMenu();
}
/// <summary>
/// Assigns the ContextMenuStrip control to the HexBox control.
/// </summary>
void CheckBuiltInContextMenu()
{
if (Util.DesignMode)
return;
if (this._contextMenuStrip == null)
{
ContextMenuStrip cms = new ContextMenuStrip();
_cutToolStripMenuItem = new ToolStripMenuItem(CutMenuItemTextInternal, CutMenuItemImage, new EventHandler(CutMenuItem_Click));
cms.Items.Add(_cutToolStripMenuItem);
_copyToolStripMenuItem = new ToolStripMenuItem(CopyMenuItemTextInternal, CopyMenuItemImage, new EventHandler(CopyMenuItem_Click));
cms.Items.Add(_copyToolStripMenuItem);
_copyAsHexToolStripMenuItem = new ToolStripMenuItem(CopyAsHexMenuItemTextInternal, CopyMenuItemImage, new EventHandler( CopyAsHexMenuItem_Click ) );
cms.Items.Add( _copyAsHexToolStripMenuItem );
_pasteToolStripMenuItem = new ToolStripMenuItem( PasteMenuItemTextInternal, PasteMenuItemImage, new EventHandler( PasteMenuItem_Click ) );
cms.Items.Add(_pasteToolStripMenuItem);
cms.Items.Add(new ToolStripSeparator());
_selectAllToolStripMenuItem = new ToolStripMenuItem(SelectAllMenuItemTextInternal, SelectAllMenuItemImage, new EventHandler(SelectAllMenuItem_Click));
cms.Items.Add(_selectAllToolStripMenuItem);
cms.Opening += new CancelEventHandler(BuildInContextMenuStrip_Opening);
_contextMenuStrip = cms;
}
if (this._hexBox.ByteProvider == null && this._hexBox.ContextMenuStrip == this._contextMenuStrip)
this._hexBox.ContextMenuStrip = null;
else if (this._hexBox.ByteProvider != null && this._hexBox.ContextMenuStrip == null)
this._hexBox.ContextMenuStrip = _contextMenuStrip;
}
/// <summary>
/// Before opening the ContextMenuStrip, we manage the availability of the items.
/// </summary>
/// <param name="sender">the sender object</param>
/// <param name="e">the event data</param>
void BuildInContextMenuStrip_Opening(object sender, CancelEventArgs e)
{
_cutToolStripMenuItem.Enabled = this._hexBox.CanCut();
_copyToolStripMenuItem.Enabled = this._hexBox.CanCopy();
_copyAsHexToolStripMenuItem.Enabled = this._hexBox.CanCopy();
_pasteToolStripMenuItem.Enabled = this._hexBox.CanPaste();
_selectAllToolStripMenuItem.Enabled = this._hexBox.CanSelectAll();
}
/// <summary>
/// The handler for the "Cut"-Click event
/// </summary>
/// <param name="sender">the sender object</param>
/// <param name="e">the event data</param>
void CutMenuItem_Click(object sender, EventArgs e) { this._hexBox.Cut(); }
/// <summary>
/// The handler for the "Copy"-Click event
/// </summary>
/// <param name="sender">the sender object</param>
/// <param name="e">the event data</param>
void CopyMenuItem_Click(object sender, EventArgs e) { this._hexBox.Copy(); }
/// <summary>
/// The handler for the "Copy as Hex"-Click event
/// </summary>
/// <param name="sender">the sender object</param>
/// <param name="e">the event data</param>
void CopyAsHexMenuItem_Click( object sender, EventArgs e )
{
this._hexBox.CopyHex();
}
/// <summary>
/// The handler for the "Paste"-Click event
/// </summary>
/// <param name="sender">the sender object</param>
/// <param name="e">the event data</param>
void PasteMenuItem_Click(object sender, EventArgs e) { this._hexBox.Paste(); }
/// <summary>
/// The handler for the "Select All"-Click event
/// </summary>
/// <param name="sender">the sender object</param>
/// <param name="e">the event data</param>
void SelectAllMenuItem_Click(object sender, EventArgs e) { this._hexBox.SelectAll(); }
/// <summary>
/// Gets or sets the custom text of the "Copy" ContextMenuStrip item.
/// </summary>
[Category("BuiltIn-ContextMenu"), DefaultValue(null), Localizable(true)]
public string CopyMenuItemText
{
get { return _copyMenuItemText; }
set { _copyMenuItemText = value; }
} string _copyMenuItemText;
/// <summary>
/// Gets or sets the custom text of the "Copy as Hex" ContextMenuStrip item.
/// </summary>
[Category("BuiltIn-ContextMenu"), DefaultValue(null), Localizable(true)]
public string CopyAsHexMenuItemText
{
get { return _copyAsHexMenuItemText; }
set
{
_copyAsHexMenuItemText = value;
}
} string _copyAsHexMenuItemText;
/// <summary>
/// Gets or sets the custom text of the "Cut" ContextMenuStrip item.
/// </summary>
[Category("BuiltIn-ContextMenu"), DefaultValue(null), Localizable(true)]
public string CutMenuItemText
{
get { return _cutMenuItemText; }
set { _cutMenuItemText = value; }
} string _cutMenuItemText;
/// <summary>
/// Gets or sets the custom text of the "Paste" ContextMenuStrip item.
/// </summary>
[Category("BuiltIn-ContextMenu"), DefaultValue(null), Localizable(true)]
public string PasteMenuItemText
{
get { return _pasteMenuItemText; }
set { _pasteMenuItemText = value; }
} string _pasteMenuItemText;
/// <summary>
/// Gets or sets the custom text of the "Select All" ContextMenuStrip item.
/// </summary>
[Category("BuiltIn-ContextMenu"), DefaultValue(null), Localizable(true)]
public string SelectAllMenuItemText
{
get { return _selectAllMenuItemText; }
set { _selectAllMenuItemText = value; }
} string _selectAllMenuItemText = null;
/// <summary>
/// Gets the text of the "Cut" ContextMenuStrip item.
/// </summary>
internal string CutMenuItemTextInternal { get { return !string.IsNullOrEmpty(CutMenuItemText) ? CutMenuItemText : "Cut"; } }
/// <summary>
/// Gets the text of the "Copy" ContextMenuStrip item.
/// </summary>
internal string CopyMenuItemTextInternal { get { return !string.IsNullOrEmpty(CopyMenuItemText) ? CopyMenuItemText : "Copy"; } }
/// <summary>
/// Gets the text of the "Copy as Hex" ContextMenuStrip item.
/// </summary>
internal string CopyAsHexMenuItemTextInternal
{
get
{
return !string.IsNullOrEmpty( CopyAsHexMenuItemText ) ? CopyAsHexMenuItemText : "Copy as Hex";
}
}
/// <summary>
/// Gets the text of the "Paste" ContextMenuStrip item.
/// </summary>
internal string PasteMenuItemTextInternal { get { return !string.IsNullOrEmpty(PasteMenuItemText) ? PasteMenuItemText : "Paste"; } }
/// <summary>
/// Gets the text of the "Select All" ContextMenuStrip item.
/// </summary>
internal string SelectAllMenuItemTextInternal { get { return !string.IsNullOrEmpty(SelectAllMenuItemText) ? SelectAllMenuItemText : "Select All"; } }
/// <summary>
/// Gets or sets the image of the "Cut" ContextMenuStrip item.
/// </summary>
[Category("BuiltIn-ContextMenu"), DefaultValue(null)]
public Image CutMenuItemImage
{
get { return _cutMenuItemImage; }
set { _cutMenuItemImage = value; }
} Image _cutMenuItemImage = null;
/// <summary>
/// Gets or sets the image of the "Copy" ContextMenuStrip item.
/// </summary>
[Category("BuiltIn-ContextMenu"), DefaultValue(null)]
public Image CopyMenuItemImage
{
get { return _copyMenuItemImage; }
set { _copyMenuItemImage = value; }
} Image _copyMenuItemImage = null;
/// <summary>
/// Gets or sets the image of the "Paste" ContextMenuStrip item.
/// </summary>
[Category("BuiltIn-ContextMenu"), DefaultValue(null)]
public Image PasteMenuItemImage
{
get { return _pasteMenuItemImage; }
set { _pasteMenuItemImage = value; }
} Image _pasteMenuItemImage = null;
/// <summary>
/// Gets or sets the image of the "Select All" ContextMenuStrip item.
/// </summary>
[Category("BuiltIn-ContextMenu"), DefaultValue(null)]
public Image SelectAllMenuItemImage
{
get { return _selectAllMenuItemImage; }
set { _selectAllMenuItemImage = value; }
} Image _selectAllMenuItemImage = null;
}
}