// will insert a new row into the table // based on the current user row and insertion after private void InsertRow(bool below = true) { // see if a table selected or insertion point inside a table HtmlTable table = null; HtmlTableRow row = null; HtmlTableCell cell = null; GetTableElement(out table, out row, out cell); // process according to table being defined if (table != null && row != null) { try { using (new SelectionPreserver(GetMarkupRange())) { using (new UndoUnit(Document, "Table row insert")) { // find the existing row the user is on and perform the insertion int index = row.rowIndex + (below ? 1 : 0); HtmlTableRow insertedRow = table.insertRow(index) as HtmlTableRow; // add the new columns to the end of each row int numberCols = row.cells.length; for (int idxCol = 0; idxCol < numberCols; idxCol++) { insertedRow.insertCell(-1); } } } } catch (Exception ex) { throw new HtmlEditorException("Unable to insert a new Row", "TableinsertRow", ex); } } else { throw new HtmlEditorException("Row not currently selected within the table", "TableInsertRow"); } }
/// <summary> /// Method to insert a basic table /// Will honour the existing table if passed in /// </summary> private void ProcessTable(mshtmlTable table, HtmlTableProperty tableProperties) { try { // obtain a reference to the body node and indicate table present mshtmlDomNode bodyNode = (mshtmlDomNode)document.body; bool tableCreated = false; // ensure a table node has been defined to work with if (table == null) { // create the table and indicate it was created table = (mshtmlTable)document.createElement(TABLE_TAG); tableCreated = true; } // define the table border, width, cell padding and spacing table.border = tableProperties.BorderSize; if (tableProperties.TableWidth > 0) table.width = (tableProperties.TableWidthMeasurement == MeasurementOption.Pixel) ? string.Format("{0}", tableProperties.TableWidth) : string.Format("{0}%", tableProperties.TableWidth); else table.width = string.Empty; if (tableProperties.TableAlignment != HorizontalAlignOption.Default) table.align = tableProperties.TableAlignment.ToString().ToLower(); else table.align = string.Empty; table.cellPadding = tableProperties.CellPadding.ToString(); table.cellSpacing = tableProperties.CellSpacing.ToString(); // define the given table caption and alignment string caption = tableProperties.CaptionText; mshtmlTableCaption tableCaption = table.caption; if (caption != null && caption != string.Empty) { // ensure table caption correctly defined if (tableCaption == null) tableCaption = table.createCaption(); ((mshtmlElement)tableCaption).innerText = caption; if (tableProperties.CaptionAlignment != HorizontalAlignOption.Default) tableCaption.align = tableProperties.CaptionAlignment.ToString().ToLower(); if (tableProperties.CaptionLocation != VerticalAlignOption.Default) tableCaption.vAlign = tableProperties.CaptionLocation.ToString().ToLower(); } else { // if no caption specified remove the existing one if (tableCaption != null) { // prior to deleting the caption the contents must be cleared ((mshtmlElement)tableCaption).innerText = null; table.deleteCaption(); } } // determine the number of rows one has to insert int numberRows, numberCols; if (tableCreated) { numberRows = Math.Max((int)tableProperties.TableRows, 1); } else { numberRows = Math.Max((int)tableProperties.TableRows, 1) - (int)table.rows.length; } // layout the table structure in terms of rows and columns table.cols = (int)tableProperties.TableColumns; if (tableCreated) { // this section is an optimization based on creating a new table // the section below works but not as efficiently numberCols = Math.Max((int)tableProperties.TableColumns, 1); // insert the appropriate number of rows mshtmlTableRow tableRow; for (int idxRow = 0; idxRow < numberRows; idxRow++) { tableRow = (mshtmlTableRow)table.insertRow(-1); // add the new columns to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } } else { // if the number of rows is increasing insert the decrepency if (numberRows > 0) { // insert the appropriate number of rows for (int idxRow = 0; idxRow < numberRows; idxRow++) { table.insertRow(-1); } } else { // remove the extra rows from the table for (int idxRow = numberRows; idxRow < 0; idxRow++) { table.deleteRow(table.rows.length - 1); } } // have the rows constructed // now ensure the columns are correctly defined for each row mshtmlElementCollection rows = table.rows; foreach (mshtmlTableRow tableRow in rows) { numberCols = Math.Max((int)tableProperties.TableColumns, 1) - (int)tableRow.cells.length; if (numberCols > 0) { // add the new column to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } else { // reduce the number of cells in the given row // remove the extra rows from the table for (int idxCol = numberCols; idxCol < 0; idxCol++) { tableRow.deleteCell(tableRow.cells.length - 1); } } } } // if the table was created then it requires insertion into the DOM // otherwise property changes are sufficient if (tableCreated) { // table processing all complete so insert into the DOM mshtmlDomNode tableNode = (mshtmlDomNode)table; mshtmlElement tableElement = (mshtmlElement)table; mshtmlSelection selection = document.selection; mshtmlTextRange textRange = GetTextRange(); // final insert dependant on what user has selected if (textRange != null) { // text range selected so overwrite with a table try { string selectedText = textRange.text; if (selectedText != null) { // place selected text into first cell mshtmlTableRow tableRow = (mshtmlTableRow)table.rows.item(0, null); ((mshtmlElement)tableRow.cells.item(0, null)).innerText = selectedText; } textRange.pasteHTML(tableElement.outerHTML); } catch (Exception ex) { throw new HtmlEditorException("Invalid Text selection for the Insertion of a Table.", "ProcessTable", ex); } } else { mshtmlControlRange controlRange = GetAllControls(); if (controlRange != null) { // overwrite any controls the user has selected try { // clear the selection and insert the table // only valid if multiple selection is enabled for (int idx = 1; idx < controlRange.length; idx++) { controlRange.remove(idx); } controlRange.item(0).outerHTML = tableElement.outerHTML; // this should work with initial count set to zero // controlRange.add((mshtmlControlElement)table); } catch (Exception ex) { throw new HtmlEditorException("Cannot Delete all previously Controls selected.", "ProcessTable", ex); } } else { // insert the table at the end of the HTML bodyNode.appendChild(tableNode); } } } else { // table has been correctly defined as being the first selected item // need to remove other selected items mshtmlControlRange controlRange = GetAllControls(); if (controlRange != null) { // clear the controls selected other than than the first table // only valid if multiple selection is enabled for (int idx = 1; idx < controlRange.length; idx++) { controlRange.remove(idx); } } } } catch (Exception ex) { // throw an exception indicating table structure change error throw new HtmlEditorException("Unable to modify Html Table properties.", "ProcessTable", ex); } }
public bool Get(ref mshtml.IHTMLTable table, bool tableCreated) { if (table == null) { return(false); } // define the table border, width, cell padding and spacing object bgColor, borderColor; base.Get(out bgColor, out borderColor); table.bgColor = bgColor; table.borderColor = borderColor; if (this.TableWidth > 0) { table.width = (this.TableWidthMeasurement == MeasurementOption.Pixel) ? string.Format("{0}", this.TableWidth) : string.Format("{0}%", this.TableWidth); } else { table.width = string.Empty; } if (this.TableAlignment != HorizontalAlignOption.Default) { table.align = this.TableAlignment.ToString().ToLower(); } else { table.align = string.Empty; } table.border = this.BorderSize; table.cellPadding = this.CellPadding.ToString(); table.cellSpacing = this.CellSpacing.ToString(); // define the given table caption and alignment string caption = this.CaptionText; mshtml.IHTMLTableCaption tableCaption = table.caption; if (caption != null && caption != string.Empty) { // ensure table caption correctly defined if (tableCaption == null) { tableCaption = table.createCaption(); } ((mshtml.IHTMLElement)tableCaption).innerText = caption; if (this.CaptionAlignment != HorizontalAlignOption.Default) { tableCaption.align = this.CaptionAlignment.ToString().ToLower(); } if (this.CaptionLocation != VerticalAlignOption.Default) { tableCaption.vAlign = this.CaptionLocation.ToString().ToLower(); } } else { // if no caption specified remove the existing one if (tableCaption != null) { // prior to deleting the caption the contents must be cleared ((mshtml.IHTMLElement)tableCaption).innerText = null; table.deleteCaption(); } } // determine the number of rows one has to insert int numberRows, numberCols; if (tableCreated) { numberRows = Math.Max((int)this.TableRows, 1); } else { numberRows = Math.Max((int)this.TableRows, 1) - (int)table.rows.length; } // layout the table structure in terms of rows and columns table.cols = (int)this.TableColumns; if (tableCreated) { // this section is an optimization based on creating a new table // the section below works but not as efficiently numberCols = Math.Max((int)this.TableColumns, 1); // insert the appropriate number of rows mshtml.IHTMLTableRow tableRow; for (int idxRow = 0; idxRow < numberRows; idxRow++) { tableRow = (mshtml.IHTMLTableRow)table.insertRow(-1); // add the new columns to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } } else { // if the number of rows is increasing insert the decrepency if (numberRows > 0) { // insert the appropriate number of rows for (int idxRow = 0; idxRow < numberRows; idxRow++) { table.insertRow(-1); } } else { // remove the extra rows from the table for (int idxRow = numberRows; idxRow < 0; idxRow++) { table.deleteRow(table.rows.length - 1); } } // have the rows constructed // now ensure the columns are correctly defined for each row mshtml.IHTMLElementCollection rows = table.rows; foreach (mshtml.IHTMLTableRow tableRow in rows) { numberCols = Math.Max((int)this.TableColumns, 1) - (int)tableRow.cells.length; if (numberCols > 0) { // add the new column to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } else { // reduce the number of cells in the given row // remove the extra rows from the table for (int idxCol = numberCols; idxCol < 0; idxCol++) { tableRow.deleteCell(tableRow.cells.length - 1); } } } } return(true); }
// function to insert a basic table // will honour the existing table if passed in private void ProcessTable(HtmlTable table, HtmlTableProperty tableProperties) { try { using (new UndoUnit(Document, "Table add/modify")) { // obtain a reference to the body node and indicate table present HtmlDomNode bodyNode = (HtmlDomNode)Document.body; bool tableCreated = false; MsHtmlWrap.MarkupRange targetMarkupRange = null; // ensure a table node has been defined to work with if (table == null) { // create the table and indicate it was created table = (HtmlTable)Document.createElement("TABLE"); tableCreated = true; //markup range for selecting first cell after table creation targetMarkupRange = GetMarkupRange(); } // define the table border, width, cell padding and spacing table.border = tableProperties.BorderSize; if (tableProperties.TableWidth > 0) { table.width = (tableProperties.TableWidthMeasurement == MeasurementOption.Pixel) ? string.Format("{0}", tableProperties.TableWidth) : string.Format("{0}%", tableProperties.TableWidth); } else { table.width = string.Empty; } if (tableProperties.TableAlignment != HorizontalAlignOption.Default) { table.align = tableProperties.TableAlignment.ToString().ToLower(); } else { table.align = string.Empty; } table.cellPadding = tableProperties.CellPadding.ToString(); table.cellSpacing = tableProperties.CellSpacing.ToString(); // define the given table caption and alignment string caption = tableProperties.CaptionText; HtmlTableCaption tableCaption = table.caption; if (caption != null && caption != string.Empty) { // ensure table caption correctly defined if (tableCaption == null) { tableCaption = table.createCaption(); } ((HtmlElement)tableCaption).innerText = caption; if (tableProperties.CaptionAlignment != HorizontalAlignOption.Default) { tableCaption.align = tableProperties.CaptionAlignment.ToString().ToLower(); } if (tableProperties.CaptionLocation != VerticalAlignOption.Default) { tableCaption.vAlign = tableProperties.CaptionLocation.ToString().ToLower(); } } else { // if no caption specified remove the existing one if (tableCaption != null) { // prior to deleting the caption the contents must be cleared ((HtmlElement)tableCaption).innerText = null; table.deleteCaption(); } } // determine the number of rows one has to insert int numberRows, numberCols; if (tableCreated) { numberRows = Math.Max((int)tableProperties.TableRows, 1); } else { numberRows = Math.Max((int)tableProperties.TableRows, 1) - (int)table.rows.length; } // layout the table structure in terms of rows and columns table.cols = (int)tableProperties.TableColumns; if (tableCreated) { // this section is an optimization based on creating a new table // the section below works but not as efficiently numberCols = Math.Max((int)tableProperties.TableColumns, 1); // insert the appropriate number of rows HtmlTableRow tableRow; for (int idxRow = 0; idxRow < numberRows; idxRow++) { tableRow = table.insertRow(-1) as HtmlTableRow; // add the new columns to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } } else { // if the number of rows is increasing insert the decrepency if (numberRows > 0) { // insert the appropriate number of rows for (int idxRow = 0; idxRow < numberRows; idxRow++) { table.insertRow(-1); } } else { // remove the extra rows from the table for (int idxRow = numberRows; idxRow < 0; idxRow++) { table.deleteRow(table.rows.length - 1); } } // have the rows constructed // now ensure the columns are correctly defined for each row HtmlElementCollection rows = table.rows; foreach (HtmlTableRow tableRow in rows) { numberCols = Math.Max((int)tableProperties.TableColumns, 1) - (int)tableRow.cells.length; if (numberCols > 0) { // add the new column to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } else { // reduce the number of cells in the given row // remove the extra rows from the table for (int idxCol = numberCols; idxCol < 0; idxCol++) { tableRow.deleteCell(tableRow.cells.length - 1); } } } } // if the table was created then it requires insertion into the DOM // otherwise property changes are sufficient if (tableCreated) { // table processing all complete so insert into the DOM HtmlDomNode tableNode = (HtmlDomNode)table; HtmlElement tableElement = (HtmlElement)table; HtmlSelection selection = Document.selection; HtmlTextRange textRange = SelectionHelper.GetTextRange(Document); // final insert dependant on what user has selected if (textRange != null) { // text range selected so overwrite with a table try { string selectedText = textRange.text; if (selectedText != null) { // place selected text into first cell HtmlTableRow tableRow = table.rows.item(0, null) as HtmlTableRow; (tableRow.cells.item(0, null) as HtmlElement).innerText = selectedText; } textRange.pasteHTML(tableElement.outerHTML); } catch (Exception ex) { throw new HtmlEditorException("Invalid Text selection for the Insertion of a Table.", "ProcessTable", ex); } } else { HtmlControlRange controlRange = SelectionHelper.GetAllControls(Document); if (controlRange != null) { // overwrite any controls the user has selected try { // clear the selection and insert the table // only valid if multiple selection is enabled for (int idx = 1; idx < controlRange.length; idx++) { controlRange.remove(idx); } controlRange.item(0).outerHTML = tableElement.outerHTML; // this should work with initial count set to zero // controlRange.add((HtmlControlElement)table); } catch (Exception ex) { throw new HtmlEditorException("Cannot Delete all previously Controls selected.", "ProcessTable", ex); } } else { // insert the table at the end of the HTML bodyNode.appendChild(tableNode); } } } else { // table has been correctly defined as being the first selected item // need to remove other selected items HtmlControlRange controlRange = SelectionHelper.GetAllControls(Document); if (controlRange != null) { // clear the controls selected other than than the first table // only valid if multiple selection is enabled for (int idx = 1; idx < controlRange.length; idx++) { controlRange.remove(idx); } } } //if table created, then focus the first cell if (tableCreated) { try { HtmlElement cell = targetMarkupRange.GetFirstElement(e => e is HtmlTableCell, true); if (cell != null) { SelectCell(cell as HtmlTableCell); } } catch (Exception e) { Log.Write(e); } } } } catch (Exception ex) { // throw an exception indicating table structure change error throw new HtmlEditorException("Unable to modify Html Table properties.", "ProcessTable", ex); } } //ProcessTable