Re: Implementing event handler for objects
Tony Kostanjsek <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
hi jonatan,
you'll probably need some kind of callback proxy object that holds the Lua
target information that needs to be kept alive during the lifetime of the
widget/target. Something like:
struct LuaCallbackProxy
{
luabind::object targetObject;
luabind::object targetFunc;
LuaCallbackProxy(luabind::object inTO, luabind::object inTF) :
inTargetObject(inTO),inTargetFunc(inTF) {}
void eventHandler(wxCommandEvent& ev)
{
luabind::call_function<void>(targetFunc,targetObject,ev);
}
};
typedef shared_ptr<LuaCallbackProxy> ProxyPtr;
ProxyPtr connect(wxButton* button, evtype , luabind::object targetObject,
luabind::object targetFunc)
{
ProxyPtr result(new LuaCallbackProxy(targetObject, targetFunc));
button->Bind(evtype, &LuaCallbackProxy::eventHandler, result.get());
return result;
}
bind all this and the wx Widget stuff and then from Lua:
t={}
function t:handler(event)
print("click")
end
local connection = connect(button, wxEVT_COMMAND_BUTTON_CLICKED,t,t.handler)
Just an idea, hope this makes sense,
best,
Tony
----- Ursprüngliche Mail ----
Von: Jonatan Magnusson <[email protected]>
An: [email protected]
Gesendet: Mittwoch, den 6. April 2011, 18:05:15 Uhr
Betreff: [luabind] Implementing event handler for objects
Hi
I'm wrapping a GUI toolkit using Luabind, and i've struck upon a problem
with handling events.
The GUI-toolkit I'm wrapping is wxWidgets. Every widget inherits
wxEvtHandler and that
class has a method called Bind() that is used to connect events to an
event handler. A stupid example:
class MyClass {
void init ()
{
b = wxButton (frm, -1, "Some button");
b->Bind (wxEVT_COMMAND_BUTTON_CLICKED,
&MyClass::HandleButtonClick, this);
}
void HandleButtonClick (wxCommandEvent& ev)
{
printf ("Button clicked.\n");
}
}
My dream API for this in Lua would be:
b = wx.Button (frm, -1, "Some button")
b.on_click = function ()
print ("Button clicked.\n")
end
Or, even better:
b.on_click:add (function ()
print ("Button clicked, with multiple event handlers.\n")
end
I guess I could live with something like this, which is more like the
C++ API:
b.bind (wx.EVT_COMMAND_BUTTON_CLICKED, function (ev)
print ("Button clicked again...\n")
end)
But I have no idea how to implement this using Luabind, without creating
a wrapper class
for every class in the wxWidgets-library that I want to use.
I guess it would help if Luabind could automatically create a helper
class for each class (say "wxLuaEventHandler"),
but I don't think that's possible? Kind of like a nested class, but not
associated with any nested class in the base class.
Any suggestions?
Best regards,
Jonatan Magnusson
------------------------------------------------------------------------------
Xperia(TM) PLAY
It's a major breakthrough. An authentic gaming
smartphone on the nation's most reliable network.
And it wants your games.
http://p.sf.net/sfu/verizon-sfdev
_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
------------------------------------------------------------------------------
Xperia(TM) PLAY
It's a major breakthrough. An authentic gaming
smartphone on the nation's most reliable network.
And it wants your games.
http://p.sf.net/sfu/verizon-sfdev