RichTextCtrl and overlapping styles

I've just started looking into replacing the word processing engine in my
software, based on the StyledTextCtrl, with the RichTextCtrl component. I
need the ability to display images with the text, among other things, and
this seemed like the right direction to go.

But right out of the gate, I'm seeing something that doesn't make any sense
to me. The issue is one of overlapping styles. Please see the attached
brief sample.

In line 1 of the sample output, I issue, BeginBold() and then BeginItalic(),
I get text in Bold and Italics. It's what I'd expect. If I then
EndItalic(), I'm back to just bold, and if I then EndBold(), I'm back to
plain, undecorated text. So far, so good.

But in line 2 of the output, I change the order a bit, it doesn't work
right. I BeginBold() and BeginItalic() as before, but then call EndBold()
rather than EndItalic(). I'd expect Italics only, but I get Bold only.
When I then EndItalic(), the Bold goes away and I'm back to plain text.

Lines 3 - 5 are just variations on this theme, using different formatting in
different orders.

At least with Bold, Italics, and Underline, which is all I've messed with so
far, the EndXXX() functions seem to end formatting in the opposite order it
was applied, regardless of which EndXXX() function you call.

I've run the attached program on both OS X and Windows with Python 2.5.x and
wxPython 2.8.9.2 and the results are the same.

Do I really need to disable all formatting, then re-enable the formatting I
want to keep at every format change? That hardly seems practical. Any
guidance would be much appreciated.

David

RTFTest.py (3.23 KB)

Please ask about this on wx-users. The RTC expert (Julian Smart) reads that list but not this one.

···

On 12/21/09 12:09 PM, David wrote:

I've just started looking into replacing the word processing engine in my
software, based on the StyledTextCtrl, with the RichTextCtrl component. I
need the ability to display images with the text, among other things, and
this seemed like the right direction to go.

But right out of the gate, I'm seeing something that doesn't make any sense
to me. The issue is one of overlapping styles. Please see the attached
brief sample.

In line 1 of the sample output, I issue, BeginBold() and then BeginItalic(),
I get text in Bold and Italics. It's what I'd expect. If I then
EndItalic(), I'm back to just bold, and if I then EndBold(), I'm back to
plain, undecorated text. So far, so good.

But in line 2 of the output, I change the order a bit, it doesn't work
right. I BeginBold() and BeginItalic() as before, but then call EndBold()
rather than EndItalic(). I'd expect Italics only, but I get Bold only.
When I then EndItalic(), the Bold goes away and I'm back to plain text.

Lines 3 - 5 are just variations on this theme, using different formatting in
different orders.

At least with Bold, Italics, and Underline, which is all I've messed with so
far, the EndXXX() functions seem to end formatting in the opposite order it
was applied, regardless of which EndXXX() function you call.

I've run the attached program on both OS X and Windows with Python 2.5.x and
wxPython 2.8.9.2 and the results are the same.

Do I really need to disable all formatting, then re-enable the formatting I
want to keep at every format change? That hardly seems practical. Any
guidance would be much appreciated.

--
Robin Dunn
Software Craftsman

> I've just started looking into replacing the word
processing engine in
> my software, based on the StyledTextCtrl, with the RichTextCtrl
> component. I need the ability to display images with the
text, among
> other things, and this seemed like the right direction to go.
>
> But right out of the gate, I'm seeing something that
doesn't make any
> sense to me. The issue is one of overlapping styles.
Please see the
> attached brief sample.
>
> In line 1 of the sample output, I issue, BeginBold() and then
> BeginItalic(), I get text in Bold and Italics. It's what
I'd expect.
> If I then EndItalic(), I'm back to just bold, and if I then
EndBold(),
> I'm back to plain, undecorated text. So far, so good.
>
> But in line 2 of the output, I change the order a bit, it
doesn't work
> right. I BeginBold() and BeginItalic() as before, but then call
> EndBold() rather than EndItalic(). I'd expect Italics
only, but I get
> Bold only. When I then EndItalic(), the Bold goes away and
I'm back to
> plain text.
>
> Lines 3 - 5 are just variations on this theme, using different
> formatting in different orders.
>
> At least with Bold, Italics, and Underline, which is all
I've messed
> with so far, the EndXXX() functions seem to end formatting in the
> opposite order it was applied, regardless of which EndXXX()
function
> you call.
>
> I've run the attached program on both OS X and Windows with Python
> 2.5.x and wxPython 2.8.9.2 and the results are the same.
>
> Do I really need to disable all formatting, then re-enable the
> formatting I want to keep at every format change? That
hardly seems
> practical. Any guidance would be much appreciated.

Please ask about this on wx-users. The RTC expert (Julian
Smart) reads
that list but not this one.

--
Robin Dunn
Software Craftsman
http://wxPython.org

Robin,

Julian says the behavior is "expected" given the way the styles involved are
stacked in the control. Basically, EndBold(), EndItalic(), and
EndUnderline() (and maybe other EndXXX() methods) just take the last
formatting change off the stack, regardless of what the change was.

I've added a bit of code to the RichTextCtrl demo file (attached) that
explains this subtlety and shows a more flexible way to handle character
formatting that solves the problem. (Sorry, I'm not set up for SVN and
diffs and patches, but as near as I could figure out, the version of this
demo file in SVN hasn't changed in 23 months, so a full-blown substitution
should work.) I would've found it helpful if this had been mentioned
somewhere, anyway, and this seemed like as good a place as any to mention
it.

Thanks for the pointer to where to get the answers I needed.

David

RichTextCtrl.py (29.4 KB)

Thanks David.

···

On 12/22/09 1:37 PM, David wrote:

I've added a bit of code to the RichTextCtrl demo file (attached) that
explains this subtlety and shows a more flexible way to handle character
formatting that solves the problem. (Sorry, I'm not set up for SVN and
diffs and patches, but as near as I could figure out, the version of this
demo file in SVN hasn't changed in 23 months, so a full-blown substitution
should work.) I would've found it helpful if this had been mentioned
somewhere, anyway, and this seemed like as good a place as any to mention
it.

--
Robin Dunn
Software Craftsman