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