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)
31 using System.ComponentModel;
32 using System.Collections.Generic;
33 using System.Windows.Forms.Layout;
34 using System.Runtime.Serialization;
36 namespace System.Windows.Forms
39 [TypeConverter (typeof (TableLayoutSettingsTypeConverter))]
40 public sealed class TableLayoutSettings : LayoutSettings, ISerializable
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;
54 #region Internal Constructor
55 internal TableLayoutSettings (TableLayoutPanel panel)
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;
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> ();
69 private TableLayoutSettings (SerializationInfo serializationInfo, StreamingContext context)
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;
90 #region Public Properties
92 public int ColumnCount {
93 get { return this.column_count; }
96 throw new ArgumentOutOfRangeException();
102 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
103 public TableLayoutColumnStyleCollection ColumnStyles {
104 get { return this.column_styles; }
107 [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
108 public TableLayoutPanelGrowStyle GrowStyle {
109 get { return this.grow_style; }
111 if (!Enum.IsDefined (typeof(TableLayoutPanelGrowStyle), value))
112 throw new ArgumentException();
118 public override LayoutEngine LayoutEngine {
119 get { return this.panel.LayoutEngine; }
123 public int RowCount {
124 get { return this.row_count; }
127 throw new ArgumentOutOfRangeException ();
132 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
133 public TableLayoutRowStyleCollection RowStyles {
134 get { return row_styles; }
138 #region Public Methods
140 public TableLayoutPanelCellPosition GetCellPosition (Object control)
143 throw new ArgumentNullException ();
148 if (!columns.TryGetValue (control, out column))
150 if (!rows.TryGetValue (control, out row))
153 return new TableLayoutPanelCellPosition (column, row);
157 public int GetColumn (Object control)
160 throw new ArgumentNullException ();
164 if (columns.TryGetValue (control, out retval))
170 public int GetColumnSpan (Object control)
173 throw new ArgumentNullException ();
177 if (column_spans.TryGetValue (control, out retval))
184 public int GetRow (Object control)
187 throw new ArgumentNullException ();
191 if (rows.TryGetValue (control, out retval))
197 public int GetRowSpan (Object control)
200 throw new ArgumentNullException ();
204 if (row_spans.TryGetValue (control, out retval))
211 public void SetCellPosition (Object control, TableLayoutPanelCellPosition cellPosition)
214 throw new ArgumentNullException ();
216 columns[control] = cellPosition.Column;
217 rows[control] = cellPosition.Row;
220 public void SetColumn (Object control, int column)
223 throw new ArgumentNullException ();
225 throw new ArgumentException ();
227 columns[control] = column;
230 public void SetColumnSpan (Object control, int value)
233 throw new ArgumentNullException ();
235 throw new ArgumentException ();
237 column_spans[control] = value;
240 public void SetRow (Object control, int row)
243 throw new ArgumentNullException ();
245 throw new ArgumentException ();
250 public void SetRowSpan (Object control, int value)
253 throw new ArgumentNullException ();
255 throw new ArgumentException ();
257 row_spans[control] = value;
261 #region Internal Methods
262 internal List<ControlInfo> GetControls ()
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);
279 #region ISerializable Members
280 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
282 TableLayoutSettingsTypeConverter conv = new TableLayoutSettingsTypeConverter ();
283 string text = conv.ConvertToInvariantString (this);
284 info.AddValue ("SerializedString", text);
288 internal class StyleConverter : TypeConverter
290 public override object ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
292 if ((value == null) || !(value is String))
293 return base.ConvertFrom (context, culture, value);
295 return Enum.Parse (typeof (StyleConverter), (string)value, true);
298 public override object ConvertTo (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
300 if ((value == null) || !(value is StyleConverter) || (destinationType != typeof (string)))
301 return base.ConvertTo (context, culture, value, destinationType);
303 return ((StyleConverter)value).ToString ();
308 internal struct ControlInfo
310 public object Control;