/// <summary> /// 设置段落样式 /// </summary> /// <param name="newStyle"></param> /// <returns></returns> public DomElementList SetParagraphStyle(DocumentContentStyle newStyle) { DomDocument document = this._DocumentContent.OwnerDocument; Dictionary <DomElement, int> styleIndexs = new Dictionary <DomElement, int>(); if (this.Document.Options.SecurityOptions.EnablePermission) { newStyle.DisableDefaultValue = true; newStyle.CreatorIndex = this.Document.UserHistories.CurrentIndex; newStyle.DeleterIndex = -1; } else { newStyle.DisableDefaultValue = true; newStyle.CreatorIndex = -1; newStyle.DeleterIndex = -1; } foreach (DomParagraphFlagElement p in this.ParagraphsEOFs) { if (document.DocumentControler.CanModify(p)) { DocumentContentStyle rs = (DocumentContentStyle)p.RuntimeStyle.Clone(); if (XDependencyObject.MergeValues(newStyle, rs, true) > 0) { rs.DefaultValuePropertyNames = newStyle.GetDefaultValuePropertyNames(); int newStyleIndex = document.ContentStyles.GetStyleIndex(rs); if (newStyleIndex != p.StyleIndex) { styleIndexs[p] = newStyleIndex; } } } }//foreach if (styleIndexs.Count > 0) { DomElementList result = document.EditorSetParagraphStyle(styleIndexs, true); return(result); } else { return(null); } }
/// <summary> /// 获得运行时的样式 /// </summary> /// <param name="styleIndex"></param> /// <returns></returns> public ContentStyle GetRuntimeStyle(int styleIndex) { ContentStyle style = GetStyle(styleIndex); if (style == _Default) { return(style); } else { if (runtimeStyles == null) { runtimeStyles = new Dictionary <ContentStyle, ContentStyle>(); } if (runtimeStyles.ContainsKey(style)) { return(runtimeStyles[style]); } else { ContentStyle rs = (ContentStyle)style.Clone(); //rs.Merge(this.Default); XDependencyObject.MergeValues(this.Default, rs, false); if (string.IsNullOrEmpty(rs.DefaultValuePropertyNames) == false) { foreach (string name in rs.DefaultValuePropertyNames.Split(',')) { XDependencyObject.RemoveProperty(rs, name); }//foreach } runtimeStyles[style] = rs; return(rs); } } }
/// <summary> /// 设置文档元素的样式 /// </summary> /// <param name="newStyle">新样式</param> /// <returns>是否修改了文档内容</returns> public bool SetElementStyle(DocumentContentStyle newStyle) { Dictionary <DomElement, int> newStyleIndexs = new Dictionary <DomElement, int>(); foreach (DomElement element in this) { DocumentContentStyle rs = (DocumentContentStyle)element.RuntimeStyle.Clone(); if (XDependencyObject.MergeValues(newStyle, rs, true) > 0) { int styleIndex = this.Document.ContentStyles.GetStyleIndex(rs); if (styleIndex != element.StyleIndex) { newStyleIndexs[element] = styleIndex; } } }//foreach if (newStyleIndexs.Count > 0) { this.Document.EditorSetElementStyle(newStyleIndexs, true); return(true); } return(false); }
/// <summary> /// 设置段落样式 /// </summary> /// <param name="newStyle"></param> /// <returns></returns> public DomElementList SetParagraphStyle(DocumentContentStyle newStyle) { CheckState(); Dictionary <DomElement, int> styleIndexs = new Dictionary <DomElement, int>(); foreach (DomParagraphFlagElement p in this.ParagraphsEOFs) { DocumentContentStyle rs = (DocumentContentStyle)p.RuntimeStyle.Clone(); if (XDependencyObject.MergeValues(newStyle, rs, true) > 0) { int newStyleIndex = this.Document.ContentStyles.GetStyleIndex(rs); if (newStyleIndex != p.StyleIndex) { styleIndexs[p] = newStyleIndex; } } }//foreach if (styleIndexs.Count > 0) { this.Document.EditorSetParagraphStyle(styleIndexs, true); } return(this.ParagraphsEOFs); }
/// <summary> /// 设置多个元素的样式 /// </summary> /// <param name="newStyle">新样式</param> /// <param name="document">文档对象</param> /// <param name="elements">要设置的元素列表</param> /// <returns>操作是否成功</returns> internal static bool SetElementStyle( DocumentContentStyle newStyle, DomDocument document, System.Collections.IEnumerable elements) { if (newStyle == null) { throw new ArgumentNullException("newStyle"); } if (document == null) { throw new ArgumentNullException("document"); } if (elements == null) { throw new ArgumentNullException("elements"); } if (document.Options.SecurityOptions.EnablePermission) { // 运行授权控制,向样式信息添加用户信息 newStyle.DisableDefaultValue = false; newStyle.CreatorIndex = document.UserHistories.CurrentIndex; newStyle.DeleterIndex = -1; } else { // 去除授权控制相关信息 newStyle.DisableDefaultValue = false; newStyle.CreatorIndex = -1; newStyle.DeleterIndex = -1; } Dictionary <DomElement, int> newStyleIndexs = new Dictionary <DomElement, int>(); DomElementList parents = new DomElementList(); DomElement lastParent = null; foreach (DomElement element in elements) { DomElement parent = element.Parent; //if (parent != lastParent) { // 记录所有涉及到的父元素 lastParent = parent; bool addParent = false; //if (element is XTextFieldBorderElement) //{ // addParent = true; //} //else if (addParent) { if (parents.Contains(element.Parent) == false) { parents.Add(element.Parent); } } }//if DocumentContentStyle rs = (DocumentContentStyle)element.RuntimeStyle.Clone(); if (XDependencyObject.MergeValues(newStyle, rs, true) > 0) { rs.DefaultValuePropertyNames = newStyle.GetDefaultValuePropertyNames(); int styleIndex = document.ContentStyles.GetStyleIndex(rs); if (styleIndex != element.StyleIndex) { newStyleIndexs[element] = styleIndex; } if (element.ShadowElement != null && styleIndex != element.ShadowElement.StyleIndex) { newStyleIndexs[element.ShadowElement] = styleIndex; } } }//foreach if (parents.Count > 0) { // 对涉及到的父元素设置样式 foreach (DomElement element in parents) { DocumentContentStyle rs = (DocumentContentStyle)element.RuntimeStyle.Clone(); if (XDependencyObject.MergeValues(newStyle, rs, true) > 0) { rs.DefaultValuePropertyNames = newStyle.GetDefaultValuePropertyNames(); int styleIndex = document.ContentStyles.GetStyleIndex(rs); if (styleIndex != element.StyleIndex) { newStyleIndexs[element] = styleIndex; } } } } if (newStyleIndexs.Count > 0) { DomElementList result = document.EditorSetElementStyle(newStyleIndexs, true); if (result != null && result.Count > 0) { return(true); } } return(false); }
private void SetStyleProperty( object sender, WriterCommandEventArgs args, string commandName) { if (args.Mode == WriterCommandEventMode.QueryState) { DocumentContentStyle style = GetCurrentStyle(args.Document); args.Enabled = args.DocumentControler != null && args.DocumentControler.Snapshot.CanModifySelection; switch (commandName) { case StandardCommandNames.Bold: args.Checked = style.Bold; break; case StandardCommandNames.BorderBottom: args.Checked = style.BorderBottom; break; case StandardCommandNames.BorderLeft: args.Checked = style.BorderLeft; break; case StandardCommandNames.BorderRight: args.Checked = style.BorderRight; break; case StandardCommandNames.BorderTop: args.Checked = style.BorderTop; break; case StandardCommandNames.Italic: args.Checked = style.Italic; break; case StandardCommandNames.Strikeout: args.Checked = style.Strikeout; break; case StandardCommandNames.Subscript: args.Checked = style.Subscript; break; case StandardCommandNames.Superscript: args.Checked = style.Superscript; break; case StandardCommandNames.Underline: args.Checked = style.Underline; break; default: args.Enabled = false; return; } } else if (args.Mode == WriterCommandEventMode.Invoke) { DocumentContentStyle cs = GetCurrentStyle(args.Document); DocumentContentStyle ns = args.Document.CreateDocumentContentStyle(); ns.DisableDefaultValue = true; switch (commandName) { case StandardCommandNames.Bold: if (args.Parameter is bool) { ns.Bold = (bool)args.Parameter; } else { ns.Bold = !cs.Bold; } break; case StandardCommandNames.BorderBottom: if (args.Parameter is bool) { ns.BorderBottom = (bool)args.Parameter; } else { ns.BorderBottom = !cs.BorderBottom; } break; case StandardCommandNames.BorderLeft: if (args.Parameter is bool) { ns.BorderLeft = (bool)args.Parameter; } else { ns.BorderLeft = !cs.BorderLeft; } break; case StandardCommandNames.BorderRight: if (args.Parameter is bool) { ns.BorderRight = (bool)args.Parameter; } else { ns.BorderRight = !cs.BorderRight; } break; case StandardCommandNames.BorderTop: if (args.Parameter is bool) { ns.BorderTop = (bool)args.Parameter; } else { ns.BorderTop = !cs.BorderTop; } break; case StandardCommandNames.Italic: if (args.Parameter is bool) { ns.Italic = (bool)args.Parameter; } else { ns.Italic = !cs.Italic; } break; case StandardCommandNames.Strikeout: if (args.Parameter is bool) { ns.Strikeout = (bool)args.Parameter; } else { ns.Strikeout = !cs.Strikeout; } break; case StandardCommandNames.Subscript: if (args.Parameter is bool) { ns.Subscript = (bool)args.Parameter; } else { ns.Subscript = !cs.Subscript; } ns.Superscript = false; break; case StandardCommandNames.Superscript: ns.Subscript = false; if (args.Parameter is bool) { ns.Superscript = (bool)args.Parameter; } else { ns.Superscript = !cs.Superscript; } break; case StandardCommandNames.Underline: if (args.Parameter is bool) { ns.Underline = (bool)args.Parameter; } else { ns.Underline = !cs.Underline; } break; case StandardCommandNames.Color: if (args.Parameter is Color) { ns.Color = (Color)args.Parameter; } break; case StandardCommandNames.BackColor: if (args.Parameter is Color) { ns.BackgroundColor = (Color)args.Parameter; } break; case StandardCommandNames.Font: if (args.Parameter is Font) { ns.Font = new XFontValue((Font)args.Parameter); } else if (args.Parameter is XFontValue) { ns.Font = ((XFontValue)args.Parameter).Clone(); } break; case StandardCommandNames.FontName: if (args.Parameter is string) { ns.FontName = (string)args.Parameter; args.Document.EditorCurrentStyle.FontName = ns.FontName; //if (args.EditorControl != null) //{ // args.EditorControl.Focus(); //} } break; case StandardCommandNames.FontSize: if (args.Parameter is string) { ns.FontSize = FontSizeInfo.GetFontSize((string)args.Parameter, args.Document.DefaultStyle.FontSize); } else if (args.Parameter is float || args.Parameter is double || args.Parameter is int) { ns.FontSize = Convert.ToSingle(args.Parameter); } args.Document.EditorCurrentStyle.FontSize = ns.FontSize; //if (args.EditorControl != null) //{ // args.EditorControl.Focus(); //} break; default: throw new NotSupportedException(commandName); }//switch XDependencyObject.MergeValues(ns, args.Document.EditorCurrentStyle, true); if (args.Document.Selection.Length != 0) { args.Document.BeginLogUndo(); args.Document.Selection.SetElementStyle(ns); args.Document.EndLogUndo(); args.Document.OnSelectionChanged(); args.Document.OnDocumentContentChanged(); } //args.Document.CurrentStyle.Underline = v; } }