Implementing event handler for objects

Jonatan Magnusson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.