added some development tools
[windows-sources.git] / developer / VSSDK / VisualStudioIntegration / Common / Source / CSharp / Shell / SelectionContainer.cs
blob54a587372cdacdbdcc048b0f34c1060af946f5a2
2 namespace Microsoft.VisualStudio.Shell
4 using System;
5 using System.Collections;
6 using System.Runtime.InteropServices;
7 using ISelectionContainer = Microsoft.VisualStudio.Shell.Interop.ISelectionContainer;
9 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer"]/*' />
10 /// <devdoc>
11 /// This class implements the ISelectionContainer interface. It can be used to show
12 /// informations on the property window.
13 /// </devdoc>
14 [CLSCompliant(false)]
15 public class SelectionContainer :
16 ISelectionContainer
18 private ICollection _selectableObjects;
19 private ICollection _selectedObjects;
20 private readonly bool _selectableReadOnly;
21 private readonly bool _selectedReadOnly;
23 private static ICollection _emptyCollection = new Object[0];
25 // Constants for selection container flags.
26 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.ALL"]/*' />
27 public const uint ALL = 0x1;
28 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.SELECTED"]/*' />
29 public const uint SELECTED = 0x2;
31 private const int SELOBJ_ACTIVATE_WINDOW = 0x1;
34 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.SelectionContainer"]/*' />
35 /// <devdoc>
36 /// Creates a container with empty collections of selected and selectable objects.
37 /// </devdoc>
38 public SelectionContainer()
40 _selectableObjects = _emptyCollection;
41 _selectedObjects = _emptyCollection;
44 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.SelectionContainer1"]/*' />
45 /// <devdoc>
46 /// Creates a selection container with empty collections of selected and selectable objects.
47 /// </devdoc>
48 /// <param name="selectableReadOnly">Specifies if the collection of the selectable objects is read only.</param>
49 /// <param name="selectedReadOnly">Specifies if the selection is read only.</param>
50 public SelectionContainer(bool selectableReadOnly, bool selectedReadOnly) : this()
52 _selectableReadOnly = selectableReadOnly;
53 _selectedReadOnly = selectedReadOnly;
56 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.SelectableObjects"]/*' />
57 /// <devdoc>
58 /// Get or set the collection of the selectable objects
59 /// </devdoc>
60 public ICollection SelectableObjects
62 get
64 return _selectableObjects;
66 set
68 if (value == null)
70 value = _emptyCollection;
72 _selectableObjects = value;
76 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.SelectedObjects"]/*' />
77 /// <devdoc>
78 /// Get or set the collection of the selected objects.
79 /// </devdoc>
80 public ICollection SelectedObjects
82 get
84 return _selectedObjects;
86 set
88 if (value == null)
90 value = _emptyCollection;
92 _selectedObjects = value;
96 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.SelectedObjectsChanged"]/*' />
97 /// <devdoc>
98 /// This event is fired when the selection changes.
99 /// </devdoc>
100 public event EventHandler SelectedObjectsChanged;
102 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.ActivateObjects"]/*' />
103 /// <devdoc>
104 /// Activates the selected objects. Its default implementation is empty.
105 /// </devdoc>
106 protected virtual void ActivateObjects()
108 // This default implementation of this function is empty.
111 // Helper function to change the selected objects
112 private void ChangeSelection(object[] prgUnkObjects, int dwFlags)
114 // Check if it is possible to change the selection.
115 if (_selectedReadOnly) throw new InvalidOperationException();
116 // Store the array of selected object in the internal array
117 SelectedObjects = prgUnkObjects;
118 // Raise the "Selected objects changed" event.
119 if (SelectedObjectsChanged != null) SelectedObjectsChanged(this, EventArgs.Empty);
120 // Check if the objects need to be activated
121 if ( (dwFlags & SELOBJ_ACTIVATE_WINDOW) != 0 )
123 ActivateObjects();
129 #region ISelectionContainer Members
131 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.ISelectionContainer.CountObjects"]/*' />
132 /// <internalonly/>
133 int ISelectionContainer.CountObjects(uint dwFlags, out uint pc)
135 switch (dwFlags)
137 case ALL:
138 pc = (uint)SelectableObjects.Count;
139 break;
141 case SELECTED:
142 pc = (uint)SelectedObjects.Count;
143 break;
145 default:
146 throw new ArgumentException(SR.GetString(SR.General_UnsupportedValue, dwFlags), "dwFlags");
148 return NativeMethods.S_OK;
151 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.ISelectionContainer.GetObjects"]/*' />
152 /// <internalonly/>
153 int ISelectionContainer.GetObjects(uint dwFlags, uint cObjects, object[] apUnkObjects)
155 ICollection objects = null;
157 switch (dwFlags)
159 case ALL:
160 objects = SelectableObjects;
161 break;
163 case SELECTED:
164 objects = SelectedObjects;
165 break;
167 default:
168 throw new ArgumentException(SR.GetString(SR.General_UnsupportedValue, dwFlags), "dwFlags");
171 int idx = 0;
172 foreach (object obj in objects)
174 if (idx >= cObjects || idx >= apUnkObjects.Length)
176 break;
178 apUnkObjects[idx++] = obj;
180 return NativeMethods.S_OK;
183 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.ISelectionContainer.SelectObjects"]/*' />
184 /// <internalonly/>
185 int ISelectionContainer.SelectObjects(uint cSelect, object[] apUnkSelect, uint dwFlags)
187 ChangeSelection(apUnkSelect, (int)dwFlags);
188 return NativeMethods.S_OK;
191 #endregion