Merge pull request #1404 from woodsb02/mono-route
[mono.git] / mcs / class / System.Design / System.ComponentModel.Design / DesignSurfaceManager.cs
1 //
2 // System.ComponentModel.Design.DesignSurfaceManager
3 //
4 // Authors:      
5 //        Ivan N. Zlatev (contact i-nZ.net)
6 //
7 // (C) 2006 Ivan N. Zlatev
8
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30
31 using System;
32 using System.ComponentModel;
33 using System.Collections;
34
35 namespace System.ComponentModel.Design
36 {
37
38         public class DesignSurfaceManager : IServiceProvider, IDisposable
39         {
40
41                 private class MergedServiceProvider : IServiceProvider
42                 {
43                         private IServiceProvider _primaryProvider;
44                         private IServiceProvider _secondaryProvider;
45                         
46                         public MergedServiceProvider (IServiceProvider primary, IServiceProvider secondary)
47                         {
48                                 if (primary == null)
49                                         throw new ArgumentNullException ("primary");
50                                 if (secondary == null)
51                                         throw new ArgumentNullException ("secondary");
52                                 
53                                 _primaryProvider = primary;
54                                 _secondaryProvider = secondary;
55                         }
56
57                         public object GetService (Type service)
58                         {
59                                 object result = _primaryProvider.GetService (service);
60                                 if (result == null)
61                                         result = _secondaryProvider.GetService (service);
62
63                                 return result;
64                         }
65                         
66                 } // MergedServiceProvider
67
68                 
69                 private IServiceProvider _parentProvider;
70                 private ServiceContainer _serviceContainer;
71                 
72                 public DesignSurfaceManager () : this (null)
73                 {
74                 }
75
76                 public DesignSurfaceManager (IServiceProvider serviceProvider)
77                 {
78                         _parentProvider = serviceProvider;
79                         this.ServiceContainer.AddService (typeof (IDesignerEventService), new DesignerEventService ());
80                 }
81
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.
86                 //
87                 protected virtual DesignSurface CreateDesignSurfaceCore (IServiceProvider parentProvider)
88                 {
89                         DesignSurface surface = new DesignSurface (parentProvider);
90                         OnDesignSurfaceCreated (surface);
91                         return surface;
92                 }
93                 
94                 public DesignSurface CreateDesignSurface ()
95                 {
96                         return CreateDesignSurfaceCore (this);  
97                 }
98
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.
102                 //
103                 public DesignSurface CreateDesignSurface (IServiceProvider parentProvider)
104                 {
105                         if (parentProvider == null)
106                                 throw new ArgumentNullException ("parentProvider");
107                         
108                         return CreateDesignSurfaceCore (new MergedServiceProvider (parentProvider, this));
109                 }
110                 
111                 public virtual DesignSurface ActiveDesignSurface {
112                         get {
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;
118                                 }
119                                 return null;
120                         }
121                         set {
122                                 if (value != null) {
123                                         DesignSurface oldSurface = null;
124                                 
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;
131                                         }
132                                         
133                                         ISelectionService selectionService = null;
134                                         
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);
141                                                 }
142                                                 // subscribe to new's selectionchanged
143                                                 selectionService = value.GetService (typeof (ISelectionService)) as ISelectionService;
144                                                 if (selectionService != null)
145                                                         selectionService.SelectionChanged += new EventHandler (OnSelectionChanged);
146                                                 
147                                                 // set it
148                                                 eventService.ActiveDesigner = value.GetService (typeof (IDesignerHost)) as IDesignerHost;
149                                                 
150                                                 // fire event
151                                                 if (this.ActiveDesignSurfaceChanged != null)
152                                                         this.ActiveDesignSurfaceChanged (this, new ActiveDesignSurfaceChangedEventArgs (oldSurface, value));
153                                         }
154                                 }
155                         }
156                 }
157                 
158                 public DesignSurfaceCollection DesignSurfaces {
159                         get {
160                                 DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
161                                 if (eventService != null) 
162                                         return new DesignSurfaceCollection (eventService.Designers);
163
164                                 return new DesignSurfaceCollection (null);
165                         }
166                 }
167                 
168                 protected ServiceContainer ServiceContainer {
169                         get {
170                                 if (_serviceContainer == null)
171                                         _serviceContainer = new ServiceContainer (_parentProvider);
172
173                                 return _serviceContainer;
174                         }
175                 }
176                                 
177                 // MSDN2 says those events are mapped through the IDesignerEventService,
178                 // but I preferd not to do that. Should not cause compitability issues.
179                 //
180                 // 
181                 // The SelectionChanged is fired only for a changed component selection on the
182                 // active designersurface.
183                 //
184                 public event EventHandler SelectionChanged;      
185                 public event DesignSurfaceEventHandler DesignSurfaceDisposed;
186                 public event DesignSurfaceEventHandler DesignSurfaceCreated;
187                 public event ActiveDesignSurfaceChangedEventHandler ActiveDesignSurfaceChanged;
188                 
189                 private void OnSelectionChanged (object sender, EventArgs args)
190                 {
191                         if (SelectionChanged != null)
192                                 SelectionChanged (this, EventArgs.Empty);
193                         
194                         DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
195                         if (eventService != null)
196                                 eventService.RaiseSelectionChanged ();
197                 }
198                 
199                 private void OnDesignSurfaceCreated (DesignSurface surface)
200                 {
201                         if (DesignSurfaceCreated != null)
202                                 DesignSurfaceCreated (this, new DesignSurfaceEventArgs (surface));
203                         
204                         // monitor disposing
205                         surface.Disposed += new EventHandler (OnDesignSurfaceDisposed);
206                         
207                         DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
208                         if (eventService != null)
209                                 eventService.RaiseDesignerCreated (surface.GetService (typeof (IDesignerHost)) as IDesignerHost);
210                 }
211
212                 private void OnDesignSurfaceDisposed (object sender, EventArgs args)
213                 {
214                         DesignSurface surface = (DesignSurface) sender;
215                         
216                         surface.Disposed -= new EventHandler (OnDesignSurfaceDisposed);
217                         
218                         if (DesignSurfaceDisposed != null)
219                                 DesignSurfaceDisposed (this, new DesignSurfaceEventArgs (surface));
220                         
221                         DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
222                         if (eventService != null)
223                                 eventService.RaiseDesignerDisposed (surface.GetService (typeof (IDesignerHost)) as IDesignerHost);
224                                 
225                 }
226                 
227                 public object GetService (Type service)
228                 {
229                         if (_serviceContainer != null)
230                                 return _serviceContainer.GetService (service);
231
232                         return null;
233                 }
234
235                 public void Dispose ()
236                 {
237                         Dispose (true);
238                 }
239
240                 protected virtual void Dispose (bool disposing)
241                 {
242                         if (disposing && _serviceContainer != null) {
243                                 _serviceContainer.Dispose ();
244                                 _serviceContainer = null;
245                         }
246                 }
247         }
248
249 }