private void AddRefreshElements(DomElement startElement, DomElement endElement)
        {
            DomContentElement ce  = myContainer.ContentElement;
            DomElementList    res = this.OwnerList.RefreshElements;

            if (res.Contains(startElement) == false)
            {
                res.Add(startElement);
            }
            DomContentLine startLine = null;

            while (startElement != null)
            {
                // 可能存在元素不显示在文档中,例如处于图形模式下的XTextShapeInputFieldElement中,
                // 内容就不显示,在此向上查询显示出来的内容。
                if (ce.PrivateContent.Contains(startElement))
                {
                    startLine = startElement.OwnerLine;
                    if (res.Contains(startElement) == false)
                    {
                        res.Add(startElement);
                    }
                    break;
                }
                startElement = startElement.Parent;
            }//while

            if (res.Contains(endElement) == false)
            {
                res.Add(endElement);
            }
            DomContentLine endLine = null;

            while (endElement != null)
            {
                if (ce.PrivateContent.Contains(endElement))
                {
                    endLine = endElement.OwnerLine;
                    if (res.Contains(endElement) == false)
                    {
                        res.Add(endElement);
                    }
                    break;
                }
                endElement = endElement.Parent;
            }//while

            ce.SetLinesInvalidateState(
                startLine,
                endLine);
        }
Esempio n. 2
0
        //public static bool SplitElements(XTextElementList SourceList)
        //{
        //    return SplitElements(SourceList, false);
        //    //bool result = false;
        //    //XTextElementList tempList = new XTextElementList();
        //    //tempList.AddRange(SourceList);
        //    //foreach (XTextElement element in tempList)
        //    //{
        //    //    if (element is XTextStringElement)
        //    //    {
        //    //        // 将文本元素拆分成多个字符元素
        //    //        int index = SourceList.IndexOf(element);
        //    //        XTextElementList cs = ((XTextStringElement)element).SplitChars();
        //    //        SourceList.RemoveAt(index);
        //    //        for (int iCount = 0; iCount < cs.Count; iCount++)
        //    //        {
        //    //            SourceList.Insert(index + iCount, cs[iCount]);
        //    //        }
        //    //        result = true;
        //    //    }
        //    //    else
        //    //    {
        //    //        //SourceList.Add(element);
        //    //    }
        //    //}
        //    //return result;
        //}

        /// <summary>
        /// 获得两个元素节点最近的共同的祖先元素
        /// </summary>
        /// <param name="element1">文档元素1</param>
        /// <param name="element2">文档元素2</param>
        /// <returns>共同的祖先元素</returns>
        public static DomElement GetRootElement(DomElement element1, DomElement element2)
        {
            if (element1 == null)
            {
                throw new ArgumentNullException("element1");
            }
            if (element2 == null)
            {
                throw new ArgumentNullException("element2");
            }

            if (element1 == element2)
            {
                return(element1);
            }
            DomElementList parents = new DomElementList();
            DomElement     parent  = element1;

            while (parent != null)
            {
                parents.Add(parent);
                parent = parent.Parent;
            }

            parent = element2;
            while (parent != null)
            {
                if (parents.Contains(parent))
                {
                    return(parent);
                }
                parent = parent.Parent;
            }
            return(null);
        }
Esempio n. 3
0
        /// <summary>
        /// 对字符元素进行合并操作
        /// </summary>
        /// <param name="sourceList">元素列表</param>
        /// <returns>合并后的元素列表</returns>
        public static DomElementList MergeElements(
            DomElementList sourceList,
            bool includeSelectionOnly)
        {
            DomElementList result = new DomElementList();

            if (sourceList.Count == 0)
            {
                return(result);
            }
            DomStringElement myString = null;
            //XTextParagraphList plist = null;
            DomDocumentContentElement ce = sourceList[0].DocumentContentElement;

            foreach (DomElement element in sourceList)
            {
                if (includeSelectionOnly)
                {
                    if (element.HasSelection == false)
                    {
                        continue;
                    }
                }
                if (element is DomCharElement)
                {
                    DomCharElement c = (DomCharElement)element;
                    if (myString != null && myString.CanMerge(c))
                    {
                        myString.Merge(c);
                    }
                    else
                    {
                        myString = new DomStringElement();
                        myString.OwnerDocument = c.OwnerDocument;
                        result.Add(myString);
                        myString.Merge(c);
                    }
                    continue;
                }

                myString = null;
                result.Add(element);
            }
            return(result);
        }
