* TextBoxBase.cs: These seem to be the correct values.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewRowCollection.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 using System.ComponentModel.Design.Serialization;
32
33 namespace System.Windows.Forms {
34
35         [DesignerSerializerAttribute ("System.Windows.Forms.Design.DataGridViewRowCollectionCodeDomSerializer, " + Consts.AssemblySystem_Design,
36                                       "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
37         [ListBindable (false)]
38         public class DataGridViewRowCollection : IList, ICollection, IEnumerable {
39
40                 private ArrayList list;
41                 private DataGridView dataGridView;
42
43                 private bool raiseEvent = true;
44
45                 public DataGridViewRowCollection (DataGridView dataGridView)
46                 {
47                         if (dataGridView == null) {
48                                 throw new ArgumentException("DataGridView is null.");
49                         }
50                         this.dataGridView = dataGridView;
51                         list = new ArrayList();
52                 }
53
54                 public int Count {
55                         get { return list.Count; }
56                 }
57
58                 bool IList.IsFixedSize {
59                         get { return list.IsFixedSize; }
60                 }
61
62                 bool IList.IsReadOnly {
63                         get { return list.IsReadOnly; }
64                 }
65
66                 bool ICollection.IsSynchronized {
67                         get { return list.IsSynchronized; }
68                 }
69
70                 object IList.this [int index] {
71                         get {
72                                 return this[index];
73                         }
74                         set { list[index] = value as DataGridViewRow; }
75                 }
76
77                 public DataGridViewRow this [int index] {
78                         get {
79                                 // Accessing a System.Windows.Forms.DataGridViewRow with this indexer causes the row to become unshared. To keep the row shared, use the System.Windows.Forms.DataGridViewRowCollection.SharedRow method. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.
80                                 return (DataGridViewRow) list[index];
81                         }
82                 }
83
84                 object ICollection.SyncRoot {
85                         get { return list.SyncRoot; }
86                 }
87
88                 public event CollectionChangeEventHandler CollectionChanged;
89
90                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
91                 public virtual int Add ()
92                 {
93                         return Add(dataGridView.RowTemplate.Clone() as DataGridViewRow);
94                 }
95
96                 int IList.Add(object o)
97                 {
98                         return Add(o as DataGridViewRow);
99                 }
100
101                 public virtual int Add (DataGridViewRow dataGridViewRow)
102                 {
103                         if (dataGridView.DataSource != null) {
104                                 throw new InvalidOperationException("DataSource of DataGridView is not null.");
105                         }
106                         if (dataGridView.Columns.Count == 0) {
107                                 throw new InvalidOperationException("DataGridView has no columns.");
108                         }
109                         dataGridViewRow.SetIndex(list.Count);
110                         dataGridViewRow.SetDataGridView(dataGridView);
111                         int result = list.Add(dataGridViewRow);
112                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewRow));
113                         if (raiseEvent) {
114                                 DataGridView.OnRowsAdded(new DataGridViewRowsAddedEventArgs(result, 1));
115                         }
116                         return result;
117                 }
118
119                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
120                 public virtual int Add (int count)
121                 {
122                         if (count <= 0) {
123                                 throw new ArgumentOutOfRangeException("Count is less than or equeal to 0.");
124                         }
125                         if (dataGridView.DataSource != null) {
126                                 throw new InvalidOperationException("DataSource of DataGridView is not null.");
127                         }
128                         if (dataGridView.Columns.Count == 0) {
129                                 throw new InvalidOperationException("DataGridView has no columns.");
130                         }
131                         raiseEvent = false;
132                         int result = 0;
133                         for (int i = 0; i < count; i++) {
134                                 result = Add(dataGridView.RowTemplate.Clone() as DataGridViewRow);
135                         }
136                         DataGridView.OnRowsAdded(new DataGridViewRowsAddedEventArgs(result - count + 1, count));
137                         raiseEvent = true;
138                         return result;
139                 }
140
141                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
142                 public virtual int Add (params object[] values)
143                 {
144                         if (values == null) {
145                                 throw new ArgumentNullException("values is null.");
146                         }
147                         if (dataGridView.VirtualMode) {
148                                 throw new InvalidOperationException("DataGridView is in virtual mode.");
149                         }
150                         DataGridViewRow row = new DataGridViewRow();
151                         int result = Add(row);
152                         row.SetValues(values);
153                         return result;
154                 }
155
156                 public virtual int AddCopies (int indexSource, int count)
157                 {
158                         raiseEvent = false;
159                         int lastIndex = 0;
160                         for (int i = 0; i < count; i++) {
161                                 lastIndex = AddCopy(indexSource);
162                         }
163                         DataGridView.OnRowsAdded(new DataGridViewRowsAddedEventArgs(lastIndex - count + 1, count));
164                         raiseEvent = true;
165                         return lastIndex;
166                 }
167
168                 public virtual int AddCopy (int indexSource)
169                 {
170                         return Add((list[indexSource] as DataGridViewRow).Clone() as DataGridViewRow);
171                 }
172
173                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
174                 public virtual void AddRange (params DataGridViewRow[] dataGridViewRows)
175                 {
176                         raiseEvent = false;
177                         int count = 0;
178                         int lastIndex = -1;
179                         foreach (DataGridViewRow row in dataGridViewRows) {
180                                 lastIndex = Add(row);
181                                 count++;
182                         }
183                         DataGridView.OnRowsAdded(new DataGridViewRowsAddedEventArgs(lastIndex - count + 1, count));
184                         raiseEvent = true;
185                 }
186
187                 public virtual void Clear ()
188                 {
189                         list.Clear();
190                 }
191
192                 bool IList.Contains (object o)
193                 {
194                         return Contains(o as DataGridViewRow);
195                 }
196
197                 public virtual bool Contains (DataGridViewRow dataGridViewRow)
198                 {
199                         return list.Contains(dataGridViewRow);
200                 }
201
202                 void ICollection.CopyTo (Array array, int index)
203                 {
204                         list.CopyTo(array, index);
205                 }
206
207                 public void CopyTo (DataGridViewRow[] array, int index)
208                 {
209                         list.CopyTo(array, index);
210                 }
211
212                 IEnumerator IEnumerable.GetEnumerator ()
213                 {
214                         return list.GetEnumerator();
215                 }
216
217                 public int GetFirstRow (DataGridViewElementStates includeFilter)
218                 {
219                         for (int i = 0; i < list.Count; i++) {
220                                 DataGridViewRow row = (DataGridViewRow) list[i];
221                                 if ((row.State & includeFilter) != 0) {
222                                         return i;
223                                 }
224                         }
225                         return -1;
226                 }
227
228                 public int GetFirstRow (DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter)
229                 {
230                         for (int i = 0; i < list.Count; i++) {
231                                 DataGridViewRow row = (DataGridViewRow) list[i];
232                                 if (((row.State & includeFilter) != 0) && ((row.State & excludeFilter) == 0)) {
233                                         return i;
234                                 }
235                         }
236                         return -1;
237                 }
238
239                 public int GetLastRow (DataGridViewElementStates includeFilter)
240                 {
241                         for (int i = list.Count - 1; i >= 0; i--) {
242                                 DataGridViewRow row = (DataGridViewRow) list[i];
243                                 if ((row.State & includeFilter) != 0) {
244                                         return i;
245                                 }
246                         }
247                         return -1;
248                 }
249
250                 public int GetNextRow (int indexStart, DataGridViewElementStates includeFilter)
251                 {
252                         for (int i = indexStart + 1; i < list.Count; i++) {
253                                 DataGridViewRow row = (DataGridViewRow) list[i];
254                                 if ((row.State & includeFilter) != 0) {
255                                         return i;
256                                 }
257                         }
258                         return -1;
259                 }
260
261                 public int GetNextRow (int indexStart, DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter)
262                 {
263                         for (int i = indexStart + 1; i < list.Count; i++) {
264                                 DataGridViewRow row = (DataGridViewRow) list[i];
265                                 if (((row.State & includeFilter) != 0) && ((row.State & excludeFilter) == 0)) {
266                                         return i;
267                                 }
268                         }
269                         return -1;
270                 }
271
272                 public int GetPreviousRow (int indexStart, DataGridViewElementStates includeFilter)
273                 {
274                         for (int i = indexStart - 1; i >= 0; i--) {
275                                 DataGridViewRow row = (DataGridViewRow) list[i];
276                                 if ((row.State & includeFilter) != 0) {
277                                         return i;
278                                 }
279                         }
280                         return -1;
281                 }
282
283                 public int GetPreviousRow (int indexStart, DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter)
284                 {
285                         for (int i = indexStart - 1; i >= 0; i--) {
286                                 DataGridViewRow row = (DataGridViewRow) list[i];
287                                 if (((row.State & includeFilter) != 0) && ((row.State & excludeFilter) == 0)) {
288                                         return i;
289                                 }
290                         }
291                         return -1;
292                 }
293
294                 public int GetRowCount (DataGridViewElementStates includeFilter)
295                 {
296                         int result = 0;
297                         foreach (DataGridViewRow row in list) {
298                                 if ((row.State & includeFilter) != 0) {
299                                         result ++;
300                                 }
301                         }
302                         return result;
303                 }
304
305                 public int GetRowsHeight (DataGridViewElementStates includeFilter)
306                 {
307                         int result = 0;
308                         foreach (DataGridViewRow row in list) {
309                                 if ((row.State & includeFilter) != 0) {
310                                         result += row.Height;
311                                 }
312                         }
313                         return result;
314                 }
315
316                 public virtual DataGridViewElementStates GetRowState (int rowIndex)
317                 {
318                         return (list[rowIndex] as DataGridViewRow).State;
319                 }
320
321                 int IList.IndexOf (object o)
322                 {
323                         return IndexOf(o as DataGridViewRow);
324                 }
325
326                 public int IndexOf (DataGridViewRow dataGridViewRow)
327                 {
328                         return list.IndexOf(dataGridViewRow);
329                 }
330
331                 void IList.Insert (int rowIndex, object o)
332                 {
333                         Insert(rowIndex, o as DataGridViewRow);
334                 }
335
336                 public virtual void Insert (int rowIndex, DataGridViewRow dataGridViewRow)
337                 {
338                         dataGridViewRow.SetIndex(rowIndex);
339                         dataGridViewRow.SetDataGridView(dataGridView);
340                         list[rowIndex] = dataGridViewRow;
341                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, dataGridViewRow));
342                         if (raiseEvent) {
343                                 DataGridView.OnRowsAdded(new DataGridViewRowsAddedEventArgs(rowIndex, 1));
344                         }
345                 }
346
347                 public virtual void Insert (int rowIndex, int count)
348                 {
349                         int index = rowIndex;
350                         raiseEvent = false;
351                         for (int i = 0; i < count; i++) {
352                                 Insert(index++, dataGridView.RowTemplate.Clone());
353                         }
354                         DataGridView.OnRowsAdded(new DataGridViewRowsAddedEventArgs(rowIndex, count));
355                         raiseEvent = true;
356                 }
357
358                 public virtual void Insert (int rowIndex, params object[] values)
359                 {
360                         if (values == null) {
361                                 throw new ArgumentNullException("Values is null.");
362                         }
363                         if (dataGridView.VirtualMode || dataGridView.DataSource != null) {
364                                 throw new InvalidOperationException();
365                         }
366                         DataGridViewRow row = new DataGridViewRow();
367                         row.SetValues(values);
368                         Insert(rowIndex, row);
369                 }
370
371                 public virtual void InsertCopies (int indexSource, int indexDestination, int count)
372                 {
373                         raiseEvent = false;
374                         int index = indexDestination;
375                         for (int i = 0; i < count; i++) {
376                                 InsertCopy(indexSource, index++);
377                         }
378                         DataGridView.OnRowsAdded(new DataGridViewRowsAddedEventArgs(indexDestination, count));
379                         raiseEvent = true;
380                 }
381
382                 public virtual void InsertCopy (int indexSource, int indexDestination)
383                 {
384                         Insert(indexDestination, (list[indexSource] as DataGridViewRow).Clone());
385                 }
386
387                 public virtual void InsertRange (int rowIndex, params DataGridViewRow[] dataGridViewRows)
388                 {
389                         raiseEvent = false;
390                         int index = rowIndex;
391                         int count = 0;
392                         foreach (DataGridViewRow row in dataGridViewRows) {
393                                 Insert(index++, row);
394                                 count++;
395                         }
396                         DataGridView.OnRowsAdded(new DataGridViewRowsAddedEventArgs(rowIndex, count));
397                         raiseEvent = true;
398                 }
399
400                 void IList.Remove (object o)
401                 {
402                         Remove(o as DataGridViewRow);
403                 }
404
405                 public virtual void Remove (DataGridViewRow dataGridViewRow)
406                 {
407                         list.Remove(dataGridViewRow);
408                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, dataGridViewRow));
409                         DataGridView.OnRowsRemoved(new DataGridViewRowsRemovedEventArgs(dataGridViewRow.Index, 1));
410                 }
411
412                 public virtual void RemoveAt (int index)
413                 {
414                         DataGridViewRow row = this[index];
415                         list.RemoveAt(index);
416                         OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, row));
417                         DataGridView.OnRowsRemoved(new DataGridViewRowsRemovedEventArgs(index, 1));
418                 }
419
420                 public DataGridViewRow SharedRow (int rowIndex)
421                 {
422                         return (DataGridViewRow) list[rowIndex];
423                 }
424
425                 protected DataGridView DataGridView {
426                         get { return dataGridView; }
427                 }
428
429                 protected ArrayList List {
430                         get { return list; }
431                 }
432
433                 protected virtual void OnCollectionChanged (CollectionChangeEventArgs e)
434                 {
435                         if (CollectionChanged != null) {
436                                 CollectionChanged (this, e);
437                         }
438                 }
439
440                 /************************/
441
442                 internal void InternalAdd (DataGridViewRow dataGridViewRow)
443                 {
444                         dataGridViewRow.SetIndex(list.Count);
445                         dataGridViewRow.SetDataGridView(dataGridView);
446                         list.Add(dataGridViewRow);
447                 }
448
449                 internal ArrayList RowIndexSortedArrayList {
450                         get {
451                                 ArrayList result = (ArrayList) list.Clone();
452                                 result.Sort(new RowIndexComparator());
453                                 return result;
454                         }
455                 }
456
457                 private class RowIndexComparator : IComparer {
458
459                         public int Compare (object o1, object o2) {
460                                 DataGridViewRow row1 = (DataGridViewRow) o1;
461                                 DataGridViewRow row2 = (DataGridViewRow) o2;
462                                 if (row1.Index < row2.Index) {
463                                         return -1;
464                                 }
465                                 else if (row1.Index > row2.Index) {
466                                         return 1;
467                                 }
468                                 else {
469                                         return 0;
470                                 }
471                         }
472
473                 }
474
475                 /************************/
476
477         }
478
479 }
480
481 #endif