Re: Implementing event handler for objects
Jason McKesson <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
On 4/6/2011 9:05 AM, Jonatan Magnusson wrote:
> 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
I love Luabind. Really, I do. But it's simply not the best tool for
every Lua association task.
If you forced me to create a binding between wxWidgets and Lua, well,
I'd just download wxLua. But if you forced *me* to do it myself? I'd use
SWIG.
The type mapping feature of SWIG makes it so much easier to have this
kind of thing, where you can insert small bits of wrapper code
automatically in functions based on parameters and return values. It
makes adapting certain kinds of interfaces much easier.
SWIG works best when you have no control over the C/C++ API you're
using. Luabind is for when you have complete control over both the Lua
API and the C/C++ equivalent.
What Luabind really needs is a way to attach certain C++ objects to
Luabind, such that whenever they appear in a function's parameter list
or signature, Luabind will automatically call a given function that will
generate this parameter and/or return the value to Lua after the fact.
Rather than having to apply policies to each and every use of a
particular parameter.
------------------------------------------------------------------------------
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