/// <summary> /// 更新文档内容元素列表 /// </summary> public override void UpdateContentElements(bool updateParentContentElement) { int count = this.Elements.Count; _ContentElements = null; if (_Content == null) { _Content = new DomContent(); } _Content._DocumentContentElement = this; _Content._OwnerDocument = this.OwnerDocument; _Content.Clear(); //this.Selection.Refresh(0, 0); //if (this.Elements.Count != count) System.Windows.Forms.MessageBox.Show("zzz-1"); base.UpdateContentElements(updateParentContentElement); //if (this.Elements.Count != count) System.Windows.Forms.MessageBox.Show("zzz-2"); this.OwnerDocument.HighlightManager.UpdateHighlightInfos(); }
/// <summary> /// 执行查找 /// </summary> /// <param name="args">参数</param> /// <param name="setSelection">找到后是否设置文档选择状态</param> /// <returns>找到的元素在文档中的序号</returns> public int Search(SearchReplaceCommandArgs args, bool setSelection, int startIndex) { if (args == null) { throw new ArgumentNullException("args"); } if (string.IsNullOrEmpty(args.SearchString)) { return(-1); } DomContent content = this.Content; int searchStringLength = args.SearchString.Length; int newStartIndex = -1; if (args.Backward) { // 向后查找 if (startIndex < 0) { startIndex = content.Selection.AbsEndIndex; } int contentLength = content.Count; for (int iCount = startIndex; iCount < contentLength; iCount++) { DomElement element = content[iCount]; if (element is DomCharElement) { char c = ((DomCharElement)element).CharValue; if (EqualsChar(c, args.SearchString[0], args.IgnoreCase)) { if (searchStringLength == 1) { newStartIndex = iCount; break; } else if (contentLength - iCount >= searchStringLength) { for (int iCount2 = 1; iCount2 < searchStringLength; iCount2++) { DomElement element2 = content[iCount + iCount2]; if (element2 is DomCharElement) { if (EqualsChar( ((DomCharElement)element2).CharValue, args.SearchString[iCount2], args.IgnoreCase)) { if (iCount2 == searchStringLength - 1) { newStartIndex = iCount; goto EndSetNewStartIndex; } } else { // 判断失败 break; } } else { // 不是字符元素,判断失败 break; } } //for } //else if } } //if } //for } //if else { // 向前查找 if (startIndex < 0) { startIndex = content.Selection.AbsStartIndex; } char lastChar = args.SearchString[args.SearchString.Length - 1]; for (int iCount = startIndex; iCount >= 0; iCount--) { DomElement element = content[iCount]; if (element is DomCharElement) { char c = ((DomCharElement)element).CharValue; if (EqualsChar(c, lastChar, args.IgnoreCase)) { if (searchStringLength == 1) { newStartIndex = iCount; break; } else if (iCount >= searchStringLength - 1) { for (int iCount2 = searchStringLength - 2; iCount2 >= 0; iCount2--) { DomElement element2 = content[iCount - searchStringLength + iCount2 + 1]; if (element2 is DomCharElement) { if (EqualsChar( ((DomCharElement)element2).CharValue, args.SearchString[iCount2], args.IgnoreCase)) { if (iCount2 == 0) { newStartIndex = iCount - searchStringLength + 1; goto EndSetNewStartIndex; } } else { break; } } else { break; } } } } } }//for } EndSetNewStartIndex :; if (setSelection) { if (newStartIndex >= 0) { content.SetSelection(newStartIndex, args.SearchString.Length); } } return(newStartIndex); }
/// <summary> /// 更新文内容选择状态 /// </summary> /// <param name="startIndex">选择区域的起始位置</param> /// <param name="length">选择区域的包含文档内容元素的个数</param> /// <param name="raiseEvent">是否触发文档事件</param> /// <returns>成功的改变了选择区域状态</returns> internal bool Refresh(int startIndex, int length) { if (_NativeStartIndex == startIndex && _NativeLength == length && this.DocumentContent.OwnerDocument.ContentVersion == _ContentVersion && this.Content.LineEndFlag == _LineEndFlag) { return(false); } if (startIndex == 0 || startIndex + length == 0) { System.Console.Write(""); } if (startIndex < 0) { System.Console.WriteLine(""); throw new ArgumentOutOfRangeException("startIndex=" + startIndex); } _SelectionVersion++; this.Document.EditorCurrentStyle = null; //if (startIndex == 872) //{ // System.Console.WriteLine(""); //} // 备份原先被选择的元素的列表 DomElementList contentElementsBack = _ContentElements.Clone(); _ContentElements = new DomElementList(); _ContentVersion = this.DocumentContent.OwnerDocument.ContentVersion; if (startIndex == 0 && length == 0) { _NativeStartIndex = 0; _NativeLength = 0; _StartIndex = 0; _Length = 0; _LineEndFlag = this.Content.LineEndFlag; this._Mode = ContentRangeMode.Content; _ContentElements = new DomElementList(); //return true; } else { _NativeStartIndex = startIndex; _NativeLength = length; _LineEndFlag = this.Content.LineEndFlag; DomContent content = this.Content; _Mode = ContentRangeMode.Content; // 选择区域经过的单元格列表 DomElementList spanElements = new DomElementList(); // 所经过的单元格个数 int cellCount = 0; //XTextElementList spanCells = new XTextElementList(); // 是否有不包含在表格单元格中的文档内容元素 bool hasContentElement = false; int absStartIndex = length > 0 ? startIndex : startIndex + length; int absLength = Math.Abs(length); int lengthFix = 1; if (content.LineEndFlag && length < 0) { lengthFix = 0; } // 遍历所有的文档元素,查找所经过的单元格 for (int iCount = 0; iCount < absLength + lengthFix; iCount++) { if (absStartIndex + iCount >= content.Count) { break; //System.Console.Write(""); } DomElement element = content[absStartIndex + iCount]; if (element.Parent == this.DocumentContent) { spanElements.Add(element); hasContentElement = true; } else { spanElements.Add(element); hasContentElement = true; } }//for // 首先判断内容模式 _Mode = ContentRangeMode.Content; // 选择纯文档内容 _ContentElements = content.GetRange(absStartIndex, absLength); _StartIndex = startIndex; _Length = length; if (length == 0 || _ContentElements.Count == 0) { _StartIndex = startIndex; _Length = length; } else { if (length > 0) { _StartIndex = _ContentElements[0].ViewIndex; _Length = _ContentElements.Count; content.LineEndFlag = false; } else { _StartIndex = _ContentElements.LastElement.ViewIndex + 1; _Length = 0 - _ContentElements.Count; { System.Console.Write(""); } } _LineEndFlag = content.LineEndFlag; } }//if if (this.Mode == ContentRangeMode.Content || this.Mode == ContentRangeMode.Mixed) { // 让选择状态发生改变的文档内容元素用户界面无效,需要重新绘制 if (contentElementsBack != null && contentElementsBack.Count > 0) { foreach (DomElement element in contentElementsBack) { if (_ContentElements.Contains(element) == false) { element.InvalidateView(); } } } if (_ContentElements.Count > 0) { foreach (DomElement element in _ContentElements) { if (contentElementsBack == null || contentElementsBack.Contains(element) == false) { element.InvalidateView(); } } } } if (this.StartIndex == -1) { System.Console.WriteLine(""); } return(true); }