Esempio n. 4
0
        public static DomElementList MergeParagraphs(
            DomElementList sourceElements,
            bool includeSelectionOnly)
        {
            if (sourceElements == null || sourceElements.Count == 0)
            {
                return(null);
                //throw new ArgumentNullException("sourceElements");
            }
            DomElementList      list     = MergeElements(sourceElements, includeSelectionOnly);
            DomElementList      result   = new DomElementList();
            DomDocument         document = sourceElements[0].OwnerDocument;
            DomParagraphElement p        = new DomParagraphElement();

            p.OwnerDocument = document;
            result.Add(p);
            foreach (DomElement element in list)
            {
                DomParagraphElement paragraph = (DomParagraphElement)result[result.Count - 1];
                if (element is DomParagraphFlagElement)
                {
                    paragraph.StyleIndex = element.StyleIndex;
                    paragraph.Elements.AddRaw(element);

                    paragraph = new DomParagraphElement();
                    paragraph.OwnerDocument = document;
                    result.Add(paragraph);
                }
                else
                {
                    paragraph.Elements.AddRaw(element);
                }
            }
            DomParagraphElement lastP = (DomParagraphElement)result[result.Count - 1];

            if (lastP.Elements.Count == 0)
            {
                // 删除最后一个没有任何内容的段落对象
                result.RemoveAt(result.Count - 1);
            }
            return(result);
        }
Esempio n. 5
0
        /// <summary>
        /// 获得指定元素的父节点对象列表,在该列表中,近亲在前,远亲在后。
        /// </summary>
        /// <param name="element">文档元素对象</param>
        /// <returns>父节点对象列表</returns>
        public static DomElementList GetParentList(DomElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            DomElementList      result = new DomElementList();
            DomContainerElement parent = element.Parent;

            while (parent != null)
            {
                result.Add(parent);
                parent = parent.Parent;
            }
            return(result);
        }
Esempio n. 6
0
 /// <summary>
 /// 添加一个插入元素操作信息
 /// </summary>
 /// <param name="c">容器元素</param>
 /// <param name="index">插入的序号</param>
 /// <param name="element">插入的元素</param>
 public void AddInsertElement(DomContainerElement c, int index, DomElement element)
 {
     if (CanLog)
     {
         DomElementList list = new DomElementList();
         list.Add(element);
         XTextUndoReplaceElements undo = new XTextUndoReplaceElements(c, index, null, list);
         undo.Document = this.Document;
         undo.InGroup  = true;
         this.Add(undo);
         //XTextUndoInsertElement undo = new XTextUndoInsertElement();
         //undo.Document = this.myDocument ;
         //undo.Element = element ;
         //undo.Container = c ;
         //undo.Index = index ;
         //this.Add( undo );
     }
 }
