using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; namespace Be.Windows.Forms { /// /// Defines a build-in ContextMenuStrip manager for HexBox control to show Copy, Cut, Paste menu in contextmenu of the control. /// [TypeConverterAttribute(typeof(ExpandableObjectConverter))] public sealed class BuiltInContextMenu : Component { /// /// Contains the HexBox control. /// HexBox _hexBox; /// /// Contains the ContextMenuStrip control. /// ContextMenuStrip _contextMenuStrip; /// /// Contains the "Cut"-ToolStripMenuItem object. /// ToolStripMenuItem _cutToolStripMenuItem; /// /// Contains the "Copy"-ToolStripMenuItem object. /// ToolStripMenuItem _copyToolStripMenuItem; /// /// Contains the "Paste"-ToolStripMenuItem object. /// ToolStripMenuItem _pasteToolStripMenuItem; /// /// Contains the "Select All"-ToolStripMenuItem object. /// ToolStripMenuItem _selectAllToolStripMenuItem; /// /// Initializes a new instance of BuildInContextMenu class. /// /// the HexBox control internal BuiltInContextMenu(HexBox hexBox) { _hexBox = hexBox; _hexBox.ByteProviderChanged += new EventHandler(HexBox_ByteProviderChanged); } /// /// If ByteProvider /// /// the sender object /// the event data void HexBox_ByteProviderChanged(object sender, EventArgs e) { CheckBuiltInContextMenu(); } /// /// Assigns the ContextMenuStrip control to the HexBox control. /// 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); _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 != null) this._hexBox.ContextMenuStrip = null; else if (this._hexBox.ByteProvider != null && this._hexBox.ContextMenuStrip == null) this._hexBox.ContextMenuStrip = _contextMenuStrip; } /// /// Before opening the ContextMenuStrip, we manage the availability of the items. /// /// the sender object /// the event data void BuildInContextMenuStrip_Opening(object sender, CancelEventArgs e) { _cutToolStripMenuItem.Enabled = this._hexBox.CanCut(); _copyToolStripMenuItem.Enabled = this._hexBox.CanCopy(); _pasteToolStripMenuItem.Enabled = this._hexBox.CanPaste(); _selectAllToolStripMenuItem.Enabled = this._hexBox.CanSelectAll(); } /// /// The handler for the "Cut"-Click event /// /// the sender object /// the event data void CutMenuItem_Click(object sender, EventArgs e) { this._hexBox.Cut(); } /// /// The handler for the "Copy"-Click event /// /// the sender object /// the event data void CopyMenuItem_Click(object sender, EventArgs e) { this._hexBox.Copy(); } /// /// The handler for the "Paste"-Click event /// /// the sender object /// the event data void PasteMenuItem_Click(object sender, EventArgs e) { this._hexBox.Paste(); } /// /// The handler for the "Select All"-Click event /// /// the sender object /// the event data void SelectAllMenuItem_Click(object sender, EventArgs e) { this._hexBox.SelectAll(); } /// /// Gets or sets the custom text of the "Copy" ContextMenuStrip item. /// [Category("BuiltIn-ContextMenu"), DefaultValue(null), Localizable(true)] public string CopyMenuItemText { get { return _copyMenuItemText; } set { _copyMenuItemText = value; } } string _copyMenuItemText; /// /// Gets or sets the custom text of the "Cut" ContextMenuStrip item. /// [Category("BuiltIn-ContextMenu"), DefaultValue(null), Localizable(true)] public string CutMenuItemText { get { return _cutMenuItemText; } set { _cutMenuItemText = value; } } string _cutMenuItemText; /// /// Gets or sets the custom text of the "Paste" ContextMenuStrip item. /// [Category("BuiltIn-ContextMenu"), DefaultValue(null), Localizable(true)] public string PasteMenuItemText { get { return _pasteMenuItemText; } set { _pasteMenuItemText = value; } } string _pasteMenuItemText; /// /// Gets or sets the custom text of the "Select All" ContextMenuStrip item. /// [Category("BuiltIn-ContextMenu"), DefaultValue(null), Localizable(true)] public string SelectAllMenuItemText { get { return _selectAllMenuItemText; } set { _selectAllMenuItemText = value; } } string _selectAllMenuItemText = null; /// /// Gets the text of the "Cut" ContextMenuStrip item. /// internal string CutMenuItemTextInternal { get { return !string.IsNullOrEmpty(CutMenuItemText) ? CutMenuItemText : "Cut"; } } /// /// Gets the text of the "Copy" ContextMenuStrip item. /// internal string CopyMenuItemTextInternal { get { return !string.IsNullOrEmpty(CopyMenuItemText) ? CopyMenuItemText : "Copy"; } } /// /// Gets the text of the "Paste" ContextMenuStrip item. /// internal string PasteMenuItemTextInternal { get { return !string.IsNullOrEmpty(PasteMenuItemText) ? PasteMenuItemText : "Paste"; } } /// /// Gets the text of the "Select All" ContextMenuStrip item. /// internal string SelectAllMenuItemTextInternal { get { return !string.IsNullOrEmpty(SelectAllMenuItemText) ? SelectAllMenuItemText : "SelectAll"; } } /// /// Gets or sets the image of the "Cut" ContextMenuStrip item. /// [Category("BuiltIn-ContextMenu"), DefaultValue(null)] public Image CutMenuItemImage { get { return _cutMenuItemImage; } set { _cutMenuItemImage = value; } } Image _cutMenuItemImage = null; /// /// Gets or sets the image of the "Copy" ContextMenuStrip item. /// [Category("BuiltIn-ContextMenu"), DefaultValue(null)] public Image CopyMenuItemImage { get { return _copyMenuItemImage; } set { _copyMenuItemImage = value; } } Image _copyMenuItemImage = null; /// /// Gets or sets the image of the "Paste" ContextMenuStrip item. /// [Category("BuiltIn-ContextMenu"), DefaultValue(null)] public Image PasteMenuItemImage { get { return _pasteMenuItemImage; } set { _pasteMenuItemImage = value; } } Image _pasteMenuItemImage = null; /// /// Gets or sets the image of the "Select All" ContextMenuStrip item. /// [Category("BuiltIn-ContextMenu"), DefaultValue(null)] public Image SelectAllMenuItemImage { get { return _selectAllMenuItemImage; } set { _selectAllMenuItemImage = value; } } Image _selectAllMenuItemImage = null; } }