| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- //
- // TableLayoutSettings.cs
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (c) 2006 Jonathan Pobst
- //
- // Authors:
- // Jonathan Pobst ([email protected])
- //
- #if NET_2_0
- using System;
- using System.ComponentModel;
- using System.Collections.Generic;
- using System.Windows.Forms.Layout;
- using System.Runtime.Serialization;
- namespace System.Windows.Forms
- {
- [Serializable]
- [TypeConverter (typeof (TableLayoutSettingsTypeConverter))]
- public sealed class TableLayoutSettings : LayoutSettings, ISerializable
- {
- private TableLayoutColumnStyleCollection column_styles;
- private TableLayoutRowStyleCollection row_styles;
- private TableLayoutPanelGrowStyle grow_style;
- private int column_count;
- private int row_count;
- private Dictionary<Object, int> columns;
- private Dictionary<Object, int> column_spans;
- private Dictionary<Object, int> rows;
- private Dictionary<Object, int> row_spans;
- private TableLayoutPanel panel;
- internal bool isSerialized;
- #region Internal Constructor
- internal TableLayoutSettings (TableLayoutPanel panel)
- {
- this.column_styles = new TableLayoutColumnStyleCollection (panel);
- this.row_styles = new TableLayoutRowStyleCollection (panel);
- this.grow_style = TableLayoutPanelGrowStyle.AddRows;
- this.column_count = 0;
- this.row_count = 0;
- this.columns = new Dictionary<object, int> ();
- this.column_spans = new Dictionary<object, int> ();
- this.rows = new Dictionary<object, int> ();
- this.row_spans = new Dictionary<object, int> ();
- this.panel = panel;
- }
- private TableLayoutSettings (SerializationInfo serializationInfo, StreamingContext context)
- {
- TypeConverter converter = TypeDescriptor.GetConverter (this);
- string text = serializationInfo.GetString ("SerializedString");
- if (!string.IsNullOrEmpty (text) && (converter != null)) {
- TableLayoutSettings settings = converter.ConvertFromInvariantString (text) as TableLayoutSettings;
- this.column_styles = settings.column_styles;
- this.row_styles = settings.row_styles;
- this.grow_style = settings.grow_style;
- this.column_count = settings.column_count;
- this.row_count = settings.row_count;
- this.columns = settings.columns;
- this.column_spans = settings.column_spans;
- this.rows = settings.rows;
- this.row_spans = settings.row_spans;
- this.panel = settings.panel;
- this.isSerialized = true;
- }
- }
- #endregion
- #region Public Properties
- [DefaultValue (0)]
- public int ColumnCount {
- get { return this.column_count; }
- set {
- if (value < 0)
- throw new ArgumentOutOfRangeException();
-
- column_count = value;
- }
- }
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public TableLayoutColumnStyleCollection ColumnStyles {
- get { return this.column_styles; }
- }
- [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
- public TableLayoutPanelGrowStyle GrowStyle {
- get { return this.grow_style; }
- set {
- if (!Enum.IsDefined (typeof(TableLayoutPanelGrowStyle), value))
- throw new ArgumentException();
-
- grow_style = value;
- }
- }
-
- public override LayoutEngine LayoutEngine {
- get { return this.panel.LayoutEngine; }
- }
-
- [DefaultValue (0)]
- public int RowCount {
- get { return this.row_count; }
- set {
- if (value < 0)
- throw new ArgumentOutOfRangeException ();
- row_count = value;
- }
- }
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public TableLayoutRowStyleCollection RowStyles {
- get { return row_styles; }
- }
- #endregion
- #region Public Methods
- [DefaultValue (-1)]
- public TableLayoutPanelCellPosition GetCellPosition (Object control)
- {
- if (control == null)
- throw new ArgumentNullException ();
- int column;
- int row;
- if (!columns.TryGetValue (control, out column))
- column = -1;
- if (!rows.TryGetValue (control, out row))
- row = -1;
- return new TableLayoutPanelCellPosition (column, row);
- }
- [DefaultValue (-1)]
- public int GetColumn (Object control)
- {
- if (control == null)
- throw new ArgumentNullException ();
- int retval;
- if (columns.TryGetValue (control, out retval))
- return retval;
- return -1;
- }
- public int GetColumnSpan (Object control)
- {
- if (control == null)
- throw new ArgumentNullException ();
- int retval;
- if (column_spans.TryGetValue (control, out retval))
- return retval;
- return 1;
- }
- [DefaultValue (-1)]
- public int GetRow (Object control)
- {
- if (control == null)
- throw new ArgumentNullException ();
- int retval;
- if (rows.TryGetValue (control, out retval))
- return retval;
- return -1;
- }
- public int GetRowSpan (Object control)
- {
- if (control == null)
- throw new ArgumentNullException ();
- int retval;
- if (row_spans.TryGetValue (control, out retval))
- return retval;
- return 1;
- }
- [DefaultValue (-1)]
- public void SetCellPosition (Object control, TableLayoutPanelCellPosition cellPosition)
- {
- if (control == null)
- throw new ArgumentNullException ();
- columns[control] = cellPosition.Column;
- rows[control] = cellPosition.Row;
- }
- public void SetColumn (Object control, int column)
- {
- if (control == null)
- throw new ArgumentNullException ();
- if (column < -1)
- throw new ArgumentException ();
-
- columns[control] = column;
- }
- public void SetColumnSpan (Object control, int value)
- {
- if (control == null)
- throw new ArgumentNullException ();
- if (value < -1)
- throw new ArgumentException ();
- column_spans[control] = value;
- }
- public void SetRow (Object control, int row)
- {
- if (control == null)
- throw new ArgumentNullException ();
- if (row < -1)
- throw new ArgumentException ();
- rows[control] = row;
- }
- public void SetRowSpan (Object control, int value)
- {
- if (control == null)
- throw new ArgumentNullException ();
- if (value < -1)
- throw new ArgumentException ();
- row_spans[control] = value;
- }
- #endregion
- #region Internal Methods
- internal List<ControlInfo> GetControls ()
- {
- List<ControlInfo> list = new List<ControlInfo>();
- foreach (KeyValuePair <object, int> control in columns) {
- ControlInfo info = new ControlInfo();
- info.Control = control.Key;
- info.Col = GetColumn(control);
- info.ColSpan = GetColumnSpan (control);
- info.Row = GetRow (control);
- info.RowSpan = GetRowSpan (control);
- list.Add (info);
- }
- return list;
- }
- #endregion
- #region ISerializable Members
- void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
- {
- TableLayoutSettingsTypeConverter conv = new TableLayoutSettingsTypeConverter ();
- string text = conv.ConvertToInvariantString (this);
- info.AddValue ("SerializedString", text);
- }
- #endregion
- }
- internal struct ControlInfo
- {
- public object Control;
- public int Row;
- public int RowSpan;
- public int Col;
- public int ColSpan;
- }
- }
- #endif
|