]> git.saurik.com Git - wxWidgets.git/blob - interface/wx/eventfilter.h
supporting alignment in single line controls, see #14452
[wxWidgets.git] / interface / wx / eventfilter.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: interface/wx/eventfilter.h
3 // Purpose: wxEventFilter class documentation
4 // Author: Vadim Zeitlin
5 // Created: 2011-11-21
6 // RCS-ID: $Id$
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 /**
12 A global event filter for pre-processing all the events generated in the
13 program.
14
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.
23
24 An example of using this class:
25 @code
26 // This class allows to determine the last time the user has worked with
27 // this application:
28 class LastActivityTimeDetector : public wxEventFilter
29 {
30 public:
31 LastActivityTimeDetector()
32 {
33 wxEvtHandler::AddFilter(this);
34
35 m_last = wxDateTime::Now();
36 }
37
38 virtual ~LastActivityTimeDetector()
39 {
40 wxEvtHandler::RemoveFilter(this);
41 }
42
43 virtual int FilterEvent(wxEvent& event)
44 {
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 )
51 {
52 m_last = wxDateTime::Now();
53 }
54
55 // Continue processing the event normally as well.
56 return Event_Skip;
57 }
58
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
63 {
64 return wxDateTime::Now() - diff > m_last;
65 }
66
67 private:
68 wxDateTime m_last;
69 };
70 @endcode
71
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.
78
79 @category{events}
80
81 @since 2.9.3
82 */
83 class wxEventFilter
84 {
85 public:
86 /// Possible return values for FilterEvent().
87 enum
88 {
89 /// Process event as usual.
90 Event_Skip = -1,
91
92 /// Don't process the event normally at all.
93 Event_Ignore = 0,
94
95 /// Event was already handled, don't process it normally.
96 Event_Processed = 1
97 };
98
99 /**
100 Default constructor.
101
102 Constructor does not register this filter using
103 wxEvtHandler::AddFilter(), it's your responsibility to do it when
104 necessary.
105
106 Notice that the objects of this class can't be copied.
107 */
108 wxEventFilter();
109
110 /**
111 Destructor.
112
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.
116 */
117 virtual ~wxEventFilter();
118
119 /**
120 Override this method to implement event pre-processing.
121
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 a crawl.
125
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).
137 */
138 virtual int FilterEvent(wxEvent& event) = 0;
139 };