Esempio n. 7
0
        private void Execute(XUndoEventArgs args, bool undo)
        {
            myDocument.UndoList.RefreshElements.Clear();
            myDocument.UndoList._ContentRefreshInfos.Clear();
            myDocument.UndoList.ContentChangedContainer.Clear();
            myDocument.UndoList.NeedRefreshDocument = false;
            if (undo)
            {
                base.Undo(args);
            }
            else
            {
                base.Redo(args);
            }
            if (myDocument.UndoList.NeedRefreshDocument)
            {
                // 需要刷新整个文档。
                if (this.Document.EditorControl != null)
                {
                    this.Document.EditorControl.RefreshDocument();
                    return;
                }
            }

            // 指定了要刷新的元素
            DomElementList RedrawElements = new DomElementList();

            Dictionary <DomContentElement, int> startIndexs
                = new Dictionary <DomContentElement, int>();

            if (this.Document.UndoList._ContentRefreshInfos.Count > 0)
            {
                // 直接获得要刷新排版的位置信息
                DomDocumentContentElement dce2 = null;
                foreach (DomContentElement ce in this.Document.UndoList._ContentRefreshInfos.Keys)
                {
                    dce2 = ce.DocumentContentElement;
                    ce.UpdateContentElements(false);
                    startIndexs[ce] = this.Document.UndoList._ContentRefreshInfos[ce];
                }
                dce2.UpdateContentElements(false);
            }//if

            if (this.Document.UndoList.RefreshElements.Count > 0)
            {
                // 获得要刷新排版的位置信息
                foreach (DomElement element in this.Document.UndoList.RefreshElements)
                {
                    DomContentElement ce = element.ContentElement;
                    if (startIndexs.ContainsKey(ce))
                    {
                        int index = ce.PrivateContent.IndexOf(element);
                        if (index >= 0 && index < startIndexs[ce])
                        {
                            for (int iCount = startIndexs[ce]; iCount >= index; iCount--)
                            {
                                DomElement element2 = ce.PrivateContent[iCount];
                                if (element2.OwnerLine != null)
                                {
                                    // 声明文本行无效
                                    element2.OwnerLine.InvalidateState = true;
                                }
                            }
                            startIndexs[ce] = index;
                        }
                    }
                    else
                    {
                        ce.UpdateContentElements(true);
                        int index = ce.PrivateContent.IndexOf(element.FirstContentElement);
                        if (index >= 0)
                        {
                            startIndexs[ce] = index;
                        }
                    }
                }//foreach

                using (System.Drawing.Graphics g = this.Document.CreateGraphics())
                {
                    foreach (DomElement element in this.Document.UndoList.RefreshElements)
                    {
                        DomContentElement ce = element.ContentElement;
                        int index2           = ce.PrivateContent.IndexOf(element.FirstContentElement);
                        if (index2 >= 0)
                        {
                            //element.SizeInvalid = true;
                            if (element.SizeInvalid || element.ViewInvalid)
                            {
                                RedrawElements.Add(element);
                            }
                            if (element.SizeInvalid)
                            {
                                this.Document.Render.RefreshSize(element, g);
                            }
                            //if (StartIndex == int.MinValue || StartIndex > index2)
                            //{
                            //    StartIndex = index2;
                            //}
                            //if (EndIndex == -1 || EndIndex < index2)
                            //{
                            //    EndIndex = index2;
                            //}
                        } //if
                    }     //foreach
                }         //using
            }             //if

            DomDocumentContentElement dce = this.Document.Body;

            foreach (DomContentElement ce in startIndexs.Keys)
            {
                dce = ce.DocumentContentElement;
                //ce.UpdateContentElements();
                int index = startIndexs[ce];
                if (index > 0)
                {
                    //index--;
                }
                ce.RefreshPrivateContent(index, -1, true);
            }//foreach
            if (this.Document.PageRefreshed == false)
            {
                this.Document.RefreshPages();
                if (this.Document.EditorControl != null)
                {
                    this.Document.EditorControl.UpdatePages();
                    this.Document.EditorControl.Invalidate();
                }
            }
            dce.Content.AutoClearSelection = true;
            dce.Content.LineEndFlag        = false;
            if (undo)
            {
                dce.SetSelection(intOldSelectionStart, 0);
            }
            else
            {
                dce.SetSelection(intNewSelectionStart, 0);
            }
            //myDocument.Content.MoveSelectStart( intOldSelectionStart );
            foreach (DomElement element in RedrawElements)
            {
                element.ViewInvalid = true;
                element.InvalidateView();
            }
            if (this.Document.UndoList.ContentChangedContainer.Count > 0)
            {
                // 触发文档内容修改事件
                foreach (DomContainerElement container in this.Document.UndoList.ContentChangedContainer)
                {
                    // 触发文档事件
                    ContentChangedEventArgs args2 = new ContentChangedEventArgs();
                    args2.UndoRedoCause = true;
                    args2.Document      = this.Document;
                    args2.Element       = container;
                    container.RaiseBubbleOnContentChanged(args2);
                }
                this.Document.HighlightManager.UpdateHighlightInfos();
                this.Document.OnDocumentContentChanged();
            }
            if (this.Document.EditorControl != null)
            {
                this.Document.EditorControl.UpdateTextCaret();
                this.Document.EditorControl.Update();
            }
        }
