In System.Windows.Forms:
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TableLayoutSettings.cs
blob3f1fbb14d0ca0d5e7720781f626a8dffdaf31cd9
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.
23 // Copyright (c) 2006 Jonathan Pobst
25 // Authors:
26 // Jonathan Pobst (monkey@jpobst.com)
29 #if NET_2_0
30 using System;
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
38 [Serializable]
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;
61 this.row_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> ();
66 this.panel = panel;
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;
88 #endregion
90 #region Public Properties
91 [DefaultValue (0)]
92 public int ColumnCount {
93 get { return this.column_count; }
94 set {
95 if (value < 0)
96 throw new ArgumentOutOfRangeException();
98 if (column_count != value) {
99 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; }
113 set {
114 if (!Enum.IsDefined (typeof(TableLayoutPanelGrowStyle), value))
115 throw new ArgumentException();
117 if (grow_style != value) {
118 grow_style = value;
119 panel.PerformLayout (panel, "GrowStyle");
124 public override LayoutEngine LayoutEngine {
125 get { return this.panel.LayoutEngine; }
128 [DefaultValue (0)]
129 public int RowCount {
130 get { return this.row_count; }
131 set {
132 if (value < 0)
133 throw new ArgumentOutOfRangeException ();
135 if (row_count != value) {
136 row_count = value;
137 panel.PerformLayout (panel, "RowCount");
142 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
143 public TableLayoutRowStyleCollection RowStyles {
144 get { return row_styles; }
146 #endregion
148 #region Public Methods
149 [DefaultValue (-1)]
150 public TableLayoutPanelCellPosition GetCellPosition (Object control)
152 if (control == null)
153 throw new ArgumentNullException ();
155 int column;
156 int row;
158 if (!columns.TryGetValue (control, out column))
159 column = -1;
160 if (!rows.TryGetValue (control, out row))
161 row = -1;
163 return new TableLayoutPanelCellPosition (column, row);
166 [DefaultValue (-1)]
167 public int GetColumn (Object control)
169 if (control == null)
170 throw new ArgumentNullException ();
172 int retval;
174 if (columns.TryGetValue (control, out retval))
175 return retval;
177 return -1;
180 public int GetColumnSpan (Object control)
182 if (control == null)
183 throw new ArgumentNullException ();
185 int retval;
187 if (column_spans.TryGetValue (control, out retval))
188 return retval;
190 return 1;
193 [DefaultValue (-1)]
194 public int GetRow (Object control)
196 if (control == null)
197 throw new ArgumentNullException ();
199 int retval;
201 if (rows.TryGetValue (control, out retval))
202 return retval;
204 return -1;
207 public int GetRowSpan (Object control)
209 if (control == null)
210 throw new ArgumentNullException ();
212 int retval;
214 if (row_spans.TryGetValue (control, out retval))
215 return retval;
217 return 1;
220 [DefaultValue (-1)]
221 public void SetCellPosition (Object control, TableLayoutPanelCellPosition cellPosition)
223 if (control == null)
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)
233 if (control == null)
234 throw new ArgumentNullException ();
235 if (column < -1)
236 throw new ArgumentException ();
238 columns[control] = column;
239 panel.PerformLayout ();
242 public void SetColumnSpan (Object control, int value)
244 if (control == null)
245 throw new ArgumentNullException ();
246 if (value < -1)
247 throw new ArgumentException ();
249 column_spans[control] = value;
250 panel.PerformLayout ();
253 public void SetRow (Object control, int row)
255 if (control == null)
256 throw new ArgumentNullException ();
257 if (row < -1)
258 throw new ArgumentException ();
260 rows[control] = row;
261 panel.PerformLayout ();
264 public void SetRowSpan (Object control, int value)
266 if (control == null)
267 throw new ArgumentNullException ();
268 if (value < -1)
269 throw new ArgumentException ();
271 row_spans[control] = value;
272 panel.PerformLayout ();
274 #endregion
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);
287 list.Add (info);
289 return list;
292 #endregion
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);
301 #endregion
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;
326 public int Row;
327 public int RowSpan;
328 public int Col;
329 public int ColSpan;
332 #endif