2009-02-21 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Managed.Windows.Forms / System.Windows.Forms / ListViewGroupCollection.cs
blob700051db456c1668d0c810a8235fb7c201dbf9e4
1 //
2 // ListViewGroupCollection.cs
3 //
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:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
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
25 // Author:
26 // Daniel Nauck (dna(at)mono-project(dot)de)
27 // Carlos Alberto Cortez <calberto.cortez@gmail.com>
29 #if NET_2_0
31 using System;
32 using System.Text;
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.ComponentModel;
37 namespace System.Windows.Forms
39 [ListBindable(false)]
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();
72 #endregion
74 #region ICollection Members
76 public void CopyTo(Array array, int index)
78 ((ICollection) list).CopyTo(array, index);
81 public int Count {
82 get { return list.Count; }
85 bool ICollection.IsSynchronized {
86 get { return true; }
89 object ICollection.SyncRoot {
90 get { return this; }
93 #endregion
95 #region IList Members
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)
107 if (Contains(group))
108 return -1;
110 AddGroup (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);
121 Add(newGroup);
123 return newGroup;
126 public void Clear()
128 foreach (ListViewGroup group in list)
129 group.ListViewOwner = null;
131 list.Clear ();
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);
141 else
142 return false;
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);
154 else
155 return -1;
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)
171 if (Contains(group))
172 return;
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);
198 if (idx != -1)
199 RemoveAt (idx);
202 public void RemoveAt (int index)
204 if (list.Count <= index || index < 0)
205 return;
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]; }
217 set {
218 if (value is ListViewGroup)
219 this[index] = (ListViewGroup)value;
223 public ListViewGroup this[int index] {
224 get {
225 if (list.Count <= index || index < 0)
226 throw new ArgumentOutOfRangeException("index");
228 return list [index];
230 set {
231 if (list.Count <= index || index < 0)
232 throw new ArgumentOutOfRangeException("index");
234 if (Contains (value))
235 return;
237 if (value != null)
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] {
248 get {
249 int idx = IndexOfKey (key);
250 if (idx != -1)
251 return this [idx];
253 return null;
255 set {
256 int idx = IndexOfKey (key);
257 if (idx == -1)
258 return;
260 this [idx] = value;
264 int IndexOfKey (string key)
266 for (int i = 0; i < list.Count; i++)
267 if (list [i].Name == key)
268 return i;
270 return -1;
273 #endregion
275 public void AddRange(ListViewGroup[] groups)
277 foreach (ListViewGroup group in groups)
278 AddGroup (group);
280 if (list_view_owner != null)
281 list_view_owner.Redraw (true);
284 public void AddRange(ListViewGroupCollection groups)
286 foreach (ListViewGroup group in groups)
287 AddGroup (group);
289 if (list_view_owner != null)
290 list_view_owner.Redraw (true);
293 internal ListViewGroup GetInternalGroup (int index)
295 if (index == 0)
296 return default_group;
298 return list [index - 1];
301 internal int InternalCount {
302 get {
303 return list.Count + 1;
307 internal ListViewGroup DefaultGroup {
308 get {
309 return default_group;
313 void AddGroup (ListViewGroup group)
315 if (Contains (group))
316 return;
318 CheckListViewItemsInGroup (group);
319 group.ListViewOwner = list_view_owner;
320 list.Add (group);
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.",
330 "ListViewGroup");
335 #endif