]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/eventfilter.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: interface/wx/eventfilter.h
3 // Purpose: wxEventFilter class documentation
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
12 A global event filter for pre-processing all the events generated in the
15 This is a very simple class which just provides FilterEvent() virtual
16 method to be called by wxEvtHandler before starting process of any event.
17 Thus, inheriting from this class and overriding FilterEvent() allows to
18 capture and possibly handle or ignore all the events happening in the
19 program. Of course, having event filters adds additional overhead to every
20 event processing and so should not be used lightly and your FilterEvent()
21 code should try to return as quickly as possible, especially for the events
22 it is not interested in.
24 An example of using this class:
26 // This class allows to determine the last time the user has worked with
28 class LastActivityTimeDetector : public wxEventFilter
31 LastActivityTimeDetector()
33 wxEvtHandler::AddFilter(this);
35 m_last = wxDateTime::Now();
38 virtual ~LastActivityTimeDetector()
40 wxEvtHandler::RemoveFilter(this);
43 virtual int FilterEvent(wxEvent& event)
45 // Update the last user activity
46 const wxEventType t = event.GetEventType();
47 if ( t == wxEVT_KEY_DOWN || t == wxEVT_MOTION ||
48 t == wxEVT_LEFT_DOWN ||
49 t == wxEVT_RIGHT_DOWN ||
50 t == wxEVT_MIDDLE_DOWN )
52 m_last = wxDateTime::Now();
55 // Continue processing the event normally as well.
59 // This function could be called periodically from some timer to
60 // do something (e.g. hide sensitive data or log out from remote
61 // server) if the user has been inactive for some time period.
62 bool IsInactiveFor(const wxTimeSpan& diff) const
64 return wxDateTime::Now() - diff > m_last;
72 Notice that wxApp derives from wxEventFilter and is registered as an event
73 filter during its creation so you may also override FilterEvent() method in
74 your wxApp-derived class and, in fact, this is often the most convenient
75 way to do it. However creating a new class deriving directly from
76 wxEventFilter allows to isolate the event filtering code in its own
77 separate class and also to have several independent filters, if necessary.
86 /// Possible return values for FilterEvent().
89 /// Process event as usual.
92 /// Don't process the event normally at all.
95 /// Event was already handled, don't process it normally.
102 Constructor does not register this filter using
103 wxEvtHandler::AddFilter(), it's your responsibility to do it when
106 Notice that the objects of this class can't be copied.
113 You must call wxEvtHandler::RemoveFilter() before destroying this
114 object (possibly from the derived class destructor), failure to do this
115 is indicated by an assert unless assertions are disabled.
117 virtual ~wxEventFilter();
120 Override this method to implement event pre-processing.
122 This method allows to filter all the events processed by the program,
123 so you should try to return quickly from it to avoid slowing down the
124 program to the crawl.
126 Although the return type of this method is @c int, this is only due to
127 backwards compatibility concerns and the actual return value must be
128 one of the @c Event_XXX constants defined above:
129 - Event_Skip to continue processing the event normally (this should
130 be used in most cases).
131 - Event_Ignore to not process this event at all (this can be used
132 to suppress some events).
133 - Event_Processed to not process this event normally but indicate
134 that it was already processed by the event filter and so no default
135 processing should take place neither (this should only be used if
136 the filter really did process the event).
138 virtual int FilterEvent(wxEvent
& event
) = 0;