SelectionService.cs 6.6 KB

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