- Implement 2.0 image key feature.
[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.Never)]
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                 [MergableProperty (false)]
93                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
94                 public TableLayoutColumnStyleCollection ColumnStyles {
95                         get { return settings.ColumnStyles; }
96                 }
97
98                 [Browsable (false)]
99                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
100                 new public TableLayoutControlCollection Controls {
101                         get { return (TableLayoutControlCollection) base.Controls; }
102                 }
103
104                 [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
105                 public TableLayoutPanelGrowStyle GrowStyle {
106                         get { return settings.GrowStyle; }
107                         set { settings.GrowStyle = value; }
108                 }
109
110                 public override System.Windows.Forms.Layout.LayoutEngine LayoutEngine {
111                         get { return TableLayoutPanel.layout_engine; }
112                 }
113
114                 [Browsable (false)]
115                 [EditorBrowsable (EditorBrowsableState.Never)]
116                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
117                 public TableLayoutSettings LayoutSettings {
118                         get { return this.settings; }
119                         set { throw new NotSupportedException (); }
120                 }
121
122                 [Localizable (true)]
123                 [DefaultValue (0)]
124                 public int RowCount {
125                         get { return settings.RowCount; }
126                         set { this.settings.RowCount = value; }
127                 }
128
129                 [Browsable (false)]
130                 [DisplayName ("Rows")]
131                 [MergableProperty (false)]
132                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
133                 public TableLayoutRowStyleCollection RowStyles {
134                         get { return settings.RowStyles; }
135                 }
136                 #endregion
137
138                 #region Public Methods
139                 [DefaultValue (-1)]
140                 [DisplayName ("Cell")]
141                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
142                 public TableLayoutPanelCellPosition GetCellPosition (Control control)
143                 {
144                         return settings.GetCellPosition (control);
145                 }
146
147                 [DisplayName ("Column")]
148                 [DefaultValue (-1)]
149                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
150                 public int GetColumn (Control control)
151                 {
152                         return settings.GetColumn (control);
153                 }
154
155                 [DisplayName ("ColumnSpan")]
156                 [DefaultValue (1)]
157                 public int GetColumnSpan (Control control)
158                 {
159                         return settings.GetColumnSpan (control);
160                 }
161
162                 [Browsable (false)]
163                 [EditorBrowsable (EditorBrowsableState.Never)]
164                 public int[] GetColumnWidths ()
165                 {
166                         return this.column_widths;
167                 }
168
169                 public Control GetControlFromPosition (int column, int row)
170                 {
171                         if (column < 0 || row < 0)
172                                 throw new ArgumentException ();
173
174                         TableLayoutPanelCellPosition pos = new TableLayoutPanelCellPosition (column, row);
175
176                         foreach (Control c in this.Controls)
177                                 if (settings.GetCellPosition (c) == pos)
178                                         return c;
179
180                         return null;
181                 }
182
183                 public TableLayoutPanelCellPosition GetPositionFromControl (Control control)
184                 {
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);
189
190                         return new TableLayoutPanelCellPosition (-1, -1);
191                 }
192
193                 [DisplayName ("Row")]
194                 [DefaultValue ("-1")]
195                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
196                 public int GetRow (Control control)
197                 {
198                         return settings.GetRow (control);
199                 }
200
201                 [Browsable (false)]
202                 [EditorBrowsable (EditorBrowsableState.Never)]
203                 public int[] GetRowHeights ()
204                 {
205                         return this.row_heights;
206                 }
207
208                 [DisplayName ("RowSpan")]
209                 [DefaultValue (1)]
210                 public int GetRowSpan (Control control)
211                 {
212                         return settings.GetRowSpan (control);
213                 }
214
215                 public void SetCellPosition (Control control, TableLayoutPanelCellPosition position)
216                 {
217                         settings.SetCellPosition (control, position);
218                         this.PerformLayout ();
219                 }
220
221                 public void SetColumn (Control control, int column)
222                 {
223                         settings.SetColumn (control, column);
224                         this.PerformLayout ();
225                 }
226
227                 public void SetColumnSpan (Control control, int value)
228                 {
229                         settings.SetColumnSpan (control, value);
230                         this.PerformLayout ();
231                 }
232
233                 public void SetRow (Control control, int row)
234                 {
235                         settings.SetRow (control, row);
236                         this.PerformLayout ();
237                 }
238
239                 public void SetRowSpan (Control control, int value)
240                 {
241                         settings.SetRowSpan (control, value);
242                         this.PerformLayout ();
243                 }
244                 #endregion
245
246                 #region Protected Methods
247                 [EditorBrowsable (EditorBrowsableState.Advanced)]
248                 protected override ControlCollection CreateControlsInstance ()
249                 {
250                         return new TableLayoutControlCollection (this);
251                 }
252
253                 protected virtual void OnCellPaint (TableLayoutCellPaintEventArgs e)
254                 {
255                         TableLayoutCellPaintEventHandler eh = (TableLayoutCellPaintEventHandler)(Events [CellPaintEvent]);
256                         if (eh != null)
257                                 eh (this, e);
258                 }
259
260                 [EditorBrowsable (EditorBrowsableState.Advanced)]
261                 protected override void OnLayout (LayoutEventArgs levent)
262                 {
263                         base.OnLayout (levent);
264                 }
265
266                 protected override void OnPaintBackground (PaintEventArgs e)
267                 {
268                         base.OnPaintBackground (e);
269                 }
270                 #endregion
271
272                 #region Public Events
273                 static object CellPaintEvent = new object ();
274
275                 public event TableLayoutCellPaintEventHandler CellPaint {
276                         add { Events.AddHandler (CellPaintEvent, value); }
277                         remove { Events.RemoveHandler (CellPaintEvent, value); }
278                 }
279                 #endregion
280                 
281                 #region IExtenderProvider
282                 bool IExtenderProvider.CanExtend (object extendee)
283                 {
284                         if (extendee is Control)
285                                 if ((extendee as Control).Parent == this)
286                                         return true;
287
288                         return false;
289                 }
290                 #endregion
291                 
292         }
293 }
294 #endif