public void TableLayoutSettings_SetRowSpan_MultipleTimes_GetReturnsExpected(TableLayoutSettings settings)
        {
            var control = new ScrollableControl();

            settings.SetRowSpan(control, 1);
            Assert.Equal(1, settings.GetRowSpan(control));

            settings.SetRowSpan(control, 2);
            Assert.Equal(2, settings.GetRowSpan(control));
        }
        public void TableLayoutSettings_SetRowSpan_ValidControl_GetReturnsExpected(TableLayoutSettings settings, int value)
        {
            var control = new ScrollableControl();

            settings.SetRowSpan(control, value);
            Assert.Equal(value, settings.GetRowSpan(control));
        }
        public void TableLayoutSettings_GetRowSpan_InvalidControlStub_ReturnsExpected()
        {
            var converter = new TableLayoutSettingsTypeConverter();
            TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(converter.ConvertFrom(@"<?xml version=""1.0"" encoding=""utf-16""?><Root />"));

            Assert.Equal(1, settings.GetRowSpan("control"));
        }
        public void TableLayoutSettings_GetRowSpan_InvalidControl_ThrowsNotSupportedException()
        {
            var toolStrip = new ToolStrip {
                LayoutStyle = ToolStripLayoutStyle.Table
            };
            TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(toolStrip.LayoutSettings);

            Assert.Throws <NotSupportedException>(() => settings.GetRowSpan("control"));
        }
Ejemplo n.º 5
0
        public void TableLayoutSettings_GetRowSpan_NullControl_ThrowsNullReferenceException()
        {
            var toolStrip = new ToolStrip {
                LayoutStyle = ToolStripLayoutStyle.Table
            };
            TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(toolStrip.LayoutSettings);

            Assert.Throws <NullReferenceException>(() => settings.GetRowSpan(null));
        }
        public void TableLayoutSettings_GetRowSpan_NullControl_ThrowsArgumentNullException()
        {
            var toolStrip = new ToolStrip {
                LayoutStyle = ToolStripLayoutStyle.Table
            };
            TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(toolStrip.LayoutSettings);

            Assert.Throws <ArgumentNullException>("control", () => settings.GetRowSpan(null));
        }
        public void TableLayoutSettings_Serialize_Deserialize_Success()
        {
            var toolStrip = new ToolStrip {
                LayoutStyle = ToolStripLayoutStyle.Table
            };
            TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(toolStrip.LayoutSettings);
            var columnStyle = new ColumnStyle(SizeType.Percent, 1);
            var rowStyle    = new RowStyle(SizeType.Percent, 1);

            var controlWithName = new ScrollableControl {
                Name = "name"
            };

            settings.SetColumnSpan(controlWithName, 1);
            settings.SetRowSpan(controlWithName, 2);
            settings.SetColumn(controlWithName, 3);
            settings.SetRow(controlWithName, 4);
            settings.ColumnStyles.Add(columnStyle);
            settings.RowStyles.Add(rowStyle);

            using (var stream = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, settings);
                stream.Seek(0, SeekOrigin.Begin);

                TableLayoutSettings result = Assert.IsType <TableLayoutSettings>(formatter.Deserialize(stream));
                Assert.Equal(columnStyle.SizeType, ((ColumnStyle)Assert.Single(result.ColumnStyles)).SizeType);
                Assert.Equal(columnStyle.Width, ((ColumnStyle)Assert.Single(result.ColumnStyles)).Width);
                Assert.Equal(rowStyle.SizeType, ((RowStyle)Assert.Single(result.RowStyles)).SizeType);
                Assert.Equal(rowStyle.Height, ((RowStyle)Assert.Single(result.RowStyles)).Height);

                Assert.Equal(1, result.GetColumnSpan(controlWithName));
                Assert.Equal(1, result.GetRowSpan(controlWithName));
                Assert.Equal(-1, result.GetColumn(controlWithName));
                Assert.Equal(-1, result.GetRow(controlWithName));
            }
        }
        public void TableLayoutSettings_GetRowSpan_NoSuchControl_ReturnsExpected(TableLayoutSettings settings)
        {
            var control = new ScrollableControl();

            Assert.Equal(1, settings.GetRowSpan(control));
        }
Ejemplo n.º 9
0
 public int GetRowSpan(object target)
 {
     return(TableLayoutSettings != null?TableLayoutSettings.GetRowSpan(target) : 1);
 }
