2 // System.ComponentModel.Design.DesignSurfaceManager
5 // Ivan N. Zlatev (contact i-nZ.net)
7 // (C) 2006 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.ComponentModel;
34 using System.Collections;
36 namespace System.ComponentModel.Design
39 public class DesignSurfaceManager : IServiceProvider, IDisposable
42 private class MergedServiceProvider : IServiceProvider
44 private IServiceProvider _primaryProvider;
45 private IServiceProvider _secondaryProvider;
47 public MergedServiceProvider (IServiceProvider primary, IServiceProvider secondary)
50 throw new ArgumentNullException ("primary");
51 if (secondary == null)
52 throw new ArgumentNullException ("secondary");
54 _primaryProvider = primary;
55 _secondaryProvider = secondary;
58 public object GetService (Type service)
60 object result = _primaryProvider.GetService (service);
62 result = _secondaryProvider.GetService (service);
67 } // MergedServiceProvider
70 private IServiceProvider _parentProvider;
71 private ServiceContainer _serviceContainer;
73 public DesignSurfaceManager () : this (null)
77 public DesignSurfaceManager (IServiceProvider serviceProvider)
79 _parentProvider = serviceProvider;
80 this.ServiceContainer.AddService (typeof (IDesignerEventService), new DesignerEventService ());
83 // The CreateDesignSurfaceCore method is called by both CreateDesignSurface methods.
84 // It is the implementation that actually creates the design surface. The default
85 // implementation just returns a new DesignSurface. You may override this method to provide
86 // a custom object that derives from the DesignSurface class.
88 protected virtual DesignSurface CreateDesignSurfaceCore (IServiceProvider parentProvider)
90 DesignSurface surface = new DesignSurface (parentProvider);
91 OnDesignSurfaceCreated (surface);
95 public DesignSurface CreateDesignSurface ()
97 return CreateDesignSurfaceCore (this);
100 // MSDN: parentProvider - A parent service provider. A new merged service provider will be created that
101 // will first ask this provider for a service, and then delegate any failures to the design surface
102 // manager object. This merged provider will be passed into the CreateDesignSurfaceCore method.
104 public DesignSurface CreateDesignSurface (IServiceProvider parentProvider)
106 if (parentProvider == null)
107 throw new ArgumentNullException ("parentProvider");
109 return CreateDesignSurfaceCore (new MergedServiceProvider (parentProvider, this));
112 public virtual DesignSurface ActiveDesignSurface {
114 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
115 if (eventService != null) {
116 IDesignerHost designer = eventService.ActiveDesigner;
117 if (designer != null)
118 return designer.GetService (typeof (DesignSurface)) as DesignSurface;
124 DesignSurface oldSurface = null;
126 // get current surface
127 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
128 if (eventService != null) {
129 IDesignerHost designer = eventService.ActiveDesigner;
130 if (designer != null)
131 oldSurface = designer.GetService (typeof (DesignSurface)) as DesignSurface;
134 ISelectionService selectionService = null;
136 if (oldSurface != value) {
137 // unsubscribe from current's selectionchanged
138 if (oldSurface != null) {
139 selectionService = oldSurface.GetService (typeof (ISelectionService)) as ISelectionService;
140 if (selectionService != null)
141 selectionService.SelectionChanged -= new EventHandler (OnSelectionChanged);
143 // subscribe to new's selectionchanged
144 selectionService = value.GetService (typeof (ISelectionService)) as ISelectionService;
145 if (selectionService != null)
146 selectionService.SelectionChanged += new EventHandler (OnSelectionChanged);
149 eventService.ActiveDesigner = value.GetService (typeof (IDesignerHost)) as IDesignerHost;
152 if (this.ActiveDesignSurfaceChanged != null)
153 this.ActiveDesignSurfaceChanged (this, new ActiveDesignSurfaceChangedEventArgs (oldSurface, value));
159 public DesignSurfaceCollection DesignSurfaces {
161 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
162 if (eventService != null)
163 return new DesignSurfaceCollection (eventService.Designers);
165 return new DesignSurfaceCollection (null);
169 protected ServiceContainer ServiceContainer {
171 if (_serviceContainer == null)
172 _serviceContainer = new ServiceContainer (_parentProvider);
174 return _serviceContainer;
178 // MSDN2 says those events are mapped through the IDesignerEventService,
179 // but I preferd not to do that. Should not cause compitability issues.
182 // The SelectionChanged is fired only for a changed component selection on the
183 // active designersurface.
185 public event EventHandler SelectionChanged;
186 public event DesignSurfaceEventHandler DesignSurfaceDisposed;
187 public event DesignSurfaceEventHandler DesignSurfaceCreated;
188 public event ActiveDesignSurfaceChangedEventHandler ActiveDesignSurfaceChanged;
190 private void OnSelectionChanged (object sender, EventArgs args)
192 if (SelectionChanged != null)
193 SelectionChanged (this, EventArgs.Empty);
195 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
196 if (eventService != null)
197 eventService.RaiseSelectionChanged ();
200 private void OnDesignSurfaceCreated (DesignSurface surface)
202 if (DesignSurfaceCreated != null)
203 DesignSurfaceCreated (this, new DesignSurfaceEventArgs (surface));
206 surface.Disposed += new EventHandler (OnDesignSurfaceDisposed);
208 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
209 if (eventService != null)
210 eventService.RaiseDesignerCreated (surface.GetService (typeof (IDesignerHost)) as IDesignerHost);
213 private void OnDesignSurfaceDisposed (object sender, EventArgs args)
215 DesignSurface surface = (DesignSurface) sender;
217 surface.Disposed -= new EventHandler (OnDesignSurfaceDisposed);
219 if (DesignSurfaceDisposed != null)
220 DesignSurfaceDisposed (this, new DesignSurfaceEventArgs (surface));
222 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
223 if (eventService != null)
224 eventService.RaiseDesignerDisposed (surface.GetService (typeof (IDesignerHost)) as IDesignerHost);
228 public object GetService (Type service)
230 if (_serviceContainer != null)
231 return _serviceContainer.GetService (service);
236 public void Dispose ()
241 protected virtual void Dispose (bool disposing)
243 if (disposing && _serviceContainer != null) {
244 _serviceContainer.Dispose ();
245 _serviceContainer = null;