コード例 #1
0
        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);
        }
コード例 #2
0
        public bool Set(MSHTML.IHTMLTable table)
        {
            if (table == null)
            {
                return(false);
            }

            try
            {
                base.Set((object)table.bgColor, (object)table.borderColor);

                // have a table so extract the properties
                MSHTML.IHTMLTableCaption caption = table.caption;
                // if have a caption persist the values
                if (caption != null)
                {
                    this.CaptionText = ((MSHTML.IHTMLElement)table.caption).innerText;

                    if (caption.align != null)
                    {
                        this.CaptionAlignment = (HorizontalAlignOption)Utils.TryParseEnum(typeof(HorizontalAlignOption), caption.align, HorizontalAlignOption.Default);
                    }

                    if (caption.vAlign != null)
                    {
                        this.CaptionLocation = (VerticalAlignOption)Utils.TryParseEnum(typeof(VerticalAlignOption), caption.vAlign, VerticalAlignOption.Default);
                    }
                }
                // look at the table properties
                if (table.border != null)
                {
                    this.BorderSize = Utils.TryParseByte(table.border.ToString(), this.BorderSize);
                }

                if (table.align != null)
                {
                    this.TableAlignment = (HorizontalAlignOption)Utils.TryParseEnum(typeof(HorizontalAlignOption), table.align, HorizontalAlignOption.Default);
                }

                // define the table rows and columns
                int rows = Math.Min(table.rows.length, Byte.MaxValue);
                int cols = Math.Min(table.cols, Byte.MaxValue);
                if (cols == 0 && rows > 0)
                {
                    // cols value not set to get the maxiumn number of cells in the rows
                    foreach (MSHTML.IHTMLTableRow tableRow in table.rows)
                    {
                        cols = Math.Max(cols, (int)tableRow.cells.length);
                    }
                }
                this.TableRows    = (byte)Math.Min(rows, byte.MaxValue);
                this.TableColumns = (byte)Math.Min(cols, byte.MaxValue);

                // define the remaining table properties
                if (table.cellPadding != null)
                {
                    this.CellPadding = Utils.TryParseByte(table.cellPadding.ToString(), this.CellPadding);
                }

                if (table.cellSpacing != null)
                {
                    this.CellSpacing = Utils.TryParseByte(table.cellSpacing.ToString(), this.CellSpacing);
                }

                if (table.width != null)
                {
                    string tableWidth = table.width.ToString();

                    if (tableWidth.TrimEnd(null).EndsWith("%"))
                    {
                        this.TableWidth            = Utils.TryParseUshort(tableWidth.Remove(tableWidth.LastIndexOf("%"), 1), this.TableWidth);
                        this.TableWidthMeasurement = MeasurementOption.Percent;
                    }
                    else
                    {
                        this.TableWidth            = Utils.TryParseUshort(tableWidth, this.TableWidth);
                        this.TableWidthMeasurement = MeasurementOption.Pixel;
                    }
                }
                else
                {
                    this.TableWidth            = 0;
                    this.TableWidthMeasurement = MeasurementOption.Pixel;
                }
            }
            catch (Exception ex)
            {
                // throw an exception indicating table structure change be determined
                throw new HtmlEditorException("Unable to determine Html Table properties.", "GetTableProperties", ex);
            }

            return(true);
        }