2 // TableLayoutSettings.cs
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
30 using System.ComponentModel;
31 using System.Collections.Generic;
32 using System.Windows.Forms.Layout;
33 using System.Runtime.Serialization;
35 namespace System.Windows.Forms
38 [TypeConverter (typeof (TableLayoutSettingsTypeConverter))]
39 public sealed class TableLayoutSettings : LayoutSettings, ISerializable
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;
53 #region Internal Constructor
54 internal TableLayoutSettings (TableLayoutPanel panel)
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;
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> ();
68 private TableLayoutSettings (SerializationInfo serializationInfo, StreamingContext context)
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;
89 #region Public Properties
91 public int ColumnCount {
92 get { return this.column_count; }
95 throw new ArgumentOutOfRangeException();
97 if (column_count != value) {
100 panel.PerformLayout (panel, "ColumnCount");
105 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
106 public TableLayoutColumnStyleCollection ColumnStyles {
107 get { return this.column_styles; }
110 [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
111 public TableLayoutPanelGrowStyle GrowStyle {
112 get { return this.grow_style; }
114 if (!Enum.IsDefined (typeof(TableLayoutPanelGrowStyle), value))
115 throw new ArgumentException();
117 if (grow_style != value) {
120 panel.PerformLayout (panel, "GrowStyle");
125 public override LayoutEngine LayoutEngine {
128 return panel.LayoutEngine;
129 return base.LayoutEngine;
134 public int RowCount {
135 get { return this.row_count; }
138 throw new ArgumentOutOfRangeException ();
140 if (row_count != value) {
144 panel.PerformLayout ();
149 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
150 public TableLayoutRowStyleCollection RowStyles {
151 get { return row_styles; }
155 #region Public Methods
157 public TableLayoutPanelCellPosition GetCellPosition (Object control)
160 throw new ArgumentNullException ();
165 if (!columns.TryGetValue (control, out column))
167 if (!(control is Control) || !columns.TryGetValue ((control as Control).Name, out column))
170 if (!rows.TryGetValue (control, out row))
172 if (!(control is Control) || !rows.TryGetValue ((control as Control).Name, out row))
176 return new TableLayoutPanelCellPosition (column, row);
180 public int GetColumn (Object control)
183 throw new ArgumentNullException ();
187 if (columns.TryGetValue (control, out retval))
189 if ((control is Control) && columns.TryGetValue ((control as Control).Name, out retval))
195 public int GetColumnSpan (Object control)
198 throw new ArgumentNullException ();
202 if (column_spans.TryGetValue (control, out retval))
204 if ((control is Control) && column_spans.TryGetValue ((control as Control).Name, out retval))
211 public int GetRow (Object control)
214 throw new ArgumentNullException ();
218 if (rows.TryGetValue (control, out retval))
220 if ((control is Control) && rows.TryGetValue ((control as Control).Name, out retval))
226 public int GetRowSpan (Object control)
229 throw new ArgumentNullException ();
233 if (row_spans.TryGetValue (control, out retval))
235 if ((control is Control) && row_spans.TryGetValue ((control as Control).Name, out retval))
242 public void SetCellPosition (Object control, TableLayoutPanelCellPosition cellPosition)
245 throw new ArgumentNullException ();
247 columns[control] = cellPosition.Column;
248 rows[control] = cellPosition.Row;
251 panel.PerformLayout ();
254 public void SetColumn (Object control, int column)
257 throw new ArgumentNullException ();
259 throw new ArgumentException ();
261 columns[control] = column;
264 panel.PerformLayout ();
267 public void SetColumnSpan (Object control, int value)
270 throw new ArgumentNullException ();
272 throw new ArgumentException ();
274 column_spans[control] = value;
277 panel.PerformLayout ();
280 public void SetRow (Object control, int row)
283 throw new ArgumentNullException ();
285 throw new ArgumentException ();
290 panel.PerformLayout ();
293 public void SetRowSpan (Object control, int value)
296 throw new ArgumentNullException ();
298 throw new ArgumentException ();
300 row_spans[control] = value;
303 panel.PerformLayout ();
307 #region Internal Methods
308 internal List<ControlInfo> GetControls ()
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);
325 #region ISerializable Members
326 void ISerializable.GetObjectData (SerializationInfo si, StreamingContext context)
328 TableLayoutSettingsTypeConverter conv = new TableLayoutSettingsTypeConverter ();
329 string text = conv.ConvertToInvariantString (this);
330 si.AddValue ("SerializedString", text);
334 internal class StyleConverter : TypeConverter
336 public override object ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
338 if ((value == null) || !(value is String))
339 return base.ConvertFrom (context, culture, value);
341 return Enum.Parse (typeof (StyleConverter), (string)value, true);
344 public override object ConvertTo (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
346 if ((value == null) || !(value is StyleConverter) || (destinationType != typeof (string)))
347 return base.ConvertTo (context, culture, value, destinationType);
349 return ((StyleConverter)value).ToString ();
354 internal struct ControlInfo
356 public object Control;