[fxruby-users] defining events in fox 1.2

Lyle Johnson lyle at knology.net
Wed May 18 22:09:51 EDT 2005

On May 12, 2005, at 11:28 PM, Jacob Hanson wrote:

> Thanks to the list I'm back to progressing with my custom control.
> Now, what I can't find after an hour of RTFM is how to define events
> so that something can .connect to them.
> I read how the older, C++ Fox-like .enum/FXMAPFUNC/Responder interface 
> was
> replaced with the .connect interface for handling events in your
> applications. But when you're building a widget, how do you define 
> those
> events?

The purpose of the connect() method is to set up a mapping between 
messages (like SEL_COMMAND) that are sent by a widget to its message 
target, and the method or block of code that should handle that 
message. So, for one of the built-in widgets, if you read in its 
documentation that it sends, say, the SEL_CHANGED message to its 
message target, you can call connect(SEL_CHANGED) on that widget:

	widgetThatSendsMessage.connect(SEL_CHANGED) { ... }

Now if you have a custom widget that you'd like for others to be able 
to call connect() on, you need to be sure that your custom widget sends 
message(s) to its target when interesting things happen (where the 
definition of "interesting" is up to you). For example,

	class MyCustomWidget
		def methodWhereSomethingInterestingHappens
			# Let's send a SEL_COMMAND message to my message target
			target.handle(self, FXSEL(SEL_COMMAND, selector), nil)

and then clients would use it as:

	x = MyCustomWidget.new(...)
	x.connect(SEL_COMMAND) { ... }

> Say I have a widget that should do nothing else but change the 
> background
> color of itself when clicked. The only thing that I can fathom at this 
> point
> is that you have to use Responder's .enum to define your desired 
> custom message
> IDs (perhaps ID_RED, ID_BLUE for background colors) and then use 
> FXMAPFUNC to map
> the message type + message ID combinations to actual methods inside
> the widget (e.g. FXMAPFUNC(SEL_COMMAND, ID_RED, :onSetRedBackground))
> Is this the correct way to do it with FXRuby 1.2?

For this particular example, it doesn't sound like your widget is 
actually sending messages out to a target object; it's just 
implementing a custom behavior in response to being "clicked". You can 
sort-of "abuse" the connect() method in this case, however, to make the 
widget itself its own target, e.g.

	self.connect(SEL_COMMAND) {

Hope this helps,


More information about the fxruby-users mailing list