From: The D. <the...@bl...> - 2005-07-22 05:33:02
|
John Labenski <jla...@gm...> wrote: (21/07/2005 22:11) >Instead of storing variables to your controls, you can get them from >the events. Try this. > > local toolBar = frame:CreateToolBar() > toolBar:AddTool(100, bmp) > bmpButton = wx.wxBitmapButton(toolBar, 101, bmp) > toolBar:AddControl(bmpButton) > toolBar:Realize() > > function bmpButtonHandler(event) > local bmpButton = event:GetEventObject():DynamicCast("wxBitmapButton") > print(bmpButton) -- this is the button itself > end > bmpButton:ConnectEvent(101, wx.wxEVT_COMMAND_BUTTON_CLICKED, >bmpButtonHandler) > But that's exactly what I don't want to do. I already tested a way to put a control on the toolbar, but what I want to do is reference a tool with a variable. The only reason I tried adding the control was to prove it could work with my existing code idea. If I could make the tool in advance, I could store it to the variable: X=MakeTool() TBAR:AddTool(X) -- if MakeTool() existed. That would then allow this, which would work exactly as I want: X:ConnectEvent(-1,wx.wxEVT_LEFT_UP, function(EV) if (EV:ControlDown()) then DoThings() end EV:Skip() end ) As it is, using TBAR as before, I can make it work by testing the flag, which is unset when any tool is clicked, but if the user holds control and clickes a blank bit of toolbar, the flag will get set, and if they then click the button that has this alternative function activated by Ctrl, they'll invoke said function even without holding Ctrl. Awkward... The only fix seems to be to make the toolbar exactly the same size as the buttons on it, to minimise misfires. A workround I've been trying is some other way to get the tool stored to X after AddTool(), by getting the pointer to it from GetId() or other means. None of these have worked though. There is FindById() and various other stuffs, but I think they are not implemented in wxLua, probably very new to wxWidgets too. >Why not just pass the event to the DoThings function and get the info >(like window id) from it inside the function? The les globals the >better since it's less likely you'll accidently overwrite them. > I am VERY careful of my globals. >:) Thankyou though, I think you're right, but I don't see how. I have two event handlers firing, and one of them does as you suggest, but the one that tests the ControlDown() can't be calling the function if the other handler is doing it, so far as I know. The function would just get called twice, with no meaning. What I have is this: FRAME:ConnectEvent(-1,wx.wxEVT_COMMAND_MENU_SELECTED, function(EV) ID=EV:GetId() DoThings(ID) X=nil end ) TBAR:ConnectEvent(-1,wx.wxEVT_LEFT_UP, function(EV) if (EV:ControlDown()) then X=1 end EV:Skip() end ) If I could store that tool to a variable I could dispense entirely with that first event handler. This is why it's so useful. Although it could be said that overriding standard behaviour with mouse events is laborious, it's doable, and the increase in power and versatility makes it entirely worth doing. >The wxToolbar is basicly a black hole, you only get the button pressed >events back. I don't think there are ways to get the mouse events for >the tools. That's why I suggested using wxButtons. > I live in hope. :) With FindById() and FindToolForPosition() it's clear that the black hole is evaporating nicely, but I guess that hasn't propagated to wxLua if it's a very recent change. Oddly, the FindToolForPosition() does appear to be in wxLua, but I still can't make it work. I guess the position has to be exact, and right now I don't know if it's screen based, or app window based. As ever, unless I see it work, it's pretty hard to derive anything useful from what I see. I might try using all bitmap buttons, but I thing they suck, aesthetically, on a toolbar. :) What I'll probably do is what I already have, and shrink the blank toolbar space to prevent misfires. It's not a great answer, but it will definitely work. Is it possible to GetId() for a mouse event? To get the ID of whatever the LEFT_UP happens on, and such? If so, this also would be an answer. Crow. |