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.
32 using System.ComponentModel;
33 using System.Collections;
35 namespace System.ComponentModel.Design
38 public class DesignSurfaceManager : IServiceProvider, IDisposable
41 private class MergedServiceProvider : IServiceProvider
43 private IServiceProvider _primaryProvider;
44 private IServiceProvider _secondaryProvider;
46 public MergedServiceProvider (IServiceProvider primary, IServiceProvider secondary)
49 throw new ArgumentNullException ("primary");
50 if (secondary == null)
51 throw new ArgumentNullException ("secondary");
53 _primaryProvider = primary;
54 _secondaryProvider = secondary;
57 public object GetService (Type service)
59 object result = _primaryProvider.GetService (service);
61 result = _secondaryProvider.GetService (service);
66 } // MergedServiceProvider
69 private IServiceProvider _parentProvider;
70 private ServiceContainer _serviceContainer;
72 public DesignSurfaceManager () : this (null)
76 public DesignSurfaceManager (IServiceProvider serviceProvider)
78 _parentProvider = serviceProvider;
79 this.ServiceContainer.AddService (typeof (IDesignerEventService), new DesignerEventService ());
82 // The CreateDesignSurfaceCore method is called by both CreateDesignSurface methods.
83 // It is the implementation that actually creates the design surface. The default
84 // implementation just returns a new DesignSurface. You may override this method to provide
85 // a custom object that derives from the DesignSurface class.
87 protected virtual DesignSurface CreateDesignSurfaceCore (IServiceProvider parentProvider)
89 DesignSurface surface = new DesignSurface (parentProvider);
90 OnDesignSurfaceCreated (surface);
94 public DesignSurface CreateDesignSurface ()
96 return CreateDesignSurfaceCore (this);
99 // MSDN: parentProvider - A parent service provider. A new merged service provider will be created that
100 // will first ask this provider for a service, and then delegate any failures to the design surface
101 // manager object. This merged provider will be passed into the CreateDesignSurfaceCore method.
103 public DesignSurface CreateDesignSurface (IServiceProvider parentProvider)
105 if (parentProvider == null)
106 throw new ArgumentNullException ("parentProvider");
108 return CreateDesignSurfaceCore (new MergedServiceProvider (parentProvider, this));
111 public virtual DesignSurface ActiveDesignSurface {
113 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
114 if (eventService != null) {
115 IDesignerHost designer = eventService.ActiveDesigner;
116 if (designer != null)
117 return designer.GetService (typeof (DesignSurface)) as DesignSurface;
123 DesignSurface oldSurface = null;
125 // get current surface
126 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
127 if (eventService != null) {
128 IDesignerHost designer = eventService.ActiveDesigner;
129 if (designer != null)
130 oldSurface = designer.GetService (typeof (DesignSurface)) as DesignSurface;
133 ISelectionService selectionService = null;
135 if (oldSurface != value) {
136 // unsubscribe from current's selectionchanged
137 if (oldSurface != null) {
138 selectionService = oldSurface.GetService (typeof (ISelectionService)) as ISelectionService;
139 if (selectionService != null)
140 selectionService.SelectionChanged -= new EventHandler (OnSelectionChanged);
142 // subscribe to new's selectionchanged
143 selectionService = value.GetService (typeof (ISelectionService)) as ISelectionService;
144 if (selectionService != null)
145 selectionService.SelectionChanged += new EventHandler (OnSelectionChanged);
148 eventService.ActiveDesigner = value.GetService (typeof (IDesignerHost)) as IDesignerHost;
151 if (this.ActiveDesignSurfaceChanged != null)
152 this.ActiveDesignSurfaceChanged (this, new ActiveDesignSurfaceChangedEventArgs (oldSurface, value));
158 public DesignSurfaceCollection DesignSurfaces {
160 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
161 if (eventService != null)
162 return new DesignSurfaceCollection (eventService.Designers);
164 return new DesignSurfaceCollection (null);
168 protected ServiceContainer ServiceContainer {
170 if (_serviceContainer == null)
171 _serviceContainer = new ServiceContainer (_parentProvider);
173 return _serviceContainer;
177 // MSDN2 says those events are mapped through the IDesignerEventService,
178 // but I preferd not to do that. Should not cause compitability issues.
181 // The SelectionChanged is fired only for a changed component selection on the
182 // active designersurface.
184 public event EventHandler SelectionChanged;
185 public event DesignSurfaceEventHandler DesignSurfaceDisposed;
186 public event DesignSurfaceEventHandler DesignSurfaceCreated;
187 public event ActiveDesignSurfaceChangedEventHandler ActiveDesignSurfaceChanged;
189 private void OnSelectionChanged (object sender, EventArgs args)
191 if (SelectionChanged != null)
192 SelectionChanged (this, EventArgs.Empty);
194 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
195 if (eventService != null)
196 eventService.RaiseSelectionChanged ();
199 private void OnDesignSurfaceCreated (DesignSurface surface)
201 if (DesignSurfaceCreated != null)
202 DesignSurfaceCreated (this, new DesignSurfaceEventArgs (surface));
205 surface.Disposed += new EventHandler (OnDesignSurfaceDisposed);
207 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
208 if (eventService != null)
209 eventService.RaiseDesignerCreated (surface.GetService (typeof (IDesignerHost)) as IDesignerHost);
212 private void OnDesignSurfaceDisposed (object sender, EventArgs args)
214 DesignSurface surface = (DesignSurface) sender;
216 surface.Disposed -= new EventHandler (OnDesignSurfaceDisposed);
218 if (DesignSurfaceDisposed != null)
219 DesignSurfaceDisposed (this, new DesignSurfaceEventArgs (surface));
221 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
222 if (eventService != null)
223 eventService.RaiseDesignerDisposed (surface.GetService (typeof (IDesignerHost)) as IDesignerHost);
227 public object GetService (Type service)
229 if (_serviceContainer != null)
230 return _serviceContainer.GetService (service);
235 public void Dispose ()
240 protected virtual void Dispose (bool disposing)
242 if (disposing && _serviceContainer != null) {
243 _serviceContainer.Dispose ();
244 _serviceContainer = null;