/// <include file='doc\LinkConverter.uex' path='docs/doc[@for="LinkConverter.ConvertFrom"]/*' /> /// <devdoc> /// Converts the given object to the converter's native type. /// </devdoc> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { XmlDocument tableLayoutSettingsXml = new XmlDocument(); tableLayoutSettingsXml.LoadXml(value as string); TableLayoutSettings settings = new TableLayoutSettings(); ParseControls(settings, tableLayoutSettingsXml.GetElementsByTagName("Control")); ParseStyles(settings, tableLayoutSettingsXml.GetElementsByTagName("Columns"), /*isColumn=*/true); ParseStyles(settings, tableLayoutSettingsXml.GetElementsByTagName("Rows"), /*isColumn=*/false); return settings; } return base.ConvertFrom(context, culture, value); }
public void TableLayoutSettings_SetRow_InvalidRow_ThrowsArgumentOutOfRangeException(TableLayoutSettings settings) { Assert.Throws <ArgumentOutOfRangeException>("row", () => settings.SetRow("control", -2)); }
public void TableLayoutSettings_SetRow_NullControl_ThrowsArgumentNullException(TableLayoutSettings settings) { Assert.Throws <ArgumentNullException>("control", () => settings.SetRow(null, 1)); }
public void TableLayoutSettings_GetRow_NoSuchControl_ReturnsExpected(TableLayoutSettings settings) { var control = new ScrollableControl(); Assert.Equal(-1, settings.GetRow(control)); }
public void TableLayoutSettings_SetColumnSpan_MultipleTimes_GetReturnsExpected(TableLayoutSettings settings) { var control = new ScrollableControl(); settings.SetColumnSpan(control, 1); Assert.Equal(1, settings.GetColumnSpan(control)); settings.SetColumnSpan(control, 2); Assert.Equal(2, settings.GetColumnSpan(control)); }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (!(value is TableLayoutSettings) || destinationType != typeof(string)) { return(base.ConvertTo(context, culture, value, destinationType)); } TableLayoutSettings settings = value as TableLayoutSettings; StringWriter sw = new StringWriter(); XmlTextWriter xw = new XmlTextWriter(sw); xw.WriteStartDocument(); List <ControlInfo> list = settings.GetControls(); xw.WriteStartElement("TableLayoutSettings"); xw.WriteStartElement("Controls"); foreach (ControlInfo info in list) { xw.WriteStartElement("Control"); xw.WriteAttributeString("Name", info.Control.ToString()); xw.WriteAttributeString("Row", info.Row.ToString()); xw.WriteAttributeString("RowSpan", info.RowSpan.ToString()); xw.WriteAttributeString("Column", info.Col.ToString()); xw.WriteAttributeString("ColumnSpan", info.ColSpan.ToString()); xw.WriteEndElement(); } xw.WriteEndElement(); List <string> styles = new List <string> (); foreach (ColumnStyle style in settings.ColumnStyles) { styles.Add(style.SizeType.ToString()); styles.Add(style.Width.ToString(CultureInfo.InvariantCulture)); } xw.WriteStartElement("Columns"); xw.WriteAttributeString("Styles", String.Join(",", styles.ToArray())); xw.WriteEndElement(); styles.Clear(); foreach (RowStyle style in settings.RowStyles) { styles.Add(style.SizeType.ToString()); styles.Add(style.Height.ToString(CultureInfo.InvariantCulture)); } xw.WriteStartElement("Rows"); xw.WriteAttributeString("Styles", String.Join(",", styles.ToArray())); xw.WriteEndElement(); xw.WriteEndElement(); xw.WriteEndDocument(); xw.Close(); return(sw.ToString()); }
private int ParseControl (XmlDocument xmldoc, TableLayoutSettings settings) { int count = 0; foreach (XmlNode node in xmldoc.GetElementsByTagName ("Control")) { if (node.Attributes["Name"] == null || string.IsNullOrEmpty(node.Attributes["Name"].Value)) continue; if (node.Attributes["Row"] != null) { settings.SetRow (node.Attributes["Name"].Value, GetValue (node.Attributes["Row"].Value)); count++; } if (node.Attributes["RowSpan"] != null) { settings.SetRowSpan (node.Attributes["Name"].Value, GetValue (node.Attributes["RowSpan"].Value)); } if (node.Attributes["Column"] != null) settings.SetColumn (node.Attributes["Name"].Value, GetValue (node.Attributes["Column"].Value)); if (node.Attributes["ColumnSpan"] != null) settings.SetColumnSpan (node.Attributes["Name"].Value, GetValue (node.Attributes["ColumnSpan"].Value)); } return count; }
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) { if (!c.VisibleInternal || c == dummy_control) { continue; } 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) { if (!c.VisibleInternal || c == dummy_control) { continue; } 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); }
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")); }
private static 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, false); // Calculate available height int available_height = size.Height - (border_width * (rows + 1)); foreach (int height in row_heights) { available_height -= height; } // Shrink the table vertically by shrinking it's rows, if necessary if (boundBySize && size.Height > 0 && available_height < 0) { // Calculate the minimum heights for each column int[] row_min_heights = new int[row_heights.Length]; CalculateRowHeights(settings, actual_positions, max_rowspan, settings.RowStyles, auto_size, column_widths, row_heights, true); available_height += Shrink(row_heights, row_min_heights, -available_height, max_rowspan); } // 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; } }
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 && c.VisibleInternal) { Size preferred; int new_x = 0; int new_y = 0; int new_width = 0; int new_height = 0; int column_width = panel.column_widths[x]; for (int i = 1; i < Math.Min(settings.GetColumnSpan(c), panel.column_widths.Length - x); i++) { column_width += panel.column_widths[x + i]; } int column_height = panel.row_heights[y]; for (int i = 1; i < Math.Min(settings.GetRowSpan(c), panel.row_heights.Length - y); i++) { column_height += panel.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.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); } }
private static void CalculateRowHeights(TableLayoutSettings settings, Control[,] actual_positions, int max_rowspan, TableLayoutRowStyleCollection row_styles, bool auto_size, int[] column_widths, int[] row_heights, bool minimum_sizes) { int columns = actual_positions.GetLength(0); float total_percent = 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; } else if (rs.SizeType == SizeType.Percent) { total_percent += 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_styles.Count - rowspan && index < row_heights.Length - rowspan; ++index) { RowStyle rs = row_styles[index]; 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. if (!minimum_sizes || c.AutoSizeInternal) { max_height = Math.Max(max_height, GetControlSize(c, new Size(current_width - c.Margin.Horizontal, 0)).Height + c.Margin.Vertical); } else { max_height = c.MinimumSize.Height; } } } 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; } } } } for (index = 0; index < row_styles.Count && index < column_widths.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 * (100f / total_percent))); } } }
private static void CalculateColumnWidths(TableLayoutSettings settings, Control[,] actual_positions, int max_colspan, TableLayoutColumnStyleCollection col_styles, bool auto_size, int[] column_widths, bool minimum_sizes) { Size proposedSize = minimum_sizes ? new Size(1, 0) : Size.Empty; int rows = actual_positions.GetLength(1); float total_percent = 0; float max_percent_size = 0; // First assign all the Absolute sized columns int index = 0; foreach (ColumnStyle cs in col_styles) { if (index >= column_widths.Length) { break; } if (cs.SizeType == SizeType.Absolute) { column_widths[index] = (int)cs.Width; } else if (cs.SizeType == SizeType.Percent) { total_percent += cs.Width; } index++; } while (index < column_widths.Length) { column_widths[index] = 0; 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 < column_widths.Length - colspan; ++index) { ColumnStyle cs = col_styles[index]; if (cs.SizeType == SizeType.AutoSize || (auto_size && cs.SizeType == SizeType.Percent)) { int max_width = column_widths[index]; // Find the widest control in the column for (int i = 0; i < rows; i++) { Control c = 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 (cs.SizeType == SizeType.Percent && minimum_sizes) { max_width = Math.Max(max_width, c.MinimumSize.Width + c.Margin.Horizontal); } else { max_width = Math.Max(max_width, GetControlSize(c, proposedSize).Width + c.Margin.Horizontal); } } } if (cs.SizeType == SizeType.Percent) { max_percent_size = Math.Max(max_percent_size, max_width / cs.Width); } // Subtract the width of prior columns, if any. for (int i = Math.Max(index - colspan, 0); i < index; ++i) { max_width -= column_widths[i]; } // If necessary, increase this column's width. if (max_width > column_widths[index]) { column_widths[index] = max_width; } } } } for (index = 0; index < col_styles.Count && index < column_widths.Length; ++index) { ColumnStyle cs = col_styles[index]; if (cs.SizeType == SizeType.Percent) { column_widths[index] = Math.Max(column_widths[index], (int)Math.Ceiling(max_percent_size * cs.Width * (100f / total_percent))); } } }
public ColorTable() { this.SuspendLayout(); // Set yellow as default highlight color. selectedColor = Color.Yellow; this.LayoutStyle = ToolStripLayoutStyle.Table; TableLayoutSettings layout = (TableLayoutSettings)this.LayoutSettings; layout.ColumnCount = 8; layout.RowCount = 5; // // Highlight color values used here have been shamelessly // copied from microsoft word's Highlight color values. Color[] colors = new Color[] { Black, Brown, OliveGreen, DarkGreen, DarkTeal, DarkBlue, Indigo, Gray80, DarkRed, Orange, DarkYellow, Green, Teal, Blue, BlueGray, Gray50, Red, LightOrange, Lime, SeaGreen, Aqua, LightBlue, Violet, Gray40, Pink, Gold, Yellow, BrightGreen, Turquoise, SkyBlue, Plum, Gray25, Rose, Tan, LightYellow, LightGreen, LightTurquoise, PaleBlue, Lavender, White }; // ToolTipText used. Same text as MS Word, what a coincidence! string[] colorNames = new string[] { "Black", "Brown", "OliveGreen", "DarkGreen", "DarkTeal", "DarkBlue", "Indigo", "Gray80", "DarkRed", "Orange", "DarkYellow", "Green", "Teal", "Blue", "BlueGray", "Gray50", "Red", "LightOrange", "Lime", "SeaGreen", "Aqua", "LightBlue", "Violet", "Gray40", "Pink", "Gold", "Yellow", "BrightGreen", "Turquoise", "SkyBlue", "Plum", "Gray25", "Rose", "Tan", "LightYellow", "LightGreen", "LightTurquoise", "PaleBlue", "Lavender", "White" }; // Set rectangle and padding values so that // when an item is selected the selection // frame highlights the color "square" with // even spacing around it. Rectangle rc = new Rectangle(1, 1, 11, 11); Padding itemPadding = new Padding(2, 1, 2, 1); // To get selection frame perfectly centered // The size of the bitmap image to draw on is // 13 pixels wide and 12 pixels high. int bmWidth = 13; int bmHeight = 12; // Add the Fourteen colors to the dropdown. for (int i = 0; i < colors.Length; i++) { Bitmap bm = new Bitmap(bmWidth, bmHeight); using (Graphics g = Graphics.FromImage(bm)) { // g.Clear(colors[i]); g.FillRectangle(new SolidBrush(colors[i]), rc); g.DrawRectangle(Pens.Gray, 1, 0, 11, 11); } ToolStripMenuItem item = (new ToolStripMenuItem(bm)); this.Items.Add(item); item.Padding = itemPadding; item.ImageScaling = ToolStripItemImageScaling.None; item.ImageAlign = ContentAlignment.MiddleCenter; item.DisplayStyle = ToolStripItemDisplayStyle.Image; item.ToolTipText = colorNames[i]; item.MouseDown += color_MouseDown; item.Tag = colors[i]; this.Opening += ColorTable_Opening; } // Select yellow item as default selected color. this.Items[0].Select(); // Also add an option to clear existing highlighting // back to default/no highlighting. ToolStripMenuItem noColor = new ToolStripMenuItem("None"); this.Items.Add(noColor); layout.SetCellPosition(noColor, new TableLayoutPanelCellPosition(0, 0)); layout.SetColumnSpan(noColor, layout.ColumnCount); // The color white is used to indicate "No Highlight". Bitmap bmp = new Bitmap(1, 1); using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.White); } noColor.Image = bmp; noColor.Tag = Color.White; noColor.DisplayStyle = ToolStripItemDisplayStyle.Text; noColor.Dock = DockStyle.Fill; noColor.ToolTipText = "No Highlight"; noColor.MouseDown += color_MouseDown; this.ResumeLayout(); }
private void ParseControls(TableLayoutSettings settings, XmlNodeList controlXmlFragments) { foreach (XmlNode controlXmlNode in controlXmlFragments) { string name = GetAttributeValue(controlXmlNode, "Name"); if (!string.IsNullOrEmpty(name)) { int row = GetAttributeValue(controlXmlNode, "Row", /*default*/-1); int rowSpan = GetAttributeValue(controlXmlNode, "RowSpan", /*default*/1); int column = GetAttributeValue(controlXmlNode, "Column", /*default*/-1); int columnSpan = GetAttributeValue(controlXmlNode, "ColumnSpan",/*default*/1); settings.SetRow(name, row); settings.SetColumn(name, column); settings.SetRowSpan(name, rowSpan); settings.SetColumnSpan(name, columnSpan); } } }
public void TableLayoutSettings_RoundTripAndExchangeWithNet() { string netBlob; using (var tableLayoutPanel = new TableLayoutPanel { Name = "table", CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble, ColumnCount = 3, RowCount = 2 }) { tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50)); tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50)); tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50)); tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 20)); tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30)); var button00_10 = new Button() { Name = "button00_10" }; tableLayoutPanel.Controls.Add(button00_10, 0, 0); tableLayoutPanel.SetColumnSpan(button00_10, 2); var label20_21 = new Label() { Name = "label20_21" }; tableLayoutPanel.Controls.Add(label20_21, 2, 0); tableLayoutPanel.SetRowSpan(label20_21, 2); tableLayoutPanel.Controls.Add(new RadioButton() { Name = "radioButton01" }, 0, 1); tableLayoutPanel.Controls.Add(new CheckBox() { Name = "checkBox11" }, 1, 1); netBlob = BinarySerialization.ToBase64String(tableLayoutPanel.LayoutSettings as TableLayoutSettings); } // ensure we can deserialise NET serialised data and continue to match the payload ValidateResult(netBlob); // ensure we can deserialise NET Fx serialised data and continue to match the payload ValidateResult(ClassicTableLayoutSettings); void ValidateResult(string blob) { TableLayoutSettings result = BinarySerialization.EnsureDeserialize <TableLayoutSettings>(blob); Assert.NotNull(result); Assert.True(result.IsStub); // This class is not associated with an owner control. Assert.Equal(TableLayoutPanelCellBorderStyle.None, result.CellBorderStyle); // This property is not serialized. Assert.NotNull(result.LayoutEngine); // These values will be accessible when the owner is set. Assert.Throws <NullReferenceException>(() => result.CellBorderWidth); Assert.Throws <NullReferenceException>(() => result.ColumnCount); Assert.Throws <NullReferenceException>(() => result.GrowStyle); Assert.Throws <NullReferenceException>(() => result.RowCount); Assert.Equal(3, result.ColumnStyles.Count); Assert.Equal(2, result.RowStyles.Count); Assert.Equal(SizeType.Percent, result.ColumnStyles[0].SizeType); Assert.Equal(SizeType.Percent, result.ColumnStyles[1].SizeType); Assert.Equal(SizeType.Percent, result.ColumnStyles[2].SizeType); Assert.Equal(SizeType.Absolute, result.RowStyles[0].SizeType); Assert.Equal(20, result.RowStyles[0].Height); Assert.Equal(30, result.RowStyles[1].Height); List <ControlInformation> controls = result.GetControlsInformation(); ValidateControlInformation("button00_10", 0, 0, 2, 1, controls[0]); ValidateControlInformation("label20_21", 2, 0, 1, 2, controls[1]); ValidateControlInformation("radioButton01", 0, 1, 1, 1, controls[2]); ValidateControlInformation("checkBox11", 1, 1, 1, 1, controls[3]); void ValidateControlInformation(string name, int column, int row, int columnSpan, int rowSpan, ControlInformation control) { Assert.Equal(name, control.Name); Assert.Equal(column, control.Column); Assert.Equal(row, control.Row); Assert.Equal(columnSpan, control.ColumnSpan); Assert.Equal(rowSpan, control.RowSpan); } } }
private void ParseStyles(TableLayoutSettings settings, XmlNodeList controlXmlFragments, bool columns) { foreach (XmlNode styleXmlNode in controlXmlFragments) { string styleString = GetAttributeValue(styleXmlNode, "Styles"); Type sizeTypeType = typeof(SizeType); // styleString will consist of N Column/Row styles serialized in the following format // (Percent | Absolute | AutoSize), (24 | 24.4 | 24,4) // Two examples: // Percent,23.3,Percent,46.7,Percent,30 // Percent,23,3,Percent,46,7,Percent,30 // Note we get either . or , based on the culture the TableLayoutSettings were serialized in if (!string.IsNullOrEmpty(styleString)) { int currentIndex = 0; int nextIndex; while (currentIndex < styleString.Length) { // ---- SizeType Parsing ----------------- nextIndex = currentIndex; while (Char.IsLetter(styleString[nextIndex])) { nextIndex++; } SizeType type = (SizeType)Enum.Parse(sizeTypeType, styleString.Substring(currentIndex, nextIndex - currentIndex), true); // ----- Float Parsing -------------- // Find the next Digit (start of the float) while (!char.IsDigit(styleString[nextIndex])) { nextIndex++; } // Append digits left of the decimal delimiter(s) StringBuilder floatStringBuilder = new StringBuilder(); while ((nextIndex < styleString.Length) && (char.IsDigit(styleString[nextIndex]))) { floatStringBuilder.Append(styleString[nextIndex]); nextIndex++; } // Append culture invariant delimiter floatStringBuilder.Append('.'); // Append digits right of the decimal delimiter(s) while ((nextIndex < styleString.Length) && (!char.IsLetter(styleString[nextIndex]))) { if (char.IsDigit(styleString[nextIndex])) { floatStringBuilder.Append(styleString[nextIndex]); } nextIndex++; } string floatString = floatStringBuilder.ToString(); float width; if (!float.TryParse(floatString, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out width)) { Debug.Fail(string.Format(CultureInfo.CurrentCulture, "Failed to parse float for style: {0}", floatString)); width = 0F; } // Add new Column/Row Style if (columns) { settings.ColumnStyles.Add(new ColumnStyle(type, width)); } else { settings.RowStyles.Add(new RowStyle(type, width)); } // Go to the next Column/Row Style currentIndex = nextIndex; } } } }
public void TableLayoutSettings_GetColumnSpan_NullControl_ThrowsArgumentNullException(TableLayoutSettings settings) { Assert.Throws <ArgumentNullException>("control", () => settings.GetColumnSpan(null)); }
public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) { if (!(value is string)) return base.ConvertFrom(context, culture, value); XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml (value as string); TableLayoutSettings settings = new TableLayoutSettings(null); int count = ParseControl (xmldoc, settings); ParseColumnStyle (xmldoc, settings); ParseRowStyle (xmldoc, settings); settings.RowCount = count; return settings; }
public void TableLayoutSettings_SetColumnSpan_ValidControl_GetReturnsExpected(TableLayoutSettings settings, int value) { var control = new ScrollableControl(); settings.SetColumnSpan(control, value); Assert.Equal(value, settings.GetColumnSpan(control)); }
private void ParseRowStyle (XmlDocument xmldoc, TableLayoutSettings settings) { foreach (XmlNode node in xmldoc.GetElementsByTagName ("Rows")) { if (node.Attributes["Styles"] == null) continue; string styles = node.Attributes["Styles"].Value; if (string.IsNullOrEmpty(styles)) continue; string[] list = BuggySplit (styles); for (int i = 0; i < list.Length; i += 2) { float height = 0f; SizeType type = (SizeType) Enum.Parse (typeof (SizeType), list[i]); float.TryParse (list[i + 1], NumberStyles.Float, CultureInfo.InvariantCulture, out height); settings.RowStyles.Add (new RowStyle (type, height)); } } }
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) { 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 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) { panel.row_heights[row_styles.Count - 1] += total_height; } }