DesignSurface.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. //
  2. // System.ComponentModel.Design.DesignSurface
  3. //
  4. // Authors:
  5. // Ivan N. Zlatev (contact i-nZ.net)
  6. //
  7. // (C) 2006-2007 Ivan N. Zlatev
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.ComponentModel;
  32. using System.ComponentModel.Design.Serialization;
  33. using System.Reflection;
  34. namespace System.ComponentModel.Design
  35. {
  36. public class DesignSurface : IServiceProvider, IDisposable
  37. {
  38. #region DefaultDesignerLoader : DesignerLoader
  39. internal class DefaultDesignerLoader : DesignerLoader
  40. {
  41. // When DesignSurface.BeginLoad is invoked, the designer loader loads the design document, displays the designer
  42. // surface using the IDesignerHost interface, and calls IDesignerLoaderHost.EndLoad
  43. // when done. The IDesignerLoaderHost implementation is usually the same class that implements IDesignerHost.
  44. // The designer loader informs the designer host that it needs to invoke a load or reload so that the designer
  45. // host can perform additional tasks at these times.
  46. private Type _componentType;
  47. private bool _loading;
  48. public override bool Loading
  49. {
  50. get { return _loading; }
  51. }
  52. public DefaultDesignerLoader (Type componentType)
  53. {
  54. if (componentType == null)
  55. throw new ArgumentNullException ("componentType");
  56. _componentType = componentType;
  57. }
  58. // Note that IDesignerLoader : IDesignerHost
  59. //
  60. public override void BeginLoad (IDesignerLoaderHost loaderHost)
  61. {
  62. _loading = true;
  63. // initializa root component and designer
  64. //
  65. loaderHost.CreateComponent (_componentType);
  66. // finish off loading - no error collection here.
  67. //
  68. loaderHost.EndLoad (_componentType.FullName, true, null);
  69. _loading = false;
  70. }
  71. public override void Dispose ()
  72. {
  73. _componentType = null;
  74. }
  75. } // DesignerLoader
  76. #endregion
  77. private DesignerHost _designerHost;
  78. private DesignSurfaceServiceContainer _serviceContainer;
  79. private ICollection _loadErrors;
  80. private bool _isLoaded;
  81. private DesignerLoader _designerLoader;
  82. public DesignSurface () : this ((IServiceProvider) null)
  83. {
  84. }
  85. public DesignSurface (Type rootComponentType) : this (null, rootComponentType)
  86. {
  87. }
  88. public DesignSurface (IServiceProvider parentProvider, Type rootComponentType) : this (parentProvider)
  89. {
  90. if (rootComponentType == null)
  91. throw new System.ArgumentNullException ("rootComponentType");
  92. BeginLoad (rootComponentType);
  93. }
  94. // this ctor doesn't load the surface
  95. //
  96. public DesignSurface (IServiceProvider parentProvider)
  97. {
  98. _serviceContainer = new DesignSurfaceServiceContainer (parentProvider);
  99. _serviceContainer.AddNonReplaceableService (typeof (IServiceContainer), _serviceContainer);
  100. _designerHost = new DesignerHost ((IServiceProvider) _serviceContainer);
  101. _designerHost.DesignerLoaderHostLoaded += new LoadedEventHandler (OnDesignerHost_Loaded);
  102. _designerHost.DesignerLoaderHostLoading += new EventHandler (OnDesignerHost_Loading);
  103. _designerHost.DesignerLoaderHostUnloading += new EventHandler (OnDesignerHost_Unloading);
  104. _designerHost.DesignerLoaderHostUnloaded += new EventHandler (OnDesignerHost_Unloaded);
  105. _designerHost.Activated += new EventHandler (OnDesignerHost_Activated);
  106. _serviceContainer.AddNonReplaceableService (typeof (IComponentChangeService), _designerHost);
  107. _serviceContainer.AddNonReplaceableService (typeof (IDesignerHost), _designerHost);
  108. _serviceContainer.AddNonReplaceableService (typeof (IContainer), _designerHost);
  109. _serviceContainer.AddService (typeof (ITypeDescriptorFilterService),
  110. (ITypeDescriptorFilterService) new TypeDescriptorFilterService (_serviceContainer));
  111. ExtenderService extenderService = new ExtenderService ();
  112. _serviceContainer.AddService (typeof (IExtenderProviderService), (IExtenderProviderService) extenderService);
  113. _serviceContainer.AddService (typeof (IExtenderListService), (IExtenderListService) extenderService);
  114. _serviceContainer.AddService (typeof (DesignSurface), this);
  115. }
  116. protected ServiceContainer ServiceContainer {
  117. get {
  118. if (_designerHost == null)
  119. throw new ObjectDisposedException ("DesignSurface");
  120. return _serviceContainer;
  121. }
  122. }
  123. public IContainer ComponentContainer {
  124. get {
  125. if (_designerHost == null)
  126. throw new ObjectDisposedException ("DesignSurface");
  127. return _designerHost.Container;
  128. }
  129. }
  130. public bool IsLoaded {
  131. get { return _isLoaded; }
  132. }
  133. // Returns a collection of loading errors or a void collection.
  134. //
  135. public ICollection LoadErrors {
  136. get {
  137. if (_loadErrors == null)
  138. _loadErrors = new object[0];
  139. return _loadErrors;
  140. }
  141. }
  142. public object View {
  143. get {
  144. if (_designerHost == null)
  145. throw new ObjectDisposedException ("DesignSurface");
  146. if (this.LoadErrors.Count > 0 || !_isLoaded)
  147. throw new InvalidOperationException ("DesignSurface isn't loaded.");
  148. IRootDesigner designer = _designerHost.GetDesigner (_designerHost.RootComponent) as IRootDesigner;
  149. ViewTechnology[] viewTech = designer.SupportedTechnologies;
  150. for (int i = 0; i < viewTech.Length; i++) {
  151. try {
  152. return designer.GetView (viewTech[i]);
  153. } catch {}
  154. }
  155. // if this code is reached - there is no supported view technology
  156. //
  157. throw new NotSupportedException ();
  158. }
  159. }
  160. public event EventHandler Disposed;
  161. public event EventHandler Flushed;
  162. public event LoadedEventHandler Loaded;
  163. public event EventHandler Loading;
  164. public event EventHandler Unloaded;
  165. public event EventHandler Unloading;
  166. public event EventHandler ViewActivated;
  167. public void BeginLoad (Type rootComponentType)
  168. {
  169. if (rootComponentType == null)
  170. throw new System.ArgumentNullException ("rootComponentType");
  171. if (_designerHost == null)
  172. throw new ObjectDisposedException ("DesignSurface");
  173. this.BeginLoad (new DefaultDesignerLoader (rootComponentType));
  174. }
  175. public void BeginLoad (DesignerLoader loader)
  176. {
  177. if (loader == null)
  178. throw new System.ArgumentNullException ("loader");
  179. if (_designerHost == null)
  180. throw new ObjectDisposedException ("DesignSurface");
  181. if (!_isLoaded) {
  182. _loadErrors = null;
  183. _designerLoader = loader;
  184. this.OnLoading (EventArgs.Empty);
  185. _designerLoader.BeginLoad (_designerHost);
  186. }
  187. }
  188. #region IDisposable
  189. public void Dispose ()
  190. {
  191. this.Dispose (true);
  192. }
  193. protected virtual void Dispose (bool disposing)
  194. {
  195. if (_designerLoader != null) {
  196. _designerLoader.Dispose ();
  197. _designerLoader = null;
  198. }
  199. if (_designerHost != null) {
  200. _designerHost.Dispose ();
  201. _designerHost.DesignerLoaderHostLoaded -= new LoadedEventHandler (OnDesignerHost_Loaded);
  202. _designerHost.DesignerLoaderHostLoading -= new EventHandler (OnDesignerHost_Loading);
  203. _designerHost.DesignerLoaderHostUnloading -= new EventHandler (OnDesignerHost_Unloading);
  204. _designerHost.DesignerLoaderHostUnloaded -= new EventHandler (OnDesignerHost_Unloaded);
  205. _designerHost.Activated -= new EventHandler (OnDesignerHost_Activated);
  206. _designerHost = null;
  207. }
  208. if (_serviceContainer != null) {
  209. _serviceContainer.Dispose ();
  210. _serviceContainer = null;
  211. }
  212. if (Disposed != null)
  213. Disposed (this, EventArgs.Empty);
  214. }
  215. #endregion
  216. public void Flush ()
  217. {
  218. _designerLoader.Flush ();
  219. if (Flushed != null)
  220. Flushed (this, EventArgs.Empty);
  221. }
  222. private void OnDesignerHost_Loaded (object sender, LoadedEventArgs e)
  223. {
  224. this.OnLoaded (e);
  225. }
  226. private void OnDesignerHost_Loading (object sender, EventArgs e)
  227. {
  228. this.OnLoading (EventArgs.Empty);
  229. }
  230. private void OnDesignerHost_Unloading (object sender, EventArgs e)
  231. {
  232. this.OnUnloading (EventArgs.Empty);
  233. }
  234. private void OnDesignerHost_Unloaded (object sender, EventArgs e)
  235. {
  236. this.OnUnloaded (EventArgs.Empty);
  237. }
  238. protected virtual void OnLoaded (LoadedEventArgs e)
  239. {
  240. _loadErrors = e.Errors;
  241. _isLoaded = e.HasSucceeded;
  242. if (Loaded != null)
  243. Loaded (this, e);
  244. }
  245. protected virtual void OnLoading (EventArgs e)
  246. {
  247. if (Loading != null)
  248. Loading (this, e);
  249. }
  250. protected virtual void OnUnloaded (EventArgs e)
  251. {
  252. if (Unloaded != null)
  253. Unloaded (this, e);
  254. }
  255. protected virtual void OnUnloading (EventArgs e)
  256. {
  257. if (Unloading != null)
  258. Unloading (this, e);
  259. }
  260. internal void OnDesignerHost_Activated (object sender, EventArgs args)
  261. {
  262. this.OnViewActivate (EventArgs.Empty);
  263. }
  264. protected virtual void OnViewActivate (EventArgs e)
  265. {
  266. if (ViewActivated != null)
  267. ViewActivated (this, e);
  268. }
  269. [ObsoleteAttribute("CreateComponent has been replaced by CreateInstance")]
  270. protected internal virtual IComponent CreateComponent (Type componentType)
  271. {
  272. return (this.CreateInstance (componentType)) as IComponent;
  273. }
  274. // XXX: I am not quite sure if this should add the created instance of the component
  275. // to the surface, but it does. (If one finds out that this is wrong just use
  276. // _designerHost.CreateInstance (..)
  277. //
  278. protected internal virtual object CreateInstance (Type type)
  279. {
  280. if (type == null)
  281. throw new System.ArgumentNullException ("type");
  282. return _designerHost.CreateComponent (type);
  283. }
  284. protected internal virtual IDesigner CreateDesigner (IComponent component, bool rootDesigner)
  285. {
  286. if (component == null)
  287. throw new System.ArgumentNullException ("component");
  288. if (_designerHost == null)
  289. throw new System.ObjectDisposedException ("DesignerSurface");
  290. return _designerHost.CreateDesigner (component, rootDesigner);
  291. }
  292. public INestedContainer CreateNestedContainer (IComponent owningComponent)
  293. {
  294. return this.CreateNestedContainer (owningComponent, null);
  295. }
  296. public INestedContainer CreateNestedContainer (IComponent owningComponent, string containerName)
  297. {
  298. if (_designerHost == null)
  299. throw new ObjectDisposedException ("DesignSurface");
  300. return new DesignModeNestedContainer (owningComponent, containerName);
  301. }
  302. #region IServiceProvider
  303. public object GetService (Type serviceType)
  304. {
  305. if (typeof (IServiceContainer) == serviceType)
  306. return _serviceContainer;
  307. return _serviceContainer.GetService (serviceType);
  308. }
  309. #endregion
  310. }
  311. }
  312. #endif