Thanks to Josiah Carlson! This version, based on his suggestion, works. (To make sense of it, you have to know that the owner of this STC also owns a list of data about the lengths and beginning offsets of sentences, as well as a good deal else about them.)
Charles Hartman
P.S. I'll be out of touch till June.
<in the __init__ for my subclass of STC:>
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_LEFT_DOWN, self.OnMousePrelimDown)
self.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)
<members of the subclass:>
def OnMousePrelimDown(self, event):
self.handled = 0
wx.CallAfter(self.OnMouseDown, event.GetPosition())
def OnMouseDown(self, mouseclickpos):
if self.handled:
return
mom = self.GetParent()
clickpos = self.PositionFromPoint(mouseclickpos)
for inx in range(len(mom.s)):
if mom.s[inx].offset > clickpos: break
if mom.s[inx].offset > clickpos: inx -= 1
sentend = mom.s[inx].offset + mom.s[inx].length - 1
mom.currSent = inx
self.SetSelection(mom.s[inx].offset, sentend)
mom.treeWin.ClearAll()
# recreate state of rand gen for BuildTemplate
temprand = random.getstate()
random.setstate(mom.s[inx].randstate)
sentenceTemplate = mom.grammar.BuildTemplate()
random.setstate(temprand)
def OnDoubleClick(self, event):
self.handled = 0 # coordination with single-click
# select a single word (including internal punct, excluding terminal)
startword = endword = self.PositionFromPoint(event.GetPosition())
while startword > 0 and not chr(self.GetCharAt(startword)).isspace():
startword -= 1
startword += 1
if not chr(self.GetCharAt(startword)).isalnum():
startword += 1
while endword < self.GetTextLength() and not chr(self.GetCharAt(endword)).isspace():
endword += 1
# flaw: excludes terminal apostrophe from plural possessives
if not chr(self.GetCharAt(endword - 1)).isalnum():
endword -= 1
self.SetSelection(startword, endword)
# <this is where further code is called that will replace the word>