Module: Canis::EventHandler

Included in:
ComboBox, DefaultTreeModel, Form, Widget
Defined in:
lib/canis/core/widgets/rwidget.rb

Overview

module }}}

Since:

  • 1.2.0

Instance Method Summary collapse

Instance Method Details

#bind(event, *xargs, &blk) ⇒ Object Also known as: add_binding

bind an event to a block, optional args will also be passed when calling

Since:

  • 1.2.0



939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
# File 'lib/canis/core/widgets/rwidget.rb', line 939

def bind event, *xargs, &blk
  #$log.debug "#{self} called EventHandler BIND #{event}, args:#{xargs} "
  if @_events
    $log.warn "bind: #{self.class} does not support this event: #{event}. #{@_events} " if !event? event
    #raise ArgumentError, "#{self.class} does not support this event: #{event}. #{@_events} " if !event? event
  else
    # it can come here if bind in initial block, since widgets add to @_event after calling super
    # maybe we can change that.
    $log.warn "BIND #{self.class} (#{event})  XXXXX no events defined in @_events. Please do so to avoid bugs and debugging. This will become a fatal error soon."
  end
  @handler ||= {}
  @event_args ||= {}
  @handler[event] ||= []
  @handler[event] << blk
  @event_args[event] ||= []
  @event_args[event] << xargs
end

#event?(eve) ⇒ Boolean

returns boolean depending on whether this widget has registered the given event

Returns:

  • (Boolean)

Since:

  • 1.2.0



1043
1044
1045
# File 'lib/canis/core/widgets/rwidget.rb', line 1043

def event? eve
  @_events.include? eve
end

#event_listObject

returns event list for this widget

Since:

  • 1.2.0



1048
1049
1050
# File 'lib/canis/core/widgets/rwidget.rb', line 1048

def event_list
  @_events
end

#fire_handler(event, object) ⇒ Object

Fire all bindings for given event e.g. fire_handler :ENTER, self The first parameter passed to the calling block is either self, or some action event The second and beyond are any objects you passed when using ‘bind` or `command`. Exceptions are caught here itself, or else they prevent objects from updating, usually the error is in the block sent in by application, not our error. TODO: if an object throws a subclass of VetoException we should not catch it and throw it back for caller to catch and take care of, such as prevent LEAVE or update etc.

Since:

  • 1.2.0



970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'lib/canis/core/widgets/rwidget.rb', line 970

def fire_handler event, object
  $log.debug "inside def fire_handler evt:#{event}, o: #{object.class}"
  if !@handler.nil?
    if @_events
      raise ArgumentError, "fire_handler: #{self.class} does not support this event: #{event}. #{@_events} " if !event? event
    else
      $log.debug "bIND #{self.class}  XXXXX TEMPO no events defined in @_events "
    end
    ablk = @handler[event]
    if !ablk.nil?
      aeve = @event_args[event]
      ablk.each_with_index do |blk, ix|
        #$log.debug "#{self} called EventHandler firehander #{@name}, #{event}, obj: #{object},args: #{aeve[ix]}"
        $log.debug "#{self} called EventHandler firehander #{@name}, #{event}"
        begin
          blk.call object,  *aeve[ix]
        rescue FieldValidationException => fve
          # added 2011-09-26 1.3.0 so a user raised exception on LEAVE
          # keeps cursor in same field.
          raise fve
        rescue PropertyVetoException => pve
          # added 2011-09-26 1.3.0 so a user raised exception on LEAVE
          # keeps cursor in same field.
          raise pve
        rescue => ex
          ## some don't have name
          #$log.error "======= Error ERROR in block event #{self}: #{name}, #{event}"
          $log.error "======= Error ERROR in block event #{self}:  #{event}"
          $log.error ex
          $log.error(ex.backtrace.join("\n")) 
          #$error_message = "#{ex}" # changed 2010  
          $error_message.value = "#{ex.to_s}"
          Ncurses.beep
        end
      end
    else
      # there is no block for this key/event
      # we must behave exactly as processkey
      # NOTE this is too risky since then buttons and radio buttons
      # that don't have any command don;t update,so removing 2011-12-2 
      #return :UNHANDLED
      return :NO_BLOCK
    end # if
  else
    # there is no handler
    # I've done this since list traps ENTER but rarely uses it.
    # For buttons default, we'd like to trap ENTER even when focus is elsewhere
    # we must behave exactly as processkey
    # NOTE this is too risky since then buttons and radio buttons
    # that don't have any command don;t update,so removing 2011-12-2 
    #return :UNHANDLED
    # If caller wants, can return UNHANDLED such as list and ENTER.
    return :NO_BLOCK
  end # if
end

#fire_property_change(text, oldvalue, newvalue) ⇒ Object

goes with dsl_property Can throw a FieldValidationException or PropertyVetoException

Since:

  • 1.2.0



1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
# File 'lib/canis/core/widgets/rwidget.rb', line 1027

def fire_property_change text, oldvalue, newvalue
  return if @_object_created.nil? #  2018-05-15 - removed check for nil, as object can be nil on creation
                                  # but set later.
  #$log.debug " FPC #{self}: #{text} #{oldvalue}, #{newvalue}"
  $log.debug " FPC #{self}: #{text} "
  if @pce.nil?
    @pce = PropertyChangeEvent.new(self, text, oldvalue, newvalue)
  else
    @pce.set( self, text, oldvalue, newvalue)
  end
  fire_handler :PROPERTY_CHANGE, @pce
  @repaint_required = true 
  repaint_all(true) # for repainting borders, headers etc 2011-09-28 V1.3.1 
end

#register_events(eves) ⇒ Object

widgets may register their events prior to calling super 2014-04-17 - 20:54 Earlier they were writing directly to a data structure after super.

Since:

  • 1.2.0



926
927
928
929
930
931
932
933
934
935
936
# File 'lib/canis/core/widgets/rwidget.rb', line 926

def register_events eves
  @_events ||= []
  case eves
  when Array
    @_events.push(*eves)
  when Symbol
    @_events << eves
  else
    raise ArgumentError "register_events: Don't know how to handle #{eves.class}"
  end
end