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>
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.ComponentModel;
35 namespace System.Windows.Forms
38 public class ListViewGroupCollection : IList, ICollection, IEnumerable
40 private List<ListViewGroup> list = null;
41 private ListView list_view_owner = null;
42 private ListViewGroup default_group;
44 ListViewGroupCollection()
46 list = new List<ListViewGroup> ();
48 default_group = new ListViewGroup ("Default Group");
49 default_group.IsDefault = true;
52 internal ListViewGroupCollection(ListView listViewOwner) : this()
54 list_view_owner = listViewOwner;
55 default_group.ListViewOwner = listViewOwner;
58 internal ListView ListViewOwner {
59 get { return list_view_owner; }
60 set { list_view_owner = value; }
63 #region IEnumerable Members
65 public IEnumerator GetEnumerator()
67 return list.GetEnumerator();
72 #region ICollection Members
74 public void CopyTo(Array array, int index)
76 ((ICollection) list).CopyTo(array, index);
80 get { return list.Count; }
83 bool ICollection.IsSynchronized {
87 object ICollection.SyncRoot {
95 int IList.Add(object value)
97 if (!(value is ListViewGroup))
98 throw new ArgumentException("value");
100 return Add((ListViewGroup)value);
103 public int Add(ListViewGroup group)
110 if (this.list_view_owner != null)
111 list_view_owner.Redraw(true);
113 return list.Count - 1;
116 public ListViewGroup Add(string key, string headerText)
118 ListViewGroup newGroup = new ListViewGroup(key, headerText);
126 foreach (ListViewGroup group in list)
127 group.ListViewOwner = null;
131 if(list_view_owner != null)
132 list_view_owner.Redraw(true);
135 bool IList.Contains(object value)
137 if (value is ListViewGroup)
138 return Contains((ListViewGroup)value);
143 public bool Contains(ListViewGroup value)
145 return list.Contains(value);
148 int IList.IndexOf(object value)
150 if (value is ListViewGroup)
151 return IndexOf((ListViewGroup)value);
156 public int IndexOf(ListViewGroup value)
158 return list.IndexOf(value);
161 void IList.Insert(int index, object value)
163 if (value is ListViewGroup)
164 Insert(index, (ListViewGroup)value);
167 public void Insert(int index, ListViewGroup group)
172 CheckListViewItemsInGroup(group);
173 group.ListViewOwner = list_view_owner;
174 list.Insert(index, group);
176 if(list_view_owner != null)
177 list_view_owner.Redraw(true);
180 bool IList.IsFixedSize {
181 get { return false; }
184 bool IList.IsReadOnly {
185 get { return false; }
188 void IList.Remove(object value)
190 Remove((ListViewGroup)value);
193 public void Remove (ListViewGroup group)
195 int idx = list.IndexOf (group);
200 public void RemoveAt (int index)
202 if (list.Count <= index || index < 0)
205 ListViewGroup group = list [index];
206 group.ListViewOwner = null;
208 list.RemoveAt (index);
209 if (list_view_owner != null)
210 list_view_owner.Redraw (true);
213 object IList.this[int index] {
214 get { return this[index]; }
216 if (value is ListViewGroup)
217 this[index] = (ListViewGroup)value;
221 public ListViewGroup this[int index] {
223 if (list.Count <= index || index < 0)
224 throw new ArgumentOutOfRangeException("index");
229 if (list.Count <= index || index < 0)
230 throw new ArgumentOutOfRangeException("index");
232 if (Contains (value))
236 CheckListViewItemsInGroup (value);
238 list [index] = value;
240 if (list_view_owner != null)
241 list_view_owner.Redraw(true);
245 public ListViewGroup this [string key] {
247 int idx = IndexOfKey (key);
254 int idx = IndexOfKey (key);
262 int IndexOfKey (string key)
264 for (int i = 0; i < list.Count; i++)
265 if (list [i].Name == key)
273 public void AddRange(ListViewGroup[] groups)
275 foreach (ListViewGroup group in groups)
278 if (list_view_owner != null)
279 list_view_owner.Redraw (true);
282 public void AddRange(ListViewGroupCollection groups)
284 foreach (ListViewGroup group in groups)
287 if (list_view_owner != null)
288 list_view_owner.Redraw (true);
291 internal ListViewGroup GetInternalGroup (int index)
294 return default_group;
296 return list [index - 1];
299 internal int InternalCount {
301 return list.Count + 1;
305 internal ListViewGroup DefaultGroup {
307 return default_group;
311 void AddGroup (ListViewGroup group)
313 if (Contains (group))
316 CheckListViewItemsInGroup (group);
317 group.ListViewOwner = list_view_owner;
321 private void CheckListViewItemsInGroup(ListViewGroup value)
323 //check for correct ListView
324 foreach (ListViewItem item in value.Items)
326 if (item.ListView != null && item.ListView != this.list_view_owner)
327 throw new ArgumentException("ListViewItem belongs to a ListView control other than the one that owns this ListViewGroupCollection.",