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

            settings.SetRow(control, 1);
            Assert.Equal(1, settings.GetRow(control));

            settings.SetRow(control, 2);
            Assert.Equal(2, settings.GetRow(control));
        }
        public void TableLayoutSettings_SetRow_ValidControl_GetReturnsExpected(TableLayoutSettings settings, int value)
        {
            var control = new ScrollableControl();

            settings.SetRow(control, value);
            Assert.Equal(value, settings.GetRow(control));
        }
        public void TableLayoutSettings_GetRow_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.GetRow("control"));
        }
        public void TableLayoutSettings_GetRow_InvalidControl_ThrowsNotSupportedException()
        {
            var toolStrip = new ToolStrip {
                LayoutStyle = ToolStripLayoutStyle.Table
            };
            TableLayoutSettings settings = Assert.IsType <TableLayoutSettings>(toolStrip.LayoutSettings);

            Assert.Throws <NotSupportedException>(() => settings.GetRow("control"));
        }
        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 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"));
        }
 public void TableLayoutSettings_GetRow_NullControl_ThrowsArgumentNullException(TableLayoutSettings settings)
 {
     Assert.Throws <ArgumentNullException>("control", () => settings.GetRow(null));
 }
        public void TableLayoutSettings_GetRow_NoSuchControl_ReturnsExpected(TableLayoutSettings settings)
        {
            var control = new ScrollableControl();

            Assert.Equal(-1, settings.GetRow(control));
        }
Example #9
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);
        }