DesignSurfaceManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.ComponentModel;
  31. using System.Collections;
  32. namespace System.ComponentModel.Design
  33. {
  34. public class DesignSurfaceManager : IServiceProvider, IDisposable
  35. {
  36. private class MergedServiceProvider : IServiceProvider
  37. {
  38. private IServiceProvider _primaryProvider;
  39. private IServiceProvider _secondaryProvider;
  40. public MergedServiceProvider (IServiceProvider primary, IServiceProvider secondary)
  41. {
  42. if (primary == null)
  43. throw new ArgumentNullException ("primary");
  44. if (secondary == null)
  45. throw new ArgumentNullException ("secondary");
  46. _primaryProvider = primary;
  47. _secondaryProvider = secondary;
  48. }
  49. public object GetService (Type service)
  50. {
  51. object result = _primaryProvider.GetService (service);
  52. if (result == null)
  53. result = _secondaryProvider.GetService (service);
  54. return result;
  55. }
  56. } // MergedServiceProvider
  57. private IServiceProvider _parentProvider;
  58. private ServiceContainer _serviceContainer;
  59. public DesignSurfaceManager () : this (null)
  60. {
  61. }
  62. public DesignSurfaceManager (IServiceProvider serviceProvider)
  63. {
  64. _parentProvider = serviceProvider;
  65. this.ServiceContainer.AddService (typeof (IDesignerEventService), new DesignerEventService ());
  66. }
  67. // The CreateDesignSurfaceCore method is called by both CreateDesignSurface methods.
  68. // It is the implementation that actually creates the design surface. The default
  69. // implementation just returns a new DesignSurface. You may override this method to provide
  70. // a custom object that derives from the DesignSurface class.
  71. //
  72. protected virtual DesignSurface CreateDesignSurfaceCore (IServiceProvider parentProvider)
  73. {
  74. DesignSurface surface = new DesignSurface (parentProvider);
  75. OnDesignSurfaceCreated (surface);
  76. return surface;
  77. }
  78. public DesignSurface CreateDesignSurface ()
  79. {
  80. return CreateDesignSurfaceCore (this);
  81. }
  82. // MSDN: parentProvider - A parent service provider. A new merged service provider will be created that
  83. // will first ask this provider for a service, and then delegate any failures to the design surface
  84. // manager object. This merged provider will be passed into the CreateDesignSurfaceCore method.
  85. //
  86. public DesignSurface CreateDesignSurface (IServiceProvider parentProvider)
  87. {
  88. if (parentProvider == null)
  89. throw new ArgumentNullException ("parentProvider");
  90. return CreateDesignSurfaceCore (new MergedServiceProvider (parentProvider, this));
  91. }
  92. public virtual DesignSurface ActiveDesignSurface {
  93. get {
  94. DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
  95. if (eventService != null) {
  96. IDesignerHost designer = eventService.ActiveDesigner;
  97. if (designer != null)
  98. return designer.GetService (typeof (DesignSurface)) as DesignSurface;
  99. }
  100. return null;
  101. }
  102. set {
  103. if (value != null) {
  104. DesignSurface oldSurface = null;
  105. // get current surface
  106. DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
  107. if (eventService != null) {
  108. IDesignerHost designer = eventService.ActiveDesigner;
  109. if (designer != null)
  110. oldSurface = designer.GetService (typeof (DesignSurface)) as DesignSurface;
  111. }
  112. ISelectionService selectionService = null;
  113. // unsubscribe from current's selectionchanged
  114. if (oldSurface != null) {
  115. selectionService = oldSurface.GetService (typeof (ISelectionService)) as ISelectionService;
  116. if (selectionService != null)
  117. selectionService.SelectionChanged -= new EventHandler (OnSelectionChanged);
  118. }
  119. // subscribe to new's selectionchanged
  120. selectionService = value.GetService (typeof (ISelectionService)) as ISelectionService;
  121. if (selectionService != null)
  122. selectionService.SelectionChanged += new EventHandler (OnSelectionChanged);
  123. // set it
  124. eventService.ActiveDesigner = value.GetService (typeof (IDesignerHost)) as IDesignerHost;
  125. // fire event
  126. if (this.ActiveDesignSurfaceChanged != null)
  127. this.ActiveDesignSurfaceChanged (this, new ActiveDesignSurfaceChangedEventArgs (oldSurface, value));
  128. }
  129. }
  130. }
  131. public DesignSurfaceCollection DesignSurfaces {
  132. get {
  133. DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
  134. if (eventService != null)
  135. return new DesignSurfaceCollection (eventService.Designers);
  136. return new DesignSurfaceCollection (null);
  137. }
  138. }
  139. protected ServiceContainer ServiceContainer {
  140. get {
  141. if (_serviceContainer == null)
  142. _serviceContainer = new ServiceContainer (_parentProvider);
  143. return _serviceContainer;
  144. }
  145. }
  146. // MSDN2 says those events are mapped through the IDesignerEventService,
  147. // but I preferd not to do that. Should not cause compitability issues.
  148. //
  149. //
  150. // The SelectionChanged is fired only for a changed component selection on the
  151. // active designersurface.
  152. //
  153. public event EventHandler SelectionChanged;
  154. public event DesignSurfaceEventHandler DesignSurfaceDisposed;
  155. public event DesignSurfaceEventHandler DesignSurfaceCreated;
  156. public event ActiveDesignSurfaceChangedEventHandler ActiveDesignSurfaceChanged;
  157. private void OnSelectionChanged (object sender, EventArgs args)
  158. {
  159. if (SelectionChanged != null)
  160. SelectionChanged (this, EventArgs.Empty);
  161. DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
  162. if (eventService != null)
  163. eventService.RaiseSelectionChanged ();
  164. }
  165. private void OnDesignSurfaceCreated (DesignSurface surface)
  166. {
  167. if (DesignSurfaceCreated != null)
  168. DesignSurfaceCreated (this, new DesignSurfaceEventArgs (surface));
  169. // monitor disposing
  170. surface.Disposed += new EventHandler (OnDesignSurfaceDisposed);
  171. DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
  172. if (eventService != null)
  173. eventService.RaiseDesignerCreated (surface.GetService (typeof (IDesignerHost)) as IDesignerHost);
  174. }
  175. private void OnDesignSurfaceDisposed (object sender, EventArgs args)
  176. {
  177. DesignSurface surface = (DesignSurface) sender;
  178. surface.Disposed -= new EventHandler (OnDesignSurfaceDisposed);
  179. if (DesignSurfaceDisposed != null)
  180. DesignSurfaceDisposed (this, new DesignSurfaceEventArgs (surface));
  181. DesignerEventService eventService = GetService (typeof (IDesignerEventService)) as DesignerEventService;
  182. if (eventService != null)
  183. eventService.RaiseDesignerDisposed (surface.GetService (typeof (IDesignerHost)) as IDesignerHost);
  184. }
  185. public object GetService (Type service)
  186. {
  187. if (_serviceContainer != null)
  188. return _serviceContainer.GetService (service);
  189. return null;
  190. }
  191. public void Dispose ()
  192. {
  193. Dispose (true);
  194. }
  195. protected virtual void Dispose (bool disposing)
  196. {
  197. if (disposing && _serviceContainer != null) {
  198. _serviceContainer.Dispose ();
  199. _serviceContainer = null;
  200. }
  201. }
  202. }
  203. }
  204. #endif