If I add a style to a range of characters in the middle of a line
using editor.SetStyle(...), how can I later remove that style?
The only thing I could think of was to try setting the style to the
default style but it doesn't seem to have any effect.
my_style = wx.richtext.TextAttrEx()
my_style.SetFlags(rt.TEXT_ATTR_TEXT_COLOUR)
my_style.SetTextColour("#ff0000")
editor.SetStyle((10,12), my_style)
...
default_style = editor.GetDefaultStyle()
editor.SetStyle(range, default_style)
In my specific case I'm applying the color to a two-byte sequence in
several places in a line (for example, making certain character
sequences stand out), then later I want to remove the color preferably
by removing it from the whole line (or paragraph or document) at once.
IIRC when setting a style is it combined with whatever styles may already be set for each position in the document. Essentially, whatever attributes of the new style that are set are combined with any of existing style's attributes that have been set. In other words, if you have text that has been set to bold, and then you apply a style that has had the text colour attribute set then the result is text with a style containing both bold and a text colour.
Now with that in mind take a look at the style returned by GetDefaultStyle:
>>> ds = obj.GetDefaultStyle()
>>> ds.Flags
6144
>>> ds.Flags & rt.TEXT_ATTR_TEXT_COLOUR
0
>>> ds.GetTextColour()
wx.Colour(-1, -1, -1, 255)
So that style has not had the text colour attribute set, (it's probably just falling back to the GetForegroundColour() value in this case,) so trying to use it to reset the text color for a range is essentially not going to do anything at all because it will not replace the attribute in the current style.
Imagine, for example, a toolbar button labeled "Remove all styles from
the current line".
If you want to be able to do that then you'll probably need to set the default style (or have a separate style object that you use for this) such that it has all the pertinent attributes set to some default value.
···
On 9/26/09 9:59 PM, Bryan Oakley wrote:
--
Robin Dunn
Software Craftsman