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.Runtime.InteropServices;
33 using System.Windows.Forms.Layout;
34 using System.ComponentModel.Design.Serialization;
36 namespace System.Windows.Forms
39 [ClassInterface (ClassInterfaceType.AutoDispatch)]
40 [ProvideProperty ("CellPosition", typeof (Control))]
41 [ProvideProperty ("Column", typeof (Control))]
42 [ProvideProperty ("ColumnSpan", typeof (Control))]
43 [ProvideProperty ("Row", typeof (Control))]
44 [ProvideProperty ("RowSpan", typeof (Control))]
45 [DefaultProperty ("ColumnCount")]
46 [Docking (DockingBehavior.Never)]
47 [Designer ("System.Windows.Forms.Design.TableLayoutPanelDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
48 [DesignerSerializer ("System.Windows.Forms.Design.TableLayoutPanelCodeDomSerializer, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
49 public class TableLayoutPanel : Panel, IExtenderProvider
51 private TableLayoutSettings settings;
52 private static TableLayout layout_engine = new TableLayout ();
53 private TableLayoutPanelCellBorderStyle cell_border_style;
55 // This is the row/column the Control actually got placed
56 internal Control[,] actual_positions;
58 // Widths and heights of each column/row
59 internal int[] column_widths;
60 internal int[] row_heights;
62 #region Public Constructor
63 public TableLayoutPanel ()
65 settings = new TableLayoutSettings(this);
66 cell_border_style = TableLayoutPanelCellBorderStyle.None;
70 #region Public Properties
73 [EditorBrowsable (EditorBrowsableState.Never)]
74 new public BorderStyle BorderStyle {
75 get { return base.BorderStyle; }
76 set { base.BorderStyle = value; }
80 [DefaultValue (TableLayoutPanelCellBorderStyle.None)]
81 public TableLayoutPanelCellBorderStyle CellBorderStyle {
82 get { return this.cell_border_style; }
83 set { this.cell_border_style = value; }
88 public int ColumnCount {
89 get { return settings.ColumnCount; }
90 set { settings.ColumnCount = value; }
94 [DisplayName ("Columns")]
95 [MergableProperty (false)]
96 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
97 public TableLayoutColumnStyleCollection ColumnStyles {
98 get { return settings.ColumnStyles; }
102 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
103 new public TableLayoutControlCollection Controls {
104 get { return (TableLayoutControlCollection) base.Controls; }
107 [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
108 public TableLayoutPanelGrowStyle GrowStyle {
109 get { return settings.GrowStyle; }
110 set { settings.GrowStyle = value; }
113 public override System.Windows.Forms.Layout.LayoutEngine LayoutEngine {
114 get { return TableLayoutPanel.layout_engine; }
118 [EditorBrowsable (EditorBrowsableState.Never)]
119 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
120 public TableLayoutSettings LayoutSettings {
121 get { return this.settings; }
123 if (value.isSerialized) {
124 this.settings = value;
125 value.isSerialized = false;
127 throw new NotSupportedException ("LayoutSettings value cannot be set directly.");
133 public int RowCount {
134 get { return settings.RowCount; }
135 set { this.settings.RowCount = value; }
139 [DisplayName ("Rows")]
140 [MergableProperty (false)]
141 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
142 public TableLayoutRowStyleCollection RowStyles {
143 get { return settings.RowStyles; }
147 #region Public Methods
149 [DisplayName ("Cell")]
150 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
151 public TableLayoutPanelCellPosition GetCellPosition (Control control)
153 return settings.GetCellPosition (control);
156 [DisplayName ("Column")]
158 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
159 public int GetColumn (Control control)
161 return settings.GetColumn (control);
164 [DisplayName ("ColumnSpan")]
166 public int GetColumnSpan (Control control)
168 return settings.GetColumnSpan (control);
172 [EditorBrowsable (EditorBrowsableState.Never)]
173 public int[] GetColumnWidths ()
175 return this.column_widths;
178 public Control GetControlFromPosition (int column, int row)
180 if (column < 0 || row < 0)
181 throw new ArgumentException ();
183 TableLayoutPanelCellPosition pos = new TableLayoutPanelCellPosition (column, row);
185 foreach (Control c in this.Controls)
186 if (settings.GetCellPosition (c) == pos)
192 public TableLayoutPanelCellPosition GetPositionFromControl (Control control)
194 for (int x = 0; x < this.actual_positions.GetLength (0); x++)
195 for (int y = 0; y < this.actual_positions.GetLength (1); y++)
196 if (this.actual_positions[x, y] == control)
197 return new TableLayoutPanelCellPosition (x, y);
199 return new TableLayoutPanelCellPosition (-1, -1);
202 [DisplayName ("Row")]
203 [DefaultValue ("-1")]
204 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
205 public int GetRow (Control control)
207 return settings.GetRow (control);
211 [EditorBrowsable (EditorBrowsableState.Never)]
212 public int[] GetRowHeights ()
214 return this.row_heights;
217 [DisplayName ("RowSpan")]
219 public int GetRowSpan (Control control)
221 return settings.GetRowSpan (control);
224 public void SetCellPosition (Control control, TableLayoutPanelCellPosition position)
226 settings.SetCellPosition (control, position);
227 this.PerformLayout ();
230 public void SetColumn (Control control, int column)
232 settings.SetColumn (control, column);
233 this.PerformLayout ();
236 public void SetColumnSpan (Control control, int value)
238 settings.SetColumnSpan (control, value);
239 this.PerformLayout ();
242 public void SetRow (Control control, int row)
244 settings.SetRow (control, row);
245 this.PerformLayout ();
248 public void SetRowSpan (Control control, int value)
250 settings.SetRowSpan (control, value);
251 this.PerformLayout ();
255 #region Protected Methods
256 [EditorBrowsable (EditorBrowsableState.Advanced)]
257 protected override ControlCollection CreateControlsInstance ()
259 return new TableLayoutControlCollection (this);
262 protected virtual void OnCellPaint (TableLayoutCellPaintEventArgs e)
264 TableLayoutCellPaintEventHandler eh = (TableLayoutCellPaintEventHandler)(Events [CellPaintEvent]);
269 [EditorBrowsable (EditorBrowsableState.Advanced)]
270 protected override void OnLayout (LayoutEventArgs levent)
272 base.OnLayout (levent);
275 protected override void OnPaintBackground (PaintEventArgs e)
277 base.OnPaintBackground (e);
281 #region Public Events
282 static object CellPaintEvent = new object ();
284 public event TableLayoutCellPaintEventHandler CellPaint {
285 add { Events.AddHandler (CellPaintEvent, value); }
286 remove { Events.RemoveHandler (CellPaintEvent, value); }
290 #region IExtenderProvider
291 bool IExtenderProvider.CanExtend (object extendee)
293 if (extendee is Control)
294 if ((extendee as Control).Parent == this)