* ListView.cs: When doing layout calculations don't use a ref
[mono.git] / mcs / class / Managed.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 #if NET_2_0
28
29 using System.ComponentModel;
30 using System.Collections;
31
32 namespace System.Windows.Forms {
33
34         [ListBindable (false)]
35         public class DataGridViewColumnCollection : BaseCollection, IList, ICollection, IEnumerable {
36
37                 private DataGridView dataGridView;
38
39                 public DataGridViewColumnCollection (DataGridView dataGridView) {
40                         this.dataGridView = dataGridView;
41                 }
42
43                 bool IList.IsFixedSize {
44                         get { return base.List.IsFixedSize; }
45                 }
46
47                 object IList.this [int index] {
48                         get { return this[index]; }
49                         set { throw new NotSupportedException(); }
50                 }
51
52                 public DataGridViewColumn this [int index] {
53                         get { return (DataGridViewColumn) base.List[index]; }
54                 }
55
56                 public DataGridViewColumn this [string columnName] {
57                         get {
58                                 foreach (DataGridViewColumn col in base.List) {
59                                         if (col.Name == columnName) {
60                                                 return col;
61                                         }
62                                 }
63                                 return null;
64                         }
65                 }
66
67                 public event CollectionChangeEventHandler CollectionChanged;
68
69                 int IList.Add (object o) {
70                         return Add(o as DataGridViewColumn);
71                 }
72
73                 public virtual int Add (DataGridViewColumn dataGridViewColumn) {
74                         dataGridViewColumn.SetIndex(base.List.Count);
75                         dataGridViewColumn.SetDataGridView(dataGridView);
76                         int result = base.List.Add(dataGridViewColumn);
77                         DataGridView.OnColumnAddedInternal (new DataGridViewColumnEventArgs (dataGridViewColumn));
78                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewColumn));
79                         return result;
80                 }
81
82                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
83                 public virtual int Add (string columnName, string headerText) {
84                         DataGridViewColumn col = new DataGridViewTextBoxColumn();
85                         col.Name = columnName;
86                         col.HeaderText = headerText;
87                         return Add(col);
88                 }
89
90                 public virtual void AddRange (params DataGridViewColumn[] dataGridViewColumns) {
91                         foreach (DataGridViewColumn col in dataGridViewColumns) {
92                                 Add(col);
93                         }
94                 }
95
96                 public virtual void Clear () {
97                         base.List.Clear();
98                 }
99
100                 bool IList.Contains (object o) {
101                         return Contains(o as DataGridViewColumn);
102                 }
103
104                 public virtual bool Contains (DataGridViewColumn dataGridViewColumn) {
105                         return base.List.Contains(dataGridViewColumn);
106                 }
107
108                 public virtual bool Contains (string columnName) {
109                         foreach (DataGridViewColumn col in base.List) {
110                                 if (col.Name == columnName) {
111                                         return true;
112                                 }
113                         }
114                         return false;
115                 }
116
117                 public void CopyTo (DataGridViewColumn[] array, int index) {
118                         base.List.CopyTo(array, index);
119                 }
120
121                 public int GetColumnCount (DataGridViewElementStates includeFilter) {
122                         return 0;
123                 }
124
125                 public int GetColumnsWidth (DataGridViewElementStates includeFilter) {
126                         return 0;
127                 }
128
129                 public DataGridViewColumn GetFirstColumn (DataGridViewElementStates includeFilter) {
130                         return null;
131                 }
132
133                 public DataGridViewColumn GetFirstColumn (DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter) {
134                         return null;
135                 }
136
137                 public DataGridViewColumn GetLastColumn (DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter) {
138                         return null;
139                 }
140
141                 public DataGridViewColumn GetNextColumn (DataGridViewColumn dataGridViewColumnStart, DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter) {
142                         return null;
143                 }
144
145                 public DataGridViewColumn GetPreviousColumn (DataGridViewColumn dataGridViewColumnStart, DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter) {
146                         return null;
147                 }
148
149                 int IList.IndexOf (object o) {
150                         return IndexOf(o as DataGridViewColumn);
151                 }
152
153                 public int IndexOf (DataGridViewColumn dataGridViewColumn) {
154                         return base.List.IndexOf(dataGridViewColumn);
155                 }
156
157                 void IList.Insert (int columnIndex, object o) {
158                         Insert(columnIndex, o as DataGridViewColumn);
159                 }
160
161                 public virtual void Insert (int columnIndex, DataGridViewColumn dataGridViewColumn) {
162                         dataGridViewColumn.SetIndex(columnIndex);
163                         base.List.Insert(columnIndex, dataGridViewColumn);
164                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewColumn));
165                 }
166
167                 void IList.Remove (object o) {
168                         Remove(o as DataGridViewColumn);
169                 }
170
171                 public virtual void Remove (DataGridViewColumn dataGridViewColumn) {
172                         base.List.Remove(dataGridViewColumn);
173                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, dataGridViewColumn));
174                 }
175
176                 public virtual void Remove (string columnName) {
177                         foreach (DataGridViewColumn col in base.List) {
178                                 if (col.Name == columnName) {
179                                         Remove(col);
180                                         return;
181                                 }
182                         }
183                 }
184
185                 public virtual void RemoveAt (int index) {
186                         DataGridViewColumn col = this[index];
187                         base.List.RemoveAt(index);
188                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, col));
189                 }
190
191                 protected DataGridView DataGridView {
192                         get { return dataGridView; }
193                 }
194
195                 protected virtual void OnCollectionChanged (CollectionChangeEventArgs e) {
196                         if (CollectionChanged != null) {
197                                 CollectionChanged(this, e);
198                         }
199                 }
200
201                 protected override ArrayList List {
202                         get { return base.List; }
203                 }
204
205                 /************************/
206
207                 internal ArrayList ColumnDisplayIndexSortedArrayList {
208                         get {
209                                 ArrayList result = (ArrayList) base.List.Clone();
210                                 result.Sort(new ColumnDisplayIndexComparator());
211                                 return result;
212                         }
213                 }
214
215                 private class ColumnDisplayIndexComparator : IComparer {
216
217                         public int Compare (object o1, object o2) {
218                                 DataGridViewColumn col1 = (DataGridViewColumn) o1;
219                                 DataGridViewColumn col2 = (DataGridViewColumn) o2;
220                                 if (col1.DisplayIndex < col2.DisplayIndex) {
221                                         return -1;
222                                 }
223                                 else if (col1.DisplayIndex > col2.DisplayIndex) {
224                                         return 1;
225                                 }
226                                 else {
227                                         return 0;
228                                 }
229                         }
230
231                 }
232
233                 /************************/
234
235
236         }
237
238 }
239
240 #endif