* TabControl.cs: Show the tooltip depending on the value
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / GridColumnStylesCollection.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 //      Jordi Mas i Hernandez <jordi@ximian.com>
24 //
25
26 using System;
27 using System.Collections;
28 using System.ComponentModel;
29
30 namespace System.Windows.Forms
31 {
32         [Editor("System.Windows.Forms.Design.DataGridColumnCollectionEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
33         [ListBindable(false)]
34         public class GridColumnStylesCollection : BaseCollection, IList
35         {
36                 private ArrayList items;
37                 private DataGridTableStyle owner;
38                 private bool fire_event;
39
40                 internal GridColumnStylesCollection (DataGridTableStyle tablestyle)
41                 {
42                         items = new ArrayList ();
43                         owner = tablestyle;
44                         fire_event = true;
45                 }
46
47                 #region Public Instance Properties
48                 public DataGridColumnStyle this [string columnName] {
49                         get {
50                                 int idx = FromColumnNameToIndex (columnName);
51                                 return idx == -1 ? null : this [idx];
52                         }
53                 }
54
55                 public DataGridColumnStyle this [int index] {
56                         get { return (DataGridColumnStyle) items[index]; }
57                 }
58
59                 
60 #if NET_2_0
61                 public DataGridColumnStyle this [PropertyDescriptor propertyDesciptor] {
62                         get {
63                                 PropertyDescriptor propDesc = propertyDesciptor;
64 #else
65                 public DataGridColumnStyle this [PropertyDescriptor propDesc] {
66                         get {
67 #endif
68                                 for (int i = 0; i < items.Count; i++) {
69                                         DataGridColumnStyle column = (DataGridColumnStyle) items[i];
70                                         if (column.PropertyDescriptor.Equals (propDesc))
71                                                 return column;
72                                 }
73                                 
74                                 return null;
75                         }
76                 }
77
78                 protected override ArrayList List {
79                         get { return items; }
80                 }
81
82                 int ICollection.Count {
83                         get { return items.Count;}
84                 }
85
86                 bool ICollection.IsSynchronized {
87                         get { return false; }
88                 }
89
90                 object ICollection.SyncRoot {
91                         get { return this; }
92                 }
93
94                 bool IList.IsFixedSize {
95                         get { return false; }
96                 }
97
98                 bool IList.IsReadOnly {
99                         get { return false; }
100                 }
101
102                 object IList.this [int index] {
103                         get { return items[index]; }
104                         set { throw new NotSupportedException (); }
105                 }
106
107                 #endregion Public Instance Properties
108                 
109                 #region Private Instance Properties
110                 internal bool FireEvents {
111                         get { return fire_event;}
112                         set { fire_event = value;}
113                 }
114                 #endregion Private Instance Properties
115
116                 #region Public Instance Methods
117                 public virtual int Add (DataGridColumnStyle column)
118                 {
119                         // TODO: MS allows duplicate columns. How they diferenciate between them?
120                         if (FromColumnNameToIndex (column.MappingName) != -1) {
121                                 throw new ArgumentException ("The ColumnStyles collection already has a column with this mapping name");
122                         }
123                         
124                         column.TableStyle = owner;
125                         column.SetDataGridInternal (owner.DataGrid);
126                         ConnectColumnEvents (column);
127                         int cnt = items.Add (column);
128                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, column));
129                         return cnt;
130                 }
131
132                 public void AddRange (DataGridColumnStyle[] columns)
133                 {
134                         foreach (DataGridColumnStyle mi in columns)
135                                 Add (mi);
136                 }
137
138                 public void Clear ()
139                 {
140                         items.Clear ();
141                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh , null));
142                 }
143
144                 public bool Contains (DataGridColumnStyle column)
145                 {
146                         return (FromColumnNameToIndex (column.MappingName) != -1);
147                 }
148
149 #if NET_2_0
150                 public bool Contains (PropertyDescriptor propertyDescriptor )
151                 {
152                         PropertyDescriptor propDesc = propertyDescriptor;
153 #else
154                 public bool Contains (PropertyDescriptor propDesc)
155                 {
156 #endif
157                         return (this [propDesc] != null);
158                 }
159
160                 public bool Contains (string name)
161                 {
162                         return (FromColumnNameToIndex (name) != -1);
163                 }
164
165                 void ICollection.CopyTo (Array array, int index)
166                 {
167                         items.CopyTo (array, index);
168                 }
169
170                 IEnumerator IEnumerable.GetEnumerator ()
171                 {
172                         return items.GetEnumerator ();
173                 }
174
175                 int IList.Add (object value)
176                 {
177                         return Add ((DataGridColumnStyle)value);
178                 }
179
180                 void IList.Clear ()
181                 {
182                         Clear ();
183                 }
184
185                 bool IList.Contains (object value)
186                 {
187                         return Contains ((DataGridColumnStyle)value);
188                 }
189
190                 int IList.IndexOf (object value)
191                 {
192                         return IndexOf ((DataGridColumnStyle)value);
193                 }
194
195                 void IList.Insert (int index, object value)
196                 {
197                         throw new NotSupportedException ();
198                 }
199
200                 void IList.Remove (object value)
201                 {
202                         Remove ((DataGridColumnStyle)value);
203                 }
204
205                 void IList.RemoveAt (int index)
206                 {
207                         RemoveAt (index);
208                 }
209                 
210                 public int IndexOf (DataGridColumnStyle element)
211                 {
212                         return items.IndexOf (element);
213                 }
214                 
215 #if NET_2_0
216                 protected void OnCollectionChanged (CollectionChangeEventArgs e)
217                 {
218                         CollectionChangeEventArgs ccevent = e;
219 #else
220                 protected void OnCollectionChanged (CollectionChangeEventArgs ccevent)
221                 {
222 #endif
223                         if (fire_event && CollectionChanged != null)
224                                 CollectionChanged (this, ccevent);
225                 }
226                 
227                 public void Remove (DataGridColumnStyle column)
228                 {
229                         items.Remove (column);
230                         DisconnectColumnEvents (column);
231                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, column));
232                 }
233                 
234                 public void RemoveAt (int index)
235                 {
236                         DataGridColumnStyle item = (DataGridColumnStyle)items[index];
237                         items.RemoveAt (index);
238                         DisconnectColumnEvents (item);
239                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, item));
240                 }
241                 
242                 public void ResetPropertyDescriptors ()
243                 {
244                         for (int i = 0; i < items.Count; i++) {
245                                 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
246                                 if (column.PropertyDescriptor != null) {
247                                         column.PropertyDescriptor = null;
248                                 }
249                         }
250                 }
251
252                 #endregion Public Instance Methods
253
254                 #region Events
255                 public event CollectionChangeEventHandler CollectionChanged;
256                 #endregion Events
257                 
258                 #region Private Instance Methods
259                 void ConnectColumnEvents (DataGridColumnStyle col)
260                 {
261                         col.AlignmentChanged += new EventHandler (ColumnAlignmentChangedEvent);
262                         col.FontChanged += new EventHandler (ColumnFontChangedEvent);
263                         col.HeaderTextChanged += new EventHandler (ColumnHeaderTextChanged);
264                         col.MappingNameChanged += new EventHandler (ColumnMappingNameChangedEvent);
265                         col.NullTextChanged += new EventHandler (ColumnNullTextChangedEvent);
266                         col.PropertyDescriptorChanged += new EventHandler (ColumnPropertyDescriptorChanged);
267                         col.ReadOnlyChanged += new EventHandler (ColumnReadOnlyChangedEvent);
268                         col.WidthChanged += new EventHandler (ColumnWidthChangedEvent);
269                 }
270
271                 void DisconnectColumnEvents (DataGridColumnStyle col)
272                 {
273                         col.AlignmentChanged -= new EventHandler (ColumnAlignmentChangedEvent);
274                         col.FontChanged -= new EventHandler (ColumnFontChangedEvent);
275                         col.HeaderTextChanged -= new EventHandler (ColumnHeaderTextChanged);
276                         col.MappingNameChanged -= new EventHandler (ColumnMappingNameChangedEvent);
277                         col.NullTextChanged -= new EventHandler (ColumnNullTextChangedEvent);
278                         col.PropertyDescriptorChanged -= new EventHandler (ColumnPropertyDescriptorChanged);
279                         col.ReadOnlyChanged -= new EventHandler (ColumnReadOnlyChangedEvent);
280                         col.WidthChanged -= new EventHandler (ColumnWidthChangedEvent);
281                 }
282
283                 void ColumnAlignmentChangedEvent (object sender, EventArgs e)
284                 {
285                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
286                 }
287
288                 void ColumnFontChangedEvent (object sender, EventArgs e)
289                 {
290                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
291                 }
292
293                 void ColumnHeaderTextChanged (object sender, EventArgs e)
294                 {
295                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
296                 }
297
298                 void ColumnMappingNameChangedEvent (object sender, EventArgs e)
299                 {
300                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
301                 }
302
303                 void ColumnNullTextChangedEvent (object sender, EventArgs e)
304                 {
305                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
306                 }
307
308                 void ColumnPropertyDescriptorChanged (object sender, EventArgs e)
309                 {
310                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
311                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, sender));
312                 }
313
314                 void ColumnReadOnlyChangedEvent (object sender, EventArgs e)
315                 {
316                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
317                 }
318
319                 void ColumnWidthChangedEvent (object sender, EventArgs e)
320                 {
321                         // XXX should this do a CollectionChangedEvent (Refresh, sender)?
322                 }
323
324                 private int FromColumnNameToIndex (string columnName)
325                 {
326                         for (int i = 0; i < items.Count; i++) {
327                                 DataGridColumnStyle column = (DataGridColumnStyle) items[i];
328                                 
329                                 if (column.MappingName == null || column.MappingName == string.Empty)
330                                         continue;
331
332                                 if (String.Compare (column.MappingName, columnName, true) == 0) {
333                                         return i;
334                                 }
335                         }
336                         
337                         return -1;
338                 }
339
340                 #endregion Private Instance Methods
341         }
342 }