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? 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? Thx. Jacob
Jacob Hanson schrieb:>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? > >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? >I''m still a little uncertain about the use of .connect since it doesn''t completely replace the old C++-style mechanism. Example: If you have a widget and want to overload the paint handler you have to call FXMAPFUNC(SEL_PAINT, 0, "onPaint") since self.connect(SEL_PAINT, method(:onPaint)) doesn''t do the job (although it actually should). However, you can also create objects this way: FXButton.new(...) do |btn| btn.connect(SEL_COMMAND, method(:OnClick)) end If you have more than one button calling the :OnClick method you can determine which button was clicked later: def OnClick(sender, sel, event) case FXSELID(sel) when ID_RED puts "Red button clicked" break when ID_YELLOW puts "Yellow button clicked" break end end This sometimes makes code a little more compact and allows you to react on similar objects the same way (and spares duplicating code to a certain extend). Hope this helps, Jannis
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) end end 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) { self.changeMyBackgroundColor() } Hope this helps, Lyle