Class: Wx::EventFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/wx/doc/gen/event_filter.rb

Overview

A global event filter for pre-processing all the events generated in the program.

This is a very simple class which just provides #filter_event virtual method to be called by EvtHandler before starting process of any event. Thus, inheriting from this class and overriding #filter_event allows capturing and possibly handling or ignoring all the events happening in the program. Of course, having event filters adds additional overhead to every event processing and so should not be used lightly and your #filter_event code should try to return as quickly as possible, especially for the events it is not interested in. An example of using this class:

# This class allows determining the last time the user has worked with
# this application:
class LastActivityTimeDetector < Wx::EventFilter
  def initialize
    Wx::EvtHandler.add_filter(self)

    @last = Time.now
  end

  def clear
    Wx::EvtHandler.remove_filter(self)
  end

  def filter_event(event)
    # Update the last user activity
    t = event.get_event_type
    case t
    when Wx::EVT_KEY_DOWN, 
         Wx::EVT_MOTION, 
         Wx::EVT_LEFT_DOWN,
         Wx::EVT_RIGHT_DOWN,
         Wx::EVT_MIDDLE_DOWN
      @last = Time.now
    end

    # Continue processing the event normally as well.
    Event_Skip
  end

  # This function could be called periodically from some timer to
  # do something (e.g. hide sensitive data or log out from remote
  # server) if the user has been inactive for some time period.
  def is_inactive_for?(diff)
    (Time.now - diff) > @last
  end

end

Notice that App derives from EventFilter and is registered as an event filter during its creation so you may also override #filter_event method in your App-derived class and, in fact, this is often the most convenient way to do it. However creating a new class deriving directly from EventFilter allows isolating the event filtering code in its own separate class and also having several independent filters, if necessary. Category: Events

Constant Summary collapse

Event_Skip =

Process event as usual.

-1
Event_Ignore =

Don’t process the event normally at all.

0
Event_Processed =

Event was already handled, don’t process it normally.

1

Instance Method Summary collapse

Constructor Details

#initializeWx::EventFilter

Default constructor.

Constructor does not register this filter using Wx::EvtHandler.add_filter, it’s your responsibility to do it when necessary. Notice that the objects of this class can’t be copied.



75
# File 'lib/wx/doc/gen/event_filter.rb', line 75

def initialize; end

Instance Method Details

#filter_event(event) ⇒ Integer

Override this method to implement event pre-processing.

This method allows filtering all the events processed by the program, so you should try to return quickly from it to avoid slowing down the program to a crawl. Although the return type of this method is int, this is only due to backwards compatibility concerns and the actual return value must be one of the Event_XXX constants defined above:

  • Event_Skip to continue processing the event normally (this should be used in most cases).

  • Event_Ignore to not process this event at all (this can be used to suppress some events).

  • Event_Processed to not process this event normally but indicate that it was already processed by the event filter and so no default processing should take place either (this should only be used if the filter really did process the event).

Parameters:

Returns:

  • (Integer)


87
# File 'lib/wx/doc/gen/event_filter.rb', line 87

def filter_event(event) end