2007-08-28 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TableLayoutSettings.cs
1 //
2 // TableLayoutSettings.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.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28
29 #if NET_2_0
30 using System;
31 using System.ComponentModel;
32 using System.Collections.Generic;
33 using System.Windows.Forms.Layout;
34 using System.Runtime.Serialization;
35
36 namespace System.Windows.Forms 
37 {
38         [Serializable]
39         [TypeConverter (typeof (TableLayoutSettingsTypeConverter))]
40         public sealed class TableLayoutSettings : LayoutSettings, ISerializable 
41         {
42                 private TableLayoutColumnStyleCollection column_styles;
43                 private TableLayoutRowStyleCollection row_styles;
44                 private TableLayoutPanelGrowStyle grow_style;
45                 private int column_count;
46                 private int row_count;
47                 private Dictionary<Object, int> columns;
48                 private Dictionary<Object, int> column_spans;
49                 private Dictionary<Object, int> rows;
50                 private Dictionary<Object, int> row_spans;
51                 private TableLayoutPanel panel;
52                 internal bool isSerialized;
53
54                 #region Internal Constructor
55                 internal TableLayoutSettings (TableLayoutPanel panel)
56                 {
57                         this.column_styles = new TableLayoutColumnStyleCollection (panel);
58                         this.row_styles = new TableLayoutRowStyleCollection (panel);
59                         this.grow_style = TableLayoutPanelGrowStyle.AddRows;
60                         this.column_count = 0;
61                         this.row_count = 0;
62                         this.columns = new Dictionary<object, int> ();
63                         this.column_spans = new Dictionary<object, int> ();
64                         this.rows = new Dictionary<object, int> ();
65                         this.row_spans = new Dictionary<object, int> ();
66                         this.panel = panel;
67                 }
68
69                 private TableLayoutSettings (SerializationInfo serializationInfo, StreamingContext context)
70                 {
71                         TypeConverter converter = TypeDescriptor.GetConverter (this);
72                         string text = serializationInfo.GetString ("SerializedString");
73                         if (!string.IsNullOrEmpty (text) && (converter != null)) {
74                                 TableLayoutSettings settings = converter.ConvertFromInvariantString (text) as TableLayoutSettings;
75                                 this.column_styles = settings.column_styles;
76                                 this.row_styles = settings.row_styles;
77                                 this.grow_style = settings.grow_style;
78                                 this.column_count = settings.column_count;
79                                 this.row_count = settings.row_count;
80                                 this.columns = settings.columns;
81                                 this.column_spans = settings.column_spans;
82                                 this.rows = settings.rows;
83                                 this.row_spans = settings.row_spans;
84                                 this.panel = settings.panel;
85                                 this.isSerialized = true;
86                         }
87                 }
88                 #endregion              
89
90                 #region Public Properties
91                 [DefaultValue (0)]
92                 public int ColumnCount {
93                         get { return this.column_count; }
94                         set {
95                                 if (value < 0)
96                                         throw new ArgumentOutOfRangeException();
97                                         
98                                 column_count = value;
99                         }
100                 }
101
102                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
103                 public TableLayoutColumnStyleCollection ColumnStyles {
104                         get { return this.column_styles; }
105                 }
106
107                 [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
108                 public TableLayoutPanelGrowStyle GrowStyle {
109                         get { return this.grow_style; }
110                         set {
111                                 if (!Enum.IsDefined (typeof(TableLayoutPanelGrowStyle), value))
112                                         throw new ArgumentException();
113                                         
114                                 grow_style = value;
115                         }
116                 }
117                 
118                 public override LayoutEngine LayoutEngine {
119                         get { return this.panel.LayoutEngine; }
120                 }
121                 
122                 [DefaultValue (0)]
123                 public int RowCount {
124                         get { return this.row_count; }
125                         set {
126                                 if (value < 0)
127                                         throw new ArgumentOutOfRangeException ();
128                                 row_count = value;
129                         }
130                 }
131
132                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
133                 public TableLayoutRowStyleCollection RowStyles {
134                         get { return row_styles; }
135                 }
136                 #endregion
137
138                 #region Public Methods
139                 [DefaultValue (-1)]
140                 public TableLayoutPanelCellPosition GetCellPosition (Object control)
141                 {
142                         if (control == null)
143                                 throw new ArgumentNullException ();
144
145                         int column;
146                         int row;
147
148                         if (!columns.TryGetValue (control, out column))
149                                 column = -1;
150                         if (!rows.TryGetValue (control, out row))
151                                 row = -1;
152
153                         return new TableLayoutPanelCellPosition (column, row);
154                 }
155
156                 [DefaultValue (-1)]
157                 public int GetColumn (Object control)
158                 {
159                         if (control == null)
160                                 throw new ArgumentNullException ();
161
162                         int retval;
163
164                         if (columns.TryGetValue (control, out retval))
165                                 return retval;
166
167                         return -1;
168                 }
169
170                 public int GetColumnSpan (Object control)
171                 {
172                         if (control == null)
173                                 throw new ArgumentNullException ();
174
175                         int retval;
176
177                         if (column_spans.TryGetValue (control, out retval))
178                                 return retval;
179
180                         return 1;
181                 }
182
183                 [DefaultValue (-1)]
184                 public int GetRow (Object control)
185                 {
186                         if (control == null)
187                                 throw new ArgumentNullException ();
188
189                         int retval;
190
191                         if (rows.TryGetValue (control, out retval))
192                                 return retval;
193
194                         return -1;
195                 }
196
197                 public int GetRowSpan (Object control)
198                 {
199                         if (control == null)
200                                 throw new ArgumentNullException ();
201
202                         int retval;
203
204                         if (row_spans.TryGetValue (control, out retval))
205                                 return retval;
206
207                         return 1;
208                 }
209
210                 [DefaultValue (-1)]
211                 public void SetCellPosition (Object control, TableLayoutPanelCellPosition cellPosition)
212                 {
213                         if (control == null)
214                                 throw new ArgumentNullException ();
215
216                         columns[control] = cellPosition.Column;
217                         rows[control] = cellPosition.Row;
218                 }
219
220                 public void SetColumn (Object control, int column)
221                 {
222                         if (control == null)
223                                 throw new ArgumentNullException ();
224                         if (column < -1)
225                                 throw new ArgumentException ();
226                                 
227                         columns[control] = column;
228                 }
229
230                 public void SetColumnSpan (Object control, int value)
231                 {
232                         if (control == null)
233                                 throw new ArgumentNullException ();
234                         if (value < -1)
235                                 throw new ArgumentException ();
236
237                         column_spans[control] = value;
238                 }
239
240                 public void SetRow (Object control, int row)
241                 {
242                         if (control == null)
243                                 throw new ArgumentNullException ();
244                         if (row < -1)
245                                 throw new ArgumentException ();
246
247                         rows[control] = row;
248                 }
249
250                 public void SetRowSpan (Object control, int value)
251                 {
252                         if (control == null)
253                                 throw new ArgumentNullException ();
254                         if (value < -1)
255                                 throw new ArgumentException ();
256
257                         row_spans[control] = value;
258                 }
259                 #endregion
260
261                 #region Internal Methods
262                 internal List<ControlInfo> GetControls ()
263                 {
264                         List<ControlInfo> list = new List<ControlInfo>();
265                         foreach (KeyValuePair <object, int> control in columns) {
266                                 ControlInfo info = new ControlInfo();
267                                 info.Control = control.Key;
268                                 info.Col = GetColumn(control.Key);
269                                 info.ColSpan = GetColumnSpan (control.Key);
270                                 info.Row = GetRow (control.Key);
271                                 info.RowSpan = GetRowSpan (control.Key);
272                                 list.Add (info);
273                         }
274                         return list;
275                 }
276
277                 #endregion
278
279                 #region ISerializable Members
280                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
281                 {
282                         TableLayoutSettingsTypeConverter conv = new TableLayoutSettingsTypeConverter ();
283                         string text = conv.ConvertToInvariantString (this);
284                         info.AddValue ("SerializedString", text);
285                 }
286                 #endregion
287                 
288                 internal class StyleConverter : TypeConverter
289                 {
290                         public override object ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
291                         {
292                                 if ((value == null) || !(value is String))
293                                         return base.ConvertFrom (context, culture, value);
294
295                                 return Enum.Parse (typeof (StyleConverter), (string)value, true);
296                         }
297
298                         public override object ConvertTo (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
299                         {
300                                 if ((value == null) || !(value is StyleConverter) || (destinationType != typeof (string)))
301                                         return base.ConvertTo (context, culture, value, destinationType);
302
303                                 return ((StyleConverter)value).ToString ();
304                         }
305                 }
306         }
307
308         internal struct ControlInfo
309         {
310                 public object Control;
311                 public int      Row;
312                 public int RowSpan;
313                 public int Col;
314                 public int ColSpan;
315         }
316 }
317 #endif