ToolboxItem.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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-2005 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.Reflection;
  37. using System.Runtime.Serialization;
  38. using System.Security.Permissions;
  39. namespace System.Drawing.Design
  40. {
  41. [Serializable]
  42. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  43. [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
  44. public class ToolboxItem : ISerializable
  45. {
  46. private AssemblyName assembly;
  47. private Bitmap bitmap = null;
  48. private ICollection filter = new ToolboxItemFilterAttribute[0];
  49. private string displayname = string.Empty;
  50. private bool locked = false;
  51. private string name = string.Empty;
  52. public ToolboxItem() {
  53. }
  54. public ToolboxItem (Type toolType) {
  55. Initialize (toolType);
  56. }
  57. public AssemblyName AssemblyName {
  58. get {
  59. return assembly;
  60. }
  61. set {
  62. CheckUnlocked ();
  63. assembly = value;
  64. }
  65. }
  66. public Bitmap Bitmap {
  67. get {
  68. return bitmap;
  69. }
  70. set {
  71. CheckUnlocked ();
  72. bitmap = value;
  73. }
  74. }
  75. public string DisplayName {
  76. get {
  77. return displayname;
  78. }
  79. set {
  80. CheckUnlocked ();
  81. displayname = value;
  82. }
  83. }
  84. public ICollection Filter {
  85. get {
  86. return filter;
  87. }
  88. set {
  89. CheckUnlocked ();
  90. filter = value;
  91. }
  92. }
  93. protected bool Locked {
  94. get {
  95. return locked;
  96. }
  97. }
  98. public string TypeName {
  99. get {
  100. return name;
  101. }
  102. set {
  103. CheckUnlocked ();
  104. name = value;
  105. }
  106. }
  107. protected void CheckUnlocked ()
  108. {
  109. if (locked)
  110. throw new InvalidOperationException ("The ToolboxItem is locked");
  111. }
  112. public IComponent[] CreateComponents ()
  113. {
  114. return CreateComponents (null);
  115. }
  116. public IComponent[] CreateComponents (IDesignerHost host)
  117. {
  118. OnComponentsCreating (new ToolboxComponentsCreatingEventArgs (host));
  119. IComponent[] Comp = CreateComponentsCore (host);
  120. OnComponentsCreated ( new ToolboxComponentsCreatedEventArgs (Comp));
  121. return Comp;
  122. }
  123. [MonoTODO ("get error handling logic correct")]
  124. protected virtual IComponent[] CreateComponentsCore (IDesignerHost host)
  125. {
  126. if (host == null)
  127. throw new ArgumentNullException("host");
  128. OnComponentsCreating(new ToolboxComponentsCreatingEventArgs(host));
  129. IComponent[] components;
  130. Type type = GetType(host, assembly, name, true);
  131. if (type == null)
  132. components = new IComponent[] { };
  133. else
  134. components = new IComponent[] { host.CreateComponent(type) };
  135. OnComponentsCreated(new ToolboxComponentsCreatedEventArgs(components));
  136. return components;
  137. }
  138. protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
  139. {
  140. assembly = (AssemblyName)info.GetValue ("AssemblyName", typeof (AssemblyName));
  141. bitmap = (Bitmap)info.GetValue ("Bitmap", typeof (Bitmap));
  142. filter = (ICollection)info.GetValue ("Filter", typeof (ICollection));
  143. displayname = info.GetString ("DisplayName");
  144. locked = info.GetBoolean ("Locked");
  145. name = info.GetString ("TypeName");
  146. }
  147. public override bool Equals (object obj)
  148. {
  149. // FIXME: too harsh??
  150. if (!(obj is ToolboxItem))
  151. return false;
  152. if (obj == this)
  153. return true;
  154. return ((ToolboxItem) obj).AssemblyName.Equals (assembly) &&
  155. ((ToolboxItem) obj).Locked.Equals (locked) &&
  156. ((ToolboxItem) obj).TypeName.Equals (name) &&
  157. ((ToolboxItem) obj).DisplayName.Equals (displayname) &&
  158. ((ToolboxItem) obj).Bitmap.Equals (bitmap);
  159. }
  160. public override int GetHashCode ()
  161. {
  162. // FIXME: other algorithm?
  163. return string.Concat (name, displayname).GetHashCode ();
  164. }
  165. [MonoTODO]
  166. protected virtual Type GetType (IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
  167. {
  168. if (host == null)
  169. throw new ArgumentNullException("host");
  170. //get ITypeResolutionService from host, as we have no other IServiceProvider here
  171. ITypeResolutionService typeRes = host.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
  172. if (typeRes == null)
  173. throw new Exception("Host does not provide an ITypeResolutionService");
  174. //TODO: Using Assembly loader to throw errors. Silent fail and return null?
  175. Assembly assembly = typeRes.GetAssembly(assemblyName, true);
  176. if (reference)
  177. typeRes.ReferenceAssembly(assemblyName);
  178. return typeRes.GetType(typeName, true);
  179. }
  180. [MonoTODO ("Should we be returning empty bitmap, or null?")]
  181. public virtual void Initialize (Type type)
  182. {
  183. assembly = type.Assembly.GetName();
  184. displayname = type.Name;
  185. name = type.FullName;
  186. // seems to be a right place to create the bitmap
  187. System.Drawing.Image image = null;
  188. foreach (object attribute in type.GetCustomAttributes(true)) {
  189. ToolboxBitmapAttribute tba = attribute as ToolboxBitmapAttribute;
  190. if (tba != null) {
  191. image = tba.GetImage (type);
  192. break;
  193. }
  194. }
  195. //fallback: check for image even if not attribute
  196. if (image == null)
  197. image = ToolboxBitmapAttribute.GetImageFromResource (type, null, false);
  198. if (image != null) {
  199. if (image is Bitmap)
  200. bitmap = (Bitmap) image;
  201. else
  202. bitmap = new Bitmap (image);
  203. }
  204. filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
  205. }
  206. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  207. {
  208. Serialize (info, context);
  209. }
  210. public void Lock ()
  211. {
  212. locked = true;
  213. }
  214. protected virtual void OnComponentsCreated (ToolboxComponentsCreatedEventArgs args)
  215. {
  216. if (ComponentsCreated != null)
  217. this.ComponentsCreated (this, args);
  218. }
  219. protected virtual void OnComponentsCreating (ToolboxComponentsCreatingEventArgs args)
  220. {
  221. if (ComponentsCreated != null)
  222. this.ComponentsCreating (this, args);
  223. }
  224. protected virtual void Serialize (SerializationInfo info, StreamingContext context)
  225. {
  226. info.AddValue ("AssemblyName", assembly);
  227. info.AddValue ("Bitmap", bitmap);
  228. info.AddValue ("Filter", filter);
  229. info.AddValue ("DisplayName", displayname);
  230. info.AddValue ("Locked", locked);
  231. info.AddValue ("TypeName", name);
  232. }
  233. public override string ToString()
  234. {
  235. return displayname;
  236. }
  237. public event ToolboxComponentsCreatedEventHandler ComponentsCreated;
  238. public event ToolboxComponentsCreatingEventHandler ComponentsCreating;
  239. }
  240. }