Ejemplo n.º 10
0
        private void LayoutControls(TableLayoutPanel panel)
        {
            TableLayoutSettings settings = panel.LayoutSettings;

            int border_width = TableLayoutPanel.GetCellBorderWidth(panel.CellBorderStyle);

            int columns = panel.actual_positions.GetLength(0);
            int rows    = panel.actual_positions.GetLength(1);

            Point_ current_pos = new Point_(panel.DisplayRectangle.Left + border_width, panel.DisplayRectangle.Top + border_width);

            for (int y = 0; y < rows; y++)
            {
                for (int x = 0; x < columns; x++)
                {
                    Control c = panel.actual_positions[x, y];

                    if (c != null && c != dummy_control)
                    {
                        Size_ preferred;

                        if (c.AutoSize)
                        {
                            preferred = c.PreferredSize;
                        }
                        else
                        {
                            preferred = c.ExplicitBounds.Size;
                        }

                        int new_x      = 0;
                        int new_y      = 0;
                        int new_width  = 0;
                        int new_height = 0;

                        // Figure out the width of the control
                        int column_width = panel.column_widths[x];

                        for (int i = 1; i < Math.Min(settings.GetColumnSpan(c), panel.column_widths.Length); i++)
                        {
                            column_width += panel.column_widths[x + i];
                        }

                        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))
                        {
                            new_width = column_width - c.Margin.Left - c.Margin.Right;
                        }
                        else
                        {
                            new_width = Math.Min(preferred.Width, column_width - c.Margin.Left - c.Margin.Right);
                        }

                        // Figure out the height of the control
                        int column_height = panel.row_heights[y];

                        for (int i = 1; i < Math.Min(settings.GetRowSpan(c), panel.row_heights.Length); i++)
                        {
                            column_height += panel.row_heights[y + i];
                        }

                        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))
                        {
                            new_height = column_height - c.Margin.Top - c.Margin.Bottom;
                        }
                        else
                        {
                            new_height = Math.Min(preferred.Height, column_height - c.Margin.Top - c.Margin.Bottom);
                        }

                        // Figure out the left location of the control
                        if (c.Dock == DockStyle.Left || c.Dock == DockStyle.Fill || (c.Anchor & AnchorStyles.Left) == AnchorStyles.Left)
                        {
                            new_x = current_pos.X + c.Margin.Left;
                        }
                        else if (c.Dock == DockStyle.Right || (c.Anchor & AnchorStyles.Right) == AnchorStyles.Right)
                        {
                            new_x = (current_pos.X + column_width) - new_width - c.Margin.Right;
                        }
                        else                            // (center control)
                        {
                            new_x = (current_pos.X + (column_width - c.Margin.Left - c.Margin.Right) / 2) + c.Margin.Left - (new_width / 2);
                        }

                        // Figure out the top location of the control
                        if (c.Dock == DockStyle.Top || c.Dock == DockStyle.Fill || (c.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
                        {
                            new_y = current_pos.Y + c.Margin.Top;
                        }
                        else if (c.Dock == DockStyle.Bottom || (c.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom)
                        {
                            new_y = (current_pos.Y + column_height) - new_height - c.Margin.Bottom;
                        }
                        else                            // (center control)
                        {
                            new_y = (current_pos.Y + (column_height - c.Margin.Top - c.Margin.Bottom) / 2) + c.Margin.Top - (new_height / 2);
                        }

                        c.SetBoundsInternal(new_x, new_y, new_width, new_height, BoundsSpecified.None);
                    }

                    current_pos.Offset(panel.column_widths[x] + border_width, 0);
                }

                current_pos.Offset((-1 * current_pos.X) + border_width + panel.DisplayRectangle.Left, panel.row_heights[y] + border_width);
            }
        }
