Module: RubyCurses::EventHandler
- Included in:
- Action, ComboBox, DefaultTreeModel, Form, Widget
- Defined in:
- lib/rbcurse/core/widgets/rwidget.rb
Overview
module
Instance Method Summary collapse
-
#bind(event, *xargs, &blk) ⇒ Object
(also: #add_binding)
bind an event to a block, optional args will also be passed when calling.
-
#fire_handler(event, object) ⇒ Object
Fire all bindings for given event e.g.
-
#fire_property_change(text, oldvalue, newvalue) ⇒ Object
added on 2009-01-08 00:33 goes with dsl_property Need to inform listeners - done 2010-02-25 23:09 Can throw a FieldValidationException or PropertyVetoException.
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
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 606 def bind event, *xargs, &blk #$log.debug "#{self} called EventHandler BIND #{event}, args:#{xargs} " if @_events $log.warn "#{self.class} does not support this event: #{event}. #{@_events} " if !@_events.include? event #raise ArgumentError, "#{self.class} does not support this event: #{event}. #{@_events} " if !@_events.include? 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 |
#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.
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 637 def fire_handler event, object $log.debug "inside def fire_handler evt:#{event}, o: #{object.class}" if !@handler.nil? if @_events raise ArgumentError, "#{self.class} does not support this event: #{event}. #{@_events} " if !@_events.include? 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
added on 2009-01-08 00:33 goes with dsl_property Need to inform listeners - done 2010-02-25 23:09 Can throw a FieldValidationException or PropertyVetoException
696 697 698 699 700 701 702 703 704 705 706 707 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 696 def fire_property_change text, oldvalue, newvalue return if oldvalue.nil? || @_object_created.nil? # added 2010-09-16 so if called by methods it is still effective $log.debug " FPC #{self}: #{text} #{oldvalue}, #{newvalue}" 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 # this was a hack and shoudl go, someone wanted to set this so it would repaint (viewport line 99 fire_prop repaint_all(true) # for repainting borders, headers etc 2011-09-28 V1.3.1 end |