Hi everyone,
I have been busy with the rich text control, and ran into this:
Basically I want to set a style on a paragraph. This works unless there is nothing in the current paragraph yet.
e.g.
I put the cursor on a new line and from the dropdown list select H1.
If I type it is as if nothing had been done.
However if I have something like this:
start of para<cursor here>
And then choose the H1, it Centers and grows etc. as expected.
I have made a hack to add a space if there is nothing in the current paragraph, set the style, and then remove the space. Fairly pathetic I know, but it works, and I cannot find anything else to get this to work.
Here is the code. If anyone can see a better solution that would be very handy.
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: # nothing in the paragraph yet
rtc.WriteText(" ") # insert a space
used_space_hack = True # set the flag
bp,ep = self.para_borders(ip) # reset the paragraph limits
r = rt.RichTextRange(bp, ep)
# This needs to handle multiple paragraghs in a selection properly.
if rtc.HasSelection():
r = rtc.GetSelection()
if rtc.GetStyle(ip, attr): # A function that expects the attr object
attr = func(attr) # is passed in, adjusts the attr, and returns
# Apply the style # it, and the adjusted style is now applied
rtc.SetFocus()
rtc.SetInsertionPoint(ip)
rtc.SetStyle(r, attr)
if used_space_hack is True: # if a space was inserted, remove it.
rtc.Delete(rt.RichTextRange(bp, ep))
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)
Thanks for any help.
Rohan