MenuCommand.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // System.ComponentModel.Design.MenuCommand.cs
  3. //
  4. // Authors:
  5. // Alejandro Sánchez Acosta <[email protected]>
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Alejandro Sánchez Acosta
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Collections;
  32. using System.Runtime.InteropServices;
  33. namespace System.ComponentModel.Design
  34. {
  35. [ComVisible(true)]
  36. public class MenuCommand
  37. {
  38. private EventHandler handler;
  39. private CommandID command;
  40. private bool ischecked = false;
  41. private bool enabled = true;
  42. private bool issupported = true;
  43. private bool visible = true;
  44. #if NET_2_0
  45. private Hashtable properties;
  46. #endif
  47. public MenuCommand (EventHandler handler, CommandID command)
  48. {
  49. this.handler = handler;
  50. this.command = command;
  51. }
  52. public virtual bool Checked {
  53. get {
  54. return ischecked;
  55. }
  56. set {
  57. if (ischecked != value) {
  58. ischecked = value;
  59. OnCommandChanged (EventArgs.Empty);
  60. }
  61. }
  62. }
  63. public virtual CommandID CommandID {
  64. get {
  65. return command;
  66. }
  67. }
  68. public virtual bool Enabled {
  69. get {
  70. return enabled;
  71. }
  72. set {
  73. if (enabled != value)
  74. {
  75. enabled = value;
  76. OnCommandChanged (EventArgs.Empty);
  77. }
  78. }
  79. }
  80. [MonoTODO]
  81. public virtual int OleStatus {
  82. get {
  83. // This is calcualted from the other properties, but the docs to not tell how
  84. // Default seems to be "3", but changes with diffentent set properties
  85. return 3;
  86. }
  87. }
  88. #if NET_2_0
  89. public virtual IDictionary Properties {
  90. get {
  91. if (properties == null)
  92. properties = new Hashtable ();
  93. return properties;
  94. }
  95. }
  96. #endif
  97. public virtual bool Supported {
  98. get {
  99. return issupported;
  100. }
  101. set {
  102. issupported = value;
  103. }
  104. }
  105. public virtual bool Visible {
  106. get {
  107. return visible;
  108. }
  109. set {
  110. visible = value;
  111. }
  112. }
  113. public virtual void Invoke()
  114. {
  115. // FIXME Not sure if this invocation is what should be done here
  116. if (handler != null)
  117. handler (this, EventArgs.Empty);
  118. }
  119. #if NET_2_0
  120. [MonoNotSupported("")]
  121. public virtual void Invoke (object arg)
  122. {
  123. throw new NotImplementedException ();
  124. }
  125. #endif
  126. protected virtual void OnCommandChanged (EventArgs e)
  127. {
  128. if (CommandChanged != null)
  129. CommandChanged (this, e);
  130. }
  131. public override string ToString()
  132. {
  133. // MS runtime produces a NullReferenceException here if CommandID property == null
  134. // which I guess isn't a good idea (throwing exceptions in ToStrings???) - bug in MS??
  135. string commandpart = string.Empty;
  136. if (command != null)
  137. commandpart = command.ToString ();
  138. commandpart = string.Concat (commandpart, " : ");
  139. if (this.Supported)
  140. commandpart = string.Concat (commandpart, "Supported");
  141. if (this.Enabled)
  142. commandpart = string.Concat (commandpart, "|Enabled");
  143. if (this.Visible)
  144. commandpart = string.Concat (commandpart, "|Visible");
  145. if (this.Checked)
  146. commandpart = string.Concat (commandpart, "|Checked");
  147. return commandpart;
  148. }
  149. public event EventHandler CommandChanged;
  150. }
  151. }