Unfortunately I've lost Robin's reply to this, but it helped. What I had already started to implement as a solution is pasted below. I tried to implement label editing by subclassing wx.Treebook and using a wx.TextCtrl. It seems to work fairly well apart from two issues.
1. When editing, if I left click on the text control to place the cursor, then I lose the control. i.e. I think the tree control is processing the event and the text control is losing focus, triggering its UpdateParent method. I could be wrong. I've tried a number of things, but haven't been able to fix this.
2. After renaming (which works fine as long as I don't left click) I'd like to resize the tree control. Elsewhere I bind events such as wx.EVT_TREEBOOK_NODE_EXPANDED to a handler that simply sends a resize event to the treebook, which works. But calling self.SendSizeEvent() at the end of the UpdateLabel method doesn't resize the tree control to reflect the edited text.
Any pointers appreciated. Thanks.
Duncan
###############################code##########################
import wx
import sys
class Treebook(wx.Treebook):
def __init__(self, *args, **kwargs):
wx.Treebook.__init__(self, *args, **kwargs)
self.text = TextCtrl(self)
self.text.Show(False)
self.item = None
def EditLabel(self, item):
# allow label editing on Windows
if sys.platform.startswith('win'):
self._EditLabel(item)
else:
wx.Treebook.GetTreeControl().EditLabel(self, item)
def _EditLabel(self, item):
self.item = item
tc = self.GetTreeCtrl()
self.text.SetValue(tc.GetItemText(item))
self.text.SetInsertionPointEnd()
self.text.SetSelection(-1, -1)
self.text.SetRect(tc.GetBoundingRect(item, textOnly=True))
self.text.Show(True)
self.text.SetFocus()
def UpdateLabel(self, s):
tc = self.GetTreeCtrl()
if not tc.GetItemText(self.item) == s:
tc.SetItemText(self.item, s)
class TextCtrl(wx.TextCtrl):
def __init__(self, *args, **kwargs):
wx.TextCtrl.__init__(self, *args, **kwargs)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_KILL_FOCUS, self.UpdateParent)
self.Bind(wx.EVT_TEXT_ENTER, self.UpdateParent)
def OnKeyDown(self, event):
keycode = event.GetKeyCode()
if keycode == wx.WXK_ESCAPE:
tb = self.GetParent()
self.SetValue(tb.GetItemText(tb.item))
event.Skip()
def UpdateParent(self, event):
self.GetParent().UpdateLabel(self.GetValue())
self.HideAndMove()
def HideAndMove(self):
self.Show(False)
self.SetPosition((0,0))
self.SetSize((0,0))