Roland King:
So the replacement for NewID is to reserve an ID, or more, from NewControlID.
It won’t conflict with legacy code which uses NewID [...]
What sequence of events makes you think that NewControlId is not a viable replacement for NewID?
I can't very well use NewId in code originally written prior to last week (a.k.a. "legacy code") if it doesn't exist any more.
If wx.NewId is going out, then I have to do something about the >100 occurrences of wx.NewId in the code I'm maintaining. Timers and custom events, mostly.
So the question I was looking for an answer to is this: Can I simply search-replace NewId into NewControlId? And my conclusion was no, that will not work, because the id's will go back into the pool when the first wx thing using them is destroyed, and the second time I create a wx thing using the same id, it's a bug because that id is not actually reserved any more.
For example:
EVT_SYNC_ID = wx.NewId()
class MyFrame(wx.Frame):
def __init__(self, ...):
self._sync_timer = wx.Timer(self, EVT_SYNC_ID)
self.Bind(wx.EVT_TIMER, self.OnSyncTimer, id=EVT_SYNC_ID)
I can't replace wx.NewId with wx.Window.NewControlId here, because when the first MyFrame instance is destroyed, and the timer with it, then EVT_SYNC_ID will be unreserved, and when the second instance is created, it will be using a rogue unreserved id.
NewId is not strictly needed here, there is a rewrite to avoid any explicit allocation:
class MyFrame(wx.Frame):
def __init__(self, ...):
self._sync_timer = wx.Timer(self, -1)
self.Bind(wx.EVT_TIMER, self.OnSyncTimer, id=self._sync_timer.GetId())
I don't like to rewrite working code, but it may come to that.
Wrt. the other typical use, custom events, I think they are safe because in this case the id is never actually passed to wx.Window.__init__ or similar, they're only passed to wx.PyEvent.SetEventType and wx.Window.Connect, and I don't see them unreserving anything.
regards, Anders