Esempio n. 8
0
        private void ReadContent(
            RTFDomElement parentNode,
            DomDocument doc,
            DomElementList result,
            DocumentFormatInfo format)
        {
            if (format == null)
            {
                format = new DocumentFormatInfo();
            }
            //if ( parentNode == null || parentNode.Elements == null)
            //{
            //    System.Console.WriteLine("");
            //    return;
            //}
            foreach (RTFDomElement element in parentNode.Elements)
            {
                if (element is RTFDomHeader)
                {
                    if (((RTFDomHeader)element).HasContentElement)
                    {
                        XTextDocumentHeaderElement header = new XTextDocumentHeaderElement();
                        doc.PageSettings.HeaderDistance = (int)(GraphicsUnitConvert.FromTwips(
                                                                    (double)this._RTFDocument.HeaderDistance,
                                                                    GraphicsUnit.Inch) * 100.0);
                        result.Add(header);
                        ReadContent(element, doc, header.Elements, format);
                    }
                }
                else if (element is RTFDomFooter)
                {
                    if (((RTFDomFooter)element).HasContentElement)
                    {
                        XTextDocumentFooterElement footer = new XTextDocumentFooterElement();
                        doc.PageSettings.FooterDistance = (int)(GraphicsUnitConvert.FromTwips(
                                                                    (double)this._RTFDocument.FooterDistance,
                                                                    GraphicsUnit.Inch) * 100.0);
                        result.Add(footer);
                        ReadContent(element, doc, footer.Elements, format);
                    }
                }
                else if (element is RTFDomParagraph)
                {
                    RTFDomParagraph domP = (RTFDomParagraph)element;
                    //XTextParagraph p = new XTextParagraph();
                    //p.OwnerDocument = doc;
                    DocumentContentStyle style = ToDocumentContentStyle(
                        domP.Format,
                        doc.DocumentGraphicsUnit);
                    ReadContent(element, doc, result, domP.Format);

                    if (domP.IsTemplateGenerated == false ||
                        this.ImportTemplateGenerateParagraph)
                    {
                        DomParagraphFlagElement eof = new DomParagraphFlagElement();
                        eof.StyleIndex = doc.ContentStyles.GetStyleIndex(style);
                        result.Add(eof);
                    }
                }
                else if (element is RTFDomText)
                {
                    RTFDomText domText = (RTFDomText)element;
                    if (domText.Format.Hidden == false &&
                        domText.Text != null &&
                        domText.Text.Length > 0)
                    {
                        DocumentContentStyle style = ToDocumentContentStyle(
                            domText.Format,
                            doc.DocumentGraphicsUnit);
                        int si = doc.ContentStyles.GetStyleIndex(style);
                        result.AddRange(doc.CreateChars(domText.Text, si));
                    }
                }
                else if (element is RTFDomImage)
                {
                    // 插入图片
                    RTFDomImage     domImg = (RTFDomImage)element;
                    DomImageElement img    = new DomImageElement();
                    img.OwnerDocument = doc;
                    img.Image         = new XImageValue(domImg.Data);
                    DocumentContentStyle style = ToDocumentContentStyle(
                        domImg.Format,
                        doc.DocumentGraphicsUnit);
                    img.StyleIndex = doc.ContentStyles.GetStyleIndex(style);
                    img.Width      = GraphicsUnitConvert.FromTwips(domImg.Width, doc.DocumentGraphicsUnit);
                    img.Height     = GraphicsUnitConvert.FromTwips(domImg.Height, doc.DocumentGraphicsUnit);
                    result.Add(img);
                }


                else if (element is RTFDomShape)
                {
                }
                else if (element is RTFDomShapeGroup)
                {
                }
                else if (element is RTFDomLineBreak)
                {
                    // 软回车
                    result.Add(new DomLineBreakElement());// doc.CreateLineBreak());
                }

                else if (element.Elements != null &&
                         element.Elements.Count > 0)
                {
                    ReadContent(element, doc, result, format.Clone());
                }
            }//foreach
        }