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