require 'fox'

module Fox
  class FXApp

    # Accepted forms are:
    #   addSignal(sig, target, selector, immediate=false, flags=0)
    #   addSignal(sig, method(:onTimeout), immediate=false, flags=0)
    #   addSignal(sig, immediate=false, flags=0) { |sender, sel, ptr| block }

    alias addSignalOrig addSignal

    def addSignal(sig, *args, &block)
      tgt, sel, immediate, flags = nil, 0, false, 0
      if args.length > 0
        if args[0].respond_to? :call
          tgt = FXPseudoTarget.new
          tgt.pconnect(SEL_SIGNAL, args[0], block)
          immediate = (args.length > 1) ? args[1] : false
          flags = (args.length > 2) ? args[2] : 0
        elsif (args[0].kind_of? TrueClass) || (args[0].kind_of? FalseClass)
          tgt = FXPseudoTarget.new
          tgt.pconnect(SEL_SIGNAL, nil, block)
          immediate = args[0]
          flags = (args.length > 1) ? args[1] : 0
        else # it's some other kind of object
          tgt = args[0]
          sel = (args.length > 1) ? args[1] : 0
          immediate = (args.length > 2) ? args[2] : false
          flags = (args.length > 3) ? args[3] : 0
        end
      else
        tgt = FXPseudoTarget.new
        tgt.pconnect(SEL_SIGNAL, nil, block)
      end
      addSignalOrig(sig, tgt, sel, immediate, flags)
    end
  end
end
