Merge pull request #1442 from ermshiperete/TextOverlapsImage
[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                                                 // Fill in the rest of this control's row/column extent with dummy
130                                                 // controls, so that other controls don't get put there.
131                                                 for (int i = 0; i < col_span; i++)
132                                                         for (int j = 0; j < row_span; j++)
133                                                                 if (i != 0 || j != 0)
134                                                                         grid[col + i, row + j] = dummy_control;
135                                         }
136                                 }
137                         }
138
139                         int x_pointer = 0;
140                         int y_pointer = 0;
141
142                         // Fill in gaps with controls that do not have an explicit col/row
143                         foreach (Control c in panel.Controls) {
144                                 int col = settings.GetColumn (c);
145                                 int row = settings.GetRow (c);
146
147                                 if ((col >= 0 && col < columns) && (row >= 0 && row < rows) && (grid[col, row] == c || grid[col, row] == dummy_control))
148                                         continue;
149
150                                 for (int y = y_pointer; y < rows; y++) {
151                                         y_pointer = y;
152                                         x_pointer = 0;
153
154                                         for (int x = x_pointer; x < columns; x++) {
155                                                 x_pointer = x;
156
157                                                 if (grid[x, y] == null) {
158                                                         int col_span = Math.Min (settings.GetColumnSpan (c), columns);
159                                                         int row_span = Math.Min (settings.GetRowSpan (c), rows);
160
161                                                         if (x + col_span > columns) {
162                                                                 if (y + 1 < rows)
163                                                                         break;
164                                                                 else if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddColumns)
165                                                                         return CalculateControlPositions (panel, columns + 1, rows);
166                                                                 else
167                                                                         throw new ArgumentException ();
168                                                         }
169
170                                                         if (y + row_span > rows) {
171                                                                 if (x + 1 < columns)
172                                                                         break;
173                                                                 else if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddRows)
174                                                                         return CalculateControlPositions (panel, columns, rows + 1);
175                                                                 else
176                                                                         throw new ArgumentException ();
177                                                         }
178
179                                                         grid[x, y] = c;
180
181                                                         // Fill in the rest of this control's row/column extent with dummy
182                                                         // controls, so that other controls don't get put there.
183                                                         for (int i = 0; i < col_span; i++)
184                                                                 for (int j = 0; j < row_span; j++)
185                                                                         if (i != 0 || j != 0)
186                                                                                 grid[x + i, y + j] = dummy_control;
187
188                                                         // I know someone will kill me for using a goto, but 
189                                                         // sometimes they really are the easiest way...
190                                                         goto Found;
191                                                 } else {
192                                                         // MS adds the controls only to the first row if 
193                                                         // GrowStyle is AddColumns and RowCount is 0,
194                                                         // so interrupt the search for a free horizontal cell 
195                                                         // beyond the first one in the given vertical
196                                                         if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddColumns && 
197                                                             settings.RowCount == 0)
198                                                                 break;
199                                                 }
200                                         }
201                                 }
202
203                                 // MS adds rows instead of columns even when GrowStyle is AddColumns, 
204                                 // but RowCount is 0.
205                                 TableLayoutPanelGrowStyle adjustedGrowStyle = settings.GrowStyle;
206                                 if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddColumns) {
207                                         if (settings.RowCount == 0)
208                                                 adjustedGrowStyle = TableLayoutPanelGrowStyle.AddRows;
209                                 }
210
211                                 switch (adjustedGrowStyle) {
212                                         case TableLayoutPanelGrowStyle.AddColumns:
213                                                 return CalculateControlPositions (panel, columns + 1, rows);
214                                         case TableLayoutPanelGrowStyle.AddRows:
215                                         default:
216                                                 return CalculateControlPositions (panel, columns, rows + 1);
217                                         case TableLayoutPanelGrowStyle.FixedSize:
218                                                 throw new ArgumentException ();
219                                 }
220
221                         Found: ;
222                         }
223
224                         return grid;
225                 }
226
227                 private void CalculateColumnRowSizes (TableLayoutPanel panel, int columns, int rows)
228                 {
229                         TableLayoutSettings settings = panel.LayoutSettings;
230
231                         panel.column_widths = new int[panel.actual_positions.GetLength (0)];
232                         panel.row_heights = new int[panel.actual_positions.GetLength (1)];
233
234                         int border_width = TableLayoutPanel.GetCellBorderWidth (panel.CellBorderStyle);
235                                 
236                         Rectangle parentDisplayRectangle = panel.DisplayRectangle;
237
238                         TableLayoutColumnStyleCollection col_styles = new TableLayoutColumnStyleCollection (panel);
239                         
240                         foreach (ColumnStyle cs in settings.ColumnStyles)
241                                 col_styles.Add( new ColumnStyle(cs.SizeType, cs.Width));
242
243                         TableLayoutRowStyleCollection row_styles = new TableLayoutRowStyleCollection (panel);
244
245                         foreach (RowStyle rs in settings.RowStyles)
246                                 row_styles.Add (new RowStyle (rs.SizeType, rs.Height));
247                 
248                         // If we have more columns than columnstyles, temporarily add enough columnstyles
249                         if (columns > col_styles.Count)
250                         {
251                                 for (int i = col_styles.Count; i < columns; i++)
252                                         col_styles.Add(new ColumnStyle());                      
253                         }
254
255                         // Same for rows..
256                         if (rows > row_styles.Count) 
257                         {
258                                 for (int i = row_styles.Count; i < rows; i++)
259                                         row_styles.Add (new RowStyle ());
260                         }
261
262                         while (row_styles.Count > rows)
263                                 row_styles.RemoveAt (row_styles.Count - 1);
264                         while (col_styles.Count > columns)
265                                 col_styles.RemoveAt (col_styles.Count - 1);
266                                 
267                         // Find the largest column-span/row-span values.
268                         int max_colspan = 0, max_rowspan = 0;
269                         foreach (Control c in panel.Controls) {
270                                 max_colspan = Math.Max (max_colspan, settings.GetColumnSpan (c));
271                                 max_rowspan = Math.Max (max_rowspan, settings.GetRowSpan (c));
272                         }
273
274                         // Figure up all the column widths
275                         int total_width = parentDisplayRectangle.Width - (border_width * (columns + 1));
276                         int index = 0;
277
278                         // First assign all the Absolute sized columns..
279                         foreach (ColumnStyle cs in col_styles) {
280                                 if (cs.SizeType == SizeType.Absolute) {
281                                         panel.column_widths[index] = (int)cs.Width;
282                                         total_width -= (int)cs.Width;
283                                 }
284
285                                 index++;
286                         }
287
288                         // Next, assign all the AutoSize columns to the width of their widest
289                         // control.  If the table-layout is auto-sized, then make sure that
290                         // no column with Percent styling clips its contents.
291                         // (per http://msdn.microsoft.com/en-us/library/ms171690.aspx)
292                         for (int colspan = 0; colspan < max_colspan; ++colspan)
293                         {
294                                 for (index = colspan; index < col_styles.Count - colspan; ++index)
295                                 {
296                                         ColumnStyle cs = col_styles[index];
297                                         if (cs.SizeType == SizeType.AutoSize
298                                         || (panel.AutoSize && cs.SizeType == SizeType.Percent))
299                                         {
300                                                 int max_width = panel.column_widths[index];
301
302                                                 // Find the widest control in the column
303                                                 for (int i = 0; i < rows; i ++)
304                                                 {
305                                                         Control c = panel.actual_positions[index - colspan, i];
306
307                                                         if (c != null && c != dummy_control && c.VisibleInternal)
308                                                         {
309                                                                 // Skip any controls not being sized in this pass.
310                                                                 if (settings.GetColumnSpan (c) != colspan + 1)
311                                                                         continue;
312
313                                                                 // Calculate the maximum control width.
314                                                                 if (c.AutoSize)
315                                                                         max_width = Math.Max (max_width, c.PreferredSize.Width + c.Margin.Horizontal);
316                                                                 else
317                                                                         max_width = Math.Max (max_width, c.ExplicitBounds.Width + c.Margin.Horizontal);
318                                                                 max_width = Math.Max (max_width, c.Width + c.Margin.Left + c.Margin.Right);
319                                                         }
320                                                 }
321
322                                                 // Subtract the width of prior columns, if any.
323                                                 for (int i = Math.Max (index - colspan, 0); i < index; ++i)
324                                                         max_width -= panel.column_widths[i];
325
326                                                 // If necessary, increase this column's width.
327                                                 if (max_width > panel.column_widths[index])
328                                                 {
329                                                         max_width -= panel.column_widths[index];
330                                                         panel.column_widths[index] += max_width;
331                                                         total_width -= max_width;
332                                                 }
333                                         }
334                                 }
335                         }
336                         
337                         index = 0;
338                         float total_percent = 0;
339                         
340                         // Finally, assign the remaining space to Percent columns, if any.
341                         if (total_width > 0)
342                         {
343                                 int percent_width = total_width; 
344                                 
345                                 // Find the total percent (not always 100%)
346                                 foreach (ColumnStyle cs in col_styles) 
347                                 {
348                                         if (cs.SizeType == SizeType.Percent)
349                                                 total_percent += cs.Width;
350                                 }
351
352                                 // Divvy up the space..
353                                 foreach (ColumnStyle cs in col_styles) 
354                                 {
355                                         if (cs.SizeType == SizeType.Percent) 
356                                         {
357                                                 int width_change = (int)(((cs.Width / total_percent) * percent_width)
358                                                         - panel.column_widths[index]);
359                                                 if (width_change > 0)
360                                                 {
361                                                         panel.column_widths[index] += width_change;
362                                                         total_width -= width_change;
363                                                 }
364                                         }
365
366                                         index++;
367                                 }
368                         }
369
370                         if (total_width > 0)
371                                 panel.column_widths[col_styles.Count - 1] += total_width;
372
373                         // Figure up all the row heights
374                         int total_height = parentDisplayRectangle.Height - (border_width * (rows + 1));
375                         index = 0;
376
377                         // First assign all the Absolute sized rows..
378                         foreach (RowStyle rs in row_styles) {
379                                 if (rs.SizeType == SizeType.Absolute) {
380                                         panel.row_heights[index] = (int)rs.Height;
381                                         total_height -= (int)rs.Height;
382                                 }
383
384                                 index++;
385                         }
386
387                         index = 0;
388
389                         // Next, assign all the AutoSize rows to the height of their tallest
390                         // control.  If the table-layout is auto-sized, then make sure that
391                         // no row with Percent styling clips its contents.
392                         // (per http://msdn.microsoft.com/en-us/library/ms171690.aspx)
393                         for (int rowspan = 0; rowspan < max_rowspan; ++rowspan)
394                         {
395                                 for (index = rowspan; index < row_styles.Count - rowspan; ++index)
396                                 {
397                                         RowStyle rs = row_styles[index];
398                                         if (rs.SizeType == SizeType.AutoSize
399                                         || (panel.AutoSize && rs.SizeType == SizeType.Percent))
400                                         {
401                                                 int max_height = panel.row_heights[index];
402
403                                                 // Find the tallest control in the row
404                                                 for (int i = 0; i < columns; i++) {
405                                                         Control c = panel.actual_positions[i, index - rowspan];
406
407                                                         if (c != null && c != dummy_control && c.VisibleInternal)
408                                                         {
409                                                                 // Skip any controls not being sized in this pass.
410                                                                 if (settings.GetRowSpan (c) != rowspan + 1)
411                                                                         continue;
412
413                                                                 // Calculate the maximum control height.
414                                                                 if (c.AutoSize)
415                                                                         max_height = Math.Max (max_height, c.PreferredSize.Height + c.Margin.Vertical);
416                                                                 else
417                                                                         max_height = Math.Max (max_height, c.ExplicitBounds.Height + c.Margin.Vertical);
418                                                                 max_height = Math.Max (max_height, c.Height + c.Margin.Top + c.Margin.Bottom);
419                                                         }
420                                                 }
421
422                                                 // Subtract the height of prior rows, if any.
423                                                 for (int i = Math.Max (index - rowspan, 0); i < index; ++i)
424                                                         max_height -= panel.row_heights[i];
425
426                                                 // If necessary, increase this row's height.
427                                                 if (max_height > panel.row_heights[index])
428                                                 {
429                                                         max_height -= panel.row_heights[index];
430                                                         panel.row_heights[index] += max_height;
431                                                         total_height -= max_height;
432                                                 }
433                                         }
434                                 }
435                         }
436
437                         index = 0;
438                         total_percent = 0;
439
440                         // Finally, assign the remaining space to Percent rows, if any.
441                         if (total_height > 0) {
442                                 int percent_height = total_height;
443                                 
444                                 // Find the total percent (not always 100%)
445                                 foreach (RowStyle rs in row_styles) {
446                                         if (rs.SizeType == SizeType.Percent)
447                                                 total_percent += rs.Height;
448                                 }
449
450                                 // Divvy up the space..
451                                 foreach (RowStyle rs in row_styles) {
452                                         if (rs.SizeType == SizeType.Percent) {
453                                                 int height_change = (int)(((rs.Height / total_percent) * percent_height)
454                                                         - panel.row_heights[index]);
455                                                 if (height_change > 0)
456                                                 {
457                                                         panel.row_heights[index] += height_change;
458                                                         total_height -= height_change;
459                                                 }
460                                         }
461
462                                         index++;
463                                 }
464                         }
465
466                         if (total_height > 0)
467                                 panel.row_heights[row_styles.Count - 1] += total_height;
468                 }
469                 
470                 private void LayoutControls (TableLayoutPanel panel)
471                 {
472                         TableLayoutSettings settings = panel.LayoutSettings;
473                         
474                         int border_width = TableLayoutPanel.GetCellBorderWidth (panel.CellBorderStyle);
475
476                         int columns = panel.actual_positions.GetLength(0);
477                         int rows = panel.actual_positions.GetLength(1);
478
479                         Point current_pos = new Point (panel.DisplayRectangle.Left + border_width, panel.DisplayRectangle.Top + border_width);
480
481                         for (int y = 0; y < rows; y++)
482                         {
483                                 for (int x = 0; x < columns; x++)
484                                 {
485                                         Control c = panel.actual_positions[x,y];
486                                         
487                                         if(c != null && c != dummy_control) {
488                                                 Size preferred;
489                                                 
490                                                 if (c.AutoSize)
491                                                         preferred = c.PreferredSize;
492                                                 else
493                                                         preferred = c.ExplicitBounds.Size;
494                                                 
495                                                 int new_x = 0;
496                                                 int new_y = 0;
497                                                 int new_width = 0;
498                                                 int new_height = 0;
499                                                 
500                                                 // Figure out the width of the control
501                                                 int column_width = panel.column_widths[x];
502                                                 
503                                                 for (int i = 1; i < Math.Min (settings.GetColumnSpan(c), panel.column_widths.Length); i++)
504                                                         column_width += panel.column_widths[x + i];
505
506                                                 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))
507                                                         new_width = column_width - c.Margin.Left - c.Margin.Right;
508                                                 else
509                                                         new_width = Math.Min (preferred.Width, column_width - c.Margin.Left - c.Margin.Right);
510                                                         
511                                                 // Figure out the height of the control
512                                                 int column_height = panel.row_heights[y];
513
514                                                 for (int i = 1; i < Math.Min (settings.GetRowSpan (c), panel.row_heights.Length); i++)
515                                                         column_height += panel.row_heights[y + i];
516
517                                                 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))
518                                                         new_height = column_height - c.Margin.Top - c.Margin.Bottom;
519                                                 else
520                                                         new_height = Math.Min (preferred.Height, column_height - c.Margin.Top - c.Margin.Bottom);
521
522                                                 // Figure out the left location of the control
523                                                 if (c.Dock == DockStyle.Left || c.Dock == DockStyle.Fill || (c.Anchor & AnchorStyles.Left) == AnchorStyles.Left)
524                                                         new_x = current_pos.X + c.Margin.Left;
525                                                 else if (c.Dock == DockStyle.Right || (c.Anchor & AnchorStyles.Right) == AnchorStyles.Right)
526                                                         new_x = (current_pos.X + column_width) - new_width - c.Margin.Right;
527                                                 else    // (center control)
528                                                         new_x = (current_pos.X + (column_width - c.Margin.Left - c.Margin.Right) / 2) + c.Margin.Left - (new_width / 2);
529
530                                                 // Figure out the top location of the control
531                                                 if (c.Dock == DockStyle.Top || c.Dock == DockStyle.Fill || (c.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
532                                                         new_y = current_pos.Y + c.Margin.Top;
533                                                 else if (c.Dock == DockStyle.Bottom || (c.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom)
534                                                         new_y = (current_pos.Y + column_height) - new_height - c.Margin.Bottom;
535                                                 else    // (center control)
536                                                         new_y = (current_pos.Y + (column_height - c.Margin.Top - c.Margin.Bottom) / 2) + c.Margin.Top - (new_height / 2);
537
538                                                 c.SetBoundsInternal (new_x, new_y, new_width, new_height, BoundsSpecified.None);
539                                         }
540
541                                         current_pos.Offset (panel.column_widths[x] + border_width, 0);
542                                 }
543
544                                 current_pos.Offset ((-1 * current_pos.X) + border_width + panel.DisplayRectangle.Left, panel.row_heights[y] + border_width);
545                         }
546                 }
547         
548 #if TABLE_DEBUG
549                 private void OutputControlGrid (Control[,] grid, TableLayoutPanel panel)
550                 {
551                         Console.WriteLine ("     Size: {0}x{1}", grid.GetLength (0), grid.GetLength (1));
552
553                         Console.Write ("        ");
554
555                         foreach (int i in panel.column_widths)
556                                 Console.Write (" {0}px  ", i.ToString ().PadLeft (3));
557
558                         Console.WriteLine ();
559                                 
560                         for (int y = 0; y < grid.GetLength (1); y++) {
561                                 Console.Write (" {0}px |", panel.row_heights[y].ToString ().PadLeft (3));
562                                 
563                                 for (int x = 0; x < grid.GetLength (0); x++) {
564                                         if (grid[x, y] == null)
565                                                 Console.Write ("  ---  |");
566                                         else if (string.IsNullOrEmpty (grid[x, y].Name))
567                                                 Console.Write ("  ???  |");
568                                         else
569                                                 Console.Write (" {0} |", grid[x, y].Name.PadRight (5).Substring (0, 5));
570                                 }
571                                 
572                                 Console.WriteLine ();
573                         }
574                 }
575 #endif
576         }
577 }