2 // ListViewGroupCollection.cs
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 // Copyright (c) 2006 Daniel Nauck
26 // Daniel Nauck (dna(at)mono-project(dot)de)
27 // Carlos Alberto Cortez <calberto.cortez@gmail.com>
33 using System
.Collections
;
34 using System
.Collections
.Generic
;
35 using System
.ComponentModel
;
37 namespace System
.Windows
.Forms
40 public class ListViewGroupCollection
: IList
, ICollection
, IEnumerable
42 private List
<ListViewGroup
> list
= null;
43 private ListView list_view_owner
= null;
44 private ListViewGroup default_group
;
46 ListViewGroupCollection()
48 list
= new List
<ListViewGroup
> ();
50 default_group
= new ListViewGroup ("Default Group");
51 default_group
.IsDefault
= true;
54 internal ListViewGroupCollection(ListView listViewOwner
) : this()
56 list_view_owner
= listViewOwner
;
57 default_group
.ListViewOwner
= listViewOwner
;
60 internal ListView ListViewOwner
{
61 get { return list_view_owner; }
62 set { list_view_owner = value; }
65 #region IEnumerable Members
67 public IEnumerator
GetEnumerator()
69 return list
.GetEnumerator();
74 #region ICollection Members
76 public void CopyTo(Array array
, int index
)
78 ((ICollection
) list
).CopyTo(array
, index
);
82 get { return list.Count; }
85 bool ICollection
.IsSynchronized
{
89 object ICollection
.SyncRoot
{
97 int IList
.Add(object value)
99 if (!(value is ListViewGroup
))
100 throw new ArgumentException("value");
102 return Add((ListViewGroup
)value);
105 public int Add(ListViewGroup
group)
112 if (this.list_view_owner
!= null)
113 list_view_owner
.Redraw(true);
115 return list
.Count
- 1;
118 public ListViewGroup
Add(string key
, string headerText
)
120 ListViewGroup newGroup
= new ListViewGroup(key
, headerText
);
128 foreach (ListViewGroup
group in list
)
129 group.ListViewOwner
= null;
133 if(list_view_owner
!= null)
134 list_view_owner
.Redraw(true);
137 bool IList
.Contains(object value)
139 if (value is ListViewGroup
)
140 return Contains((ListViewGroup
)value);
145 public bool Contains(ListViewGroup
value)
147 return list
.Contains(value);
150 int IList
.IndexOf(object value)
152 if (value is ListViewGroup
)
153 return IndexOf((ListViewGroup
)value);
158 public int IndexOf(ListViewGroup
value)
160 return list
.IndexOf(value);
163 void IList
.Insert(int index
, object value)
165 if (value is ListViewGroup
)
166 Insert(index
, (ListViewGroup
)value);
169 public void Insert(int index
, ListViewGroup
group)
174 CheckListViewItemsInGroup(group);
175 group.ListViewOwner
= list_view_owner
;
176 list
.Insert(index
, group);
178 if(list_view_owner
!= null)
179 list_view_owner
.Redraw(true);
182 bool IList
.IsFixedSize
{
183 get { return false; }
186 bool IList
.IsReadOnly
{
187 get { return false; }
190 void IList
.Remove(object value)
192 Remove((ListViewGroup
)value);
195 public void Remove (ListViewGroup
group)
197 int idx
= list
.IndexOf (group);
202 public void RemoveAt (int index
)
204 if (list
.Count
<= index
|| index
< 0)
207 ListViewGroup
group = list
[index
];
208 group.ListViewOwner
= null;
210 list
.RemoveAt (index
);
211 if (list_view_owner
!= null)
212 list_view_owner
.Redraw (true);
215 object IList
.this[int index
] {
216 get { return this[index]; }
218 if (value is ListViewGroup
)
219 this[index
] = (ListViewGroup
)value;
223 public ListViewGroup
this[int index
] {
225 if (list
.Count
<= index
|| index
< 0)
226 throw new ArgumentOutOfRangeException("index");
231 if (list
.Count
<= index
|| index
< 0)
232 throw new ArgumentOutOfRangeException("index");
234 if (Contains (value))
238 CheckListViewItemsInGroup (value);
240 list
[index
] = value;
242 if (list_view_owner
!= null)
243 list_view_owner
.Redraw(true);
247 public ListViewGroup
this [string key
] {
249 int idx
= IndexOfKey (key
);
256 int idx
= IndexOfKey (key
);
264 int IndexOfKey (string key
)
266 for (int i
= 0; i
< list
.Count
; i
++)
267 if (list
[i
].Name
== key
)
275 public void AddRange(ListViewGroup
[] groups
)
277 foreach (ListViewGroup
group in groups
)
280 if (list_view_owner
!= null)
281 list_view_owner
.Redraw (true);
284 public void AddRange(ListViewGroupCollection groups
)
286 foreach (ListViewGroup
group in groups
)
289 if (list_view_owner
!= null)
290 list_view_owner
.Redraw (true);
293 internal ListViewGroup
GetInternalGroup (int index
)
296 return default_group
;
298 return list
[index
- 1];
301 internal int InternalCount
{
303 return list
.Count
+ 1;
307 internal ListViewGroup DefaultGroup
{
309 return default_group
;
313 void AddGroup (ListViewGroup
group)
315 if (Contains (group))
318 CheckListViewItemsInGroup (group);
319 group.ListViewOwner
= list_view_owner
;
323 private void CheckListViewItemsInGroup(ListViewGroup
value)
325 //check for correct ListView
326 foreach (ListViewItem item
in value.Items
)
328 if (item
.ListView
!= null && item
.ListView
!= this.list_view_owner
)
329 throw new ArgumentException("ListViewItem belongs to a ListView control other than the one that owns this ListViewGroupCollection.",