Ejemplo n.º 11
0
        private void CalculateColumnRowSizes(TableLayoutPanel panel, int columns, int rows)
        {
            TableLayoutSettings settings = panel.LayoutSettings;

            panel.column_widths = new int[panel.actual_positions.GetLength(0)];
            panel.row_heights   = new int[panel.actual_positions.GetLength(1)];

            int border_width = TableLayoutPanel.GetCellBorderWidth(panel.CellBorderStyle);

            Rectangle_ parentDisplayRectangle = panel.DisplayRectangle;

            TableLayoutColumnStyleCollection col_styles = new TableLayoutColumnStyleCollection(panel);

            foreach (ColumnStyle cs in settings.ColumnStyles)
            {
                col_styles.Add(new ColumnStyle(cs.SizeType, cs.Width));
            }

            TableLayoutRowStyleCollection row_styles = new TableLayoutRowStyleCollection(panel);

            foreach (RowStyle rs in settings.RowStyles)
            {
                row_styles.Add(new RowStyle(rs.SizeType, rs.Height));
            }

            // If we have more columns than columnstyles, temporarily add enough columnstyles
            if (columns > col_styles.Count)
            {
                for (int i = col_styles.Count; i < columns; i++)
                {
                    col_styles.Add(new ColumnStyle());
                }
            }

            // Same for rows..
            if (rows > row_styles.Count)
            {
                for (int i = row_styles.Count; i < rows; i++)
                {
                    row_styles.Add(new RowStyle());
                }
            }

            while (row_styles.Count > rows)
            {
                row_styles.RemoveAt(row_styles.Count - 1);
            }
            while (col_styles.Count > columns)
            {
                col_styles.RemoveAt(col_styles.Count - 1);
            }

            // Find the largest column-span/row-span values.
            int max_colspan = 0, max_rowspan = 0;

            foreach (Control c in panel.Controls)
            {
                max_colspan = Math.Max(max_colspan, settings.GetColumnSpan(c));
                max_rowspan = Math.Max(max_rowspan, settings.GetRowSpan(c));
            }

            // Figure up all the column widths
            int total_width = parentDisplayRectangle.Width - (border_width * (columns + 1));
            int index       = 0;

            // First assign all the Absolute sized columns..
            foreach (ColumnStyle cs in col_styles)
            {
                if (cs.SizeType == SizeType.Absolute)
                {
                    panel.column_widths[index] = (int)cs.Width;
                    total_width -= (int)cs.Width;
                }

                index++;
            }

            // Next, assign all the AutoSize columns to the width of their widest
            // control.  If the table-layout is auto-sized, then make sure that
            // no column with Percent styling clips its contents.
            // (per http://msdn.microsoft.com/en-us/library/ms171690.aspx)
            for (int colspan = 0; colspan < max_colspan; ++colspan)
            {
                for (index = colspan; index < col_styles.Count - colspan; ++index)
                {
                    ColumnStyle cs = col_styles[index];
                    if (cs.SizeType == SizeType.AutoSize ||
                        (panel.AutoSize && cs.SizeType == SizeType.Percent))
                    {
                        int max_width = panel.column_widths[index];

                        // Find the widest control in the column
                        for (int i = 0; i < rows; i++)
                        {
                            Control c = panel.actual_positions[index - colspan, i];

                            if (c != null && c != dummy_control && c.VisibleInternal)
                            {
                                // Skip any controls not being sized in this pass.
                                if (settings.GetColumnSpan(c) != colspan + 1)
                                {
                                    continue;
                                }

                                // Calculate the maximum control width.
                                if (c.AutoSize)
                                {
                                    max_width = Math.Max(max_width, c.PreferredSize.Width + c.Margin.Horizontal);
                                }
                                else
                                {
                                    max_width = Math.Max(max_width, c.ExplicitBounds.Width + c.Margin.Horizontal);
                                }
                                max_width = Math.Max(max_width, c.Width + c.Margin.Left + c.Margin.Right);
                            }
                        }

                        // Subtract the width of prior columns, if any.
                        for (int i = Math.Max(index - colspan, 0); i < index; ++i)
                        {
                            max_width -= panel.column_widths[i];
                        }

                        // If necessary, increase this column's width.
                        if (max_width > panel.column_widths[index])
                        {
                            max_width -= panel.column_widths[index];
                            panel.column_widths[index] += max_width;
                            total_width -= max_width;
                        }
                    }
                }
            }

            index = 0;
            float total_percent = 0;

            // Finally, assign the remaining space to Percent columns, if any.
            if (total_width > 0)
            {
                int percent_width = total_width;

                // Find the total percent (not always 100%)
                foreach (ColumnStyle cs in col_styles)
                {
                    if (cs.SizeType == SizeType.Percent)
                    {
                        total_percent += cs.Width;
                    }
                }

                // Divvy up the space..
                foreach (ColumnStyle cs in col_styles)
                {
                    if (cs.SizeType == SizeType.Percent)
                    {
                        int width_change = (int)(((cs.Width / total_percent) * percent_width)
                                                 - panel.column_widths[index]);
                        if (width_change > 0)
                        {
                            panel.column_widths[index] += width_change;
                            total_width -= width_change;
                        }
                    }

                    index++;
                }
            }

            if (total_width > 0)
            {
                // Find the last column that isn't an Absolute SizeType, and give it
                // all this free space.  (Absolute sized columns need to retain their
                // absolute width if at all possible!)
                int col = col_styles.Count - 1;
                for (; col >= 0; --col)
                {
                    if (col_styles[col].SizeType != SizeType.Absolute)
                    {
                        break;
                    }
                }
                if (col < 0)
                {
                    col = col_styles.Count - 1;
                }
                panel.column_widths[col] += total_width;
            }

            // Figure up all the row heights
            int total_height = parentDisplayRectangle.Height - (border_width * (rows + 1));

            index = 0;

            // First assign all the Absolute sized rows..
            foreach (RowStyle rs in row_styles)
            {
                if (rs.SizeType == SizeType.Absolute)
                {
                    panel.row_heights[index] = (int)rs.Height;
                    total_height            -= (int)rs.Height;
                }

                index++;
            }

            index = 0;

            // Next, assign all the AutoSize rows to the height of their tallest
            // control.  If the table-layout is auto-sized, then make sure that
            // no row with Percent styling clips its contents.
            // (per http://msdn.microsoft.com/en-us/library/ms171690.aspx)
            for (int rowspan = 0; rowspan < max_rowspan; ++rowspan)
            {
                for (index = rowspan; index < row_styles.Count - rowspan; ++index)
                {
                    RowStyle rs = row_styles[index];
                    if (rs.SizeType == SizeType.AutoSize ||
                        (panel.AutoSize && rs.SizeType == SizeType.Percent))
                    {
                        int max_height = panel.row_heights[index];

                        // Find the tallest control in the row
                        for (int i = 0; i < columns; i++)
                        {
                            Control c = panel.actual_positions[i, index - rowspan];

                            if (c != null && c != dummy_control && c.VisibleInternal)
                            {
                                // Skip any controls not being sized in this pass.
                                if (settings.GetRowSpan(c) != rowspan + 1)
                                {
                                    continue;
                                }

                                // Calculate the maximum control height.
                                if (c.AutoSize)
                                {
                                    max_height = Math.Max(max_height, c.PreferredSize.Height + c.Margin.Vertical);
                                }
                                else
                                {
                                    max_height = Math.Max(max_height, c.ExplicitBounds.Height + c.Margin.Vertical);
                                }
                                max_height = Math.Max(max_height, c.Height + c.Margin.Top + c.Margin.Bottom);
                            }
                        }

                        // Subtract the height of prior rows, if any.
                        for (int i = Math.Max(index - rowspan, 0); i < index; ++i)
                        {
                            max_height -= panel.row_heights[i];
                        }

                        // If necessary, increase this row's height.
                        if (max_height > panel.row_heights[index])
                        {
                            max_height -= panel.row_heights[index];
                            panel.row_heights[index] += max_height;
                            total_height             -= max_height;
                        }
                    }
                }
            }

            index         = 0;
            total_percent = 0;

            // Finally, assign the remaining space to Percent rows, if any.
            if (total_height > 0)
            {
                int percent_height = total_height;

                // Find the total percent (not always 100%)
                foreach (RowStyle rs in row_styles)
                {
                    if (rs.SizeType == SizeType.Percent)
                    {
                        total_percent += rs.Height;
                    }
                }

                // Divvy up the space..
                foreach (RowStyle rs in row_styles)
                {
                    if (rs.SizeType == SizeType.Percent)
                    {
                        int height_change = (int)(((rs.Height / total_percent) * percent_height)
                                                  - panel.row_heights[index]);
                        if (height_change > 0)
                        {
                            panel.row_heights[index] += height_change;
                            total_height             -= height_change;
                        }
                    }

                    index++;
                }
            }

            if (total_height > 0)
            {
                // Find the last row that isn't an Absolute SizeType, and give it
                // all this free space.  (Absolute sized rows need to retain their
                // absolute height if at all possible!)
                int row = row_styles.Count - 1;
                for (; row >= 0; --row)
                {
                    if (row_styles[row].SizeType != SizeType.Absolute)
                    {
                        break;
                    }
                }
                if (row < 0)
                {
                    row = row_styles.Count - 1;
                }
                panel.row_heights[row] += total_height;
            }
        }
