Merge pull request #396 from directhex/master
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms.Layout / TableLayout.cs
1 //
2 // TableLayout.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
30 #undef TABLE_DEBUG
31
32 using System;
33 using System.Drawing;
34
35 namespace System.Windows.Forms.Layout
36 {
37         internal class TableLayout : LayoutEngine
38         {
39                 private static Control dummy_control = new Control ("Dummy");   // Used as a placeholder for row/col spans
40
41                 public TableLayout () : base ()
42                 {
43                 }
44                 
45                 public override void InitLayout (object child, BoundsSpecified specified)
46                 {
47                         base.InitLayout (child, specified);
48                 }
49
50                 // There are 3 steps to doing a table layout:
51                 // 1) Figure out which row/column each control goes into
52                 // 2) Figure out the sizes of each row/column
53                 // 3) Size and position each control
54                 public override bool Layout (object container, LayoutEventArgs args)
55                 {
56                         TableLayoutPanel panel = container as TableLayoutPanel;
57                         TableLayoutSettings settings = panel.LayoutSettings;
58                         
59 #if TABLE_DEBUG
60                         Console.WriteLine ("Beginning layout on panel: {0}, control count: {1}, col/row count: {2}x{3}", panel.Name, panel.Controls.Count, settings.ColumnCount, settings.RowCount);
61 #endif
62
63                         // STEP 1:
64                         // - Figure out which row/column each control goes into
65                         // - Store data in the TableLayoutPanel.actual_positions
66                         panel.actual_positions = CalculateControlPositions (panel, Math.Max (settings.ColumnCount, 1), Math.Max (settings.RowCount, 1));
67
68                         // STEP 2:
69                         // - Figure out the sizes of each row/column
70                         // - Store data in the TableLayoutPanel.widths/heights
71                         CalculateColumnRowSizes (panel, panel.actual_positions.GetLength (0), panel.actual_positions.GetLength (1));
72                         
73                         // STEP 3:
74                         // - Size and position each control
75                         LayoutControls(panel);
76
77 #if TABLE_DEBUG
78                         Console.WriteLine ("-- CalculatedPositions:");
79                         OutputControlGrid (panel.actual_positions, panel);
80
81                         Console.WriteLine ("Finished layout on panel: {0}", panel.Name);
82                         Console.WriteLine ();
83 #endif
84
85                         return false;
86                 }
87
88                 internal Control[,] CalculateControlPositions (TableLayoutPanel panel, int columns, int rows)
89                 {
90                         Control[,] grid = new Control[columns, rows];
91
92                         TableLayoutSettings settings = panel.LayoutSettings;
93
94                         // First place all controls that have an explicit col/row
95                         foreach (Control c in panel.Controls) {
96                                 int col = settings.GetColumn (c);
97                                 int row = settings.GetRow (c);
98                                 if (col >= 0 && row >= 0) {
99                                         if (col >= columns)
100                                                  return CalculateControlPositions (panel, col + 1, rows);
101                                         if (row >= rows)
102                                                  return CalculateControlPositions (panel, columns, row + 1);
103
104                                         if (grid[col, row] == null) {
105                                                 int col_span = Math.Min (settings.GetColumnSpan (c), columns);
106                                                 int row_span = Math.Min (settings.GetRowSpan (c), rows);
107
108                                                 if (col + col_span > columns) {
109                                                         if (row + 1 < rows) {
110                                                                 grid[col, row] = dummy_control;
111                                                                 row++;
112                                                                 col = 0;
113                                                         }
114                                                         else if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddColumns)
115                                                                 return CalculateControlPositions (panel, columns + 1, rows);
116                                                         else
117                                                                 throw new ArgumentException ();
118                                                 }
119
120                                                 if (row + row_span > rows) {
121                                                         if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddRows)
122                                                                 return CalculateControlPositions (panel, columns, rows + 1);
123                                                         else
124                                                                 throw new ArgumentException ();
125                                                 }
126
127                                                 grid[col, row] = c;
128
129                                                 for (int i = 1; i < col_span; i++)
130                                                         grid[col + i, row] = dummy_control;
131
132                                                 for (int i = 1; i < row_span; i++)
133                                                         grid[col, row + i] = dummy_control;
134                                         }
135                                 }
136                         }
137
138                         int x_pointer = 0;
139                         int y_pointer = 0;
140
141                         // Fill in gaps with controls that do not have an explicit col/row
142                         foreach (Control c in panel.Controls) {
143                                 int col = settings.GetColumn (c);
144                                 int row = settings.GetRow (c);
145
146                                 if ((col >= 0 && col < columns) && (row >= 0 && row < rows) && (grid[col, row] == c || grid[col, row] == dummy_control))
147                                         continue;
148
149                                 for (int y = y_pointer; y < rows; y++) {
150                                         y_pointer = y;
151                                         x_pointer = 0;
152
153                                         for (int x = x_pointer; x < columns; x++) {
154                                                 x_pointer = x;
155
156                                                 if (grid[x, y] == null) {
157                                                         int col_span = Math.Min (settings.GetColumnSpan (c), columns);
158                                                         int row_span = Math.Min (settings.GetRowSpan (c), rows);
159
160                                                         if (x + col_span > columns) {
161                                                                 if (y + 1 < rows)
162                                                                         break;
163                                                                 else if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddColumns)
164                                                                         return CalculateControlPositions (panel, columns + 1, rows);
165                                                                 else
166                                                                         throw new ArgumentException ();
167                                                         }
168
169                                                         if (y + row_span > rows) {
170                                                                 if (x + 1 < columns)
171                                                                         break;
172                                                                 else if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddRows)
173                                                                         return CalculateControlPositions (panel, columns, rows + 1);
174                                                                 else
175                                                                         throw new ArgumentException ();
176                                                         }
177
178                                                         grid[x, y] = c;
179
180                                                         for (int i = 1; i < col_span; i++)
181                                                                 grid[x + i, y] = dummy_control;
182
183                                                         for (int i = 1; i < row_span; i++)
184                                                                 grid[x, y + i] = dummy_control;
185
186                                                         // I know someone will kill me for using a goto, but 
187                                                         // sometimes they really are the easiest way...
188                                                         goto Found;
189                                                 } else {
190                                                         // MS adds the controls only to the first row if 
191                                                         // GrowStyle is AddColumns and RowCount is 0,
192                                                         // so interrupt the search for a free horizontal cell 
193                                                         // beyond the first one in the given vertical
194                                                         if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddColumns && 
195                                                             settings.RowCount == 0)
196                                                                 break;
197                                                 }
198                                         }
199                                 }
200
201                                 // MS adds rows instead of columns even when GrowStyle is AddColumns, 
202                                 // but RowCount is 0.
203                                 TableLayoutPanelGrowStyle adjustedGrowStyle = settings.GrowStyle;
204                                 if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddColumns) {
205                                         if (settings.RowCount == 0)
206                                                 adjustedGrowStyle = TableLayoutPanelGrowStyle.AddRows;
207                                 }
208
209                                 switch (adjustedGrowStyle) {
210                                         case TableLayoutPanelGrowStyle.AddColumns:
211                                                 return CalculateControlPositions (panel, columns + 1, rows);
212                                         case TableLayoutPanelGrowStyle.AddRows:
213                                         default:
214                                                 return CalculateControlPositions (panel, columns, rows + 1);
215                                         case TableLayoutPanelGrowStyle.FixedSize:
216                                                 throw new ArgumentException ();
217                                 }
218
219                         Found: ;
220                         }
221
222                         return grid;
223                 }
224
225                 private void CalculateColumnRowSizes (TableLayoutPanel panel, int columns, int rows)
226                 {
227                         TableLayoutSettings settings = panel.LayoutSettings;
228
229                         panel.column_widths = new int[panel.actual_positions.GetLength (0)];
230                         panel.row_heights = new int[panel.actual_positions.GetLength (1)];
231
232                         int border_width = TableLayoutPanel.GetCellBorderWidth (panel.CellBorderStyle);
233                                 
234                         Rectangle parentDisplayRectangle = panel.DisplayRectangle;
235
236                         TableLayoutColumnStyleCollection col_styles = new TableLayoutColumnStyleCollection (panel);
237                         
238                         foreach (ColumnStyle cs in settings.ColumnStyles)
239                                 col_styles.Add( new ColumnStyle(cs.SizeType, cs.Width));
240
241                         TableLayoutRowStyleCollection row_styles = new TableLayoutRowStyleCollection (panel);
242
243                         foreach (RowStyle rs in settings.RowStyles)
244                                 row_styles.Add (new RowStyle (rs.SizeType, rs.Height));
245                 
246                         // If we have more columns than columnstyles, temporarily add enough columnstyles
247                         if (columns > col_styles.Count)
248                         {
249                                 for (int i = col_styles.Count; i < columns; i++)
250                                         col_styles.Add(new ColumnStyle());                      
251                         }
252
253                         // Same for rows..
254                         if (rows > row_styles.Count) 
255                         {
256                                 for (int i = row_styles.Count; i < rows; i++)
257                                         row_styles.Add (new RowStyle ());
258                         }
259
260                         while (row_styles.Count > rows)
261                                 row_styles.RemoveAt (row_styles.Count - 1);
262                         while (col_styles.Count > columns)
263                                 col_styles.RemoveAt (col_styles.Count - 1);
264                                 
265                         // Figure up all the column widths
266                         int total_width = parentDisplayRectangle.Width - (border_width * (columns + 1));
267                         int index = 0;
268
269                         // First assign all the Absolute sized columns..
270                         foreach (ColumnStyle cs in col_styles) {
271                                 if (cs.SizeType == SizeType.Absolute) {
272                                         panel.column_widths[index] = (int)cs.Width;
273                                         total_width -= (int)cs.Width;
274                                 }
275
276                                 index++;
277                         }
278
279                         index = 0;
280
281                         // Next, assign all the AutoSize columns..
282                         foreach (ColumnStyle cs in col_styles)
283                         {
284                                 if (cs.SizeType == SizeType.AutoSize)
285                                 {
286                                         int max_width = 0; 
287                                         
288                                         // Find the widest control in the column
289                                         for (int i = 0; i < rows; i ++)
290                                         {
291                                                 Control c = panel.actual_positions[index, i];
292
293                                                 if (c != null && c != dummy_control && c.VisibleInternal)
294                                                 {
295                                                         if (settings.GetColumnSpan (c) > 1)
296                                                                 continue;
297                                                                 
298                                                         if (c.AutoSize)
299                                                                 max_width = Math.Max (max_width, c.PreferredSize.Width + c.Margin.Horizontal);
300                                                         else
301                                                                 max_width = Math.Max (max_width, c.ExplicitBounds.Width + c.Margin.Horizontal);
302                                                         
303                                                         if (c.Width + c.Margin.Left + c.Margin.Right > max_width)
304                                                                 max_width = c.Width + c.Margin.Left + c.Margin.Right;                                           
305                                                 }
306                                         }
307
308                                         panel.column_widths[index] = max_width;
309                                         total_width -= max_width;                               
310                                 }
311                                 
312                                 index++;
313                         }
314                         
315                         index = 0;
316                         float total_percent = 0;
317                         
318                         // Finally, assign the remaining space to Percent columns..
319                         if (total_width > 0)
320                         {
321                                 int percent_width = total_width; 
322                                 
323                                 // Find the total percent (not always 100%)
324                                 foreach (ColumnStyle cs in col_styles) 
325                                 {
326                                         if (cs.SizeType == SizeType.Percent)
327                                                 total_percent += cs.Width;
328                                 }
329
330                                 // Divy up the space..
331                                 foreach (ColumnStyle cs in col_styles) 
332                                 {
333                                         if (cs.SizeType == SizeType.Percent) 
334                                         {
335                                                 panel.column_widths[index] = (int)((cs.Width / total_percent) * percent_width);
336                                                 total_width -= panel.column_widths[index];
337                                         }
338
339                                         index++;
340                                 }
341                         }
342
343                         if (total_width > 0)
344                                 panel.column_widths[col_styles.Count - 1] += total_width;
345
346                         // Figure up all the row heights
347                         int total_height = parentDisplayRectangle.Height - (border_width * (rows + 1));
348                         index = 0;
349
350                         // First assign all the Absolute sized rows..
351                         foreach (RowStyle rs in row_styles) {
352                                 if (rs.SizeType == SizeType.Absolute) {
353                                         panel.row_heights[index] = (int)rs.Height;
354                                         total_height -= (int)rs.Height;
355                                 }
356
357                                 index++;
358                         }
359
360                         index = 0;
361
362                         // Next, assign all the AutoSize rows..
363                         foreach (RowStyle rs in row_styles) {
364                                 if (rs.SizeType == SizeType.AutoSize) {
365                                         int max_height = 0;
366
367                                         // Find the tallest control in the row
368                                         for (int i = 0; i < columns; i++) {
369                                                 Control c = panel.actual_positions[i, index];
370
371                                                 if (c != null && c != dummy_control && c.VisibleInternal) {
372                                                         if (settings.GetRowSpan (c) > 1)
373                                                                 continue; 
374                                                                 
375                                                         if (c.AutoSize)
376                                                                 max_height = Math.Max (max_height, c.PreferredSize.Height + c.Margin.Vertical);
377                                                         else
378                                                                 max_height = Math.Max (max_height, c.ExplicitBounds.Height + c.Margin.Vertical);
379
380                                                         if (c.Height + c.Margin.Top + c.Margin.Bottom > max_height)
381                                                                 max_height = c.Height + c.Margin.Top + c.Margin.Bottom;
382                                                 }
383                                         }
384
385                                         panel.row_heights[index] = max_height;
386                                         total_height -= max_height;
387                                 }
388
389                                 index++;
390                         }
391
392                         index = 0;
393                         total_percent = 0;
394
395                         // Finally, assign the remaining space to Percent columns..
396                         if (total_height > 0) {
397                                 int percent_height = total_height;
398                                 
399                                 // Find the total percent (not always 100%)
400                                 foreach (RowStyle rs in row_styles) {
401                                         if (rs.SizeType == SizeType.Percent)
402                                                 total_percent += rs.Height;
403                                 }
404
405                                 // Divy up the space..
406                                 foreach (RowStyle rs in row_styles) {
407                                         if (rs.SizeType == SizeType.Percent) {
408                                                 panel.row_heights[index] = (int)((rs.Height / total_percent) * percent_height);
409                                                 total_height -= panel.row_heights[index];
410                                         }
411
412                                         index++;
413                                 }
414                         }
415
416                         if (total_height > 0)
417                                 panel.row_heights[row_styles.Count - 1] += total_height;
418                 }
419                 
420                 private void LayoutControls (TableLayoutPanel panel)
421                 {
422                         TableLayoutSettings settings = panel.LayoutSettings;
423                         
424                         int border_width = TableLayoutPanel.GetCellBorderWidth (panel.CellBorderStyle);
425
426                         int columns = panel.actual_positions.GetLength(0);
427                         int rows = panel.actual_positions.GetLength(1);
428
429                         Point current_pos = new Point (panel.DisplayRectangle.Left + border_width, panel.DisplayRectangle.Top + border_width);
430
431                         for (int y = 0; y < rows; y++)
432                         {
433                                 for (int x = 0; x < columns; x++)
434                                 {
435                                         Control c = panel.actual_positions[x,y];
436                                         
437                                         if(c != null && c != dummy_control) {
438                                                 Size preferred;
439                                                 
440                                                 if (c.AutoSize)
441                                                         preferred = c.PreferredSize;
442                                                 else
443                                                         preferred = c.ExplicitBounds.Size;
444                                                 
445                                                 int new_x = 0;
446                                                 int new_y = 0;
447                                                 int new_width = 0;
448                                                 int new_height = 0;
449                                                 
450                                                 // Figure out the width of the control
451                                                 int column_width = panel.column_widths[x];
452                                                 
453                                                 for (int i = 1; i < Math.Min (settings.GetColumnSpan(c), panel.column_widths.Length); i++)
454                                                         column_width += panel.column_widths[x + i];
455
456                                                 if (c.Dock == DockStyle.Fill || c.Dock == DockStyle.Top || c.Dock == DockStyle.Bottom || ((c.Anchor & AnchorStyles.Left) == AnchorStyles.Left && (c.Anchor & AnchorStyles.Right) == AnchorStyles.Right))
457                                                         new_width = column_width - c.Margin.Left - c.Margin.Right;
458                                                 else
459                                                         new_width = Math.Min (preferred.Width, column_width - c.Margin.Left - c.Margin.Right);
460                                                         
461                                                 // Figure out the height of the control
462                                                 int column_height = panel.row_heights[y];
463
464                                                 for (int i = 1; i < Math.Min (settings.GetRowSpan (c), panel.row_heights.Length); i++)
465                                                         column_height += panel.row_heights[y + i];
466
467                                                 if (c.Dock == DockStyle.Fill || c.Dock == DockStyle.Left || c.Dock == DockStyle.Right || ((c.Anchor & AnchorStyles.Top) == AnchorStyles.Top && (c.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom))
468                                                         new_height = column_height - c.Margin.Top - c.Margin.Bottom;
469                                                 else
470                                                         new_height = Math.Min (preferred.Height, column_height - c.Margin.Top - c.Margin.Bottom);
471
472                                                 // Figure out the left location of the control
473                                                 if (c.Dock == DockStyle.Left || c.Dock == DockStyle.Fill || (c.Anchor & AnchorStyles.Left) == AnchorStyles.Left)
474                                                         new_x = current_pos.X + c.Margin.Left;
475                                                 else if (c.Dock == DockStyle.Right || (c.Anchor & AnchorStyles.Right) == AnchorStyles.Right)
476                                                         new_x = (current_pos.X + column_width) - new_width - c.Margin.Right;
477                                                 else    // (center control)
478                                                         new_x = (current_pos.X + (column_width - c.Margin.Left - c.Margin.Right) / 2) + c.Margin.Left - (new_width / 2);
479
480                                                 // Figure out the top location of the control
481                                                 if (c.Dock == DockStyle.Top || c.Dock == DockStyle.Fill || (c.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
482                                                         new_y = current_pos.Y + c.Margin.Top;
483                                                 else if (c.Dock == DockStyle.Bottom || (c.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom)
484                                                         new_y = (current_pos.Y + column_height) - new_height - c.Margin.Bottom;
485                                                 else    // (center control)
486                                                         new_y = (current_pos.Y + (column_height - c.Margin.Top - c.Margin.Bottom) / 2) + c.Margin.Top - (new_height / 2);
487
488                                                 c.SetBoundsInternal (new_x, new_y, new_width, new_height, BoundsSpecified.None);
489                                         }
490
491                                         current_pos.Offset (panel.column_widths[x] + border_width, 0);
492                                 }
493
494                                 current_pos.Offset ((-1 * current_pos.X) + border_width + panel.DisplayRectangle.Left, panel.row_heights[y] + border_width);
495                         }
496                 }
497         
498 #if TABLE_DEBUG
499                 private void OutputControlGrid (Control[,] grid, TableLayoutPanel panel)
500                 {
501                         Console.WriteLine ("     Size: {0}x{1}", grid.GetLength (0), grid.GetLength (1));
502
503                         Console.Write ("        ");
504
505                         foreach (int i in panel.column_widths)
506                                 Console.Write (" {0}px  ", i.ToString ().PadLeft (3));
507
508                         Console.WriteLine ();
509                                 
510                         for (int y = 0; y < grid.GetLength (1); y++) {
511                                 Console.Write (" {0}px |", panel.row_heights[y].ToString ().PadLeft (3));
512                                 
513                                 for (int x = 0; x < grid.GetLength (0); x++) {
514                                         if (grid[x, y] == null)
515                                                 Console.Write ("  ---  |");
516                                         else if (string.IsNullOrEmpty (grid[x, y].Name))
517                                                 Console.Write ("  ???  |");
518                                         else
519                                                 Console.Write (" {0} |", grid[x, y].Name.PadRight (5).Substring (0, 5));
520                                 }
521                                 
522                                 Console.WriteLine ();
523                         }
524                 }
525 #endif
526         }
527 }