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 internal 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();
98 if (column_count != value) {
101 panel.PerformLayout (panel, "ColumnCount");
106 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
107 public TableLayoutColumnStyleCollection ColumnStyles {
108 get { return this.column_styles; }
111 [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
112 public TableLayoutPanelGrowStyle GrowStyle {
113 get { return this.grow_style; }
115 if (!Enum.IsDefined (typeof(TableLayoutPanelGrowStyle), value))
116 throw new ArgumentException();
118 if (grow_style != value) {
121 panel.PerformLayout (panel, "GrowStyle");
126 public override LayoutEngine LayoutEngine {
129 return panel.LayoutEngine;
130 return base.LayoutEngine;
135 public int RowCount {
136 get { return this.row_count; }
139 throw new ArgumentOutOfRangeException ();
141 if (row_count != value) {
145 panel.PerformLayout ();
150 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
151 public TableLayoutRowStyleCollection RowStyles {
152 get { return row_styles; }
156 #region Public Methods
158 public TableLayoutPanelCellPosition GetCellPosition (Object control)
161 throw new ArgumentNullException ();
166 if (!columns.TryGetValue (control, out column))
168 if (!rows.TryGetValue (control, out row))
171 return new TableLayoutPanelCellPosition (column, row);
175 public int GetColumn (Object control)
178 throw new ArgumentNullException ();
182 if (columns.TryGetValue (control, out retval))
188 public int GetColumnSpan (Object control)
191 throw new ArgumentNullException ();
195 if (column_spans.TryGetValue (control, out retval))
202 public int GetRow (Object control)
205 throw new ArgumentNullException ();
209 if (rows.TryGetValue (control, out retval))
215 public int GetRowSpan (Object control)
218 throw new ArgumentNullException ();
222 if (row_spans.TryGetValue (control, out retval))
229 public void SetCellPosition (Object control, TableLayoutPanelCellPosition cellPosition)
232 throw new ArgumentNullException ();
234 columns[control] = cellPosition.Column;
235 rows[control] = cellPosition.Row;
238 panel.PerformLayout ();
241 public void SetColumn (Object control, int column)
244 throw new ArgumentNullException ();
246 throw new ArgumentException ();
248 columns[control] = column;
251 panel.PerformLayout ();
254 public void SetColumnSpan (Object control, int value)
257 throw new ArgumentNullException ();
259 throw new ArgumentException ();
261 column_spans[control] = value;
264 panel.PerformLayout ();
267 public void SetRow (Object control, int row)
270 throw new ArgumentNullException ();
272 throw new ArgumentException ();
277 panel.PerformLayout ();
280 public void SetRowSpan (Object control, int value)
283 throw new ArgumentNullException ();
285 throw new ArgumentException ();
287 row_spans[control] = value;
290 panel.PerformLayout ();
294 #region Internal Methods
295 internal List<ControlInfo> GetControls ()
297 List<ControlInfo> list = new List<ControlInfo>();
298 foreach (KeyValuePair <object, int> control in columns) {
299 ControlInfo info = new ControlInfo();
300 info.Control = control.Key;
301 info.Col = GetColumn(control.Key);
302 info.ColSpan = GetColumnSpan (control.Key);
303 info.Row = GetRow (control.Key);
304 info.RowSpan = GetRowSpan (control.Key);
312 #region ISerializable Members
313 void ISerializable.GetObjectData (SerializationInfo si, StreamingContext context)
315 TableLayoutSettingsTypeConverter conv = new TableLayoutSettingsTypeConverter ();
316 string text = conv.ConvertToInvariantString (this);
317 si.AddValue ("SerializedString", text);
321 internal class StyleConverter : TypeConverter
323 public override object ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
325 if ((value == null) || !(value is String))
326 return base.ConvertFrom (context, culture, value);
328 return Enum.Parse (typeof (StyleConverter), (string)value, true);
331 public override object ConvertTo (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
333 if ((value == null) || !(value is StyleConverter) || (destinationType != typeof (string)))
334 return base.ConvertTo (context, culture, value, destinationType);
336 return ((StyleConverter)value).ToString ();
341 internal struct ControlInfo
343 public object Control;