]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: interface/wx/eventfilter.h | |
3 | // Purpose: wxEventFilter class documentation | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2011-11-21 | |
6 | // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org> | |
7 | // Licence: wxWindows licence | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | /** | |
11 | A global event filter for pre-processing all the events generated in the | |
12 | program. | |
13 | ||
14 | This is a very simple class which just provides FilterEvent() virtual | |
15 | method to be called by wxEvtHandler before starting process of any event. | |
16 | Thus, inheriting from this class and overriding FilterEvent() allows to | |
17 | capture and possibly handle or ignore all the events happening in the | |
18 | program. Of course, having event filters adds additional overhead to every | |
19 | event processing and so should not be used lightly and your FilterEvent() | |
20 | code should try to return as quickly as possible, especially for the events | |
21 | it is not interested in. | |
22 | ||
23 | An example of using this class: | |
24 | @code | |
25 | // This class allows to determine the last time the user has worked with | |
26 | // this application: | |
27 | class LastActivityTimeDetector : public wxEventFilter | |
28 | { | |
29 | public: | |
30 | LastActivityTimeDetector() | |
31 | { | |
32 | wxEvtHandler::AddFilter(this); | |
33 | ||
34 | m_last = wxDateTime::Now(); | |
35 | } | |
36 | ||
37 | virtual ~LastActivityTimeDetector() | |
38 | { | |
39 | wxEvtHandler::RemoveFilter(this); | |
40 | } | |
41 | ||
42 | virtual int FilterEvent(wxEvent& event) | |
43 | { | |
44 | // Update the last user activity | |
45 | const wxEventType t = event.GetEventType(); | |
46 | if ( t == wxEVT_KEY_DOWN || t == wxEVT_MOTION || | |
47 | t == wxEVT_LEFT_DOWN || | |
48 | t == wxEVT_RIGHT_DOWN || | |
49 | t == wxEVT_MIDDLE_DOWN ) | |
50 | { | |
51 | m_last = wxDateTime::Now(); | |
52 | } | |
53 | ||
54 | // Continue processing the event normally as well. | |
55 | return Event_Skip; | |
56 | } | |
57 | ||
58 | // This function could be called periodically from some timer to | |
59 | // do something (e.g. hide sensitive data or log out from remote | |
60 | // server) if the user has been inactive for some time period. | |
61 | bool IsInactiveFor(const wxTimeSpan& diff) const | |
62 | { | |
63 | return wxDateTime::Now() - diff > m_last; | |
64 | } | |
65 | ||
66 | private: | |
67 | wxDateTime m_last; | |
68 | }; | |
69 | @endcode | |
70 | ||
71 | Notice that wxApp derives from wxEventFilter and is registered as an event | |
72 | filter during its creation so you may also override FilterEvent() method in | |
73 | your wxApp-derived class and, in fact, this is often the most convenient | |
74 | way to do it. However creating a new class deriving directly from | |
75 | wxEventFilter allows to isolate the event filtering code in its own | |
76 | separate class and also to have several independent filters, if necessary. | |
77 | ||
78 | @category{events} | |
79 | ||
80 | @since 2.9.3 | |
81 | */ | |
82 | class wxEventFilter | |
83 | { | |
84 | public: | |
85 | /// Possible return values for FilterEvent(). | |
86 | enum | |
87 | { | |
88 | /// Process event as usual. | |
89 | Event_Skip = -1, | |
90 | ||
91 | /// Don't process the event normally at all. | |
92 | Event_Ignore = 0, | |
93 | ||
94 | /// Event was already handled, don't process it normally. | |
95 | Event_Processed = 1 | |
96 | }; | |
97 | ||
98 | /** | |
99 | Default constructor. | |
100 | ||
101 | Constructor does not register this filter using | |
102 | wxEvtHandler::AddFilter(), it's your responsibility to do it when | |
103 | necessary. | |
104 | ||
105 | Notice that the objects of this class can't be copied. | |
106 | */ | |
107 | wxEventFilter(); | |
108 | ||
109 | /** | |
110 | Destructor. | |
111 | ||
112 | You must call wxEvtHandler::RemoveFilter() before destroying this | |
113 | object (possibly from the derived class destructor), failure to do this | |
114 | is indicated by an assert unless assertions are disabled. | |
115 | */ | |
116 | virtual ~wxEventFilter(); | |
117 | ||
118 | /** | |
119 | Override this method to implement event pre-processing. | |
120 | ||
121 | This method allows to filter all the events processed by the program, | |
122 | so you should try to return quickly from it to avoid slowing down the | |
123 | program to a crawl. | |
124 | ||
125 | Although the return type of this method is @c int, this is only due to | |
126 | backwards compatibility concerns and the actual return value must be | |
127 | one of the @c Event_XXX constants defined above: | |
128 | - Event_Skip to continue processing the event normally (this should | |
129 | be used in most cases). | |
130 | - Event_Ignore to not process this event at all (this can be used | |
131 | to suppress some events). | |
132 | - Event_Processed to not process this event normally but indicate | |
133 | that it was already processed by the event filter and so no default | |
134 | processing should take place neither (this should only be used if | |
135 | the filter really did process the event). | |
136 | */ | |
137 | virtual int FilterEvent(wxEvent& event) = 0; | |
138 | }; |