Re: [Fxruby-users] implementing connect()
Status: Inactive
Brought to you by:
lyle
|
From: Lyle J. <ly...@kn...> - 2004-05-13 02:27:21
|
On May 12, 2004, at 3:02 PM, Claus Spitzer wrote:
> Greetings! I'm at a loss on how to implement event handlers in my
> classes. I looked at the API references and saw that connect() is
> implemented in Responder2, which led me to believe that I need to
> include it in whichever class needs the connect() method. This did not
> work, as the objects 'selector', 'target', and 'value' were missing.
> I've looked at the class that had those (FXDataTarget) and saw that
> they were accessors, so I added those for my class, and it prevents
> the compiler from throwing an error at me, but it's a crude solution,
> and it doesn't do anything except getting rid of the errors. I'm 99%
> sure that what I'm doing is wrong.
OK, first let's try to pin down what it is that you want to do. You
want your class to be able to have a message target, i.e. to be able to
call connect() on that object and provide a block to handle that
message?
If that's the case, your class needs to provide a "target" accessor
method that returns a reference to the message target (an FXObject
instance) for that object. If your class is a subclass of FXWindow or
FXDataTarget you've already got that. Otherwise, do something like:
class MyClass < FXObject
include Responder2
attr_accessor :target
def initialize
@target = nil
end
end
You also need to mix-in the Responder2 module to get the connect()
method, as shown.
Now you should be able to do something like this:
obj = MyClass.new
obj.connect(SEL_COMMAND) { puts "hey hey hey" }
And inside your class, when you want to "trigger" that callback, send a
SEL_COMMAND message to the message target, e.g.
class MyClass
def make_it_so
target.handle(self, MKUINT(0, SEL_COMMAND), nil)
end
end
I'm sort of rushing through this, so I may have omitted some details.
Let me know how it goes and if you have some follow-up questions.
Hope this helps,
Lyle
|