+/// Apply a named style to the selection
+bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
+{
+ // Flags are defined within each definition, so only certain
+ // attributes are applied.
+ wxRichTextAttr attr(def->GetStyle());
+
+ int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE;
+
+ // Make sure the attr has the style name
+ if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition)))
+ {
+ attr.SetParagraphStyleName(def->GetName());
+
+ // If applying a paragraph style, we only want the paragraph nodes to adopt these
+ // attributes, and not the leaf nodes. This will allow the context (e.g. text)
+ // to change its style independently.
+ flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
+ }
+ else
+ attr.SetCharacterStyleName(def->GetName());
+
+ if (HasSelection())
+ return SetStyleEx(GetSelectionRange(), attr, flags);
+ else
+ {
+ SetAndShowDefaultStyle(attr);
+ return true;
+ }
+}
+
+/// Apply the style sheet to the buffer, for example if the styles have changed.
+bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet)
+{
+ if (!styleSheet)
+ styleSheet = GetBuffer().GetStyleSheet();
+ if (!styleSheet)
+ return false;
+
+ if (GetBuffer().ApplyStyleSheet(styleSheet))
+ {
+ GetBuffer().Invalidate(wxRICHTEXT_ALL);
+ Refresh(false);
+ return true;
+ }
+ else
+ return false;
+}
+