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();
98 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) {
119 panel
.PerformLayout (panel
, "GrowStyle");
124 public override LayoutEngine LayoutEngine
{
125 get { return this.panel.LayoutEngine; }
129 public int RowCount
{
130 get { return this.row_count; }
133 throw new ArgumentOutOfRangeException ();
135 if (row_count
!= value) {
137 panel
.PerformLayout (panel
, "RowCount");
142 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
143 public TableLayoutRowStyleCollection RowStyles
{
144 get { return row_styles; }
148 #region Public Methods
150 public TableLayoutPanelCellPosition
GetCellPosition (Object control
)
153 throw new ArgumentNullException ();
158 if (!columns
.TryGetValue (control
, out column
))
160 if (!rows
.TryGetValue (control
, out row
))
163 return new TableLayoutPanelCellPosition (column
, row
);
167 public int GetColumn (Object control
)
170 throw new ArgumentNullException ();
174 if (columns
.TryGetValue (control
, out retval
))
180 public int GetColumnSpan (Object control
)
183 throw new ArgumentNullException ();
187 if (column_spans
.TryGetValue (control
, out retval
))
194 public int GetRow (Object control
)
197 throw new ArgumentNullException ();
201 if (rows
.TryGetValue (control
, out retval
))
207 public int GetRowSpan (Object control
)
210 throw new ArgumentNullException ();
214 if (row_spans
.TryGetValue (control
, out retval
))
221 public void SetCellPosition (Object control
, TableLayoutPanelCellPosition cellPosition
)
224 throw new ArgumentNullException ();
226 columns
[control
] = cellPosition
.Column
;
227 rows
[control
] = cellPosition
.Row
;
228 panel
.PerformLayout ();
231 public void SetColumn (Object control
, int column
)
234 throw new ArgumentNullException ();
236 throw new ArgumentException ();
238 columns
[control
] = column
;
239 panel
.PerformLayout ();
242 public void SetColumnSpan (Object control
, int value)
245 throw new ArgumentNullException ();
247 throw new ArgumentException ();
249 column_spans
[control
] = value;
250 panel
.PerformLayout ();
253 public void SetRow (Object control
, int row
)
256 throw new ArgumentNullException ();
258 throw new ArgumentException ();
261 panel
.PerformLayout ();
264 public void SetRowSpan (Object control
, int value)
267 throw new ArgumentNullException ();
269 throw new ArgumentException ();
271 row_spans
[control
] = value;
272 panel
.PerformLayout ();
276 #region Internal Methods
277 internal List
<ControlInfo
> GetControls ()
279 List
<ControlInfo
> list
= new List
<ControlInfo
>();
280 foreach (KeyValuePair
<object, int> control
in columns
) {
281 ControlInfo info
= new ControlInfo();
282 info
.Control
= control
.Key
;
283 info
.Col
= GetColumn(control
.Key
);
284 info
.ColSpan
= GetColumnSpan (control
.Key
);
285 info
.Row
= GetRow (control
.Key
);
286 info
.RowSpan
= GetRowSpan (control
.Key
);
294 #region ISerializable Members
295 void ISerializable
.GetObjectData (SerializationInfo si
, StreamingContext context
)
297 TableLayoutSettingsTypeConverter conv
= new TableLayoutSettingsTypeConverter ();
298 string text
= conv
.ConvertToInvariantString (this);
299 si
.AddValue ("SerializedString", text
);
303 internal class StyleConverter
: TypeConverter
305 public override object ConvertFrom (ITypeDescriptorContext context
, System
.Globalization
.CultureInfo culture
, object value)
307 if ((value == null) || !(value is String
))
308 return base.ConvertFrom (context
, culture
, value);
310 return Enum
.Parse (typeof (StyleConverter
), (string)value, true);
313 public override object ConvertTo (ITypeDescriptorContext context
, System
.Globalization
.CultureInfo culture
, object value, Type destinationType
)
315 if ((value == null) || !(value is StyleConverter
) || (destinationType
!= typeof (string)))
316 return base.ConvertTo (context
, culture
, value, destinationType
);
318 return ((StyleConverter
)value).ToString ();
323 internal struct ControlInfo
325 public object Control
;