SelectionService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // System.ComponentModel.Design.SelectionService
  3. //
  4. // Authors:
  5. // Ivan N. Zlatev (contact i-nZ.net)
  6. //
  7. // (C) 2006-2007 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. using System;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.ComponentModel.Design;
  32. using System.Windows.Forms;
  33. namespace System.ComponentModel.Design
  34. {
  35. internal class SelectionService : ISelectionService
  36. {
  37. private IServiceProvider _serviceProvider;
  38. private ArrayList _selection;
  39. private IComponent _primarySelection;
  40. public SelectionService (IServiceProvider provider)
  41. {
  42. _serviceProvider = provider;
  43. _selection = new ArrayList();
  44. IComponentChangeService changeService = provider.GetService (typeof (IComponentChangeService)) as IComponentChangeService;
  45. if (changeService != null)
  46. changeService.ComponentRemoving += new ComponentEventHandler (OnComponentRemoving);
  47. }
  48. private void OnComponentRemoving (object sender, ComponentEventArgs args)
  49. {
  50. if (this.GetComponentSelected (args.Component))
  51. #if NET_2_0
  52. this.SetSelectedComponents (new IComponent[] { args.Component }, SelectionTypes.Remove);
  53. #else
  54. this.SetSelectedComponents (new IComponent[] { this.RootComponent }, SelectionTypes.Click);
  55. #endif
  56. }
  57. public event EventHandler SelectionChanging;
  58. public event EventHandler SelectionChanged;
  59. public ICollection GetSelectedComponents()
  60. {
  61. if (_selection != null)
  62. return _selection.ToArray ();
  63. return new object[0];
  64. }
  65. protected virtual void OnSelectionChanging ()
  66. {
  67. if (SelectionChanging != null)
  68. SelectionChanging (this, EventArgs.Empty);
  69. }
  70. protected virtual void OnSelectionChanged ()
  71. {
  72. if (SelectionChanged != null)
  73. SelectionChanged (this, EventArgs.Empty);
  74. }
  75. public object PrimarySelection {
  76. get { return _primarySelection; }
  77. }
  78. public int SelectionCount {
  79. get {
  80. if (_selection != null)
  81. return _selection.Count;
  82. return 0;
  83. }
  84. }
  85. private IComponent RootComponent {
  86. get {
  87. if (_serviceProvider != null) {
  88. IDesignerHost designerHost = _serviceProvider.GetService (typeof (IDesignerHost)) as IDesignerHost;
  89. if (designerHost != null)
  90. return designerHost.RootComponent;
  91. }
  92. return null;
  93. }
  94. }
  95. public bool GetComponentSelected (object component)
  96. {
  97. if (_selection != null)
  98. return _selection.Contains (component);
  99. return false;
  100. }
  101. public void SetSelectedComponents (ICollection components)
  102. {
  103. #if NET_2_0
  104. SetSelectedComponents (components, SelectionTypes.Auto);
  105. #else
  106. SetSelectedComponents (components, SelectionTypes.Normal);
  107. #endif
  108. }
  109. // If the array is a null reference or does not contain any components,
  110. // SetSelectedComponents selects the top-level component in the designer.
  111. //
  112. public void SetSelectedComponents (ICollection components, SelectionTypes selectionType)
  113. {
  114. bool primary, add, remove, replace, toggle, auto;
  115. primary = add = remove = replace = toggle = auto = false;
  116. OnSelectionChanging ();
  117. if (_selection == null)
  118. throw new InvalidOperationException("_selection == null");
  119. if (components == null || components.Count == 0) {
  120. components = new ArrayList ();
  121. ((ArrayList) components).Add (this.RootComponent);
  122. selectionType = SelectionTypes.Replace;
  123. }
  124. if (!Enum.IsDefined (typeof (SelectionTypes), selectionType)) {
  125. #if NET_2_0
  126. selectionType = SelectionTypes.Auto;
  127. #else
  128. selectionType = SelectionTypes.Normal;
  129. #endif
  130. }
  131. #if NET_2_0
  132. auto = ((selectionType & SelectionTypes.Auto) == SelectionTypes.Auto);
  133. #else
  134. if ((selectionType & SelectionTypes.Normal) == SelectionTypes.Normal ||
  135. (selectionType & SelectionTypes.MouseDown) == SelectionTypes.MouseDown ||
  136. (selectionType & SelectionTypes.MouseUp) == SelectionTypes.MouseUp) {
  137. auto = true;
  138. }
  139. #endif
  140. if (auto) {
  141. if ((((Control.ModifierKeys & Keys.Control) == Keys.Control) || ((Control.ModifierKeys & Keys.Shift) == Keys.Shift))) {
  142. toggle = true;
  143. }
  144. else if (components.Count == 1) {
  145. object component = null;
  146. foreach (object c in components) {
  147. component = c;
  148. break;
  149. }
  150. if (this.GetComponentSelected (component))
  151. primary = true;
  152. else
  153. replace = true;
  154. }
  155. else {
  156. replace = true;
  157. }
  158. }
  159. else {
  160. #if NET_2_0
  161. primary = ((selectionType & SelectionTypes.Primary) == SelectionTypes.Primary);
  162. add = ((selectionType & SelectionTypes.Add) == SelectionTypes.Add);
  163. remove = ((selectionType & SelectionTypes.Remove) == SelectionTypes.Remove);
  164. toggle = ((selectionType & SelectionTypes.Toggle) == SelectionTypes.Toggle);
  165. #else
  166. primary = ((selectionType & SelectionTypes.Click) == SelectionTypes.Click);
  167. #endif
  168. replace = ((selectionType & SelectionTypes.Replace) == SelectionTypes.Replace);
  169. }
  170. if (replace) {
  171. _selection.Clear ();
  172. add = true;
  173. }
  174. if (add) {
  175. foreach (object component in components) {
  176. if (component is IComponent && !_selection.Contains (component)) {
  177. _selection.Add (component);
  178. _primarySelection = (IComponent) component;
  179. }
  180. }
  181. }
  182. if (remove) {
  183. foreach (object component in components) {
  184. if (component is IComponent && _selection.Contains (component)) {
  185. _selection.Remove (component);
  186. }
  187. }
  188. if (_selection.Count == 0) {
  189. _primarySelection = this.RootComponent;
  190. _selection.Add (this.RootComponent);
  191. }
  192. }
  193. if (toggle) {
  194. foreach (object component in components) {
  195. if (component is IComponent) {
  196. if (_selection.Contains (component)) {
  197. _selection.Remove (component);
  198. if (component == _primarySelection)
  199. _primarySelection = this.RootComponent;
  200. }
  201. else {
  202. _selection.Add (component);
  203. _primarySelection = (IComponent) component;
  204. }
  205. }
  206. }
  207. }
  208. if (primary) {
  209. object primarySelection = null;
  210. foreach (object component in components) {
  211. primarySelection = component;
  212. break;
  213. }
  214. if (!this.GetComponentSelected (primarySelection))
  215. _selection.Add (_primarySelection);
  216. _primarySelection = (IComponent) primarySelection;
  217. }
  218. OnSelectionChanged ();
  219. }
  220. }
  221. }