[System.Windows.Forms] Fixes DataGridViewColumnCollection comparer not to return...
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / DataGridViewColumnCollection.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Pedro Martínez Juliá <pedromj@gmail.com>
24 //
25
26
27 using System.ComponentModel;
28 using System.Collections;
29 using System.Collections.Generic;
30
31 namespace System.Windows.Forms
32 {
33         [ListBindable (false)]
34         public class DataGridViewColumnCollection : BaseCollection, IList, ICollection, IEnumerable
35         {
36                 private DataGridView dataGridView;
37                 private List<DataGridViewColumn> display_index_sorted;
38                 
39                 public DataGridViewColumnCollection (DataGridView dataGridView)
40                 {
41                         this.dataGridView = dataGridView;
42                         RegenerateSortedList ();
43                 }
44
45                 bool IList.IsFixedSize {
46                         get { return base.List.IsFixedSize; }
47                 }
48
49                 object IList.this [int index] {
50                         get { return this [index]; }
51                         set { throw new NotSupportedException(); }
52                 }
53
54                 public DataGridViewColumn this [int index] {
55                         get { return (DataGridViewColumn) base.List[index]; }
56                 }
57
58                 public DataGridViewColumn this [string columnName] {
59                         get {
60                                 foreach (DataGridViewColumn col in base.List) {
61                                         if (col.Name == columnName) {
62                                                 return col;
63                                         }
64                                 }
65                                 return null;
66                         }
67                 }
68
69                 public event CollectionChangeEventHandler CollectionChanged;
70
71                 int IList.Add (object value)
72                 {
73                         return Add(value as DataGridViewColumn);
74                 }
75
76                 public virtual int Add (DataGridViewColumn dataGridViewColumn)
77                 {
78                         int result = base.List.Add(dataGridViewColumn);
79                         if (dataGridViewColumn.DisplayIndex == -1)
80                                 dataGridViewColumn.DisplayIndexInternal = result;
81                         dataGridViewColumn.SetIndex(result);
82                         dataGridViewColumn.SetDataGridView(dataGridView);
83                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewColumn));
84                         return result;
85                 }
86
87                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
88                 public virtual int Add (string columnName, string headerText)
89                 {
90                         DataGridViewColumn col = new DataGridViewTextBoxColumn ();
91                         col.Name = columnName;
92                         col.HeaderText = headerText;
93                         return Add (col);
94                 }
95
96                 public virtual void AddRange (params DataGridViewColumn[] dataGridViewColumns)
97                 {
98                         foreach (DataGridViewColumn col in dataGridViewColumns)
99                                 Add (col);
100                 }
101
102                 public virtual void Clear ()
103                 {
104                         base.List.Clear ();
105                         
106                         // When we clear the column collection, all rows get deleted
107                         dataGridView.Rows.Clear ();
108                         dataGridView.RemoveEditingRow ();
109                         
110                         RegenerateSortedList ();
111
112                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
113                 }
114
115                 bool IList.Contains (object value)
116                 {
117                         return Contains (value as DataGridViewColumn);
118                 }
119
120                 public virtual bool Contains (DataGridViewColumn dataGridViewColumn)
121                 {
122                         return base.List.Contains (dataGridViewColumn);
123                 }
124
125                 public virtual bool Contains (string columnName)
126                 {
127                         foreach (DataGridViewColumn col in base.List)
128                                 if (col.Name == columnName)
129                                         return true;
130                         return false;
131                 }
132
133                 public void CopyTo (DataGridViewColumn [] array, int index)
134                 {
135                         base.List.CopyTo (array, index);
136                 }
137
138                 public int GetColumnCount (DataGridViewElementStates includeFilter)
139                 {
140                         return 0;
141                 }
142
143                 public int GetColumnsWidth (DataGridViewElementStates includeFilter)
144                 {
145                         return 0;
146                 }
147
148                 public DataGridViewColumn GetFirstColumn (DataGridViewElementStates includeFilter)
149                 {
150                         return null;
151                 }
152
153                 public DataGridViewColumn GetFirstColumn (DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter)
154                 {
155                         return null;
156                 }
157
158                 public DataGridViewColumn GetLastColumn (DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter)
159                 {
160                         return null;
161                 }
162
163                 public DataGridViewColumn GetNextColumn (DataGridViewColumn dataGridViewColumnStart, DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter)
164                 {
165                         return null;
166                 }
167
168                 public DataGridViewColumn GetPreviousColumn (DataGridViewColumn dataGridViewColumnStart, DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter)
169                 {
170                         return null;
171                 }
172
173                 int IList.IndexOf (object value)
174                 {
175                         return IndexOf (value as DataGridViewColumn);
176                 }
177
178                 public int IndexOf (DataGridViewColumn dataGridViewColumn)
179                 {
180                         return base.List.IndexOf (dataGridViewColumn);
181                 }
182
183                 void IList.Insert (int index, object value)
184                 {
185                         Insert (index, value as DataGridViewColumn);
186                 }
187
188                 public virtual void Insert (int columnIndex, DataGridViewColumn dataGridViewColumn)
189                 {
190                         base.List.Insert (columnIndex, dataGridViewColumn);
191                         if (dataGridViewColumn.DisplayIndex == -1)
192                                 dataGridViewColumn.DisplayIndexInternal = columnIndex;
193                         dataGridViewColumn.SetIndex (columnIndex);
194                         dataGridViewColumn.SetDataGridView (dataGridView);
195                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, dataGridViewColumn));
196                 }
197
198                 void IList.Remove (object value)
199                 {
200                         Remove (value as DataGridViewColumn);
201                 }
202
203                 public virtual void Remove (DataGridViewColumn dataGridViewColumn)
204                 {
205                         DataGridView.OnColumnPreRemovedInternal (new DataGridViewColumnEventArgs (dataGridViewColumn));
206                         base.List.Remove (dataGridViewColumn);
207                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, dataGridViewColumn));
208                 }
209
210                 public virtual void Remove (string columnName) {
211                         foreach (DataGridViewColumn col in base.List) {
212                                 if (col.Name == columnName) {
213                                         Remove(col);
214                                         return;
215                                 }
216                         }
217                 }
218
219                 public virtual void RemoveAt (int index)
220                 {
221                         DataGridViewColumn col = this [index];
222                         Remove (col);
223                 }
224
225                 protected DataGridView DataGridView {
226                         get { return dataGridView; }
227                 }
228
229                 protected virtual void OnCollectionChanged (CollectionChangeEventArgs e)
230                 {
231                         RegenerateIndexes ();
232                         RegenerateSortedList ();
233
234                         if (CollectionChanged != null)
235                                 CollectionChanged(this, e);
236                 }
237
238                 protected override ArrayList List {
239                         get { return base.List; }
240                 }
241
242                 internal List<DataGridViewColumn> ColumnDisplayIndexSortedArrayList {
243                         get { return display_index_sorted; }
244                 }
245
246                 private void RegenerateIndexes ()
247                 {
248                         for (int i = 0; i < Count; i++)
249                                 this[i].SetIndex (i);
250                 }
251                 
252                 internal void RegenerateSortedList ()
253                 {
254                         DataGridViewColumn[] array = (DataGridViewColumn[])base.List.ToArray (typeof (DataGridViewColumn));
255                         List<DataGridViewColumn> result = new List<DataGridViewColumn> (array);
256
257                         result.Sort (new ColumnDisplayIndexComparator ());
258                         for (int i = 0; i < result.Count; i++)
259                                 result[i].DisplayIndexInternal = i;
260                         
261                         display_index_sorted = result;
262                 }
263                 
264                 internal void ClearAutoGeneratedColumns ()
265                 {
266                         for (int i = list.Count - 1; i >= 0; i--)
267                                 if ((list[i] as DataGridViewColumn).AutoGenerated)
268                                         RemoveAt (i);
269                 }
270                 
271                 private class ColumnDisplayIndexComparator : IComparer<DataGridViewColumn>
272                 {
273                         public int Compare (DataGridViewColumn o1, DataGridViewColumn o2)
274                         {
275                                 return o1.DisplayIndex.CompareTo (o2.DisplayIndex);
276                         }
277                 }
278         }
279 }
280