The Labs \ Source Viewer \ SSCLI \ System.ComponentModel.Design \ MenuCommand

  1. //------------------------------------------------------------------------------
  2. // <copyright file="MenuCommand.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*
  16. */
  17. namespace System.ComponentModel.Design
  18. {
  19.     using Microsoft.Win32;
  20.     using System;
  21.     using System.Collections;
  22.     using System.Collections.Specialized;
  23.     using System.ComponentModel;
  24.     using System.Diagnostics;
  25.     using System.Security.Permissions;
  26.    
  27.     /// <devdoc>
  28.     /// <para>
  29.     /// Represents a Windows
  30.     /// menu or toolbar item.
  31.     /// </para>
  32.     /// </devdoc>
  33.     [HostProtection(SharedState = true)]
  34.     [System.Runtime.InteropServices.ComVisible(true)]
  35.     [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.InheritanceDemand, Name = "FullTrust")]
  36.     [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name = "FullTrust")]
  37.     public class MenuCommand
  38.     {
  39.        
  40.         // Events that we suface or call on
  41.         //
  42.         private EventHandler execHandler;
  43.         private EventHandler statusHandler;
  44.        
  45.         private CommandID commandID;
  46.         private int status;
  47.         private IDictionary properties;
  48.        
  49.         /// <devdoc>
  50.         /// Indicates that the given command is enabled. An enabled command may
  51.         /// be selected by the user (it's not greyed out).
  52.         /// </devdoc>
  53.         private const int ENABLED = 2;
  54.         //tagOLECMDF.OLECMDF_ENABLED;
  55.         /// <devdoc>
  56.         /// Indicates that the given command is not visible on the command bar.
  57.         /// </devdoc>
  58.         private const int INVISIBLE = 16;
  59.        
  60.         /// <devdoc>
  61.         /// Indicates that the given command is checked in the "on" state.
  62.         /// </devdoc>
  63.         private const int CHECKED = 4;
  64.         // tagOLECMDF.OLECMDF_LATCHED;
  65.         /// <devdoc>
  66.         /// Indicates that the given command is supported. Marking a command
  67.         /// as supported indicates that the shell will not look any further up
  68.         /// the command target chain.
  69.         /// </devdoc>
  70.         private const int SUPPORTED = 1;
  71.         // tagOLECMDF.OLECMDF_SUPPORTED
  72.        
  73.         /// <devdoc>
  74.         /// <para>
  75.         /// Initializes a new instance of <see cref='System.ComponentModel.Design.MenuCommand'/>.
  76.         /// </para>
  77.         /// </devdoc>
  78.         public MenuCommand(EventHandler handler, CommandID command)
  79.         {
  80.             this.execHandler = handler;
  81.             this.commandID = command;
  82.             this.status = SUPPORTED | ENABLED;
  83.         }
  84.        
  85.         /// <devdoc>
  86.         /// <para> Gets or sets a value indicating whether this menu item is checked.</para>
  87.         /// </devdoc>
  88.         public virtual bool Checked {
  89.             get { return (status & CHECKED) != 0; }
  90.            
  91.             set { SetStatus(CHECKED, value); }
  92.         }
  93.        
  94.         /// <devdoc>
  95.         /// <para> Gets or
  96.         /// sets a value indicating whether this menu item is available.</para>
  97.         /// </devdoc>
  98.         public virtual bool Enabled {
  99.             get { return (status & ENABLED) != 0; }
  100.            
  101.             set { SetStatus(ENABLED, value); }
  102.         }
  103.        
  104.         private void SetStatus(int mask, bool value)
  105.         {
  106.             int newStatus = status;
  107.            
  108.             if (value) {
  109.                 newStatus |= mask;
  110.             }
  111.             else {
  112.                 newStatus &= ~mask;
  113.             }
  114.            
  115.             if (newStatus != status) {
  116.                 status = newStatus;
  117.                 OnCommandChanged(EventArgs.Empty);
  118.             }
  119.         }
  120.        
  121.         /// <devdoc>
  122.         /// </devdoc>
  123.         public virtual IDictionary Properties {
  124.             get {
  125.                 if (properties == null) {
  126.                     properties = new HybridDictionary();
  127.                 }
  128.                
  129.                 return properties;
  130.             }
  131.         }
  132.        
  133.        
  134.        
  135.        
  136.         /// <devdoc>
  137.         /// <para> Gets or sets a value
  138.         /// indicating whether this menu item is supported.</para>
  139.         /// </devdoc>
  140.         public virtual bool Supported {
  141.             get { return (status & SUPPORTED) != 0; }
  142.             set { SetStatus(SUPPORTED, value); }
  143.         }
  144.        
  145.         /// <devdoc>
  146.         /// <para> Gets or sets a value
  147.         /// indicating if this menu item is visible.</para>
  148.         /// </devdoc>
  149.         public virtual bool Visible {
  150.             get { return (status & INVISIBLE) == 0; }
  151.             set { SetStatus(INVISIBLE, !value); }
  152.         }
  153.        
  154.         /// <devdoc>
  155.         /// <para>
  156.         /// Occurs when the menu command changes.
  157.         /// </para>
  158.         /// </devdoc>
  159.         public event EventHandler CommandChanged {
  160.             add { statusHandler += value; }
  161.             remove { statusHandler -= value; }
  162.         }
  163.        
  164.         /// <devdoc>
  165.         /// <para>Gets the <see cref='System.ComponentModel.Design.CommandID'/> associated with this menu command.</para>
  166.         /// </devdoc>
  167.         public virtual CommandID CommandID {
  168.             get { return commandID; }
  169.         }
  170.        
  171.         /// <devdoc>
  172.         /// <para>
  173.         /// Invokes a menu item.
  174.         /// </para>
  175.         /// </devdoc>
  176.         public virtual void Invoke()
  177.         {
  178.             if (execHandler != null) {
  179.                 try {
  180.                     execHandler(this, EventArgs.Empty);
  181.                 }
  182.                 catch (CheckoutException cxe) {
  183.                     if (cxe == CheckoutException.Canceled)
  184.                         return;
  185.                    
  186.                     throw;
  187.                 }
  188.             }
  189.         }
  190.        
  191.         /// <devdoc>
  192.         /// <para>
  193.         /// Invokes a menu item. The default implementation of this method ignores
  194.         /// the argument, but deriving classes may override this method.
  195.         /// </para>
  196.         /// </devdoc>
  197.         public virtual void Invoke(object arg)
  198.         {
  199.             Invoke();
  200.         }
  201.        
  202.         /// <devdoc>
  203.         /// <para>
  204.         /// Gets the OLE command status code for this menu item.
  205.         /// </para>
  206.         /// </devdoc>
  207.         public virtual int OleStatus {
  208.             get { return status; }
  209.         }
  210.        
  211.         /// <devdoc>
  212.         /// <para>Provides notification and is called in response to
  213.         /// a <see cref='System.ComponentModel.Design.MenuCommand.CommandChanged'/> event.</para>
  214.         /// </devdoc>
  215.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
  216.         // Safe: FullTrust LinkDemand to instantiate an object of this class.
  217.         protected virtual void OnCommandChanged(EventArgs e)
  218.         {
  219.             if (statusHandler != null) {
  220.                 statusHandler(this, e);
  221.             }
  222.         }
  223.        
  224.         /// <devdoc>
  225.         /// Overrides object's ToString().
  226.         /// </devdoc>
  227.         public override string ToString()
  228.         {
  229.             string str = commandID.ToString() + " : ";
  230.             if ((status & SUPPORTED) != 0) {
  231.                 str += "Supported";
  232.             }
  233.             if ((status & ENABLED) != 0) {
  234.                 str += "|Enabled";
  235.             }
  236.             if ((status & INVISIBLE) == 0) {
  237.                 str += "|Visible";
  238.             }
  239.             if ((status & CHECKED) != 0) {
  240.                 str += "|Checked";
  241.             }
  242.             return str;
  243.         }
  244.     }
  245.    
  246. }

Developer Fusion