2 namespace Microsoft
.VisualStudio
.Shell
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"]/*' />
11 /// This class implements the ISelectionContainer interface. It can be used to show
12 /// informations on the property window.
15 public class SelectionContainer
:
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"]/*' />
36 /// Creates a container with empty collections of selected and selectable objects.
38 public SelectionContainer()
40 _selectableObjects
= _emptyCollection
;
41 _selectedObjects
= _emptyCollection
;
44 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.SelectionContainer1"]/*' />
46 /// Creates a selection container with empty collections of selected and selectable objects.
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"]/*' />
58 /// Get or set the collection of the selectable objects
60 public ICollection SelectableObjects
64 return _selectableObjects
;
70 value = _emptyCollection
;
72 _selectableObjects
= value;
76 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.SelectedObjects"]/*' />
78 /// Get or set the collection of the selected objects.
80 public ICollection SelectedObjects
84 return _selectedObjects
;
90 value = _emptyCollection
;
92 _selectedObjects
= value;
96 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.SelectedObjectsChanged"]/*' />
98 /// This event is fired when the selection changes.
100 public event EventHandler SelectedObjectsChanged
;
102 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.ActivateObjects"]/*' />
104 /// Activates the selected objects. Its default implementation is empty.
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 )
129 #region ISelectionContainer Members
131 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.ISelectionContainer.CountObjects"]/*' />
133 int ISelectionContainer
.CountObjects(uint dwFlags
, out uint pc
)
138 pc
= (uint)SelectableObjects
.Count
;
142 pc
= (uint)SelectedObjects
.Count
;
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"]/*' />
153 int ISelectionContainer
.GetObjects(uint dwFlags
, uint cObjects
, object[] apUnkObjects
)
155 ICollection objects
= null;
160 objects
= SelectableObjects
;
164 objects
= SelectedObjects
;
168 throw new ArgumentException(SR
.GetString(SR
.General_UnsupportedValue
, dwFlags
), "dwFlags");
172 foreach (object obj
in objects
)
174 if (idx
>= cObjects
|| idx
>= apUnkObjects
.Length
)
178 apUnkObjects
[idx
++] = obj
;
180 return NativeMethods
.S_OK
;
183 /// <include file='doc\SelectionContainer.uex' path='docs/doc[@for="SelectionContainer.ISelectionContainer.SelectObjects"]/*' />
185 int ISelectionContainer
.SelectObjects(uint cSelect
, object[] apUnkSelect
, uint dwFlags
)
187 ChangeSelection(apUnkSelect
, (int)dwFlags
);
188 return NativeMethods
.S_OK
;