List Info

Thread: Data Targets and message handlers




Data Targets and message handlers
user name
2006-10-11 06:37:44
fxruby-users-bouncesrubyforge.org wrote:

>> This makes FXDataTarget objects pretty useless
under FXRuby...
> 
> Have you looked at FoxTails/Observable? FoxTails
subclasses
> some of the Fox/FXRuby classes and defines an
initialize
> method that (typically) takes an object and an
attribute name
> instead of the usual target arguments. The attribute
name is
> used to keep that attr in sync when the widget is used.
> 
> FoxTails uses Fox's #connect mechanism for this
functionality
> (typically, using SEL_COMMAND, SEL_FOCUSOUT, etc)
rather than
> data targets. This means you can still use connect
(with
> other messages) for other purposes. You can also use
the
> #when_foo methods to register blocks of code to execute
when
> the attr changes or matches a pattern.
> 
> Here's a simple combobox example that lets you select
an
> object (in terms of its string representation using
#inspect)
> and then changes a label to display the class of the
object.
> 
> #!/usr/bin/env ruby
> 
> require 'foxtails'
...

Thanks Joel, I have to check your library.

Otherwise, Lyle, do you think it would be possible to
implement messaging in FXRuby differently, without the
FXPseudoTarget? I'm wondering why the content of the
FXPseudoTarget class cannot be included directly, at
run-time, in the object itself, rather than in in a separate
class. This would avoid modifying the target of an object
when connecting a handler to it.


  class FXPseudoTarget < FXObject

    include Responder

    targets_of_pending_timers = {}
    targets_of_pending_chores = {}
    targets_of_pending_signals = {}
    targets_of_pending_inputs = {}

    #
    # Returns an initialized FXPseudoTarget object.
    #
    def initialize
      super
      blocks = {}
    end

    #
    # Store an association between a message of type
    # _messageType_ with a callable object or a block.
    #
    def pconnect(messageType, callableObject, block)
      if callableObject.nil?
	blocks[messageType] = block
      else
	blocks[messageType] = callableObject
      end
      FXMAPTYPE(messageType, :onHandleMsg)
      
      case messageType
        when SEL_TIMEOUT
	  targets_of_pending_timers[self] = self
        when SEL_CHORE
	  targets_of_pending_chores[self] = self
        when SEL_SIGNAL
	  targets_of_pending_signals[self] = self
        when SEL_IO_READ, SEL_IO_WRITE, SEL_IO_EXCEPT
	  targets_of_pending_inputs[self] = self
      end
    end

    #
    # Handle a message from _sender_, with selector _sel_
and
    # message data _ptr_.
    #
    def onHandleMsg(sender, sel, ptr)
      messageType = Fox.FXSELTYPE(sel)
      result = blocks[messageType].call(sender, sel, ptr)
      case messageType
        when SEL_TIMEOUT
	  targets_of_pending_timers.delete(self)
        when SEL_CHORE
	  targets_of_pending_chores.delete(self)
      end
      result
    end
  end


---------------
Philippe Lang
Attik System

_______________________________________________
fxruby-users mailing list
fxruby-usersrubyforge.org
ht
tp://rubyforge.org/mailman/listinfo/fxruby-users
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )