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.Ask)]
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 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
93 public TableLayoutColumnStyleCollection ColumnStyles {
94 get { return settings.ColumnStyles; }
98 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
99 new public TableLayoutControlCollection Controls {
100 get { return (TableLayoutControlCollection) base.Controls; }
103 [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
104 public TableLayoutPanelGrowStyle GrowStyle {
105 get { return settings.GrowStyle; }
106 set { settings.GrowStyle = value; }
109 public override System.Windows.Forms.Layout.LayoutEngine LayoutEngine {
110 get { return TableLayoutPanel.layout_engine; }
114 [EditorBrowsable (EditorBrowsableState.Never)]
115 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
116 public TableLayoutSettings LayoutSettings {
117 get { return this.settings; }
118 set { throw new NotSupportedException (); }
123 public int RowCount {
124 get { return settings.RowCount; }
125 set { this.settings.RowCount = value; }
129 [DisplayName ("Rows")]
130 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
131 public TableLayoutRowStyleCollection RowStyles {
132 get { return settings.RowStyles; }
136 #region Public Methods
138 [DisplayName ("Cell")]
139 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
140 public TableLayoutPanelCellPosition GetCellPosition (Control control)
142 return settings.GetCellPosition (control);
145 [DisplayName ("Column")]
147 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
148 public int GetColumn (Control control)
150 return settings.GetColumn (control);
153 [DisplayName ("ColumnSpan")]
155 public int GetColumnSpan (Control control)
157 return settings.GetColumnSpan (control);
161 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
162 public int[] GetColumnWidths ()
164 return this.column_widths;
167 public Control GetControlFromPosition (int column, int row)
169 if (column < 0 || row < 0)
170 throw new ArgumentException ();
172 TableLayoutPanelCellPosition pos = new TableLayoutPanelCellPosition (column, row);
174 foreach (Control c in this.Controls)
175 if (settings.GetCellPosition (c) == pos)
181 public TableLayoutPanelCellPosition GetPositionFromControl (Control control)
183 for (int x = 0; x < this.actual_positions.GetLength (0); x++)
184 for (int y = 0; y < this.actual_positions.GetLength (1); y++)
185 if (this.actual_positions[x, y] == control)
186 return new TableLayoutPanelCellPosition (x, y);
188 return new TableLayoutPanelCellPosition (-1, -1);
191 [DisplayName ("Row")]
192 [DefaultValue ("-1")]
193 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
194 public int GetRow (Control control)
196 return settings.GetRow (control);
200 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
201 public int[] GetRowHeights ()
203 return this.row_heights;
206 [DisplayName ("RowSpan")]
208 public int GetRowSpan (Control control)
210 return settings.GetRowSpan (control);
213 public void SetCellPosition (Control control, TableLayoutPanelCellPosition position)
215 settings.SetCellPosition (control, position);
216 this.PerformLayout ();
219 public void SetColumn (Control control, int column)
221 settings.SetColumn (control, column);
222 this.PerformLayout ();
225 public void SetColumnSpan (Control control, int value)
227 settings.SetColumnSpan (control, value);
228 this.PerformLayout ();
231 public void SetRow (Control control, int row)
233 settings.SetRow (control, row);
234 this.PerformLayout ();
237 public void SetRowSpan (Control control, int value)
239 settings.SetRowSpan (control, value);
240 this.PerformLayout ();
244 #region Protected Methods
245 [EditorBrowsable (EditorBrowsableState.Never)]
246 protected override ControlCollection CreateControlsInstance ()
248 return new TableLayoutControlCollection (this);
251 protected virtual void OnCellPaint (TableLayoutCellPaintEventArgs e)
253 TableLayoutCellPaintEventHandler eh = (TableLayoutCellPaintEventHandler)(Events [CellPaintEvent]);
258 [EditorBrowsable (EditorBrowsableState.Never)]
259 protected override void OnLayout (LayoutEventArgs levent)
261 base.OnLayout (levent);
264 protected override void OnPaintBackground (PaintEventArgs e)
266 base.OnPaintBackground (e);
270 #region Public Events
271 static object CellPaintEvent = new object ();
273 public event TableLayoutCellPaintEventHandler CellPaint {
274 add { Events.AddHandler (CellPaintEvent, value); }
275 remove { Events.RemoveHandler (CellPaintEvent, value); }
279 #region IExtenderProvider
280 bool IExtenderProvider.CanExtend (object extendee)
282 if (extendee is Control)
283 if ((extendee as Control).Parent == this)