Ejemplo n.º 12
0
        private void CalculateColumnRowSizes(TableLayoutPanel panel, int columns, int rows)
        {
            TableLayoutSettings settings = panel.LayoutSettings;

            panel.column_widths = new int[panel.actual_positions.GetLength(0)];
            panel.row_heights   = new int[panel.actual_positions.GetLength(1)];

            int border_width = TableLayoutPanel.GetCellBorderWidth(panel.CellBorderStyle);

            Rectangle parentDisplayRectangle = panel.DisplayRectangle;

            TableLayoutColumnStyleCollection col_styles = new TableLayoutColumnStyleCollection(panel);

            foreach (ColumnStyle cs in settings.ColumnStyles)
            {
                col_styles.Add(new ColumnStyle(cs.SizeType, cs.Width));
            }

            TableLayoutRowStyleCollection row_styles = new TableLayoutRowStyleCollection(panel);

            foreach (RowStyle rs in settings.RowStyles)
            {
                row_styles.Add(new RowStyle(rs.SizeType, rs.Height));
            }

            // If we have more columns than columnstyles, temporarily add enough columnstyles
            if (columns > col_styles.Count)
            {
                for (int i = col_styles.Count; i < columns; i++)
                {
                    col_styles.Add(new ColumnStyle());
                }
            }

            // Same for rows..
            if (rows > row_styles.Count)
            {
                for (int i = row_styles.Count; i < rows; i++)
                {
                    row_styles.Add(new RowStyle());
                }
            }

            while (row_styles.Count > rows)
            {
                row_styles.RemoveAt(row_styles.Count - 1);
            }
            while (col_styles.Count > columns)
            {
                col_styles.RemoveAt(col_styles.Count - 1);
            }

            // Figure up all the column widths
            int total_width = parentDisplayRectangle.Width - (border_width * (columns + 1));
            int index       = 0;

            // First assign all the Absolute sized columns..
            foreach (ColumnStyle cs in col_styles)
            {
                if (cs.SizeType == SizeType.Absolute)
                {
                    panel.column_widths[index] = (int)cs.Width;
                    total_width -= (int)cs.Width;
                }

                index++;
            }

            index = 0;

            // Next, assign all the AutoSize columns..
            foreach (ColumnStyle cs in col_styles)
            {
                if (cs.SizeType == SizeType.AutoSize)
                {
                    int max_width = 0;

                    // Find the widest control in the column
                    for (int i = 0; i < rows; i++)
                    {
                        Control c = panel.actual_positions[index, i];

                        if (c != null && c != dummy_control && c.VisibleInternal)
                        {
                            if (settings.GetColumnSpan(c) > 1)
                            {
                                continue;
                            }

                            if (c.AutoSize)
                            {
                                max_width = Math.Max(max_width, c.PreferredSize.Width + c.Margin.Horizontal);
                            }
                            else
                            {
                                max_width = Math.Max(max_width, c.ExplicitBounds.Width + c.Margin.Horizontal);
                            }

                            if (c.Width + c.Margin.Left + c.Margin.Right > max_width)
                            {
                                max_width = c.Width + c.Margin.Left + c.Margin.Right;
                            }
                        }
                    }

                    panel.column_widths[index] = max_width;
                    total_width -= max_width;
                }

                index++;
            }

            index = 0;
            float total_percent = 0;

            // Finally, assign the remaining space to Percent columns..
            if (total_width > 0)
            {
                int percent_width = total_width;

                // Find the total percent (not always 100%)
                foreach (ColumnStyle cs in col_styles)
                {
                    if (cs.SizeType == SizeType.Percent)
                    {
                        total_percent += cs.Width;
                    }
                }

                // Divy up the space..
                foreach (ColumnStyle cs in col_styles)
                {
                    if (cs.SizeType == SizeType.Percent)
                    {
                        panel.column_widths[index] = (int)((cs.Width / total_percent) * percent_width);
                        total_width -= panel.column_widths[index];
                    }

                    index++;
                }
            }

            if (total_width > 0)
            {
                panel.column_widths[col_styles.Count - 1] += total_width;
            }

            // Figure up all the row heights
            int total_height = parentDisplayRectangle.Height - (border_width * (rows + 1));

            index = 0;

            // First assign all the Absolute sized rows..
            foreach (RowStyle rs in row_styles)
            {
                if (rs.SizeType == SizeType.Absolute)
                {
                    panel.row_heights[index] = (int)rs.Height;
                    total_height            -= (int)rs.Height;
                }

                index++;
            }

            index = 0;

            // Next, assign all the AutoSize rows..
            foreach (RowStyle rs in row_styles)
            {
                if (rs.SizeType == SizeType.AutoSize)
                {
                    int max_height = 0;

                    // Find the tallest control in the row
                    for (int i = 0; i < columns; i++)
                    {
                        Control c = panel.actual_positions[i, index];

                        if (c != null && c != dummy_control && c.VisibleInternal)
                        {
                            if (settings.GetRowSpan(c) > 1)
                            {
                                continue;
                            }

                            if (c.AutoSize)
                            {
                                max_height = Math.Max(max_height, c.PreferredSize.Height + c.Margin.Vertical);
                            }
                            else
                            {
                                max_height = Math.Max(max_height, c.ExplicitBounds.Height + c.Margin.Vertical);
                            }

                            if (c.Height + c.Margin.Top + c.Margin.Bottom > max_height)
                            {
                                max_height = c.Height + c.Margin.Top + c.Margin.Bottom;
                            }
                        }
                    }

                    panel.row_heights[index] = max_height;
                    total_height            -= max_height;
                }

                index++;
            }

            index         = 0;
            total_percent = 0;

            // Finally, assign the remaining space to Percent columns..
            if (total_height > 0)
            {
                int percent_height = total_height;

                // Find the total percent (not always 100%)
                foreach (RowStyle rs in row_styles)
                {
                    if (rs.SizeType == SizeType.Percent)
                    {
                        total_percent += rs.Height;
                    }
                }

                // Divy up the space..
                foreach (RowStyle rs in row_styles)
                {
                    if (rs.SizeType == SizeType.Percent)
                    {
                        panel.row_heights[index] = (int)((rs.Height / total_percent) * percent_height);
                        total_height            -= panel.row_heights[index];
                    }

                    index++;
                }
            }

            if (total_height > 0)
            {
                panel.row_heights[row_styles.Count - 1] += total_height;
            }
        }
