My application dynamically creates toolbar tools, thus there is only one mouse event handler assigned to any tool the user might mouse click on. Is there a way to get either a reference index to the tool, or the object of the tool, that the user clicked on?
The toolbar is created like this,
self.m_toolBar = wx.ToolBar( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TB_HORIZONTAL )
self.m_toolBar.Bind( wx.EVT_LEFT_DOWN, self.onToolBar )
New tools are added like this,
self.m_toolBar.AddTool(wx.NewId(), "foo", wx.Bitmap(path), "bar")
The event handler, onToolBar, I can get the original toolbar object, self.m_toolBar,
event.GetEventObject()
but I can't find any reference to the actual tool that the user selected. The event.GetId() always returns the ID of wx.ToolBar, not the ToolBarBase object that I seek.
Options,
1) I did notice that I can get the (x,y) position of the mouse, and I could calculate which tool (index) was pressed based on the size of the tool icons, etc, but that just doesn't feel like the right way to do this. 2) I think I could create multiple wx.ToolBar objects and put only one tool in each, thus the event.GetId() would relate directly to the tool being selected.
3) Another option I tried is to,
obj = self.m_toolBar.AddTool(wx.NewId(), status_tip, wx.Bitmap(path), tooltip)
obj.Bind(wx.EVT_LEFT_DOWN, self.onToolBarObj)
But 'ToolBarToolBase' object has no attribute 'Bind'
But there must be a better way?
So the core issue was not using the right bind method for the added tool, instead of,
obj = self.m_toolBar.AddTool(wx.NewId(), status_tip, wx.Bitmap(path), tooltip)
obj.Bind(wx.EVT_LEFT_DOWN, self.onToolBarObj)
Should have been,
obj = self.m_toolBar.AddTool(wx.NewId(), status_tip, wx.Bitmap(path), tooltip)
self.Bind(wx.EVT_MENU, self.onToolBarObj, obj)