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;
35 namespace System.Windows.Forms
38 [ClassInterface (ClassInterfaceType.AutoDispatch)]
39 [ProvideProperty ("CellPosition", typeof (Control))]
40 [ProvideProperty ("Column", typeof (Control))]
41 [ProvideProperty ("ColumnSpan", typeof (Control))]
42 [ProvideProperty ("Row", typeof (Control))]
43 [ProvideProperty ("RowSpan", typeof (Control))]
44 [DefaultProperty ("ColumnCount")]
45 [Docking (DockingBehavior.Never)]
46 public class TableLayoutPanel : Panel, IExtenderProvider
48 private TableLayoutSettings settings;
49 private static TableLayout layout_engine = new TableLayout ();
50 private TableLayoutPanelCellBorderStyle cell_border_style;
52 // This is the row/column the Control actually got placed
53 internal Control[,] actual_positions;
55 // Widths and heights of each column/row
56 internal int[] column_widths;
57 internal int[] row_heights;
59 #region Public Constructor
60 public TableLayoutPanel ()
62 settings = new TableLayoutSettings(this);
63 cell_border_style = TableLayoutPanelCellBorderStyle.None;
67 #region Public Properties
70 [EditorBrowsable (EditorBrowsableState.Never)]
71 new public BorderStyle BorderStyle {
72 get { return base.BorderStyle; }
73 set { base.BorderStyle = value; }
77 [DefaultValue (TableLayoutPanelCellBorderStyle.None)]
78 public TableLayoutPanelCellBorderStyle CellBorderStyle {
79 get { return this.cell_border_style; }
80 set { this.cell_border_style = value; }
85 public int ColumnCount {
86 get { return settings.ColumnCount; }
87 set { settings.ColumnCount = value; }
91 [DisplayName ("Columns")]
92 [MergableProperty (false)]
93 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
94 public TableLayoutColumnStyleCollection ColumnStyles {
95 get { return settings.ColumnStyles; }
99 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
100 new public TableLayoutControlCollection Controls {
101 get { return (TableLayoutControlCollection) base.Controls; }
104 [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
105 public TableLayoutPanelGrowStyle GrowStyle {
106 get { return settings.GrowStyle; }
107 set { settings.GrowStyle = value; }
110 public override System.Windows.Forms.Layout.LayoutEngine LayoutEngine {
111 get { return TableLayoutPanel.layout_engine; }
115 [EditorBrowsable (EditorBrowsableState.Never)]
116 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
117 public TableLayoutSettings LayoutSettings {
118 get { return this.settings; }
119 set { throw new NotSupportedException (); }
124 public int RowCount {
125 get { return settings.RowCount; }
126 set { this.settings.RowCount = value; }
130 [DisplayName ("Rows")]
131 [MergableProperty (false)]
132 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
133 public TableLayoutRowStyleCollection RowStyles {
134 get { return settings.RowStyles; }
138 #region Public Methods
140 [DisplayName ("Cell")]
141 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
142 public TableLayoutPanelCellPosition GetCellPosition (Control control)
144 return settings.GetCellPosition (control);
147 [DisplayName ("Column")]
149 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
150 public int GetColumn (Control control)
152 return settings.GetColumn (control);
155 [DisplayName ("ColumnSpan")]
157 public int GetColumnSpan (Control control)
159 return settings.GetColumnSpan (control);
163 [EditorBrowsable (EditorBrowsableState.Never)]
164 public int[] GetColumnWidths ()
166 return this.column_widths;
169 public Control GetControlFromPosition (int column, int row)
171 if (column < 0 || row < 0)
172 throw new ArgumentException ();
174 TableLayoutPanelCellPosition pos = new TableLayoutPanelCellPosition (column, row);
176 foreach (Control c in this.Controls)
177 if (settings.GetCellPosition (c) == pos)
183 public TableLayoutPanelCellPosition GetPositionFromControl (Control control)
185 for (int x = 0; x < this.actual_positions.GetLength (0); x++)
186 for (int y = 0; y < this.actual_positions.GetLength (1); y++)
187 if (this.actual_positions[x, y] == control)
188 return new TableLayoutPanelCellPosition (x, y);
190 return new TableLayoutPanelCellPosition (-1, -1);
193 [DisplayName ("Row")]
194 [DefaultValue ("-1")]
195 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
196 public int GetRow (Control control)
198 return settings.GetRow (control);
202 [EditorBrowsable (EditorBrowsableState.Never)]
203 public int[] GetRowHeights ()
205 return this.row_heights;
208 [DisplayName ("RowSpan")]
210 public int GetRowSpan (Control control)
212 return settings.GetRowSpan (control);
215 public void SetCellPosition (Control control, TableLayoutPanelCellPosition position)
217 settings.SetCellPosition (control, position);
218 this.PerformLayout ();
221 public void SetColumn (Control control, int column)
223 settings.SetColumn (control, column);
224 this.PerformLayout ();
227 public void SetColumnSpan (Control control, int value)
229 settings.SetColumnSpan (control, value);
230 this.PerformLayout ();
233 public void SetRow (Control control, int row)
235 settings.SetRow (control, row);
236 this.PerformLayout ();
239 public void SetRowSpan (Control control, int value)
241 settings.SetRowSpan (control, value);
242 this.PerformLayout ();
246 #region Protected Methods
247 [EditorBrowsable (EditorBrowsableState.Advanced)]
248 protected override ControlCollection CreateControlsInstance ()
250 return new TableLayoutControlCollection (this);
253 protected virtual void OnCellPaint (TableLayoutCellPaintEventArgs e)
255 TableLayoutCellPaintEventHandler eh = (TableLayoutCellPaintEventHandler)(Events [CellPaintEvent]);
260 [EditorBrowsable (EditorBrowsableState.Advanced)]
261 protected override void OnLayout (LayoutEventArgs levent)
263 base.OnLayout (levent);
266 protected override void OnPaintBackground (PaintEventArgs e)
268 base.OnPaintBackground (e);
272 #region Public Events
273 static object CellPaintEvent = new object ();
275 public event TableLayoutCellPaintEventHandler CellPaint {
276 add { Events.AddHandler (CellPaintEvent, value); }
277 remove { Events.RemoveHandler (CellPaintEvent, value); }
281 #region IExtenderProvider
282 bool IExtenderProvider.CanExtend (object extendee)
284 if (extendee is Control)
285 if ((extendee as Control).Parent == this)