Ejemplo n.º 13
0
        private void CalculateColumnRowSizes(TableLayoutPanel panel, Control[,] actual_positions, out int[] column_widths, out int[] row_heights, Size size, bool measureOnly)
        {
            TableLayoutSettings settings = panel.LayoutSettings;
            int  columns     = actual_positions.GetLength(0);
            int  rows        = actual_positions.GetLength(1);
            bool auto_size   = panel.AutoSizeInternal && measureOnly;
            bool boundBySize = !measureOnly;

            column_widths = new int[actual_positions.GetLength(0)];
            row_heights   = new int[actual_positions.GetLength(1)];

            // Calculate the bounded size only if we are in default layout and docked, otherwise calculate unbounded.
            if (measureOnly && size.Width > 0)
            {
                if (panel.Parent != null && panel.Parent.LayoutEngine is DefaultLayout)
                {
                    boundBySize |= panel.Dock == DockStyle.Top || panel.Dock == DockStyle.Bottom || panel.Dock == DockStyle.Fill;
                    boundBySize |= (panel.Anchor & (AnchorStyles.Left | AnchorStyles.Right)) == (AnchorStyles.Left | AnchorStyles.Right);
                }
            }

            int border_width = TableLayoutPanel.GetCellBorderWidth(panel.CellBorderStyle);

            // Find the largest column-span/row-span values.
            int max_colspan = 0, max_rowspan = 0;

            foreach (Control c in panel.Controls)
            {
                if (c.VisibleInternal && c != dummy_control)
                {
                    max_colspan = Math.Max(max_colspan, settings.GetColumnSpan(c));
                    max_rowspan = Math.Max(max_rowspan, settings.GetRowSpan(c));
                }
            }

            // Figure up all the column widths
            CalculateColumnWidths(settings, actual_positions, max_colspan, settings.ColumnStyles, auto_size, column_widths, false);

            // Calculate available width
            int available_width = size.Width - (border_width * (columns + 1));

            foreach (int width in column_widths)
            {
                available_width -= width;
            }

            // Shrink the table horizontally by shrinking it's columns, if necessary
            if (boundBySize && size.Width > 0 && available_width < 0)
            {
                // Calculate the minimum widths for each column
                int[] col_min_widths = new int[column_widths.Length];
                CalculateColumnWidths(settings, actual_positions, max_colspan, settings.ColumnStyles, auto_size, col_min_widths, true);
                available_width += Shrink(column_widths, col_min_widths, -available_width, max_colspan);
            }

            // Finally, assign the remaining space to Percent columns, if any.
            if (available_width > 0)
            {
                available_width -= RedistributePercents(available_width, settings.ColumnStyles, column_widths);
            }

            if (available_width > 0 && column_widths.Length > 0)
            {
                // Find the last column that isn't an Absolute SizeType, and give it
                // all this free space.  (Absolute sized columns need to retain their
                // absolute width if at all possible!)
                int col = Math.Min(settings.ColumnStyles.Count, column_widths.Length) - 1;
                for (; col >= 0; --col)
                {
                    if (settings.ColumnStyles[col].SizeType != SizeType.Absolute)
                    {
                        break;
                    }
                }
                if (col < 0)
                {
                    col = column_widths.Length - 1;
                }
                column_widths[col] += available_width;
            }

            // Figure up all the row heights
            CalculateRowHeights(settings, actual_positions, max_rowspan, settings.RowStyles, auto_size, column_widths, row_heights);

            // Calculate available height
            int available_height = size.Height - (border_width * (rows + 1));

            foreach (int height in row_heights)
            {
                available_height -= height;
            }

            // NOTE: We don't do shrinking here, since there's no space to save.

            // Finally, assign the remaining space to Percent rows, if any.
            if (available_height > 0)
            {
                available_height -= RedistributePercents(available_height, settings.RowStyles, row_heights);
            }

            if (available_height > 0 && row_heights.Length > 0 && !measureOnly)
            {
                // Find the last row that isn't an Absolute SizeType, and give it
                // all this free space.  (Absolute sized rows need to retain their
                // absolute height if at all possible!)
                int row = Math.Min(settings.RowStyles.Count, row_heights.Length) - 1;
                for (; row >= 0; --row)
                {
                    if (settings.RowStyles[row].SizeType != SizeType.Absolute)
                    {
                        break;
                    }
                }
                if (row < 0)
                {
                    row = row_heights.Length - 1;
                }
                row_heights[row] += available_height;
            }
        }
