* Application.cs: fix compilation errors when debug is enabled.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TableLayoutPanel.cs
1 //
2 // TableLayoutPanel.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.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28
29 #if NET_2_0
30 using System;
31 using System.ComponentModel;
32 using System.Runtime.InteropServices;
33 using System.Windows.Forms.Layout;
34
35 namespace System.Windows.Forms
36 {
37         [ComVisible (true)]
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
47         {
48                 private TableLayoutSettings settings;
49                 private static TableLayout layout_engine = new TableLayout ();
50                 private TableLayoutPanelCellBorderStyle cell_border_style;
51
52                 // This is the row/column the Control actually got placed
53                 internal Control[,] actual_positions;
54                 
55                 // Widths and heights of each column/row
56                 internal int[] column_widths;
57                 internal int[] row_heights;
58
59                 #region Public Constructor
60                 public TableLayoutPanel ()
61                 {
62                         settings = new TableLayoutSettings(this);
63                         cell_border_style = TableLayoutPanelCellBorderStyle.None;
64                 }
65                 #endregion
66
67                 #region Public Properties
68                 [Localizable (true)]
69                 [Browsable (false)]
70                 [EditorBrowsable (EditorBrowsableState.Never)]
71                 new public BorderStyle BorderStyle {
72                         get { return base.BorderStyle; }
73                         set { base.BorderStyle = value; }
74                 }
75
76                 [Localizable (true)]
77                 [DefaultValue (TableLayoutPanelCellBorderStyle.None)]
78                 public TableLayoutPanelCellBorderStyle CellBorderStyle {
79                         get { return this.cell_border_style; }
80                         set { this.cell_border_style = value; }
81                 }
82
83                 [Localizable (true)]
84                 [DefaultValue (0)]
85                 public int ColumnCount {
86                         get { return settings.ColumnCount; }
87                         set { settings.ColumnCount = value; }
88                 }
89
90                 [Browsable (false)]
91                 [DisplayName ("Columns")]
92                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
93                 public TableLayoutColumnStyleCollection ColumnStyles {
94                         get { return settings.ColumnStyles; }
95                 }
96
97                 [Browsable (false)]
98                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
99                 new public TableLayoutControlCollection Controls {
100                         get { return (TableLayoutControlCollection) base.Controls; }
101                 }
102
103                 [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
104                 public TableLayoutPanelGrowStyle GrowStyle {
105                         get { return settings.GrowStyle; }
106                         set { settings.GrowStyle = value; }
107                 }
108
109                 public override System.Windows.Forms.Layout.LayoutEngine LayoutEngine {
110                         get { return TableLayoutPanel.layout_engine; }
111                 }
112
113                 [Browsable (false)]
114                 [EditorBrowsable (EditorBrowsableState.Never)]
115                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
116                 public TableLayoutSettings LayoutSettings {
117                         get { return this.settings; }
118                         set { throw new NotSupportedException (); }
119                 }
120
121                 [Localizable (true)]
122                 [DefaultValue (0)]
123                 public int RowCount {
124                         get { return settings.RowCount; }
125                         set { this.settings.RowCount = value; }
126                 }
127
128                 [Browsable (false)]
129                 [DisplayName ("Rows")]
130                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
131                 public TableLayoutRowStyleCollection RowStyles {
132                         get { return settings.RowStyles; }
133                 }
134                 #endregion
135
136                 #region Public Methods
137                 [DefaultValue (-1)]
138                 [DisplayName ("Cell")]
139                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
140                 public TableLayoutPanelCellPosition GetCellPosition (Control control)
141                 {
142                         return settings.GetCellPosition (control);
143                 }
144
145                 [DisplayName ("Column")]
146                 [DefaultValue (-1)]
147                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
148                 public int GetColumn (Control control)
149                 {
150                         return settings.GetColumn (control);
151                 }
152
153                 [DisplayName ("ColumnSpan")]
154                 [DefaultValue (1)]
155                 public int GetColumnSpan (Control control)
156                 {
157                         return settings.GetColumnSpan (control);
158                 }
159
160                 [Browsable (false)]
161                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
162                 public int[] GetColumnWidths ()
163                 {
164                         return this.column_widths;
165                 }
166
167                 public Control GetControlFromPosition (int column, int row)
168                 {
169                         if (column < 0 || row < 0)
170                                 throw new ArgumentException ();
171
172                         TableLayoutPanelCellPosition pos = new TableLayoutPanelCellPosition (column, row);
173
174                         foreach (Control c in this.Controls)
175                                 if (settings.GetCellPosition (c) == pos)
176                                         return c;
177
178                         return null;
179                 }
180
181                 public TableLayoutPanelCellPosition GetPositionFromControl (Control control)
182                 {
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);
187
188                         return new TableLayoutPanelCellPosition (-1, -1);
189                 }
190
191                 [DisplayName ("Row")]
192                 [DefaultValue ("-1")]
193                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
194                 public int GetRow (Control control)
195                 {
196                         return settings.GetRow (control);
197                 }
198
199                 [Browsable (false)]
200                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
201                 public int[] GetRowHeights ()
202                 {
203                         return this.row_heights;
204                 }
205
206                 [DisplayName ("RowSpan")]
207                 [DefaultValue (1)]
208                 public int GetRowSpan (Control control)
209                 {
210                         return settings.GetRowSpan (control);
211                 }
212
213                 public void SetCellPosition (Control control, TableLayoutPanelCellPosition position)
214                 {
215                         settings.SetCellPosition (control, position);
216                         this.PerformLayout ();
217                 }
218
219                 public void SetColumn (Control control, int column)
220                 {
221                         settings.SetColumn (control, column);
222                         this.PerformLayout ();
223                 }
224
225                 public void SetColumnSpan (Control control, int value)
226                 {
227                         settings.SetColumnSpan (control, value);
228                         this.PerformLayout ();
229                 }
230
231                 public void SetRow (Control control, int row)
232                 {
233                         settings.SetRow (control, row);
234                         this.PerformLayout ();
235                 }
236
237                 public void SetRowSpan (Control control, int value)
238                 {
239                         settings.SetRowSpan (control, value);
240                         this.PerformLayout ();
241                 }
242                 #endregion
243
244                 #region Protected Methods
245                 [EditorBrowsable (EditorBrowsableState.Never)]
246                 protected override ControlCollection CreateControlsInstance ()
247                 {
248                         return new TableLayoutControlCollection (this);
249                 }
250
251                 protected virtual void OnCellPaint (TableLayoutCellPaintEventArgs e)
252                 {
253                         TableLayoutCellPaintEventHandler eh = (TableLayoutCellPaintEventHandler)(Events [CellPaintEvent]);
254                         if (eh != null)
255                                 eh (this, e);
256                 }
257
258                 [EditorBrowsable (EditorBrowsableState.Never)]
259                 protected override void OnLayout (LayoutEventArgs levent)
260                 {
261                         base.OnLayout (levent);
262                 }
263
264                 protected override void OnPaintBackground (PaintEventArgs e)
265                 {
266                         base.OnPaintBackground (e);
267                 }
268                 #endregion
269
270                 #region Public Events
271                 static object CellPaintEvent = new object ();
272
273                 public event TableLayoutCellPaintEventHandler CellPaint {
274                         add { Events.AddHandler (CellPaintEvent, value); }
275                         remove { Events.RemoveHandler (CellPaintEvent, value); }
276                 }
277                 #endregion
278                 
279                 #region IExtenderProvider
280                 bool IExtenderProvider.CanExtend (object extendee)
281                 {
282                         if (extendee is Control)
283                                 if ((extendee as Control).Parent == this)
284                                         return true;
285
286                         return false;
287                 }
288                 #endregion
289                 
290         }
291 }
292 #endif