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);
148 SelectionService selectionService = new SelectionService (_serviceContainer);
149 _serviceContainer.AddService (typeof (ISelectionService), (ISelectionService) selectionService);
152 protected ServiceContainer ServiceContainer {
154 if (_designerHost == null)
155 throw new ObjectDisposedException ("DesignSurface");
157 return _serviceContainer;
161 public IContainer ComponentContainer {
163 if (_designerHost == null)
164 throw new ObjectDisposedException ("DesignSurface");
166 return _designerHost.Container;
170 public bool IsLoaded {
171 get { return _isLoaded; }
174 // Returns a collection of loading errors or a void collection.
176 public ICollection LoadErrors {
178 if (_loadErrors == null)
179 _loadErrors = new object[0];
187 if (_designerHost == null)
188 throw new ObjectDisposedException ("DesignSurface");
190 if (_designerHost.RootComponent == null || this.LoadErrors.Count > 0)
191 throw new InvalidOperationException ("The DesignSurface isn't loaded.");
193 IRootDesigner designer = _designerHost.GetDesigner (_designerHost.RootComponent) as IRootDesigner;
194 if (designer == null)
195 throw new InvalidOperationException ("The DesignSurface isn't loaded.");
197 ViewTechnology[] viewTech = designer.SupportedTechnologies;
198 for (int i = 0; i < viewTech.Length; i++) {
200 return designer.GetView (viewTech[i]);
204 throw new NotSupportedException ("No supported View Technology found.");
208 public event EventHandler Disposed;
209 public event EventHandler Flushed;
210 public event LoadedEventHandler Loaded;
211 public event EventHandler Loading;
212 public event EventHandler Unloaded;
213 public event EventHandler Unloading;
214 public event EventHandler ViewActivated;
216 public void BeginLoad (Type rootComponentType)
218 if (rootComponentType == null)
219 throw new System.ArgumentNullException ("rootComponentType");
220 if (_designerHost == null)
221 throw new ObjectDisposedException ("DesignSurface");
223 this.BeginLoad (new DefaultDesignerLoader (rootComponentType));
226 public void BeginLoad (DesignerLoader loader)
229 throw new System.ArgumentNullException ("loader");
230 if (_designerHost == null)
231 throw new ObjectDisposedException ("DesignSurface");
235 _designerLoader = loader;
236 this.OnLoading (EventArgs.Empty);
237 _designerLoader.BeginLoad (_designerHost);
244 public void Dispose ()
250 protected virtual void Dispose (bool disposing)
252 if (_designerLoader != null) {
253 _designerLoader.Dispose ();
254 _designerLoader = null;
256 if (_designerHost != null) {
257 _designerHost.Dispose ();
258 _designerHost.DesignerLoaderHostLoaded -= new LoadedEventHandler (OnDesignerHost_Loaded);
259 _designerHost.DesignerLoaderHostLoading -= new EventHandler (OnDesignerHost_Loading);
260 _designerHost.DesignerLoaderHostUnloading -= new EventHandler (OnDesignerHost_Unloading);
261 _designerHost.DesignerLoaderHostUnloaded -= new EventHandler (OnDesignerHost_Unloaded);
262 _designerHost.Activated -= new EventHandler (OnDesignerHost_Activated);
263 _designerHost = null;
265 if (_serviceContainer != null) {
266 _serviceContainer.Dispose ();
267 _serviceContainer = null;
270 if (Disposed != null)
271 Disposed (this, EventArgs.Empty);
279 if (_designerLoader != null)
280 _designerLoader.Flush ();
283 Flushed (this, EventArgs.Empty);
286 private void OnDesignerHost_Loaded (object sender, LoadedEventArgs e)
291 private void OnDesignerHost_Loading (object sender, EventArgs e)
293 this.OnLoading (EventArgs.Empty);
297 private void OnDesignerHost_Unloading (object sender, EventArgs e)
299 this.OnUnloading (EventArgs.Empty);
303 private void OnDesignerHost_Unloaded (object sender, EventArgs e)
305 this.OnUnloaded (EventArgs.Empty);
308 protected virtual void OnLoaded (LoadedEventArgs e)
310 _loadErrors = e.Errors;
311 _isLoaded = e.HasSucceeded;
317 protected virtual void OnLoading (EventArgs e)
324 protected virtual void OnUnloaded (EventArgs e)
326 if (Unloaded != null)
331 protected virtual void OnUnloading (EventArgs e)
333 if (Unloading != null)
337 internal void OnDesignerHost_Activated (object sender, EventArgs args)
339 this.OnViewActivate (EventArgs.Empty);
342 protected virtual void OnViewActivate (EventArgs e)
344 if (ViewActivated != null)
345 ViewActivated (this, e);
349 [ObsoleteAttribute("CreateComponent has been replaced by CreateInstance")]
350 protected internal virtual IComponent CreateComponent (Type componentType)
352 return (this.CreateInstance (componentType)) as IComponent;
356 // XXX: I am not quite sure if this should add the created instance of the component
357 // to the surface, but it does. (If one finds out that this is wrong just use
358 // _designerHost.CreateInstance (..)
360 protected internal virtual object CreateInstance (Type type)
363 throw new System.ArgumentNullException ("type");
365 return _designerHost.CreateComponent (type);
369 protected internal virtual IDesigner CreateDesigner (IComponent component, bool rootDesigner)
371 if (component == null)
372 throw new System.ArgumentNullException ("component");
373 if (_designerHost == null)
374 throw new System.ObjectDisposedException ("DesignerSurface");
376 return _designerHost.CreateDesigner (component, rootDesigner);
379 public INestedContainer CreateNestedContainer (IComponent owningComponent)
381 return this.CreateNestedContainer (owningComponent, null);
384 public INestedContainer CreateNestedContainer (IComponent owningComponent, string containerName)
386 if (_designerHost == null)
387 throw new ObjectDisposedException ("DesignSurface");
389 return new DesignModeNestedContainer (owningComponent, containerName);
393 #region IServiceProvider
395 public object GetService (Type serviceType)
397 if (typeof (IServiceContainer) == serviceType)
398 return _serviceContainer;
400 return _serviceContainer.GetService (serviceType);