Ejemplo n.º 14
0
        private static void CalculateRowHeights(TableLayoutSettings settings, Control[,] actual_positions, int max_rowspan, TableLayoutRowStyleCollection row_styles, bool auto_size, int[] column_widths, int[] row_heights)
        {
            int   columns          = actual_positions.GetLength(0);
            float max_percent_size = 0;

            // First assign all the Absolute sized rows..
            int index = 0;

            foreach (RowStyle rs in row_styles)
            {
                if (index >= row_heights.Length)
                {
                    break;
                }
                if (rs.SizeType == SizeType.Absolute)
                {
                    row_heights[index] = (int)rs.Height;
                }
                index++;
            }
            while (index < row_heights.Length)
            {
                row_heights[index] = 0;
                index++;
            }

            // Next, assign all the AutoSize rows to the height of their tallest
            // control.  If the table-layout is auto-sized, then make sure that
            // no row with Percent styling clips its contents.
            // (per http://msdn.microsoft.com/en-us/library/ms171690.aspx)
            for (int rowspan = 0; rowspan < max_rowspan; ++rowspan)
            {
                for (index = rowspan; index < row_heights.Length - rowspan; ++index)
                {
                    RowStyle rs = index < row_styles.Count ? row_styles[index] : default_row_style;
                    if (rs.SizeType == SizeType.AutoSize || (auto_size && rs.SizeType == SizeType.Percent))
                    {
                        int max_height = row_heights[index];
                        // Find the tallest control in the row
                        for (int i = 0; i < columns; i++)
                        {
                            Control c = actual_positions[i, index - rowspan];
                            if (c != null && c != dummy_control && c.VisibleInternal)
                            {
                                // Skip any controls not being sized in this pass.
                                if (settings.GetRowSpan(c) != rowspan + 1)
                                {
                                    continue;
                                }

                                int current_width = 0;
                                int column_span   = settings.GetColumnSpan(c);
                                for (int j = i; j < i + column_span && j < column_widths.Length; j++)
                                {
                                    current_width += column_widths[j];
                                }

                                // Calculate the maximum control height.
                                max_height = Math.Max(max_height, GetControlSize(c, new Size(current_width - c.Margin.Horizontal, 0)).Height + c.Margin.Vertical);
                            }
                        }

                        if (rs.SizeType == SizeType.Percent)
                        {
                            max_percent_size = Math.Max(max_percent_size, max_height / rs.Height);
                        }

                        // Subtract the height of prior rows, if any.
                        for (int i = Math.Max(index - rowspan, 0); i < index; ++i)
                        {
                            max_height -= row_heights[i];
                        }

                        // If necessary, increase this row's height.
                        if (max_height > row_heights[index])
                        {
                            row_heights[index] = max_height;
                        }
                    }
                }
            }

            // Resize the percent columns to match other percent columns relatively.
            for (index = 0; index < row_styles.Count && index < row_heights.Length; ++index)
            {
                RowStyle rs = row_styles[index];
                if (rs.SizeType == SizeType.Percent)
                {
                    row_heights[index] = Math.Max(row_heights[index], (int)Math.Ceiling(max_percent_size * rs.Height));
                }
            }
        }
