Example #1
0
        /// <summary>
        /// 设置文档内容的样式
        /// </summary>
        /// <param name="newStyle">新样式</param>
        /// <param name="logUndo">是否记录撤销操作信息</param>
        public virtual void EditorSetContentStyle(DocumentContentStyle newStyle, bool logUndo)
        {
            if (newStyle == null)
            {
                throw new ArgumentNullException("newStyle");
            }
            DomElementList list = new DomElementList();

            list.Add(this);
            this.AppendContent(list, true);
            if (logUndo)
            {
                this.OwnerDocument.BeginLogUndo();
            }
            bool result = DomSelection.SetElementStyle(newStyle, this.OwnerDocument, list);

            if (logUndo)
            {
                this.OwnerDocument.EndLogUndo();
            }
            if (result)
            {
                this.OwnerDocument.EditorCurrentStyle = null;
                this.OwnerDocument.OnSelectionChanged();
                this.OwnerDocument.OnDocumentContentChanged();
            }
        }
Example #2
0
        /// <summary>
        /// 全局替换
        /// </summary>
        /// <param name="args">参数</param>
        /// <returns>替换的次数</returns>
        public int ReplaceAll(SearchReplaceCommandArgs args)
        {
            int        result = 0;
            List <int> indexs = new List <int>();
            SearchReplaceCommandArgs args2 = args.Clone();

            args2.Backward = false;
            int currentIndex = this.Content.Count - 1;

            this.Document.BeginLogUndo();
            DocumentControler controler = this.Document.DocumentControler;
            Dictionary <DomContentElement, int> startIndexs = new Dictionary <DomContentElement, int>();

            while (true)
            {
                int index = Search(args2, false, currentIndex);
                if (index >= 0)
                {
                    DomSelection mySelection = new DomSelection(this.Document.CurrentContentElement);
                    mySelection.Refresh(index, args.SearchString.Length);
                    DomContainerElement container = null;
                    int elementIndex = 0;
                    this.Content.GetPositonInfo(index, out container, out elementIndex, false);
                    DomContentElement contentElement = container.ContentElement;
                    int pi = contentElement.PrivateContent.IndexOf(this.Content[index]);
                    if (startIndexs.ContainsKey(contentElement))
                    {
                        startIndexs[contentElement] = Math.Min(startIndexs[contentElement], pi);
                    }
                    else
                    {
                        startIndexs[contentElement] = pi;
                    }
                    indexs.Add(index);
                    if (string.IsNullOrEmpty(args.ReplaceString))
                    {
                        this.Content.DeleteSelection(true, false, true, mySelection);
                    }
                    else
                    {
                        DomElementList newElements = this.Document.CreateTextElements(
                            args.ReplaceString,
                            (DocumentContentStyle)this.Document.CurrentParagraphStyle,
                            (DocumentContentStyle)this.Document.EditorCurrentStyle.Clone());
                        ReplaceElementsArgs args3 = new ReplaceElementsArgs(
                            container,
                            index,
                            0,
                            newElements,
                            true,
                            false,
                            true);
                        int repResult = this.Document.ReplaceElements(args3);
                    }
                    result++;
                }
                else
                {
                    break;
                }
                currentIndex = index + args2.SearchString.Length;
            }//while
            this.Document.EndLogUndo();
            if (startIndexs.Count > 0)
            {
                bool refreshPage = false;
                foreach (DomContentElement ce in startIndexs.Keys)
                {
                    ce.UpdateContentElements(true);
                    ce.UpdateContentVersion();
                    ce._NeedRefreshPage = false;
                    ce.RefreshPrivateContent(startIndexs[ce]);
                    if (ce._NeedRefreshPage)
                    {
                        refreshPage = true;
                    }
                }
                if (refreshPage)
                {
                    this.Document.RefreshPages();
                    if (this.Document.EditorControl != null)
                    {
                        this.Document.EditorControl.UpdatePages();
                        this.Document.EditorControl.UpdateTextCaret();
                        this.Document.EditorControl.Invalidate();
                    }
                }
            }
            return(startIndexs.Count);
        }