2 // System.ComponentModel.Design.DesignSurface
5 // Ivan N. Zlatev (contact i-nZ.net)
7 // (C) 2006-2007 Ivan N. Zlatev
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.ComponentModel;
35 using System.ComponentModel.Design.Serialization;
36 using System.Reflection;
38 namespace System.ComponentModel.Design
41 public class DesignSurface : IServiceProvider, IDisposable
44 #region DefaultDesignerLoader : DesignerLoader
46 internal class DefaultDesignerLoader : DesignerLoader
48 // When DesignSurface.BeginLoad is invoked, the designer loader loads the design document, displays the designer
49 // surface using the IDesignerHost interface, and calls IDesignerLoaderHost.EndLoad
50 // when done. The IDesignerLoaderHost implementation is usually the same class that implements IDesignerHost.
52 // The designer loader informs the designer host that it needs to invoke a load or reload so that the designer
53 // host can perform additional tasks at these times.
55 private Type _componentType;
56 private bool _loading;
59 public override bool Loading
61 get { return _loading; }
64 public DefaultDesignerLoader (Type componentType)
66 if (componentType == null)
67 throw new ArgumentNullException ("componentType");
69 _componentType = componentType;
72 // Note that IDesignerLoader : IDesignerHost
74 public override void BeginLoad (IDesignerLoaderHost loaderHost)
77 // initializa root component and designer
79 loaderHost.CreateComponent (_componentType);
80 // finish off loading - no error collection here.
82 loaderHost.EndLoad (_componentType.FullName, true, null);
86 public override void Dispose ()
88 _componentType = null;
97 private DesignerHost _designerHost;
98 private DesignSurfaceServiceContainer _serviceContainer;
99 private ICollection _loadErrors;
100 private bool _isLoaded;
101 private DesignerLoader _designerLoader;
104 public DesignSurface () : this ((IServiceProvider) null)
108 public DesignSurface (Type rootComponentType) : this (null, rootComponentType)
113 public DesignSurface (IServiceProvider parentProvider, Type rootComponentType) : this (parentProvider)
115 if (rootComponentType == null)
116 throw new System.ArgumentNullException ("rootComponentType");
118 BeginLoad (rootComponentType);
121 // this ctor doesn't load the surface
123 public DesignSurface (IServiceProvider parentProvider)
126 _serviceContainer = new DesignSurfaceServiceContainer (parentProvider);
127 _serviceContainer.AddNonReplaceableService (typeof (IServiceContainer), _serviceContainer);
129 _designerHost = new DesignerHost ((IServiceProvider) _serviceContainer);
130 _designerHost.DesignerLoaderHostLoaded += new LoadedEventHandler (OnDesignerHost_Loaded);
131 _designerHost.DesignerLoaderHostLoading += new EventHandler (OnDesignerHost_Loading);
132 _designerHost.DesignerLoaderHostUnloading += new EventHandler (OnDesignerHost_Unloading);
133 _designerHost.DesignerLoaderHostUnloaded += new EventHandler (OnDesignerHost_Unloaded);
135 _designerHost.Activated += new EventHandler (OnDesignerHost_Activated);
137 _serviceContainer.AddNonReplaceableService (typeof (IComponentChangeService), _designerHost);
138 _serviceContainer.AddNonReplaceableService (typeof (IDesignerHost), _designerHost);
139 _serviceContainer.AddNonReplaceableService (typeof (IContainer), _designerHost);
140 _serviceContainer.AddService (typeof (ITypeDescriptorFilterService),
141 (ITypeDescriptorFilterService) new TypeDescriptorFilterService (_serviceContainer));
143 ExtenderService extenderService = new ExtenderService ();
144 _serviceContainer.AddService (typeof (IExtenderProviderService), (IExtenderProviderService) extenderService);
145 _serviceContainer.AddService (typeof (IExtenderListService), (IExtenderListService) extenderService);
146 _serviceContainer.AddService (typeof (DesignSurface), this);
149 protected ServiceContainer ServiceContainer {
151 if (_designerHost == null)
152 throw new ObjectDisposedException ("DesignSurface");
154 return _serviceContainer;
158 public IContainer ComponentContainer {
160 if (_designerHost == null)
161 throw new ObjectDisposedException ("DesignSurface");
163 return _designerHost.Container;
167 public bool IsLoaded {
168 get { return _isLoaded; }
171 // Returns a collection of loading errors or a void collection.
173 public ICollection LoadErrors {
175 if (_loadErrors == null)
176 _loadErrors = new object[0];
184 if (_designerHost == null)
185 throw new ObjectDisposedException ("DesignSurface");
187 if (_designerHost.RootComponent == null || this.LoadErrors.Count > 0)
188 throw new InvalidOperationException ("The DesignSurface isn't loaded.");
190 IRootDesigner designer = _designerHost.GetDesigner (_designerHost.RootComponent) as IRootDesigner;
191 if (designer == null)
192 throw new InvalidOperationException ("The DesignSurface isn't loaded.");
194 ViewTechnology[] viewTech = designer.SupportedTechnologies;
195 for (int i = 0; i < viewTech.Length; i++) {
197 return designer.GetView (viewTech[i]);
201 throw new NotSupportedException ("No supported View Technology found.");
205 public event EventHandler Disposed;
206 public event EventHandler Flushed;
207 public event LoadedEventHandler Loaded;
208 public event EventHandler Loading;
209 public event EventHandler Unloaded;
210 public event EventHandler Unloading;
211 public event EventHandler ViewActivated;
213 public void BeginLoad (Type rootComponentType)
215 if (rootComponentType == null)
216 throw new System.ArgumentNullException ("rootComponentType");
217 if (_designerHost == null)
218 throw new ObjectDisposedException ("DesignSurface");
220 this.BeginLoad (new DefaultDesignerLoader (rootComponentType));
223 public void BeginLoad (DesignerLoader loader)
226 throw new System.ArgumentNullException ("loader");
227 if (_designerHost == null)
228 throw new ObjectDisposedException ("DesignSurface");
232 _designerLoader = loader;
233 this.OnLoading (EventArgs.Empty);
234 _designerLoader.BeginLoad (_designerHost);
241 public void Dispose ()
247 protected virtual void Dispose (bool disposing)
249 if (_designerLoader != null) {
250 _designerLoader.Dispose ();
251 _designerLoader = null;
253 if (_designerHost != null) {
254 _designerHost.Dispose ();
255 _designerHost.DesignerLoaderHostLoaded -= new LoadedEventHandler (OnDesignerHost_Loaded);
256 _designerHost.DesignerLoaderHostLoading -= new EventHandler (OnDesignerHost_Loading);
257 _designerHost.DesignerLoaderHostUnloading -= new EventHandler (OnDesignerHost_Unloading);
258 _designerHost.DesignerLoaderHostUnloaded -= new EventHandler (OnDesignerHost_Unloaded);
259 _designerHost.Activated -= new EventHandler (OnDesignerHost_Activated);
260 _designerHost = null;
262 if (_serviceContainer != null) {
263 _serviceContainer.Dispose ();
264 _serviceContainer = null;
267 if (Disposed != null)
268 Disposed (this, EventArgs.Empty);
276 _designerLoader.Flush ();
279 Flushed (this, EventArgs.Empty);
282 private void OnDesignerHost_Loaded (object sender, LoadedEventArgs e)
287 private void OnDesignerHost_Loading (object sender, EventArgs e)
289 this.OnLoading (EventArgs.Empty);
293 private void OnDesignerHost_Unloading (object sender, EventArgs e)
295 this.OnUnloading (EventArgs.Empty);
299 private void OnDesignerHost_Unloaded (object sender, EventArgs e)
301 this.OnUnloaded (EventArgs.Empty);
304 protected virtual void OnLoaded (LoadedEventArgs e)
306 _loadErrors = e.Errors;
307 _isLoaded = e.HasSucceeded;
313 protected virtual void OnLoading (EventArgs e)
320 protected virtual void OnUnloaded (EventArgs e)
322 if (Unloaded != null)
327 protected virtual void OnUnloading (EventArgs e)
329 if (Unloading != null)
333 internal void OnDesignerHost_Activated (object sender, EventArgs args)
335 this.OnViewActivate (EventArgs.Empty);
338 protected virtual void OnViewActivate (EventArgs e)
340 if (ViewActivated != null)
341 ViewActivated (this, e);
345 [ObsoleteAttribute("CreateComponent has been replaced by CreateInstance")]
346 protected internal virtual IComponent CreateComponent (Type componentType)
348 return (this.CreateInstance (componentType)) as IComponent;
352 // XXX: I am not quite sure if this should add the created instance of the component
353 // to the surface, but it does. (If one finds out that this is wrong just use
354 // _designerHost.CreateInstance (..)
356 protected internal virtual object CreateInstance (Type type)
359 throw new System.ArgumentNullException ("type");
361 return _designerHost.CreateComponent (type);
365 protected internal virtual IDesigner CreateDesigner (IComponent component, bool rootDesigner)
367 if (component == null)
368 throw new System.ArgumentNullException ("component");
369 if (_designerHost == null)
370 throw new System.ObjectDisposedException ("DesignerSurface");
372 return _designerHost.CreateDesigner (component, rootDesigner);
375 public INestedContainer CreateNestedContainer (IComponent owningComponent)
377 return this.CreateNestedContainer (owningComponent, null);
380 public INestedContainer CreateNestedContainer (IComponent owningComponent, string containerName)
382 if (_designerHost == null)
383 throw new ObjectDisposedException ("DesignSurface");
385 return new DesignModeNestedContainer (owningComponent, containerName);
389 #region IServiceProvider
391 public object GetService (Type serviceType)
393 if (typeof (IServiceContainer) == serviceType)
394 return _serviceContainer;
396 return _serviceContainer.GetService (serviceType);