Ejemplo n.º 15
0
        private void LayoutControls(IArrangedContainer panel, IArrangedElement[,] actual_positions, int[] column_widths, int[] row_heights)
        {
            TableLayoutSettings settings = GetLayoutSettings(panel);

            int border_width = 0;

            if (panel is TableLayoutPanel table_panel)
            {
                border_width = TableLayoutPanel.GetCellBorderWidth(table_panel.CellBorderStyle);
            }

            int columns = actual_positions.GetLength(0);
            int rows    = actual_positions.GetLength(1);

            Point current_pos = new Point(panel.DisplayRectangle.Left + border_width, panel.DisplayRectangle.Top + border_width);

            for (int y = 0; y < rows; y++)
            {
                for (int x = 0; x < columns; x++)
                {
                    IArrangedElement c = actual_positions[x, y];
                    if (c != null && c != dummy_control && c.Visible)
                    {
                        Size preferred;

                        int new_x      = 0;
                        int new_y      = 0;
                        int new_width  = 0;
                        int new_height = 0;

                        int column_width = column_widths[x];
                        for (int i = 1; i < Math.Min(settings.GetColumnSpan(c), column_widths.Length - x); i++)
                        {
                            column_width += column_widths[x + i];
                        }

                        int column_height = row_heights[y];
                        for (int i = 1; i < Math.Min(settings.GetRowSpan(c), row_heights.Length - y); i++)
                        {
                            column_height += row_heights[y + i];
                        }

                        preferred = GetControlSize(c, new Size(column_width - c.Margin.Horizontal, column_height - c.Margin.Vertical));

                        // Figure out the width of the control
                        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))
                        {
                            new_width = column_width - c.Margin.Left - c.Margin.Right;
                        }
                        else
                        {
                            new_width = Math.Min(preferred.Width, column_width - c.Margin.Left - c.Margin.Right);
                        }

                        // Figure out the height of the control
                        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))
                        {
                            new_height = column_height - c.Margin.Top - c.Margin.Bottom;
                        }
                        else
                        {
                            new_height = Math.Min(preferred.Height, column_height - c.Margin.Top - c.Margin.Bottom);
                        }

                        // Figure out the left location of the control
                        if (c.Dock == DockStyle.Left || c.Dock == DockStyle.Fill || (c.Anchor & AnchorStyles.Left) == AnchorStyles.Left)
                        {
                            new_x = current_pos.X + c.Margin.Left;
                        }
                        else if (c.Dock == DockStyle.Right || (c.Anchor & AnchorStyles.Right) == AnchorStyles.Right)
                        {
                            new_x = (current_pos.X + column_width) - new_width - c.Margin.Right;
                        }
                        else                            // (center control)
                        {
                            new_x = (current_pos.X + (column_width - c.Margin.Left - c.Margin.Right) / 2) + c.Margin.Left - (new_width / 2);
                        }

                        // Figure out the top location of the control
                        if (c.Dock == DockStyle.Top || c.Dock == DockStyle.Fill || (c.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
                        {
                            new_y = current_pos.Y + c.Margin.Top;
                        }
                        else if (c.Dock == DockStyle.Bottom || (c.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom)
                        {
                            new_y = (current_pos.Y + column_height) - new_height - c.Margin.Bottom;
                        }
                        else                            // (center control)
                        {
                            new_y = (current_pos.Y + (column_height - c.Margin.Top - c.Margin.Bottom) / 2) + c.Margin.Top - (new_height / 2);
                        }

                        c.SetBounds(new_x, new_y, new_width, new_height, BoundsSpecified.None);
                    }

                    current_pos.Offset(column_widths[x] + border_width, 0);
                }

                current_pos.Offset((-1 * current_pos.X) + border_width + panel.DisplayRectangle.Left, row_heights[y] + border_width);
            }
        }
        public void TableLayoutSettingsTypeConverter_ConvertFrom_HasStylesAndControls_ReturnsExpected()
        {
            var converter = new TableLayoutSettingsTypeConverter();
            TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(converter.ConvertFrom(
                                                                                   @"<?xml version=""1.0"" encoding=""utf-16""?>
<TableLayoutSettings>
    <Controls>
        <Control Name=""simple"" />
        <Control Name=""name"" Row=""1"" RowSpan=""2"" Column=""3"" ColumnSpan=""4"" />
        <Control Name=""invalidRow"" Row=""abc"" />
        <Control Name=""invalidRowSpan"" RowSpan=""abc"" />
        <Control Name=""invalidColumn"" Column=""abc"" />
        <Control Name=""invalidColumnSpan"" ColumnSpan=""abc"" />
    </Controls>
    <Columns Styles=""AutoSize,1,Absolute,2.2"" />
    <Columns Styles=""AutoSize,1.1#2Percent!1"" />
    <Columns Styles=""Percent," + '\u0664' + @""" />
    <Rows Styles=""AutoSize,1,Absolute,2"" />
</TableLayoutSettings>"));

            Assert.Equal(5, settings.ColumnStyles.Count);
            Assert.Equal(SizeType.AutoSize, settings.ColumnStyles[0].SizeType);
            Assert.Equal(1, settings.ColumnStyles[0].Width);
            Assert.Equal(SizeType.Absolute, settings.ColumnStyles[1].SizeType);
            Assert.Equal(2.2f, settings.ColumnStyles[1].Width);
            Assert.Equal(SizeType.AutoSize, settings.ColumnStyles[2].SizeType);
            Assert.Equal(1.12f, settings.ColumnStyles[2].Width);
            Assert.Equal(SizeType.Percent, settings.ColumnStyles[3].SizeType);
            Assert.Equal(1, settings.ColumnStyles[3].Width);
            Assert.Equal(SizeType.Percent, settings.ColumnStyles[4].SizeType);
            Assert.Equal(0, settings.ColumnStyles[4].Width);

            Assert.Equal(2, settings.RowStyles.Count);
            Assert.Equal(SizeType.AutoSize, settings.RowStyles[0].SizeType);
            Assert.Equal(1, settings.RowStyles[0].Height);
            Assert.Equal(SizeType.Absolute, settings.RowStyles[1].SizeType);
            Assert.Equal(2, settings.RowStyles[1].Height);

            Assert.Equal(-1, settings.GetRow("simple"));
            Assert.Equal(1, settings.GetRowSpan("simple"));
            Assert.Equal(-1, settings.GetColumn("simple"));
            Assert.Equal(1, settings.GetColumnSpan("simple"));

            Assert.Equal(1, settings.GetRow("name"));
            Assert.Equal(2, settings.GetRowSpan("name"));
            Assert.Equal(3, settings.GetColumn("name"));
            Assert.Equal(4, settings.GetColumnSpan("name"));

            Assert.Equal(-1, settings.GetRow("invalidRow"));
            Assert.Equal(1, settings.GetRowSpan("invalidRow"));
            Assert.Equal(-1, settings.GetColumn("invalidRow"));
            Assert.Equal(1, settings.GetColumnSpan("invalidRow"));

            Assert.Equal(-1, settings.GetRow("invalidRowSpan"));
            Assert.Equal(1, settings.GetRowSpan("invalidRowSpan"));
            Assert.Equal(-1, settings.GetColumn("invalidRowSpan"));
            Assert.Equal(1, settings.GetColumnSpan("invalidRowSpan"));

            Assert.Equal(-1, settings.GetRow("invalidColumn"));
            Assert.Equal(1, settings.GetRowSpan("invalidColumn"));
            Assert.Equal(-1, settings.GetColumn("invalidColumn"));
            Assert.Equal(1, settings.GetColumnSpan("invalidColumn"));

            Assert.Equal(-1, settings.GetRow("invalidColumnSpan"));
            Assert.Equal(1, settings.GetRowSpan("invalidColumnSpan"));
            Assert.Equal(-1, settings.GetColumn("invalidColumnSpan"));
            Assert.Equal(1, settings.GetColumnSpan("invalidColumnSpan"));
        }
Ejemplo n.º 17
0
        internal Control[,] CalculateControlPositions(TableLayoutPanel panel, int columns, int rows)
        {
            Control[,] grid = new Control[columns, rows];

            TableLayoutSettings settings = panel.LayoutSettings;

            // First place all controls that have an explicit col/row
            foreach (Control c in panel.Controls)
            {
                int col = settings.GetColumn(c);
                int row = settings.GetRow(c);
                if (col >= 0 && row >= 0)
                {
                    if (col >= columns)
                    {
                        return(CalculateControlPositions(panel, col + 1, rows));
                    }
                    if (row >= rows)
                    {
                        return(CalculateControlPositions(panel, columns, row + 1));
                    }

                    if (grid[col, row] == null)
                    {
                        int col_span = Math.Min(settings.GetColumnSpan(c), columns);
                        int row_span = Math.Min(settings.GetRowSpan(c), rows);

                        if (col + col_span > columns)
                        {
                            if (row + 1 < rows)
                            {
                                grid[col, row] = dummy_control;
                                row++;
                                col = 0;
                            }
                            else if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddColumns)
                            {
                                return(CalculateControlPositions(panel, columns + 1, rows));
                            }
                            else
                            {
                                throw new ArgumentException();
                            }
                        }

                        if (row + row_span > rows)
                        {
                            if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddRows)
                            {
                                return(CalculateControlPositions(panel, columns, rows + 1));
                            }
                            else
                            {
                                throw new ArgumentException();
                            }
                        }

                        grid[col, row] = c;

                        // Fill in the rest of this control's row/column extent with dummy
                        // controls, so that other controls don't get put there.
                        for (int i = 0; i < col_span; i++)
                        {
                            for (int j = 0; j < row_span; j++)
                            {
                                if (i != 0 || j != 0)
                                {
                                    grid[col + i, row + j] = dummy_control;
                                }
                            }
                        }
                    }
                }
            }

            int x_pointer = 0;
            int y_pointer = 0;

            // Fill in gaps with controls that do not have an explicit col/row
            foreach (Control c in panel.Controls)
            {
                int col = settings.GetColumn(c);
                int row = settings.GetRow(c);

                if ((col >= 0 && col < columns) && (row >= 0 && row < rows) && (grid[col, row] == c || grid[col, row] == dummy_control))
                {
                    continue;
                }

                for (int y = y_pointer; y < rows; y++)
                {
                    y_pointer = y;
                    x_pointer = 0;

                    for (int x = x_pointer; x < columns; x++)
                    {
                        x_pointer = x;

                        if (grid[x, y] == null)
                        {
                            int col_span = Math.Min(settings.GetColumnSpan(c), columns);
                            int row_span = Math.Min(settings.GetRowSpan(c), rows);

                            if (x + col_span > columns)
                            {
                                if (y + 1 < rows)
                                {
                                    break;
                                }
                                else if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddColumns)
                                {
                                    return(CalculateControlPositions(panel, columns + 1, rows));
                                }
                                else
                                {
                                    throw new ArgumentException();
                                }
                            }

                            if (y + row_span > rows)
                            {
                                if (x + 1 < columns)
                                {
                                    break;
                                }
                                else if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddRows)
                                {
                                    return(CalculateControlPositions(panel, columns, rows + 1));
                                }
                                else
                                {
                                    throw new ArgumentException();
                                }
                            }

                            grid[x, y] = c;

                            // Fill in the rest of this control's row/column extent with dummy
                            // controls, so that other controls don't get put there.
                            for (int i = 0; i < col_span; i++)
                            {
                                for (int j = 0; j < row_span; j++)
                                {
                                    if (i != 0 || j != 0)
                                    {
                                        grid[x + i, y + j] = dummy_control;
                                    }
                                }
                            }

                            // I know someone will kill me for using a goto, but
                            // sometimes they really are the easiest way...
                            goto Found;
                        }
                        else
                        {
                            // MS adds the controls only to the first row if
                            // GrowStyle is AddColumns and RowCount is 0,
                            // so interrupt the search for a free horizontal cell
                            // beyond the first one in the given vertical
                            if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddColumns &&
                                settings.RowCount == 0)
                            {
                                break;
                            }
                        }
                    }
                }

                // MS adds rows instead of columns even when GrowStyle is AddColumns,
                // but RowCount is 0.
                TableLayoutPanelGrowStyle adjustedGrowStyle = settings.GrowStyle;
                if (settings.GrowStyle == TableLayoutPanelGrowStyle.AddColumns)
                {
                    if (settings.RowCount == 0)
                    {
                        adjustedGrowStyle = TableLayoutPanelGrowStyle.AddRows;
                    }
                }

                switch (adjustedGrowStyle)
                {
                case TableLayoutPanelGrowStyle.AddColumns:
                    return(CalculateControlPositions(panel, columns + 1, rows));

                case TableLayoutPanelGrowStyle.AddRows:
                default:
                    return(CalculateControlPositions(panel, columns, rows + 1));

                case TableLayoutPanelGrowStyle.FixedSize:
                    throw new ArgumentException();
                }

                Found :;
            }

            return(grid);
        }