ToolboxItem.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // System.Drawing.Design.ToolboxItem.cs
  3. //
  4. // Authors:
  5. // Alejandro Sánchez Acosta
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Alejandro Sánchez Acosta
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Collections;
  34. using System.ComponentModel;
  35. using System.ComponentModel.Design;
  36. using System.Drawing;
  37. using System.Reflection;
  38. using System.Runtime.Serialization;
  39. namespace System.Drawing.Design
  40. {
  41. [Serializable]
  42. public class ToolboxItem : ISerializable
  43. {
  44. private AssemblyName assembly;
  45. private Bitmap bitmap;
  46. private ICollection filter = new ToolboxItemFilterAttribute[0];
  47. private string displayname = string.Empty;
  48. private bool locked = false;
  49. private string name = string.Empty;
  50. public ToolboxItem() {
  51. }
  52. public ToolboxItem (Type toolType) {
  53. Initialize (toolType);
  54. }
  55. public AssemblyName AssemblyName {
  56. get {
  57. return assembly;
  58. }
  59. set {
  60. CheckUnlocked ();
  61. assembly = value;
  62. }
  63. }
  64. public Bitmap Bitmap {
  65. get {
  66. return bitmap;
  67. }
  68. set {
  69. CheckUnlocked ();
  70. bitmap = value;
  71. }
  72. }
  73. public string DisplayName {
  74. get {
  75. return displayname;
  76. }
  77. set {
  78. CheckUnlocked ();
  79. displayname = value;
  80. }
  81. }
  82. public ICollection Filter {
  83. get {
  84. return filter;
  85. }
  86. set {
  87. CheckUnlocked ();
  88. filter = value;
  89. }
  90. }
  91. protected bool Locked {
  92. get {
  93. return locked;
  94. }
  95. }
  96. public string TypeName {
  97. get {
  98. return name;
  99. }
  100. set {
  101. CheckUnlocked ();
  102. name = value;
  103. }
  104. }
  105. protected void CheckUnlocked ()
  106. {
  107. throw new InvalidOperationException ("The ToolboxItem is locked");
  108. }
  109. public IComponent[] CreateComponents ()
  110. {
  111. return CreateComponents (null);
  112. }
  113. public IComponent[] CreateComponents (IDesignerHost host)
  114. {
  115. OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
  116. IComponent[] Comp = CreateComponentsCore (host);
  117. OnComponentsCreated ( new ToolboxComponentsCreatedEventArgs (Comp));
  118. return Comp;
  119. }
  120. [MonoTODO]
  121. protected virtual IComponent[] CreateComponentsCore (IDesignerHost host)
  122. {
  123. throw new NotImplementedException ();
  124. }
  125. protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
  126. {
  127. assembly = (AssemblyName)info.GetValue ("AssemblyName", typeof (AssemblyName));
  128. bitmap = (Bitmap)info.GetValue ("Bitmap", typeof (Bitmap));
  129. filter = (ICollection)info.GetValue ("Filter", typeof (ICollection));
  130. displayname = info.GetString ("DisplayName");
  131. locked = info.GetBoolean ("Locked");
  132. name = info.GetString ("TypeName");
  133. }
  134. public override bool Equals (object obj)
  135. {
  136. // FIXME: too harsh??
  137. if (!(obj is ToolboxItem))
  138. return false;
  139. if (obj == this)
  140. return true;
  141. return ((ToolboxItem) obj).AssemblyName.Equals (assembly) &&
  142. ((ToolboxItem) obj).Locked.Equals (locked) &&
  143. ((ToolboxItem) obj).TypeName.Equals (name) &&
  144. ((ToolboxItem) obj).DisplayName.Equals (displayname) &&
  145. ((ToolboxItem) obj).Bitmap.Equals (bitmap);
  146. }
  147. public override int GetHashCode ()
  148. {
  149. // FIXME: other algorithm?
  150. return string.Concat (name, displayname).GetHashCode ();
  151. }
  152. [MonoTODO]
  153. protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
  154. {
  155. throw new NotImplementedException ();
  156. }
  157. [MonoTODO]
  158. public virtual void Initialize (Type type)
  159. {
  160. // assembly = // FIXME we need to get the AssemblyName data from somewhere or create a new one
  161. displayname = type.Name;
  162. name = type.FullName;
  163. // seems to be a right place to create the bitmap
  164. bitmap = new Bitmap (16, 16); // FIXME set some default bitmap !?
  165. filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
  166. throw new NotImplementedException ();
  167. }
  168. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  169. {
  170. Serialize (info, context);
  171. }
  172. public void Lock ()
  173. {
  174. locked = true;
  175. }
  176. protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args)
  177. {
  178. if (ComponentsCreated != null)
  179. this.ComponentsCreated (this, args);
  180. }
  181. protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args)
  182. {
  183. if (ComponentsCreated != null)
  184. this.ComponentsCreating (this, args);
  185. }
  186. protected virtual void Serialize (SerializationInfo info, StreamingContext context)
  187. {
  188. info.AddValue ("AssemblyName", assembly);
  189. info.AddValue ("Bitmap", bitmap);
  190. info.AddValue ("Filter", filter);
  191. info.AddValue ("DisplayName", displayname);
  192. info.AddValue ("Locked", locked);
  193. info.AddValue ("TypeName", name);
  194. }
  195. public override string ToString()
  196. {
  197. return displayname;
  198. }
  199. public event ToolboxComponentsCreatedEventHandler ComponentsCreated;
  200. public event ToolboxComponentsCreatingEventHandler ComponentsCreating;
  201. }
  202. }