Hey all,
Related to the other email, but not the same (hence the separate mail).
In the code below, I want to apply a style on a paragraph level. And if there is as selection I want to apply it to all the paragraphs touched by the selection.
What actually happens is that only the last paragraph touched by the
selection is affected.
Obviously I am missing something here, and would love to know what it
is. I was also hoping that if I have the style setting on paragraphs, it
will automatically grab anything in the selection on a paragraph level
of granularity. Is this so?
e.g.
my text that<selection start> spans
various paragraphs and
other <selection end> things
What I would expect is that "my text that" and "things" would also be affected by the new styles.
Here is the code:
This is the function that actually sets the style. It is then passed to the methods that actually make the style take effect in the editor. This function is passed a TextAttrEx object.
If it is not some missing flag on my part, is there an easy way to cycle through the paragraphs within a selection, or is it a matter of using rtc.MoveToParagraphStart and rtc.MoveToParagraphEnd and hacking it together?
Thanks in advance for any help with this.
Rohan
def new_format(attr):
font = attr.GetFont()
font.SetUnderlined(spec['underline'])
font.SetPointSize(spec['size'])
spec['bold'] and font.SetWeight(wx.BOLD)
spec['italic'] and font.SetStyle(wx.ITALIC)
attr.SetFont(font)
# Now for the alignment etc.
# Apply the style on a paragraph level.
attr.SetFlags(attr.GetFlags() | rt.TEXT_ATTR_PARAGRAPH)
# alignment
attr.SetAlignment(spec['align'])
attr.SetFlags(attr.GetFlags() | rt.TEXT_ATTR_ALIGNMENT)
# spacing
if spec['spacing']:
attr.SetParagraphSpacingBefore(spec['spacing'][0])
attr.SetParagraphSpacingAfter(spec['spacing'][1])
attr.SetFlags(attr.GetFlags() | rt.TEXT_ATTR_PARA_SPACING_BEFORE |
rt.TEXT_ATTR_PARA_SPACING_AFTER)
return attr
The code that is in the other post is supposed to apply it in the editor, and this is where the selection is not working.
def para_borders(self, point):
""" Grab the boundaries of the paragraph that point is sitting
in, and return the (beginning, end) points"""
rtc = self.rtc
rtc.SetInsertionPoint(point)
rtc.MoveToParagraphStart()
bp = rtc.GetInsertionPoint()
rtc.MoveToParagraphEnd()
ep = rtc.GetInsertionPoint()
return (bp, ep)
def ApplyNewParaAttr(self, func):
""" This takes a function that expects to be handed a
rt.TextAttrEx object, which it then manipulates as needed, and
then the attributes are applied to the current paragragh or all
paragraghs is the selection"""
##### NOTE:At this moment this does not handle selections in
## more than one paragragh properly
# Basic Features
rtc = self.rtc
ip = rtc.GetInsertionPoint()
attr = rt.TextAttrEx()
bp,ep = self.para_borders(ip)
used_space_hack = False
if bp == ep:
rtc.WriteText(" ")
used_space_hack = True
bp,ep = self.para_borders(ip)
r = rt.RichTextRange(bp, ep)
# This needs to handle multiple paragraghs in a selection properly.
if rtc.HasSelection(): # set range to selection
r = rtc.GetSelection()
if rtc.GetStyle(ip, attr):
attr = func(attr) # apply above function to style and
#return it
# Apply the style
rtc.SetFocus()
rtc.SetInsertionPoint(ip)
rtc.SetStyle(r, attr) # apply the style to the range
# (selection)
if used_space_hack is True:
rtc.Delete(rt.RichTextRange(bp, ep))