public void TableNoWide3Rows4Columns()
        {
            // Arrange
            var editor = new TestHtmlEditor();
            TableProperties tableProperties = new TableProperties();
            tableProperties.Width = new PixelPercent(0, PixelPercentUnits.Undefined);
            int rows = 3;
            int columns = 4;
            TableCreationParameters tableCreationParamters = new TableCreationParameters(rows, columns, tableProperties);

            // Act
            TableEditor.InsertTable(editor, null, tableCreationParamters);

            // Assert
            Approvals.VerifyHtml(editor.Html);
        }
示例#2
0
        public static void InsertTable(IHtmlEditor htmlEditor, IHtmlEditorComponentContext editorContext, TableCreationParameters parameters)
        {
            // note whether we are inserting into mshtml
            bool insertingIntoMshtml = editorContext != null;

            // build table html
            StringBuilder tableHtml = new StringBuilder();

            // clip the width if necessary (if inserting a table within another table cell)
            if (insertingIntoMshtml)
            {
                IHTMLElement2 parentCellBlock = editorContext.Selection.SelectedMarkupRange.Start.GetParentElement(ElementFilters.BLOCK_OR_TABLE_CELL_ELEMENTS) as IHTMLElement2;
                if (parentCellBlock is IHTMLTableCell)
                {
                    int parentCellWidth = parentCellBlock.clientWidth != 0 ? parentCellBlock.clientWidth : parentCellBlock.scrollWidth;
                    parameters.Properties.Width = Math.Min(parentCellWidth, parameters.Properties.Width);
                }
            }

            // table properties
            TableProperties properties = parameters.Properties;
            StringBuilder propertiesString = new StringBuilder();
            if (properties.Width.Units != PixelPercentUnits.Undefined)
                propertiesString.AppendFormat("width=\"{0}\"", properties.Width);
            if (properties.BorderSize != String.Empty)
                propertiesString.AppendFormat(" border=\"{0}\"", properties.BorderSize);
            if (properties.CellPadding != String.Empty)
                propertiesString.AppendFormat(" cellpadding=\"{0}\"", properties.CellPadding);
            if (properties.CellSpacing != String.Empty)
                propertiesString.AppendFormat(" cellspacing=\"{0}\"", properties.CellSpacing);

            // begin table
            tableHtml.AppendFormat("<table {0} unselectable=\"on\">\r\n", propertiesString.ToString());
            tableHtml.Append("<tbody>\r\n");

            // write cells
            string columnWidth = String.Empty;

            switch (parameters.Properties.Width.Units)
            {
                case PixelPercentUnits.Pixels:
                    int width = parameters.Properties.Width / parameters.Columns;
                    columnWidth = string.Format(" width=\"{0}\"", width);
                    break;
                case PixelPercentUnits.Percentage:
                    columnWidth = string.Format(" width=\"{0}%\"", 100 / parameters.Columns);
                    break;
            }

            for (int r = 0; r < parameters.Rows; r++)
            {
                tableHtml.Append("<tr>\r\n");

                for (int c = 0; c < parameters.Columns; c++)
                {
                    // add default alignment and width to each cell
                    string valign = " valign=\"top\""; //    (more natural/expected behavior than middle)
                    tableHtml.AppendFormat("<td {0}{1}></td>\r\n", valign, columnWidth);
                }

                tableHtml.Append("</tr>\r\n");
            }

            // end table
            tableHtml.Append("</tbody>\r\n");
            tableHtml.Append("</table>\r\n");

            // if we have an mshml selection, save it before inserting (so we can locate the table after insert)
            MarkupRange targetMarkupRange = null;
            if (insertingIntoMshtml)
            {
                targetMarkupRange = editorContext.Selection.SelectedMarkupRange.Clone();
                targetMarkupRange.Start.Gravity = _POINTER_GRAVITY.POINTER_GRAVITY_Left;
                targetMarkupRange.End.Gravity = _POINTER_GRAVITY.POINTER_GRAVITY_Right;
            }

            IDisposable insertionNotification = null;
            if (insertingIntoMshtml)
            {
                insertionNotification = new HtmlEditorControl.InitialInsertionNotify((HtmlEditorControl)editorContext);
            }

            using (insertionNotification)
            {
                // insert the table
                htmlEditor.InsertHtml(tableHtml.ToString(), false);

                // select the first cell for editing if we have an mshtml selection
                if (insertingIntoMshtml)
                {
                    IHTMLElement[] cells = targetMarkupRange.GetElements(ElementFilters.TABLE_CELL_ELEMENT, true);
                    if (cells.Length > 0)
                    {
                        SelectCell(editorContext, cells[0] as IHTMLTableCell);
                    }
                }
            }
        }
        public TableCreationParameters CreateTable(IWin32Window owner)
        {
            // populate the form
            TableCreationParameters creationParameters = CreateDefaultParameters();
            numericTextBoxRows.Text = creationParameters.Rows.ToString(CultureInfo.CurrentCulture);
            numericTextBoxColumns.Text = creationParameters.Columns.ToString(CultureInfo.CurrentCulture);

            var width = creationParameters.Properties.Width;
            columnWidthControl.Text = width.ToString(CultureInfo.CurrentCulture);
            InitializeFormProperties(creationParameters.Properties);

            // show the dialog
            if (ShowDialog(owner) == DialogResult.OK)
            {
                // read input
                TableCreationParameters parameters = new TableCreationParameters(
                    int.Parse(numericTextBoxRows.Text, CultureInfo.CurrentCulture),
                    int.Parse(numericTextBoxColumns.Text, CultureInfo.CurrentCulture),
                    ReadFormProperties());

                // save as default for new tables
                SaveDefaultTableCreationParameters(parameters);

                // return
                return parameters;
            }
            else
            {
                return null;
            }
        }
示例#4
0
 public static void InsertTable(IHtmlEditor htmlEditor, TableCreationParameters parameters)
 {
     InsertTable(htmlEditor, null, parameters);
 }
 private void SaveDefaultTableCreationParameters(TableCreationParameters parameters)
 {
     TableEditingSettings.DefaultRows = parameters.Rows;
     TableEditingSettings.DefaultColumns = parameters.Columns;
     TableEditingSettings.DefaultCellPadding = parameters.Properties.CellPadding;
     TableEditingSettings.DefaultCellSpacing = parameters.Properties.CellSpacing;
     TableEditingSettings.DefaultBorderSize = parameters.Properties.BorderSize;
     TableEditingSettings.DefaultWidth = parameters.Properties.Width;
 }